Statistics
| Branch: | Revision:

root / hw / virtio-blk.c @ 9d0d3138

History | View | Annotate | Download (14.2 kB)

1
/*
2
 * Virtio Block Device
3
 *
4
 * Copyright IBM, Corp. 2007
5
 *
6
 * Authors:
7
 *  Anthony Liguori   <aliguori@us.ibm.com>
8
 *
9
 * This work is licensed under the terms of the GNU GPL, version 2.  See
10
 * the COPYING file in the top-level directory.
11
 *
12
 */
13

    
14
#include <qemu-common.h>
15
#include "qemu-error.h"
16
#include "virtio-blk.h"
17
#ifdef __linux__
18
# include <scsi/sg.h>
19
#endif
20

    
21
typedef struct VirtIOBlock
22
{
23
    VirtIODevice vdev;
24
    BlockDriverState *bs;
25
    VirtQueue *vq;
26
    void *rq;
27
    QEMUBH *bh;
28
    BlockConf *conf;
29
    unsigned short sector_mask;
30
    char sn[BLOCK_SERIAL_STRLEN];
31
    DeviceState *qdev;
32
} VirtIOBlock;
33

    
34
static VirtIOBlock *to_virtio_blk(VirtIODevice *vdev)
35
{
36
    return (VirtIOBlock *)vdev;
37
}
38

    
39
typedef struct VirtIOBlockReq
40
{
41
    VirtIOBlock *dev;
42
    VirtQueueElement elem;
43
    struct virtio_blk_inhdr *in;
44
    struct virtio_blk_outhdr *out;
45
    struct virtio_scsi_inhdr *scsi;
46
    QEMUIOVector qiov;
47
    struct VirtIOBlockReq *next;
48
} VirtIOBlockReq;
49

    
50
static void virtio_blk_req_complete(VirtIOBlockReq *req, int status)
51
{
52
    VirtIOBlock *s = req->dev;
53

    
54
    req->in->status = status;
55
    virtqueue_push(s->vq, &req->elem, req->qiov.size + sizeof(*req->in));
56
    virtio_notify(&s->vdev, s->vq);
57

    
58
    qemu_free(req);
59
}
60

    
61
static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
62
    int is_read)
63
{
64
    BlockErrorAction action = bdrv_get_on_error(req->dev->bs, is_read);
65
    VirtIOBlock *s = req->dev;
66

    
67
    if (action == BLOCK_ERR_IGNORE) {
68
        bdrv_mon_event(s->bs, BDRV_ACTION_IGNORE, is_read);
69
        return 0;
70
    }
71

    
72
    if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
73
            || action == BLOCK_ERR_STOP_ANY) {
74
        req->next = s->rq;
75
        s->rq = req;
76
        bdrv_mon_event(s->bs, BDRV_ACTION_STOP, is_read);
77
        vm_stop(0);
78
    } else {
79
        virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
80
        bdrv_mon_event(s->bs, BDRV_ACTION_REPORT, is_read);
81
    }
82

    
83
    return 1;
