Statistics
| Branch: | Revision:

root / block / iscsi.c @ 65f3e339

History | View | Annotate | Download (42.3 kB)

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

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

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

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

    
61
typedef struct IscsiTask {
62
    int status;
63
    int complete;
64
    int retries;
65
    int do_retry;
66
    struct scsi_task *task;
67
    Coroutine *co;
68
} IscsiTask;
69

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

    
87
#define NOP_INTERVAL 5000
88
#define MAX_NOP_FAILURES 3
89
#define ISCSI_CMD_RETRIES 5
90
#define ISCSI_MAX_UNMAP 131072
91

    
92
static void
93
iscsi_bh_cb(void *p)
94
{
95
    IscsiAIOCB *acb = p;
96

    
97
    qemu_bh_delete(acb->bh);
98

    
99
    g_free(acb->buf);
100
    acb->buf = NULL;
101

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

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

    
111
    qemu_aio_release(acb);
112
}
113

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

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

    
131
    iTask->complete = 1;
132
    iTask->status = status;
133
    iTask->do_retry = 0;
134
    iTask->task = task;
135

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

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

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

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

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

    
166
    acb->status = -ECANCELED;
167
    iscsi_schedule_bh(acb);
168
}
169

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

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

    
180
    acb->canceled = 1;
181

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

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

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

    
196

    
197
static void iscsi_process_read(void *arg);
198
static void iscsi_process_write(void *arg);
199

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

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

    
215
    }
216

    
217
    iscsilun->events = ev;
218
}
219

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

    
226
    iscsi_service(iscsi, POLLIN);
227
    iscsi_set_events(iscsilun);
228
}
229

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

    
236
    iscsi_service(iscsi, POLLOUT);
237
    iscsi_set_events(iscsilun);
238
}
239

    
240
static int
241
iscsi_aio_writev_acb(IscsiAIOCB *acb);
242

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

    
249
    trace_iscsi_aio_write16_cb(iscsi, status, acb, acb->canceled);
250

    
251
    g_free(acb->buf);
252
    acb->buf = NULL;
253

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

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

    
275
    iscsi_schedule_bh(acb);
276
}
277

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

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

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

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

    
314
    acb->canceled   = 0;
315
    acb->bh         = NULL;
316
    acb->status     = -EINPROGRESS;
317
    acb->buf        = NULL;
318

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

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

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

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

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

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

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

    
374
    return 0;
375
}
376

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

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

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

    
393
    acb->iscsilun    = iscsilun;
394
    acb->qiov        = qiov;
395
    acb->nb_sectors  = nb_sectors;
396
    acb->sector_num  = sector_num;
397
    acb->retries     = ISCSI_CMD_RETRIES;
398

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

    
404
    iscsi_set_events(iscsilun);
405
    return &acb->common;
406
}
407

    
408
static int
409
iscsi_aio_readv_acb(IscsiAIOCB *acb);
410

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

    
417
    trace_iscsi_aio_read16_cb(iscsi, status, acb, acb->canceled);
418

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

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

    
440
    iscsi_schedule_bh(acb);
441
}
442

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

    
455
    acb->canceled    = 0;
456
    acb->bh          = NULL;
457
    acb->status      = -EINPROGRESS;
458
    acb->buf         = NULL;
459

    
460
    size = acb->nb_sectors * BDRV_SECTOR_SIZE;
461

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

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

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

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

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

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

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

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

    
528
    acb->nb_sectors  = nb_sectors;
529
    acb->sector_num  = sector_num;
530
    acb->iscsilun    = iscsilun;
531
    acb->qiov        = qiov;
532
    acb->retries     = ISCSI_CMD_RETRIES;
533

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

    
539
    iscsi_set_events(iscsilun);
540
    return &acb->common;
541
}
542

    
543
static int
544
iscsi_aio_flush_acb(IscsiAIOCB *acb);
545

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

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

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

    
573
    iscsi_schedule_bh(acb);
574
}
575

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

    
581
    acb->canceled   = 0;
