Statistics
| Branch: | Revision:

root / hw / virtio-blk.c @ 7d0d6950

History | View | Annotate | Download (13.3 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 "virtio-blk.h"
16
#ifdef __linux__
17
# include <scsi/sg.h>
18
#endif
19

    
20
typedef struct VirtIOBlock
21
{
22
    VirtIODevice vdev;
23
    BlockDriverState *bs;
24
    VirtQueue *vq;
25
    void *rq;
26
    QEMUBH *bh;
27
    BlockConf *conf;
28
    unsigned short sector_mask;
29
} VirtIOBlock;
30

    
31
static VirtIOBlock *to_virtio_blk(VirtIODevice *vdev)
32
{
33
    return (VirtIOBlock *)vdev;
34
}
35

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

    
47
static void virtio_blk_req_complete(VirtIOBlockReq *req, int status)
48
{
49
    VirtIOBlock *s = req->dev;
50

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

    
55
    qemu_free(req);
56
}
57

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

    
64
    if (action == BLOCK_ERR_IGNORE) {
65
        bdrv_mon_event(s->bs, BDRV_ACTION_IGNORE, is_read);
66
        return 0;
67
    }
68

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

    
80
    return 1;
81
}
82

    
83
static void virtio_blk_rw_complete(void *opaque, int ret)
84
{
85
    VirtIOBlockReq *req = opaque;
86

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

    
93
    virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
94
}
95

    
96
static void virtio_blk_flush_complete(void *opaque, int ret)
97
{
98
    VirtIOBlockReq *req = opaque;
99

    
100
    virtio_blk_req_complete(req, ret ? VIRTIO_BLK_S_IOERR : VIRTIO_BLK_S_OK);
101
}
102

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

    
112
static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s)
113
{
114
    VirtIOBlockReq *req = virtio_blk_alloc_request(s);
115

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

    
123
    return req;
124
}
125

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

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

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

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

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

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

    
174
        for (i = 0; i < hdr.iovec_count; i++)
175
            hdr.dxfer_len += req->elem.out_sg[i + 2].iov_len;
176

    
177
        hdr.dxferp = req->elem.out_sg + 2;
178

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

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

    
197
    hdr.sbp = req->elem.in_sg[req->elem.in_num - 3].iov_base;
198
    hdr.mx_sb_len = req->elem.in_sg[req->elem.in_num - 3].iov_len;
199

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

    
211
    req->scsi->errors = hdr.status;
212
    req->scsi->residual = hdr.resid;
213
    req->scsi->sense_len = hdr.sb_len_wr;
214
    req->scsi->data_len = hdr.dxfer_len;
215

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

    
225
typedef struct MultiReqBuffer {
226
    BlockRequest        blkreq[32];
227
    unsigned int        num_writes;
228
} MultiReqBuffer;
229

    
230
static void virtio_submit_multiwrite(BlockDriverState *bs, MultiReqBuffer *mrb)
231
{
232
    int i, ret;
233

    
234
    if (!mrb->num_writes) {
235
        return;
236
    }
237

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

    
247
    mrb->num_writes = 0;
248
}
249

    
250
static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
251
{
252
    BlockDriverAIOCB *acb;
253

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

    
259
    acb = bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req);
260
    if (!acb) {
261
        virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
262
    }
263
}
264

    
265
static void virtio_blk_handle_write(VirtIOBlockReq *req, MultiReqBuffer *mrb)
266
{
267
    BlockRequest *blkreq;
268

    
269
    if (req->out->sector & req->dev->sector_mask) {
270
        virtio_blk_rw_complete(req, -EIO);
271
        return;
272
    }
273

    
274
    if (mrb->num_writes == 32) {
275
        virtio_submit_multiwrite(req->dev->bs, mrb);
276
    }
277

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

    
286
    mrb->num_writes++;
287
}
288

    
289
static void virtio_blk_handle_read(VirtIOBlockReq *req)
290
{
291
    BlockDriverAIOCB *acb;
292

    
293
    if (req->out->sector & req->dev->sector_mask) {
294
        virtio_blk_rw_complete(req, -EIO);
295
        return;
296
    }
297

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

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

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

    
320
    req->out = (void *)req->elem.out_sg[0].iov_base;
321
    req->in = (void *)req->elem.in_sg[req->elem.in_num - 1].iov_base;
322

    
323
    if (req->out->type & VIRTIO_BLK_T_FLUSH) {
324
        virtio_blk_handle_flush(req, mrb);
325
    } else if (req->out->type & VIRTIO_BLK_T_SCSI_CMD) {
326
        virtio_blk_handle_scsi(req);
327
    } else if (req->out->type & VIRTIO_BLK_T_OUT) {
328
        qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1],
329
                                 req->elem.out_num - 1);
330
        virtio_blk_handle_write(req, mrb);
331
    } else {
332
        qemu_iovec_init_external(&req->qiov, &req->elem.in_sg[0],
333
                                 req->elem.in_num - 1);
334
        virtio_blk_handle_read(req);
335
    }