84
}
85

    
86
static void virtio_blk_rw_complete(void *opaque, int ret)
87
{
88
    VirtIOBlockReq *req = opaque;
89

    
90
    if (ret) {
91
        int is_read = !(req->out->type & VIRTIO_BLK_T_OUT);
92
        if (virtio_blk_handle_rw_error(req, -ret, is_read))
93
            return;
94
    }
95

    
96
    virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
97
}
98

    
99
static void virtio_blk_flush_complete(void *opaque, int ret)
100
{
101
    VirtIOBlockReq *req = opaque;
102

    
103
    virtio_blk_req_complete(req, ret ? VIRTIO_BLK_S_IOERR : VIRTIO_BLK_S_OK);
104
}
105

    
106
static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
107
{
108
    VirtIOBlockReq *req = qemu_malloc(sizeof(*req));
109
    req->dev = s;
110
    req->qiov.size = 0;
111
    req->next = NULL;
112
    return req;
113
}
114

    
115
static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s)
116
{
117
    VirtIOBlockReq *req = virtio_blk_alloc_request(s);
118

    
119
    if (req != NULL) {
120
        if (!virtqueue_pop(s->vq, &req->elem)) {
121
            qemu_free(req);
122
            return NULL;
123
        }
124
    }
125

    
126
    return req;
127
}
128

    
129
#ifdef __linux__
130
static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
131
{
132
    struct sg_io_hdr hdr;
133
    int ret;
134
    int status;
135
    int i;
136

    
137
    /*
138
     * We require at least one output segment each for the virtio_blk_outhdr
139
     * and the SCSI command block.
140
     *
141
     * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
142
     * and the sense buffer pointer in the input segments.
143
     */
144
    if (req->elem.out_num < 2 || req->elem.in_num < 3) {
145
        virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
146
        return;
147
    }
148

    
149
    /*
150
     * No support for bidirection commands yet.
151
     */
152
    if (req->elem.out_num > 2 && req->elem.in_num > 3) {
153
        virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
154
        return;
155
    }
156

    
157
    /*
158
     * The scsi inhdr is placed in the second-to-last input segment, just
159
     * before the regular inhdr.
160
     */
161
    req->scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base;
162

    
163
    memset(&hdr, 0, sizeof(struct sg_io_hdr));
164
    hdr.interface_id = 'S';
165
    hdr.cmd_len = req->elem.out_sg[1].iov_len;
166
    hdr.cmdp = req->elem.out_sg[1].iov_base;
167
    hdr.dxfer_len = 0;
168

    
169
    if (req->elem.out_num > 2) {
170
        /*
171
         * If there are more than the minimally required 2 output segments
172
         * there is write payload starting from the third iovec.
173
         */
174
        hdr.dxfer_direction = SG_DXFER_TO_DEV;
175
        hdr.iovec_count = req->elem.out_num - 2;
176

    
177
        for (i = 0; i < hdr.iovec_count; i++)
178
            hdr.dxfer_len += req->elem.out_sg[i + 2].iov_len;
179

    
180
        hdr.dxferp = req->elem.out_sg + 2;
181

    
182
    } else if (req->elem.in_num > 3) {
183
        /*
184
         * If we have more than 3 input segments the guest wants to actually
185
         * read data.
186
         */
187
        hdr.dxfer_direction = SG_DXFER_FROM_DEV;
188
        hdr.iovec_count = req->elem.in_num - 3;
189
        for (i = 0; i < hdr.iovec_count; i++)
190
            hdr.dxfer_len += req->elem.in_sg[i].iov_len;
191

    
192
        hdr.dxferp = req->elem.in_sg;
193
    } else {
194
        /*
195
         * Some SCSI commands don't actually transfer any data.
196
         */
197
        hdr.dxfer_direction = SG_DXFER_NONE;
198
    }
199

    
200
    hdr.sbp = req->elem.in_sg[req->elem.in_num - 3].iov_base;
201
    hdr.mx_sb_len = req->elem.in_sg[req->elem.in_num - 3].iov_len;
202

    
203
    ret = bdrv_ioctl(req->dev->bs, SG_IO, &hdr);
204
    if (ret) {
205
        status = VIRTIO_BLK_S_UNSUPP;
206
        hdr.status = ret;
207
        hdr.resid = hdr.dxfer_len;
208
    } else if (hdr.status) {
209
        status = VIRTIO_BLK_S_IOERR;
210
    } else {
211
        status = VIRTIO_BLK_S_OK;
212
    }
213

    
214
    req->scsi->errors = hdr.status;
215
    req->scsi->residual = hdr.resid;
216
    req->scsi->sense_len = hdr.sb_len_wr;
217
    req->scsi->data_len = hdr.dxfer_len;
218

    
219
    virtio_blk_req_complete(req, status);
220
}
221
#else
222
static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
223
{
224
    virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
225
}
226
#endif /* __linux__ */
227

    
228
typedef struct MultiReqBuffer {
229
    BlockRequest        blkreq[32];
230
    unsigned int        num_writes;
231
} MultiReqBuffer;
232

    
233
static void virtio_submit_multiwrite(BlockDriverState *bs, MultiReqBuffer *mrb)
234
{
235
    int i, ret;
236

    
237
    if (!mrb->num_writes) {
238
        return;
239
    }
240

    
241
    ret = bdrv_aio_multiwrite(bs, mrb->blkreq, mrb->num_writes);
242
    if (ret != 0) {
243
        for (i = 0; i < mrb->num_writes; i++) {
244
            if (mrb->blkreq[i].error) {
245
                virtio_blk_rw_complete(mrb->blkreq[i].opaque, -EIO);
246
            }
247
        }
248
    }
249

    
250
    mrb->num_writes = 0;
251
}
252

    
253
static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
254
{
255
    BlockDriverAIOCB *acb;
256

    
257
    /*
258
     * Make sure all outstanding writes are posted to the backing device.
259
     */
260
    virtio_submit_multiwrite(req->dev->bs, mrb);
261

    
262
    acb = bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req);