582
    acb->bh         = NULL;
583
    acb->status     = -EINPROGRESS;
584
    acb->buf        = NULL;
585

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

    
596
    return 0;
597
}
598

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

    
605
    IscsiAIOCB *acb;
606

    
607
    acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
608

    
609
    acb->iscsilun    = iscsilun;
610
    acb->retries     = ISCSI_CMD_RETRIES;
611

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

    
617
    iscsi_set_events(iscsilun);
618

    
619
    return &acb->common;
620
}
621

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

    
629
    g_free(acb->buf);
630
    acb->buf = NULL;
631

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

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

    
643
    acb->ioh->driver_status = 0;
644
    acb->ioh->host_status   = 0;
645
    acb->ioh->resid         = 0;
646

    
647
#define SG_ERR_DRIVER_SENSE    0x08
648

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

    
652
        acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE;
653

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

    
660
    iscsi_schedule_bh(acb);
661
}
662

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

    
672
    assert(req == SG_IO);
673

    
674
    acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
675

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

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

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

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

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

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

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

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

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

    
762
    iscsi_set_events(iscsilun);
763

    
764
    return &acb->common;
765
}
766

    
767

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

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

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

    
790
        while (status == -EINPROGRESS) {
791
            qemu_aio_wait();
792
        }
793

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

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

    
808
    len  = iscsilun->num_blocks;
809
    len *= iscsilun->block_size;
810

    
811
    return len;
812
}
813

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

    
824
    iscsi_co_init_iscsitask(iscsilun, &iTask);
825

    
826
    if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
827
        ret = -EINVAL;
828
        goto out;
829
    }
830

    
831
    /* default to all sectors allocated */
832
    ret = BDRV_BLOCK_DATA;
833
    ret |= (sector_num << BDRV_SECTOR_BITS) | BDRV_BLOCK_OFFSET_VALID;
834
    *pnum = nb_sectors;
835

    
836
    /* LUN does not support logical block provisioning */
837
    if (iscsilun->lbpme == 0) {
838
        goto out;
839
    }
840

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

    
850
    while (!iTask.complete) {
851
        iscsi_set_events(iscsilun);
852
        qemu_coroutine_yield();
853
    }
854

    
855
    if (iTask.do_retry) {
856
        if (iTask.task != NULL) {
857
            scsi_free_scsi_task(iTask.task);
858
            iTask.task = NULL;
859
        }
860
        goto retry;
861
    }
862

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

    
871
    lbas = scsi_datain_unmarshall(iTask.task);
872
    if (lbas == NULL) {
873
        ret = -EIO;
874
        goto out;
875
    }
876

    
877
    lbasd = &lbas->descriptors[0];
878

    
879
    if (sector_qemu2lun(sector_num, iscsilun) != lbasd->lba) {
880
        ret = -EIO;
881
        goto out;
882
    }
883

    
884
    *pnum = sector_lun2qemu(lbasd->num_blocks, iscsilun);
885
    if (*pnum > nb_sectors) {
886
        *pnum = nb_sectors;
887
    }
888

    
889
    if (lbasd->provisioning == SCSI_PROVISIONING_TYPE_DEALLOCATED ||
890
        lbasd->provisioning == SCSI_PROVISIONING_TYPE_ANCHORED) {
891
        ret &= ~BDRV_BLOCK_DATA;
892
        if (iscsilun->lbprz) {
893
            ret |= BDRV_BLOCK_ZERO;
894
        }
895
    }
896

    
897
out:
898
    if (iTask.task != NULL) {
899
        scsi_free_scsi_task(iTask.task);
900
    }
901
    return ret;
902
}
903

    
904
static int
905
coroutine_fn iscsi_co_discard(BlockDriverState *bs, int64_t sector_num,
906
                                   int nb_sectors)
907
{
908
    IscsiLun *iscsilun = bs->opaque;
909
    struct IscsiTask iTask;
910
    struct unmap_list list;
911
    uint32_t nb_blocks;
912
    uint32_t max_unmap;
913

    
914
    if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
915
        return -EINVAL;
916
    }
