Statistics
| Branch: | Revision:

root / hw / virtio-blk.c @ 37d5ddd6

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

    
22
typedef struct VirtIOBlock
23
{
24
    VirtIODevice vdev;
25
    BlockDriverState *bs;
26
    VirtQueue *vq;
27
    void *rq;
28
    QEMUBH *bh;
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
    BlockInterfaceErrorAction action =
62
        drive_get_on_error(req->dev->bs, is_read);
63
    VirtIOBlock *s = req->dev;
64

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

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

    
81
    return 1;
82
}
83

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

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

    
94
    virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
95
}
96

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

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

    
104
static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
105
{
106
    VirtIOBlockReq *req = qemu_mallocz(sizeof(*req));
107
    req->dev = s;
108
    return req;
109
}
110

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

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

    
122
    return req;
123
}
124

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
224
static void do_multiwrite(BlockDriverState *bs, BlockRequest *blkreq,
225
    int num_writes)
226
{
227
    int i, ret;
228
    ret = bdrv_aio_multiwrite(bs, blkreq, num_writes);
229

    
230
    if (ret != 0) {
231
        for (i = 0; i < num_writes; i++) {
232
            if (blkreq[i].error) {
233
                virtio_blk_rw_complete(blkreq[i].opaque, -EIO);
234
            }
235
        }
236
    }
237
}
238

    
239
static void virtio_blk_handle_flush(VirtIOBlockReq *req)
240
{
241
    BlockDriverAIOCB *acb;
242

    
243
    acb = bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req);
244
    if (!acb) {
245
        virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
246
    }
247
}
248

    
249
static void virtio_blk_handle_write(BlockRequest *blkreq, int *num_writes,
250
    VirtIOBlockReq *req, BlockDriverState **old_bs)
251
{
252
    if (req->dev->bs != *old_bs || *num_writes == 32) {
253
        if (*old_bs != NULL) {
254
            do_multiwrite(*old_bs, blkreq, *num_writes);
255
        }
256
        *num_writes = 0;
257
        *old_bs = req->dev->bs;
258
    }
259

    
260
    blkreq[*num_writes].sector = req->out->sector;
261
    blkreq[*num_writes].nb_sectors = req->qiov.size / 512;
262
    blkreq[*num_writes].qiov = &req->qiov;
263
    blkreq[*num_writes].cb = virtio_blk_rw_complete;
264
    blkreq[*num_writes].opaque = req;
265
    blkreq[*num_writes].error = 0;
266

    
267
    (*num_writes)++;
268
}
269

    
270
static void virtio_blk_handle_read(VirtIOBlockReq *req)
271
{
272
    BlockDriverAIOCB *acb;
273

    
274
    acb = bdrv_aio_readv(req->dev->bs, req->out->sector, &req->qiov,
275
                         req->qiov.size / 512, virtio_blk_rw_complete, req);
276
    if (!acb) {
277
        virtio_blk_rw_complete(req, -EIO);
278
    }
279
}
280

    
281
typedef struct MultiReqBuffer {
282
    BlockRequest        blkreq[32];
283
    int                 num_writes;
284
    BlockDriverState    *old_bs;
285
} MultiReqBuffer;
286

    
287
static void virtio_blk_handle_request(VirtIOBlockReq *req,
288
    MultiReqBuffer *mrb)
289
{
290
    if (req->elem.out_num < 1 || req->elem.in_num < 1) {
291
        fprintf(stderr, "virtio-blk missing headers\n");
292
        exit(1);
293
    }
294

    
295
    if (req->elem.out_sg[0].iov_len < sizeof(*req->out) ||
296
        req->elem.in_sg[req->elem.in_num - 1].iov_len < sizeof(*req->in)) {
297
        fprintf(stderr, "virtio-blk header not in correct element\n");
298
        exit(1);
299
    }
300

    
301
    req->out = (void *)req->elem.out_sg[0].iov_base;
302
    req->in = (void *)req->elem.in_sg[req->elem.in_num - 1].iov_base;
303

    
304
    if (req->out->type & VIRTIO_BLK_T_FLUSH) {
305
        virtio_blk_handle_flush(req);
306
    } else if (req->out->type & VIRTIO_BLK_T_SCSI_CMD) {
307
        virtio_blk_handle_scsi(req);
308
    } else if (req->out->type & VIRTIO_BLK_T_OUT) {
309
        qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1],
310
                                 req->elem.out_num - 1);
311
        virtio_blk_handle_write(mrb->blkreq, &mrb->num_writes,
312
            req, &mrb->old_bs);
313
    } else {
314
        qemu_iovec_init_external(&req->qiov, &req->elem.in_sg[0],
315
                                 req->elem.in_num - 1);
316
        virtio_blk_handle_read(req);
317
    }