263
    if (!acb) {
264
        virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
265
    }
266
}
267

    
268
static void virtio_blk_handle_write(VirtIOBlockReq *req, MultiReqBuffer *mrb)
269
{
270
    BlockRequest *blkreq;
271

    
272
    if (req->out->sector & req->dev->sector_mask) {
273
        virtio_blk_rw_complete(req, -EIO);
274
        return;
275
    }
276

    
277
    if (mrb->num_writes == 32) {
278
        virtio_submit_multiwrite(req->dev->bs, mrb);
279
    }
280

    
281
    blkreq = &mrb->blkreq[mrb->num_writes];
282
    blkreq->sector = req->out->sector;
283
    blkreq->nb_sectors = req->qiov.size / BDRV_SECTOR_SIZE;
284
    blkreq->qiov = &req->qiov;
285
    blkreq->cb = virtio_blk_rw_complete;
286
    blkreq->opaque = req;
287
    blkreq->error = 0;
288

    
289
    mrb->num_writes++;
290
}
291

    
292
static void virtio_blk_handle_read(VirtIOBlockReq *req)
293
{
294
    BlockDriverAIOCB *acb;
295

    
296
    if (req->out->sector & req->dev->sector_mask) {
297
        virtio_blk_rw_complete(req, -EIO);
298
        return;
299
    }
300

    
301
    acb = bdrv_aio_readv(req->dev->bs, req->out->sector, &req->qiov,
302
                         req->qiov.size / BDRV_SECTOR_SIZE,
303
                         virtio_blk_rw_complete, req);
304
    if (!acb) {
305
        virtio_blk_rw_complete(req, -EIO);
306
    }
307
}
308

    
309
static void virtio_blk_handle_request(VirtIOBlockReq *req,
310
    MultiReqBuffer *mrb)
311
{
312
    if (req->elem.out_num < 1 || req->elem.in_num < 1) {
313
        fprintf(stderr, "virtio-blk missing headers\n");
314
        exit(1);
315
    }
316

    
317
    if (req->elem.out_sg[0].iov_len < sizeof(*req->out) ||
318
        req->elem.in_sg[req->elem.in_num - 1].iov_len < sizeof(*req->in)) {
319
        fprintf(stderr, "virtio-blk header not in correct element\n");
320
        exit(1);
321
    }
322

    
323
    req->out = (void *)req->elem.out_sg[0].iov_base;
324
    req->in = (void *)req->elem.in_sg[req->elem.in_num - 1].iov_base;
325

    
326
    if (req->out->type & VIRTIO_BLK_T_FLUSH) {
327
        virtio_blk_handle_flush(req, mrb);
328
    } else if (req->out->type & VIRTIO_BLK_T_SCSI_CMD) {
329
        virtio_blk_handle_scsi(req);
330
    } else if (req->out->type & VIRTIO_BLK_T_GET_ID) {
331
        VirtIOBlock *s = req->dev;
332

    
333
        memcpy(req->elem.in_sg[0].iov_base, s->sn,
334
               MIN(req->elem.in_sg[0].iov_len, sizeof(s->sn)));
335
        virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
336
    } else if (req->out->type & VIRTIO_BLK_T_OUT) {
337
        qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1],
338
                                 req->elem.out_num - 1);