917

    
918
    if (!iscsilun->lbp.lbpu) {
919
        /* UNMAP is not supported by the target */
920
        return 0;
921
    }
922

    
923
    list.lba = sector_qemu2lun(sector_num, iscsilun);
924
    nb_blocks = sector_qemu2lun(nb_sectors, iscsilun);
925

    
926
    max_unmap = iscsilun->bl.max_unmap;
927
    if (max_unmap == 0xffffffff) {
928
        max_unmap = ISCSI_MAX_UNMAP;
929
    }
930

    
931
    while (nb_blocks > 0) {
932
        iscsi_co_init_iscsitask(iscsilun, &iTask);
933
        list.num = nb_blocks;
934
        if (list.num > max_unmap) {
935
            list.num = max_unmap;
936
        }
937
retry:
938
        if (iscsi_unmap_task(iscsilun->iscsi, iscsilun->lun, 0, 0, &list, 1,
939
                         iscsi_co_generic_cb, &iTask) == NULL) {
940
            return -EIO;
941
        }
942

    
943
        while (!iTask.complete) {
944
            iscsi_set_events(iscsilun);
945
            qemu_coroutine_yield();
946
        }
947

    
948
        if (iTask.task != NULL) {
949
            scsi_free_scsi_task(iTask.task);
950
            iTask.task = NULL;
951
        }
952

    
953
        if (iTask.do_retry) {
954
            goto retry;
955
        }
956

    
957
        if (iTask.status == SCSI_STATUS_CHECK_CONDITION) {
958
            /* the target might fail with a check condition if it
959
               is not happy with the alignment of the UNMAP request
960
               we silently fail in this case */
961
            return 0;
962
        }
963

    
964
        if (iTask.status != SCSI_STATUS_GOOD) {
965
            return -EIO;
966
        }
967

    
968
        list.lba += list.num;
969
        nb_blocks -= list.num;
970
    }
971

    
972
    return 0;
973
}
974

    
975
static int parse_chap(struct iscsi_context *iscsi, const char *target)
976
{
977
    QemuOptsList *list;
978
    QemuOpts *opts;
979
    const char *user = NULL;
980
    const char *password = NULL;
981

    
982
    list = qemu_find_opts("iscsi");
983
    if (!list) {
984
        return 0;
985
    }
986

    
987
    opts = qemu_opts_find(list, target);
988
    if (opts == NULL) {
989
        opts = QTAILQ_FIRST(&list->head);
990
        if (!opts) {
991
            return 0;
992
        }
993
    }
994

    
995
    user = qemu_opt_get(opts, "user");
996
    if (!user) {
997
        return 0;
998
    }
999

    
1000
    password = qemu_opt_get(opts, "password");
1001
    if (!password) {
1002
        error_report("CHAP username specified but no password was given");
1003
        return -1;
1004
    }
1005

    
1006
    if (iscsi_set_initiator_username_pwd(iscsi, user, password)) {
1007
        error_report("Failed to set initiator username and password");
1008
        return -1;
1009
    }
1010

    
1011
    return 0;
1012
}
1013

    
1014
static void parse_header_digest(struct iscsi_context *iscsi, const char *target)
1015
{
1016
    QemuOptsList *list;
1017
    QemuOpts *opts;
1018
    const char *digest = NULL;
1019

    
1020
    list = qemu_find_opts("iscsi");
1021
    if (!list) {
1022
        return;
1023
    }
1024

    
1025
    opts = qemu_opts_find(list, target);
1026
    if (opts == NULL) {
1027
        opts = QTAILQ_FIRST(&list->head);
1028
        if (!opts) {
1029
            return;
1030
        }
1031
    }
1032

    
1033
    digest = qemu_opt_get(opts, "header-digest");
1034
    if (!digest) {
1035
        return;
1036
    }
1037

    
1038
    if (!strcmp(digest, "CRC32C")) {
1039
        iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C);
1040
    } else if (!strcmp(digest, "NONE")) {
1041
        iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE);