336
}
337

    
338
static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
339
{
340
    VirtIOBlock *s = to_virtio_blk(vdev);
341
    VirtIOBlockReq *req;
342
    MultiReqBuffer mrb = {
343
        .num_writes = 0,
344
    };
345

    
346
    while ((req = virtio_blk_get_request(s))) {
347
        virtio_blk_handle_request(req, &mrb);
348
    }
349

    
350
    virtio_submit_multiwrite(s->bs, &mrb);
351

    
352
    /*
353
     * FIXME: Want to check for completions before returning to guest mode,
354
     * so cached reads and writes are reported as quickly as possible. But
355
     * that should be done in the generic block layer.
356
     */
357
}
358

    
359
static void virtio_blk_dma_restart_bh(void *opaque)
360
{
361
    VirtIOBlock *s = opaque;
362
    VirtIOBlockReq *req = s->rq;
363
    MultiReqBuffer mrb = {
364
        .num_writes = 0,
365
    };
366

    
367
    qemu_bh_delete(s->bh);
368
    s->bh = NULL;
369

    
370
    s->rq = NULL;
371

    
372
    while (req) {
373
        virtio_blk_handle_request(req, &mrb);
374
        req = req->next;
375
    }
376

    
377
    virtio_submit_multiwrite(s->bs, &mrb);
378
}
379

    
380
static void virtio_blk_dma_restart_cb(void *opaque, int running, int reason)
381
{
382
    VirtIOBlock *s = opaque;
383

    
384
    if (!running)
385
        return;
386

    
387
    if (!s->bh) {
388
        s->bh = qemu_bh_new(virtio_blk_dma_restart_bh, s);
389
        qemu_bh_schedule(s->bh);
390
    }
391
}
392

    
393
static void virtio_blk_reset(VirtIODevice *vdev)
394
{
395
    /*
396
     * This should cancel pending requests, but can't do nicely until there
397
     * are per-device request lists.
398
     */
399
    qemu_aio_flush();
400
}
401

    
402
/* coalesce internal state, copy to pci i/o region 0
403
 */
404
static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
405
{
406
    VirtIOBlock *s = to_virtio_blk(vdev);
407
    struct virtio_blk_config blkcfg;
408
    uint64_t capacity;
409
    int cylinders, heads, secs;
410

    
411
    bdrv_get_geometry(s->bs, &capacity);
412
    bdrv_get_geometry_hint(s->bs, &cylinders, &heads, &secs);
413
    memset(&blkcfg, 0, sizeof(blkcfg));
414
    stq_raw(&blkcfg.capacity, capacity);
415
    stl_raw(&blkcfg.seg_max, 128 - 2);
416
    stw_raw(&blkcfg.cylinders, cylinders);
417
    blkcfg.heads = heads;
418
    blkcfg.sectors = secs & ~s->sector_mask;
419
    blkcfg.blk_size = s->conf->logical_block_size;
420
    blkcfg.size_max = 0;
421
    blkcfg.physical_block_exp = get_physical_block_exp(s->conf);
422
    blkcfg.alignment_offset = 0;
423
    blkcfg.min_io_size = s->conf->min_io_size / blkcfg.blk_size;
424
    blkcfg.opt_io_size = s->conf->opt_io_size / blkcfg.blk_size;
425
    memcpy(config, &blkcfg, sizeof(struct virtio_blk_config));
426
}
427

    
428
static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features)
429
{
430
    VirtIOBlock *s = to_virtio_blk(vdev);
431

    
432
    features |= (1 << VIRTIO_BLK_F_SEG_MAX);
433
    features |= (1 << VIRTIO_BLK_F_GEOMETRY);
434
    features |= (1 << VIRTIO_BLK_F_TOPOLOGY);
435
    features |= (1 << VIRTIO_BLK_F_BLK_SIZE);
436

    
437
    if (bdrv_enable_write_cache(s->bs))
438
        features |= (1 << VIRTIO_BLK_F_WCACHE);
439
    
440
    if (bdrv_is_read_only(s->bs))
441
        features |= 1 << VIRTIO_BLK_F_RO;
442

    
443
    return features;
444
}
445

    
446
static void virtio_blk_save(QEMUFile *f, void *opaque)
447
{
448
    VirtIOBlock *s = opaque;
449
    VirtIOBlockReq *req = s->rq;
450

    
451
    virtio_save(&s->vdev, f);
452
    
453
    while (req) {
454
        qemu_put_sbyte(f, 1);
455
        qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
456
        req = req->next;
457
    }
458
    qemu_put_sbyte(f, 0);
459
}
460

    
461
static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
462
{
463
    VirtIOBlock *s = opaque;
464

    
465
    if (version_id != 2)
466
        return -EINVAL;
467

    
468
    virtio_load(&s->vdev, f);
469
    while (qemu_get_sbyte(f)) {
470
        VirtIOBlockReq *req = virtio_blk_alloc_request(s);
471
        qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
472
        req->next = s->rq;
473
        s->rq = req;
474
    }
475

    
476
    return 0;
477
}
478

    
479
VirtIODevice *virtio_blk_init(DeviceState *dev, BlockConf *conf)
480
{
481
    VirtIOBlock *s;
482
    int cylinders, heads, secs;
483
    static int virtio_blk_id;
484

    
485
    s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK,
486
                                          sizeof(struct virtio_blk_config),
487
                                          sizeof(VirtIOBlock));
488

    
489
    s->vdev.get_config = virtio_blk_update_config;
490
    s->vdev.get_features = virtio_blk_get_features;
491
    s->vdev.reset = virtio_blk_reset;
492
    s->bs = conf->bs;
493
    s->conf = conf;
494
    s->rq = NULL;
495
    s->sector_mask = (s->conf->logical_block_size / BDRV_SECTOR_SIZE) - 1;
496
    bdrv_guess_geometry(s->bs, &cylinders, &heads, &secs);
497

    
498
    s->vq = virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output);
499

    
500
    qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
501
    register_savevm("virtio-blk", virtio_blk_id++, 2,
502
                    virtio_blk_save, virtio_blk_load, s);
503
    bdrv_set_removable(s->bs, 0);
504

    
505
    return &s->vdev;
506
}