339
        virtio_blk_handle_write(req, mrb);
340
    } else {
341
        qemu_iovec_init_external(&req->qiov, &req->elem.in_sg[0],
342
                                 req->elem.in_num - 1);
343
        virtio_blk_handle_read(req);
344
    }
345
}
346

    
347
static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
348
{
349
    VirtIOBlock *s = to_virtio_blk(vdev);
350
    VirtIOBlockReq *req;
351
    MultiReqBuffer mrb = {
352
        .num_writes = 0,
353
    };
354

    
355
    while ((req = virtio_blk_get_request(s))) {
356
        virtio_blk_handle_request(req, &mrb);
357
    }
358

    
359
    virtio_submit_multiwrite(s->bs, &mrb);
360

    
361
    /*
362
     * FIXME: Want to check for completions before returning to guest mode,
363
     * so cached reads and writes are reported as quickly as possible. But
364
     * that should be done in the generic block layer.
365
     */
366
}
367

    
368
static void virtio_blk_dma_restart_bh(void *opaque)
369
{
370
    VirtIOBlock *s = opaque;
371
    VirtIOBlockReq *req = s->rq;
372
    MultiReqBuffer mrb = {
373
        .num_writes = 0,
374
    };
375

    
376
    qemu_bh_delete(s->bh);
377
    s->bh = NULL;
378

    
379
    s->rq = NULL;
380

    
381
    while (req) {
382
        virtio_blk_handle_request(req, &mrb);
383
        req = req->next;
384
    }
385

    
386
    virtio_submit_multiwrite(s->bs, &mrb);
387
}
388

    
389
static void virtio_blk_dma_restart_cb(void *opaque, int running, int reason)
390
{
391
    VirtIOBlock *s = opaque;
392

    
393
    if (!running)
394
        return;
395

    
396
    if (!s->bh) {
397
        s->bh = qemu_bh_new(virtio_blk_dma_restart_bh, s);
398
        qemu_bh_schedule(s->bh);
399
    }
400
}
401

    
402
static void virtio_blk_reset(VirtIODevice *vdev)
403
{
404
    /*
405
     * This should cancel pending requests, but can't do nicely until there
406
     * are per-device request lists.
407
     */
408
    qemu_aio_flush();
409
}
410

    
411
/* coalesce internal state, copy to pci i/o region 0
412
 */