1042
    } else if (!strcmp(digest, "CRC32C-NONE")) {
1043
        iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE);
1044
    } else if (!strcmp(digest, "NONE-CRC32C")) {
1045
        iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1046
    } else {
1047
        error_report("Invalid header-digest setting : %s", digest);
1048
    }
1049
}
1050

    
1051
static char *parse_initiator_name(const char *target)
1052
{
1053
    QemuOptsList *list;
1054
    QemuOpts *opts;
1055
    const char *name;
1056
    char *iscsi_name;
1057
    UuidInfo *uuid_info;
1058

    
1059
    list = qemu_find_opts("iscsi");
1060
    if (list) {
1061
        opts = qemu_opts_find(list, target);
1062
        if (!opts) {
1063
            opts = QTAILQ_FIRST(&list->head);
1064
        }
1065
        if (opts) {
1066
            name = qemu_opt_get(opts, "initiator-name");
1067
            if (name) {
1068
                return g_strdup(name);
1069
            }
1070
        }
1071
    }
1072

    
1073
    uuid_info = qmp_query_uuid(NULL);
1074
    if (strcmp(uuid_info->UUID, UUID_NONE) == 0) {
1075
        name = qemu_get_vm_name();
1076
    } else {
1077
        name = uuid_info->UUID;
1078
    }
1079
    iscsi_name = g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
1080
                                 name ? ":" : "", name ? name : "");
1081
    qapi_free_UuidInfo(uuid_info);
1082
    return iscsi_name;
1083
}
1084

    
1085
#if defined(LIBISCSI_FEATURE_NOP_COUNTER)
1086
static void iscsi_nop_timed_event(void *opaque)
1087
{
1088
    IscsiLun *iscsilun = opaque;
1089

    
1090
    if (iscsi_get_nops_in_flight(iscsilun->iscsi) > MAX_NOP_FAILURES) {
1091
        error_report("iSCSI: NOP timeout. Reconnecting...");
1092
        iscsi_reconnect(iscsilun->iscsi);
1093
    }
1094

    
1095
    if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) {
1096
        error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
1097
        return;
1098
    }
1099

    
1100
    timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
1101
    iscsi_set_events(iscsilun);
1102
}
1103
#endif
1104

    
1105
static int iscsi_readcapacity_sync(IscsiLun *iscsilun)
1106
{
1107
    struct scsi_task *task = NULL;
1108
    struct scsi_readcapacity10 *rc10 = NULL;
1109
    struct scsi_readcapacity16 *rc16 = NULL;
1110
    int ret = 0;
1111
    int retries = ISCSI_CMD_RETRIES; 
1112

    
1113
    do {
1114
        if (task != NULL) {
1115
            scsi_free_scsi_task(task);
1116
            task = NULL;
1117
        }
1118

    
1119
        switch (iscsilun->type) {
1120
        case TYPE_DISK:
1121
            task = iscsi_readcapacity16_sync(iscsilun->iscsi, iscsilun->lun);
1122
            if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1123
                rc16 = scsi_datain_unmarshall(task);
1124
                if (rc16 == NULL) {
1125
                    error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
1126
                    ret = -EINVAL;
1127
                } else {
1128
                    iscsilun->block_size = rc16->block_length;
1129
                    iscsilun->num_blocks = rc16->returned_lba + 1;
1130
                    iscsilun->lbpme = rc16->lbpme;
1131
                    iscsilun->lbprz = rc16->lbprz;
1132
                }
1133
            }
1134
            break;
1135
        case TYPE_ROM:
1136
            task = iscsi_readcapacity10_sync(iscsilun->iscsi, iscsilun->lun, 0, 0);
1137
            if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1138
                rc10 = scsi_datain_unmarshall(task);
1139
                if (rc10 == NULL) {
1140
                    error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
1141
                    ret = -EINVAL;
1142
                } else {
1143
                    iscsilun->block_size = rc10->block_size;
1144
                    if (rc10->lba == 0) {
1145
                        /* blank disk loaded */
1146
                        iscsilun->num_blocks = 0;
1147
                    } else {
1148
                        iscsilun->num_blocks = rc10->lba + 1;
1149
                    }
1150
                }
1151
            }
1152
            break;
1153
        default:
1154
            return 0;
1155
        }