318
}
319

    
320
static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
321
{
322
    VirtIOBlock *s = to_virtio_blk(vdev);
323
    VirtIOBlockReq *req;
324
    MultiReqBuffer mrb = {
325
        .num_writes = 0,
326
        .old_bs = NULL,
327
    };
328

    
329
    while ((req = virtio_blk_get_request(s))) {
330
        virtio_blk_handle_request(req, &mrb);
331
    }
332

    
333
    if (mrb.num_writes > 0) {
334
        do_multiwrite(mrb.old_bs, mrb.blkreq, mrb.num_writes);
335
    }
336

    
337
    /*
338
     * FIXME: Want to check for completions before returning to guest mode,
339
     * so cached reads and writes are reported as quickly as possible. But
340
     * that should be done in the generic block layer.
341
     */
342
}
343

    
344
static void virtio_blk_dma_restart_bh(void *opaque)
345
{
346
    VirtIOBlock *s = opaque;
347
    VirtIOBlockReq *req = s->rq;
348
    MultiReqBuffer mrb = {
349
        .num_writes = 0,
350
        .old_bs = NULL,
351
    };
352

    
353
    qemu_bh_delete(s->bh);
354
    s->bh = NULL;
355

    
356
    s->rq = NULL;
357

    
358
    while (req) {
359
        virtio_blk_handle_request(req, &mrb);
360
        req = req->next;
361
    }
362

    
363
    if (mrb.num_writes > 0) {
364
        do_multiwrite(mrb.old_bs, mrb.blkreq, mrb.num_writes);
365
    }
366
}
367

    
368
static void virtio_blk_dma_restart_cb(void *opaque, int running, int reason)
369
{
370
    VirtIOBlock *s = opaque;
371

    
372
    if (!running)
373
        return;
374

    
375
    if (!s->bh) {
376
        s->bh = qemu_bh_new(virtio_blk_dma_restart_bh, s);
377
        qemu_bh_schedule(s->bh);
378
    }
379
}
380

    
381
static void virtio_blk_reset(VirtIODevice *vdev)
382
{
383
    /*
384
     * This should cancel pending requests, but can't do nicely until there
385
     * are per-device request lists.
386
     */
387
    qemu_aio_flush();
388
}
389

    
390
/* coalesce internal state, copy to pci i/o region 0
391
 */
392
static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
393
{
394
    VirtIOBlock *s = to_virtio_blk(vdev);
395
    struct virtio_blk_config blkcfg;
396
    uint64_t capacity;
397
    int cylinders, heads, secs;
398

    
399
    bdrv_get_geometry(s->bs, &capacity);
400
    bdrv_get_geometry_hint(s->bs, &cylinders, &heads, &secs);
401
    memset(&blkcfg, 0, sizeof(blkcfg));
402
    stq_raw(&blkcfg.capacity, capacity);
403
    stl_raw(&blkcfg.seg_max, 128 - 2);
404
    stw_raw(&blkcfg.cylinders, cylinders);
405
    blkcfg.heads = heads;
406
    blkcfg.sectors = secs;
407
    blkcfg.size_max = 0;
408
    memcpy(config, &blkcfg, sizeof(struct virtio_blk_config));
409
}
410

    
411
static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features)
412
{
413
    VirtIOBlock *s = to_virtio_blk(vdev);
414

    
415
    features |= (1 << VIRTIO_BLK_F_SEG_MAX);
416
    features |= (1 << VIRTIO_BLK_F_GEOMETRY);
417

    
418
    if (bdrv_enable_write_cache(s->bs))
419
        features |= (1 << VIRTIO_BLK_F_WCACHE);
420
    
421
    if (bdrv_is_read_only(s->bs))
422
        features |= 1 << VIRTIO_BLK_F_RO;
423

    
424
    return features;
425
}
426

    
427
static void virtio_blk_save(QEMUFile *f, void *opaque)
428
{
429
    VirtIOBlock *s = opaque;
430
    VirtIOBlockReq *req = s->rq;
431

    
432
    virtio_save(&s->vdev, f);
433
    
434
    while (req) {
435
        qemu_put_sbyte(f, 1);
436
        qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
437
        req = req->next;
438
    }
439
    qemu_put_sbyte(f, 0);
440
}
441

    
442
static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
443
{
444
    VirtIOBlock *s = opaque;
445

    
446
    if (version_id != 2)
447
        return -EINVAL;
448

    
449
    virtio_load(&s->vdev, f);
450
    while (qemu_get_sbyte(f)) {
451
        VirtIOBlockReq *req = virtio_blk_alloc_request(s);
452
        qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
453
        req->next = s->rq;
454
        s->rq = req->next;
455
    }
456

    
457
    return 0;
458
}
459

    
460
VirtIODevice *virtio_blk_init(DeviceState *dev, DriveInfo *dinfo)
461
{
462
    VirtIOBlock *s;
463
    int cylinders, heads, secs;
464
    static int virtio_blk_id;
465

    
466
    s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK,
467
                                          sizeof(struct virtio_blk_config),
468
                                          sizeof(VirtIOBlock));
469

    
470
    s->vdev.get_config = virtio_blk_update_config;
471
    s->vdev.get_features = virtio_blk_get_features;
472
    s->vdev.reset = virtio_blk_reset;
473
    s->bs = dinfo->bdrv;
474
    s->rq = NULL;
475
    bdrv_guess_geometry(s->bs, &cylinders, &heads, &secs);
476
    bdrv_set_geometry_hint(s->bs, cylinders, heads, secs);
477

    
478
    s->vq = virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output);
479

    
480
    qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
481
    register_savevm("virtio-blk", virtio_blk_id++, 2,
482
                    virtio_blk_save, virtio_blk_load, s);
483

    
484
    return &s->vdev;
485
}