413
static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
414
{
415
    VirtIOBlock *s = to_virtio_blk(vdev);
416
    struct virtio_blk_config blkcfg;
417
    uint64_t capacity;
418
    int cylinders, heads, secs;
419

    
420
    bdrv_get_geometry(s->bs, &capacity);
421
    bdrv_get_geometry_hint(s->bs, &cylinders, &heads, &secs);
422
    memset(&blkcfg, 0, sizeof(blkcfg));
423
    stq_raw(&blkcfg.capacity, capacity);
424
    stl_raw(&blkcfg.seg_max, 128 - 2);
425
    stw_raw(&blkcfg.cylinders, cylinders);
426
    blkcfg.heads = heads;
427
    blkcfg.sectors = secs & ~s->sector_mask;
428
    blkcfg.blk_size = s->conf->logical_block_size;
429
    blkcfg.size_max = 0;
430
    blkcfg.physical_block_exp = get_physical_block_exp(s->conf);
431
    blkcfg.alignment_offset = 0;
432
    blkcfg.min_io_size = s->conf->min_io_size / blkcfg.blk_size;
433
    blkcfg.opt_io_size = s->conf->opt_io_size / blkcfg.blk_size;
434
    memcpy(config, &blkcfg, sizeof(struct virtio_blk_config));
435
}
436

    
437
static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features)
438
{
439
    VirtIOBlock *s = to_virtio_blk(vdev);
440

    
441
    features |= (1 << VIRTIO_BLK_F_SEG_MAX);
442
    features |= (1 << VIRTIO_BLK_F_GEOMETRY);
443
    features |= (1 << VIRTIO_BLK_F_TOPOLOGY);
444
    features |= (1 << VIRTIO_BLK_F_BLK_SIZE);
445

    
446
    if (bdrv_enable_write_cache(s->bs))
447
        features |= (1 << VIRTIO_BLK_F_WCACHE);
448
    
449
    if (bdrv_is_read_only(s->bs))
450
        features |= 1 << VIRTIO_BLK_F_RO;
451

    
452
    return features;
453
}
454

    
455
static void virtio_blk_save(QEMUFile *f, void *opaque)
456
{
457
    VirtIOBlock *s = opaque;
458
    VirtIOBlockReq *req = s->rq;
459

    
460
    virtio_save(&s->vdev, f);
461
    
462
    while (req) {
463
        qemu_put_sbyte(f, 1);
464
        qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
465
        req = req->next;
466
    }
467
    qemu_put_sbyte(f, 0);
468
}
469

    
470
static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
471
{
472
    VirtIOBlock *s = opaque;
473

    
474
    if (version_id != 2)
475
        return -EINVAL;
476

    
477
    virtio_load(&s->vdev, f);
478
    while (qemu_get_sbyte(f)) {
479
        VirtIOBlockReq *req = virtio_blk_alloc_request(s);
480
        qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
481
        req->next = s->rq;
482
        s->rq = req;
483
    }
484

    
485
    return 0;
486
}
487

    
488
VirtIODevice *virtio_blk_init(DeviceState *dev, BlockConf *conf)
489
{
490
    VirtIOBlock *s;
491
    int cylinders, heads, secs;
492
    static int virtio_blk_id;
493
    DriveInfo *dinfo;
494

    
495
    if (!conf->bs) {
496
        error_report("virtio-blk-pci: drive property not set");
497
        return NULL;
498
    }
499
    if (!bdrv_is_inserted(conf->bs)) {
500
        error_report("Device needs media, but drive is empty");
501
        return NULL;
502
    }
503

    
504
    s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK,
505
                                          sizeof(struct virtio_blk_config),
506
                                          sizeof(VirtIOBlock));
507

    
508
    s->vdev.get_config = virtio_blk_update_config;
509
    s->vdev.get_features = virtio_blk_get_features;
510
    s->vdev.reset = virtio_blk_reset;
511
    s->bs = conf->bs;
512
    s->conf = conf;
513
    s->rq = NULL;
514
    s->sector_mask = (s->conf->logical_block_size / BDRV_SECTOR_SIZE) - 1;
515
    bdrv_guess_geometry(s->bs, &cylinders, &heads, &secs);
516

    
517
    /* NB: per existing s/n string convention the string is terminated
518
     * by '\0' only when less than sizeof (s->sn)
519
     */
520
    dinfo = drive_get_by_blockdev(s->bs);
521
    strncpy(s->sn, dinfo->serial, sizeof (s->sn));
522

    
523
    s->vq = virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output);
524

    
525
    qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
526
    s->qdev = dev;
527
    register_savevm(dev, "virtio-blk", virtio_blk_id++, 2,
528
                    virtio_blk_save, virtio_blk_load, s);
529
    bdrv_set_removable(s->bs, 0);
530

    
531
    return &s->vdev;
532
}
533

    
534
void virtio_blk_exit(VirtIODevice *vdev)
535
{
536
    VirtIOBlock *s = to_virtio_blk(vdev);
537
    unregister_savevm(s->qdev, "virtio-blk", s);
538
}