1156
    } while (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION
1157
             && task->sense.key == SCSI_SENSE_UNIT_ATTENTION
1158
             && retries-- > 0);
1159

    
1160
    if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1161
        error_report("iSCSI: failed to send readcapacity10 command.");
1162
        ret = -EINVAL;
1163
    }
1164
    if (task) {
1165
        scsi_free_scsi_task(task);
1166
    }
1167
    return ret;
1168
}
1169

    
1170
/* TODO Convert to fine grained options */
1171
static QemuOptsList runtime_opts = {
1172
    .name = "iscsi",
1173
    .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
1174
    .desc = {
1175
        {
1176
            .name = "filename",
1177
            .type = QEMU_OPT_STRING,
1178
            .help = "URL to the iscsi image",
1179
        },
1180
        { /* end of list */ }
1181
    },
1182
};
1183

    
1184
static struct scsi_task *iscsi_do_inquiry(struct iscsi_context *iscsi,
1185
                                          int lun, int evpd, int pc) {
1186
        int full_size;
1187
        struct scsi_task *task = NULL;
1188
        task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, 64);
1189
        if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1190
            goto fail;
1191
        }
1192
        full_size = scsi_datain_getfullsize(task);
1193
        if (full_size > task->datain.size) {
1194
            scsi_free_scsi_task(task);
1195

    
1196
            /* we need more data for the full list */
1197
            task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, full_size);
1198
            if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1199
                goto fail;
1200
            }
1201
        }
1202

    
1203
        return task;
1204

    
1205
fail:
1206
        error_report("iSCSI: Inquiry command failed : %s",
1207
                     iscsi_get_error(iscsi));
1208
        if (task) {
1209
            scsi_free_scsi_task(task);
1210
            return NULL;
1211
        }
1212
        return NULL;
1213
}
1214

    
1215
/*
1216
 * We support iscsi url's on the form
1217
 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1218
 */
1219
static int iscsi_open(BlockDriverState *bs, QDict *options, int flags)
1220
{
1221
    IscsiLun *iscsilun = bs->opaque;
1222
    struct iscsi_context *iscsi = NULL;
1223
    struct iscsi_url *iscsi_url = NULL;
1224
    struct scsi_task *task = NULL;
1225
    struct scsi_inquiry_standard *inq = NULL;
1226
    char *initiator_name = NULL;
1227
    QemuOpts *opts;
1228
    Error *local_err = NULL;
1229
    const char *filename;
1230
    int ret;
1231

    
1232
    if ((BDRV_SECTOR_SIZE % 512) != 0) {
1233
        error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
1234
                     "BDRV_SECTOR_SIZE(%lld) is not a multiple "
1235
                     "of 512", BDRV_SECTOR_SIZE);
1236
        return -EINVAL;
1237
    }
1238

    
1239
    opts = qemu_opts_create_nofail(&runtime_opts);
1240
    qemu_opts_absorb_qdict(opts, options, &local_err);
1241
    if (error_is_set(&local_err)) {
1242
        qerror_report_err(local_err);
1243
        error_free(local_err);
1244
        ret = -EINVAL;
1245
        goto out;
1246
    }
1247

    
1248
    filename = qemu_opt_get(opts, "filename");
1249

    
1250

    
1251
    iscsi_url = iscsi_parse_full_url(iscsi, filename);
1252
    if (iscsi_url == NULL) {
1253
        error_report("Failed to parse URL : %s", filename);
1254
        ret = -EINVAL;
1255
        goto out;
1256
    }
1257

    
1258
    memset(iscsilun, 0, sizeof(IscsiLun));
1259

    
1260
    initiator_name = parse_initiator_name(iscsi_url->target);
1261

    
1262
    iscsi = iscsi_create_context(initiator_name);
1263
    if (iscsi == NULL) {
1264
        error_report("iSCSI: Failed to create iSCSI context.");
1265
        ret = -ENOMEM;
1266
        goto out;
1267
    }
1268

    
1269
    if (iscsi_set_targetname(iscsi, iscsi_url->target)) {
1270
        error_report("iSCSI: Failed to set target name.");
1271
        ret = -EINVAL;
1272
        goto out;
1273
    }
1274

    
1275
    if (iscsi_url->user != NULL) {
1276
        ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user,
1277
                                              iscsi_url->passwd);
1278
        if (ret != 0) {
1279
            error_report("Failed to set initiator username and password");
1280
            ret = -EINVAL;
1281
            goto out;
1282
        }
1283
    }
1284

    
1285
    /* check if we got CHAP username/password via the options */
1286
    if (parse_chap(iscsi, iscsi_url->target) != 0) {
1287
        error_report("iSCSI: Failed to set CHAP user/password");
1288
        ret = -EINVAL;
1289
        goto out;
1290
    }
1291

    
1292
    if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
1293
        error_report("iSCSI: Failed to set session type to normal.");
1294
        ret = -EINVAL;
1295
        goto out;
1296
    }
1297

    
1298
    iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1299

    
1300
    /* check if we got HEADER_DIGEST via the options */
1301
    parse_header_digest(iscsi, iscsi_url->target);
1302

    
1303
    if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) {
1304
        error_report("iSCSI: Failed to connect to LUN : %s",
1305
            iscsi_get_error(iscsi));
1306
        ret = -EINVAL;
1307
        goto out;
1308
    }
1309

    
1310
    iscsilun->iscsi = iscsi;
1311
    iscsilun->lun   = iscsi_url->lun;
1312

    
1313
    task = iscsi_inquiry_sync(iscsi, iscsilun->lun, 0, 0, 36);
1314

    
1315
    if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1316
        error_report("iSCSI: failed to send inquiry command.");
1317
        ret = -EINVAL;
1318
        goto out;
1319
    }
1320

    
1321
    inq = scsi_datain_unmarshall(task);
1322
    if (inq == NULL) {
1323
        error_report("iSCSI: Failed to unmarshall inquiry data.");
1324
        ret = -EINVAL;
1325
        goto out;
1326
    }
1327

    
1328
    iscsilun->type = inq->periperal_device_type;
1329

    
1330
    if ((ret = iscsi_readcapacity_sync(iscsilun)) != 0) {
1331
        goto out;
1332
    }
1333
    bs->total_sectors = sector_lun2qemu(iscsilun->num_blocks, iscsilun);
1334

    
1335
    /* Medium changer or tape. We dont have any emulation for this so this must
1336
     * be sg ioctl compatible. We force it to be sg, otherwise qemu will try
1337
     * to read from the device to guess the image format.
1338
     */
1339
    if (iscsilun->type == TYPE_MEDIUM_CHANGER ||
1340
        iscsilun->type == TYPE_TAPE) {
1341
        bs->sg = 1;
1342
    }
1343

    
1344
    if (iscsilun->lbpme) {
1345
        struct scsi_inquiry_logical_block_provisioning *inq_lbp;
1346
        task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1347
                                SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING);
1348
        if (task == NULL) {
1349
            ret = -EINVAL;
1350
            goto out;
1351
        }
1352
        inq_lbp = scsi_datain_unmarshall(task);
1353
        if (inq_lbp == NULL) {
1354
            error_report("iSCSI: failed to unmarshall inquiry datain blob");
1355
            ret = -EINVAL;
1356
            goto out;
1357
        }
1358
        memcpy(&iscsilun->lbp, inq_lbp,
1359
               sizeof(struct scsi_inquiry_logical_block_provisioning));
1360
        scsi_free_scsi_task(task);
1361
        task = NULL;
1362
    }
1363

    
1364
    if (iscsilun->lbp.lbpu || iscsilun->lbp.lbpws) {
1365
        struct scsi_inquiry_block_limits *inq_bl;
1366
        task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1367
                                SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS);
1368
        if (task == NULL) {
1369
            ret = -EINVAL;
1370
            goto out;
1371
        }
1372
        inq_bl = scsi_datain_unmarshall(task);
1373
        if (inq_bl == NULL) {
1374
            error_report("iSCSI: failed to unmarshall inquiry datain blob");
1375
            ret = -EINVAL;
1376
            goto out;
1377
        }
1378
        memcpy(&iscsilun->bl, inq_bl,
1379
               sizeof(struct scsi_inquiry_block_limits));
1380
        scsi_free_scsi_task(task);
1381
        task = NULL;
1382
    }
1383

    
1384
#if defined(LIBISCSI_FEATURE_NOP_COUNTER)
1385
    /* Set up a timer for sending out iSCSI NOPs */
1386
    iscsilun->nop_timer = timer_new_ms(QEMU_CLOCK_REALTIME, iscsi_nop_timed_event, iscsilun);
1387
    timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
1388
#endif
1389

    
1390
out:
1391
    qemu_opts_del(opts);
1392
    if (initiator_name != NULL) {
1393
        g_free(initiator_name);
1394
    }
1395
    if (iscsi_url != NULL) {
1396
        iscsi_destroy_url(iscsi_url);
1397
    }
1398
    if (task != NULL) {
1399
        scsi_free_scsi_task(task);
1400
    }
1401

    
1402
    if (ret) {
1403
        if (iscsi != NULL) {
1404
            iscsi_destroy_context(iscsi);
1405
        }
1406
        memset(iscsilun, 0, sizeof(IscsiLun));
1407
    }
1408
    return ret;
1409
}
1410

    
1411
static void iscsi_close(BlockDriverState *bs)
1412
{
1413
    IscsiLun *iscsilun = bs->opaque;
1414
    struct iscsi_context *iscsi = iscsilun->iscsi;
1415

    
1416
    if (iscsilun->nop_timer) {
1417
        timer_del(iscsilun->nop_timer);
1418
        timer_free(iscsilun->nop_timer);
1419
    }
1420
    qemu_aio_set_fd_handler(iscsi_get_fd(iscsi), NULL, NULL, NULL);
1421
    iscsi_destroy_context(iscsi);
1422
    memset(iscsilun, 0, sizeof(IscsiLun));
1423
}
1424

    
1425
static int iscsi_truncate(BlockDriverState *bs, int64_t offset)
1426
{
1427
    IscsiLun *iscsilun = bs->opaque;
1428
    int ret = 0;
1429

    
1430
    if (iscsilun->type != TYPE_DISK) {
1431
        return -ENOTSUP;
1432
    }
1433

    
1434
    if ((ret = iscsi_readcapacity_sync(iscsilun)) != 0) {
1435
        return ret;
1436
    }
1437

    
1438
    if (offset > iscsi_getlength(bs)) {
1439
        return -EINVAL;
1440
    }
1441

    
1442
    return 0;
1443
}
1444

    
1445
static int iscsi_has_zero_init(BlockDriverState *bs)
1446
{
1447
    return 0;
1448
}
1449

    
1450
static int iscsi_create(const char *filename, QEMUOptionParameter *options)
1451
{
1452
    int ret = 0;
1453
    int64_t total_size = 0;
1454
    BlockDriverState *bs;
1455
    IscsiLun *iscsilun = NULL;
1456
    QDict *bs_options;
1457

    
1458
    bs = bdrv_new("");
1459

    
1460
    /* Read out options */
1461
    while (options && options->name) {
1462
        if (!strcmp(options->name, "size")) {
1463
            total_size = options->value.n / BDRV_SECTOR_SIZE;
1464
        }
1465
        options++;
1466
    }
1467

    
1468
    bs->opaque = g_malloc0(sizeof(struct IscsiLun));
1469
    iscsilun = bs->opaque;
1470

    
1471
    bs_options = qdict_new();
1472
    qdict_put(bs_options, "filename", qstring_from_str(filename));
1473
    ret = iscsi_open(bs, bs_options, 0);
1474
    QDECREF(bs_options);
1475

    
1476
    if (ret != 0) {
1477
        goto out;
1478
    }
1479
    if (iscsilun->nop_timer) {
1480
        timer_del(iscsilun->nop_timer);
1481
        timer_free(iscsilun->nop_timer);
1482
    }
1483
    if (iscsilun->type != TYPE_DISK) {
1484
        ret = -ENODEV;
1485
        goto out;
1486
    }
1487
    if (bs->total_sectors < total_size) {
1488
        ret = -ENOSPC;
1489
        goto out;
1490
    }
1491

    
1492
    ret = 0;
1493
out:
1494
    if (iscsilun->iscsi != NULL) {
1495
        iscsi_destroy_context(iscsilun->iscsi);
1496
    }
1497
    g_free(bs->opaque);
1498
    bs->opaque = NULL;
1499
    bdrv_unref(bs);
1500
    return ret;
1501
}
1502

    
1503
static QEMUOptionParameter iscsi_create_options[] = {
1504
    {
1505
        .name = BLOCK_OPT_SIZE,
1506
        .type = OPT_SIZE,
1507
        .help = "Virtual disk size"
1508
    },
1509
    { NULL }
1510
};
1511

    
1512
static BlockDriver bdrv_iscsi = {
1513
    .format_name     = "iscsi",
1514
    .protocol_name   = "iscsi",
1515

    
1516
    .instance_size   = sizeof(IscsiLun),
1517
    .bdrv_file_open  = iscsi_open,
1518
    .bdrv_close      = iscsi_close,
1519
    .bdrv_create     = iscsi_create,
1520
    .create_options  = iscsi_create_options,
1521

    
1522
    .bdrv_getlength  = iscsi_getlength,
1523
    .bdrv_truncate   = iscsi_truncate,
1524

    
1525
    .bdrv_co_get_block_status = iscsi_co_get_block_status,
1526
    .bdrv_co_discard      = iscsi_co_discard,
1527

    
1528
    .bdrv_aio_readv  = iscsi_aio_readv,
1529
    .bdrv_aio_writev = iscsi_aio_writev,
1530
    .bdrv_aio_flush  = iscsi_aio_flush,
1531

    
1532
    .bdrv_has_zero_init = iscsi_has_zero_init,
1533

    
1534
#ifdef __linux__
1535
    .bdrv_ioctl       = iscsi_ioctl,
1536
    .bdrv_aio_ioctl   = iscsi_aio_ioctl,
1537
#endif
1538
};
1539

    
1540
static QemuOptsList qemu_iscsi_opts = {
1541
    .name = "iscsi",
1542
    .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head),
1543
    .desc = {
1544
        {
1545
            .name = "user",
1546
            .type = QEMU_OPT_STRING,
1547
            .help = "username for CHAP authentication to target",
1548
        },{
1549
            .name = "password",
1550
            .type = QEMU_OPT_STRING,
1551
            .help = "password for CHAP authentication to target",
1552
        },{
1553
            .name = "header-digest",
1554
            .type = QEMU_OPT_STRING,
1555
            .help = "HeaderDigest setting. "
1556
                    "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
1557
        },{
1558
            .name = "initiator-name",
1559
            .type = QEMU_OPT_STRING,
1560
            .help = "Initiator iqn name to use when connecting",
1561
        },
1562
        { /* end of list */ }
1563
    },
1564
};
1565

    
1566
static void iscsi_block_init(void)
1567
{
1568
    bdrv_register(&bdrv_iscsi);
1569
    qemu_add_opts(&qemu_iscsi_opts);
1570
}
1571

    
1572
block_init(iscsi_block_init);