Statistics
| Branch: | Revision:

root / hw / virtio-blk.c @ be62a2eb

History | View | Annotate | Download (16.8 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 "trace.h"
17
#include "blockdev.h"
18
#include "virtio-blk.h"
19
#include "scsi-defs.h"
20
#ifdef __linux__
21
# include <scsi/sg.h>
22
#endif
23

    
24
typedef struct VirtIOBlock
25
{
26
    VirtIODevice vdev;
27
    BlockDriverState *bs;
28
    VirtQueue *vq;
29
    void *rq;
30
    QEMUBH *bh;
31
    BlockConf *conf;
32
    char *serial;
33
    unsigned short sector_mask;
34
    DeviceState *qdev;
35
} VirtIOBlock;
36

    
37
static VirtIOBlock *to_virtio_blk(VirtIODevice *vdev)
38
{
39
    return (VirtIOBlock *)vdev;
40
}
41

    
42
typedef struct VirtIOBlockReq
43
{
44
    VirtIOBlock *dev;
45
    VirtQueueElement elem;
46
    struct virtio_blk_inhdr *in;
47
    struct virtio_blk_outhdr *out;
48
    struct virtio_scsi_inhdr *scsi;
49
    QEMUIOVector qiov;
50
    struct VirtIOBlockReq *next;
51
    BlockAcctCookie acct;
52
} VirtIOBlockReq;
53

    
54
static void virtio_blk_req_complete(VirtIOBlockReq *req, int status)
55
{
56
    VirtIOBlock *s = req->dev;
57

    
58
    trace_virtio_blk_req_complete(req, status);
59

    
60
    stb_p(&req->in->status, status);
61
    virtqueue_push(s->vq, &req->elem, req->qiov.size + sizeof(*req->in));
62
    virtio_notify(&s->vdev, s->vq);
63
}
64

    
65
static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
66
    int is_read)
67
{
68
    BlockErrorAction action = bdrv_get_on_error(req->dev->bs, is_read);
69
    VirtIOBlock *s = req->dev;
70

    
71
    if (action == BLOCK_ERR_IGNORE) {
72
        bdrv_mon_event(s->bs, BDRV_ACTION_IGNORE, is_read);
73
        return 0;
74
    }
75

    
76
    if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
77
            || action == BLOCK_ERR_STOP_ANY) {
78
        req->next = s->rq;
79
        s->rq = req;
80
        bdrv_mon_event(s->bs, BDRV_ACTION_STOP, is_read);
81
        vm_stop(RUN_STATE_IO_ERROR);
82
        bdrv_iostatus_set_err(s->bs, error);
83
    } else {
84
        virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
85
        bdrv_acct_done(s->bs, &req->acct);
86
        g_free(req);
87
        bdrv_mon_event(s->bs, BDRV_ACTION_REPORT, is_read);
88
    }
89

    
90
    return 1;
91
}
92

    
93
static void virtio_blk_rw_complete(void *opaque, int ret)
94
{
95
    VirtIOBlockReq *req = opaque;
96

    
97
    trace_virtio_blk_rw_complete(req, ret);
98

    
99
    if (ret) {
100
        int is_read = !(ldl_p(&req->out->type) & VIRTIO_BLK_T_OUT);
101
        if (virtio_blk_handle_rw_error(req, -ret, is_read))
102
            return;
103
    }
104

    
105
    virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
106
    bdrv_acct_done(req->dev->bs, &req->acct);
107
    g_free(req);
108
}
109

    
110
static void virtio_blk_flush_complete(void *opaque, int ret)
111
{
112
    VirtIOBlockReq *req = opaque;
113

    
114
    if (ret) {
115
        if (virtio_blk_handle_rw_error(req, -ret, 0)) {
116
            return;
117
        }
118
    }
119

    
120
    virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
121
    bdrv_acct_done(req->dev->bs, &req->acct);
122
    g_free(req);
123
}
124

    
125
static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
126
{
127
    VirtIOBlockReq *req = g_malloc(sizeof(*req));
128
    req->dev = s;
129
    req->qiov.size = 0;
130
    req->next = NULL;
131
    return req;
132
}
133

    
134
static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s)
135
{
136
    VirtIOBlockReq *req = virtio_blk_alloc_request(s);
137

    
138
    if (req != NULL) {
139
        if (!virtqueue_pop(s->vq, &req->elem)) {
140
            g_free(req);
141
            return NULL;
142
        }
143
    }
144

    
145
    return req;
146
}
147

    
148
#ifdef __linux__
149
static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
150
{
151
    struct sg_io_hdr hdr;
152
    int ret;
153
    int status;
154
    int i;
155

    
156
    /*
157
     * We require at least one output segment each for the virtio_blk_outhdr
158
     * and the SCSI command block.
159
     *
160
     * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
161
     * and the sense buffer pointer in the input segments.
162
     */
163
    if (req->elem.out_num < 2 || req->elem.in_num < 3) {
164
        virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
165
        g_free(req);
166
        return;
167
    }
168

    
169
    /*
170
     * No support for bidirection commands yet.
171
     */
172
    if (req->elem.out_num > 2 && req->elem.in_num > 3) {
173
        virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
174
        g_free(req);
175
        return;
176
    }
177

    
178
    /*
179
     * The scsi inhdr is placed in the second-to-last input segment, just
180
     * before the regular inhdr.
181
     */
182
    req->scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base;
183

    
184
    memset(&hdr, 0, sizeof(struct sg_io_hdr));
185
    hdr.interface_id = 'S';
186
    hdr.cmd_len = req->elem.out_sg[1].iov_len;
187
    hdr.cmdp = req->elem.out_sg[1].iov_base;
188
    hdr.dxfer_len = 0;
189

    
190
    if (req->elem.out_num > 2) {
191
        /*
192
         * If there are more than the minimally required 2 output segments
193
         * there is write payload starting from the third iovec.
194
         */
195
        hdr.dxfer_direction = SG_DXFER_TO_DEV;
196
        hdr.iovec_count = req->elem.out_num - 2;
197

    
198
        for (i = 0; i < hdr.iovec_count; i++)
199
            hdr.dxfer_len += req->elem.out_sg[i + 2].iov_len;
200

    
201
        hdr.dxferp = req->elem.out_sg + 2;
202

    
203
    } else if (req->elem.in_num > 3) {
204
        /*
205
         * If we have more than 3 input segments the guest wants to actually
206
         * read data.
207
         */
208
        hdr.dxfer_direction = SG_DXFER_FROM_DEV;
209
        hdr.iovec_count = req->elem.in_num - 3;
210
        for (i = 0; i < hdr.iovec_count; i++)
211
            hdr.dxfer_len += req->elem.in_sg[i].iov_len;
212

    
213
        hdr.dxferp = req->elem.in_sg;
214
    } else {
215
        /*
216
         * Some SCSI commands don't actually transfer any data.
217
         */
218
        hdr.dxfer_direction = SG_DXFER_NONE;
219
    }
220

    
221
    hdr.sbp = req->elem.in_sg[req->elem.in_num - 3].iov_base;
222
    hdr.mx_sb_len = req->elem.in_sg[req->elem.in_num - 3].iov_len;
223

    
224
    ret = bdrv_ioctl(req->dev->bs, SG_IO, &hdr);
225
    if (ret) {
226
        status = VIRTIO_BLK_S_UNSUPP;
227
        hdr.status = ret;
228
        hdr.resid = hdr.dxfer_len;
229
    } else if (hdr.status) {
230
        status = VIRTIO_BLK_S_IOERR;
231
    } else {
232
        status = VIRTIO_BLK_S_OK;
233
    }
234

    
235
    /*
236
     * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi)
237
     * clear the masked_status field [hence status gets cleared too, see
238
     * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED
239
     * status has occurred.  However they do set DRIVER_SENSE in driver_status
240
     * field. Also a (sb_len_wr > 0) indicates there is a sense buffer.
241
     */
242
    if (hdr.status == 0 && hdr.sb_len_wr > 0) {
243
        hdr.status = CHECK_CONDITION;
244
    }
245

    
246
    stl_p(&req->scsi->errors,
247
          hdr.status | (hdr.msg_status << 8) |
248
          (hdr.host_status << 16) | (hdr.driver_status << 24));
249
    stl_p(&req->scsi->residual, hdr.resid);
250
    stl_p(&req->scsi->sense_len, hdr.sb_len_wr);
251
    stl_p(&req->scsi->data_len, hdr.dxfer_len);
252

    
253
    virtio_blk_req_complete(req, status);
254
    g_free(req);
255
}
256
#else
257
static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
258
{
259
    virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
260
    g_free(req);
261
}
262
#endif /* __linux__ */
263

    
264
typedef struct MultiReqBuffer {
265
    BlockRequest        blkreq[32];
266
    unsigned int        num_writes;
267
} MultiReqBuffer;
268

    
269
static void virtio_submit_multiwrite(BlockDriverState *bs, MultiReqBuffer *mrb)
270
{
271
    int i, ret;
272

    
273
    if (!mrb->num_writes) {
274
        return;
275
    }
276

    
277
    ret = bdrv_aio_multiwrite(bs, mrb->blkreq, mrb->num_writes);
278
    if (ret != 0) {
279
        for (i = 0; i < mrb->num_writes; i++) {
280
            if (mrb->blkreq[i].error) {
281
                virtio_blk_rw_complete(mrb->blkreq[i].opaque, -EIO);
282
            }
283
        }
284
    }
285

    
286
    mrb->num_writes = 0;
287
}
288

    
289
static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
290
{
291
    bdrv_acct_start(req->dev->bs, &req->acct, 0, BDRV_ACCT_FLUSH);
292

    
293
    /*
294
     * Make sure all outstanding writes are posted to the backing device.
295
     */
296
    virtio_submit_multiwrite(req->dev->bs, mrb);
297
    bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req);
298
}
299

    
300
static void virtio_blk_handle_write(VirtIOBlockReq *req, MultiReqBuffer *mrb)
301
{
302
    BlockRequest *blkreq;
303
    uint64_t sector;
304

    
305
    sector = ldq_p(&req->out->sector);
306

    
307
    bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_WRITE);
308

    
309
    trace_virtio_blk_handle_write(req, sector, req->qiov.size / 512);
310

    
311
    if (sector & req->dev->sector_mask) {
312
        virtio_blk_rw_complete(req, -EIO);
313
        return;
314
    }
315
    if (req->qiov.size % req->dev->conf->logical_block_size) {
316
        virtio_blk_rw_complete(req, -EIO);
317
        return;
318
    }
319

    
320
    if (mrb->num_writes == 32) {
321
        virtio_submit_multiwrite(req->dev->bs, mrb);
322
    }
323

    
324
    blkreq = &mrb->blkreq[mrb->num_writes];
325
    blkreq->sector = sector;
326
    blkreq->nb_sectors = req->qiov.size / BDRV_SECTOR_SIZE;
327
    blkreq->qiov = &req->qiov;
328
    blkreq->cb = virtio_blk_rw_complete;
329
    blkreq->opaque = req;
330
    blkreq->error = 0;
331

    
332
    mrb->num_writes++;
333
}
334

    
335
static void virtio_blk_handle_read(VirtIOBlockReq *req)
336
{
337
    uint64_t sector;
338

    
339
    sector = ldq_p(&req->out->sector);
340

    
341
    bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_READ);
342

    
343
    if (sector & req->dev->sector_mask) {
344
        virtio_blk_rw_complete(req, -EIO);
345
        return;
346
    }
347
    if (req->qiov.size % req->dev->conf->logical_block_size) {
348
        virtio_blk_rw_complete(req, -EIO);
349
        return;
350
    }
351
    bdrv_aio_readv(req->dev->bs, sector, &req->qiov,
352
                   req->qiov.size / BDRV_SECTOR_SIZE,
353
                   virtio_blk_rw_complete, req);
354
}
355

    
356
static void virtio_blk_handle_request(VirtIOBlockReq *req,
357
    MultiReqBuffer *mrb)
358
{
359
    uint32_t type;
360

    
361
    if (req->elem.out_num < 1 || req->elem.in_num < 1) {
362
        error_report("virtio-blk missing headers");
363
        exit(1);
364
    }
365

    
366
    if (req->elem.out_sg[0].iov_len < sizeof(*req->out) ||
367
        req->elem.in_sg[req->elem.in_num - 1].iov_len < sizeof(*req->in)) {
368
        error_report("virtio-blk header not in correct element");
369
        exit(1);
370
    }
371

    
372
    req->out = (void *)req->elem.out_sg[0].iov_base;
373
    req->in = (void *)req->elem.in_sg[req->elem.in_num - 1].iov_base;
374

    
375
    type = ldl_p(&req->out->type);
376

    
377
    if (type & VIRTIO_BLK_T_FLUSH) {
378
        virtio_blk_handle_flush(req, mrb);
379
    } else if (type & VIRTIO_BLK_T_SCSI_CMD) {
380
        virtio_blk_handle_scsi(req);
381
    } else if (type & VIRTIO_BLK_T_GET_ID) {
382
        VirtIOBlock *s = req->dev;
383

    
384
        /*
385
         * NB: per existing s/n string convention the string is
386
         * terminated by '\0' only when shorter than buffer.
387
         */
388
        strncpy(req->elem.in_sg[0].iov_base,
389
                s->serial ? s->serial : "",
390
                MIN(req->elem.in_sg[0].iov_len, VIRTIO_BLK_ID_BYTES));
391
        virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
392
        g_free(req);
393
    } else if (type & VIRTIO_BLK_T_OUT) {
394
        qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1],
395
                                 req->elem.out_num - 1);
396
        virtio_blk_handle_write(req, mrb);
397
    } else {
398
        qemu_iovec_init_external(&req->qiov, &req->elem.in_sg[0],
399
                                 req->elem.in_num - 1);
400
        virtio_blk_handle_read(req);
401
    }
402
}
403

    
404
static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
405
{
406
    VirtIOBlock *s = to_virtio_blk(vdev);
407
    VirtIOBlockReq *req;
408
    MultiReqBuffer mrb = {
409
        .num_writes = 0,
410
    };
411

    
412
    while ((req = virtio_blk_get_request(s))) {
413
        virtio_blk_handle_request(req, &mrb);
414
    }
415

    
416
    virtio_submit_multiwrite(s->bs, &mrb);
417

    
418
    /*
419
     * FIXME: Want to check for completions before returning to guest mode,
420
     * so cached reads and writes are reported as quickly as possible. But
421
     * that should be done in the generic block layer.
422
     */
423
}
424

    
425
static void virtio_blk_dma_restart_bh(void *opaque)
426
{
427
    VirtIOBlock *s = opaque;
428
    VirtIOBlockReq *req = s->rq;
429
    MultiReqBuffer mrb = {
430
        .num_writes = 0,
431
    };
432

    
433
    qemu_bh_delete(s->bh);
434
    s->bh = NULL;
435

    
436
    s->rq = NULL;
437

    
438
    while (req) {
439
        virtio_blk_handle_request(req, &mrb);
440
        req = req->next;
441
    }
442

    
443
    virtio_submit_multiwrite(s->bs, &mrb);
444
}
445

    
446
static void virtio_blk_dma_restart_cb(void *opaque, int running,
447
                                      RunState state)
448
{
449
    VirtIOBlock *s = opaque;
450

    
451
    if (!running)
452
        return;
453

    
454
    if (!s->bh) {
455
        s->bh = qemu_bh_new(virtio_blk_dma_restart_bh, s);
456
        qemu_bh_schedule(s->bh);
457
    }
458
}
459

    
460
static void virtio_blk_reset(VirtIODevice *vdev)
461
{
462
    /*
463
     * This should cancel pending requests, but can't do nicely until there
464
     * are per-device request lists.
465
     */
466
    bdrv_drain_all();
467
}
468

    
469
/* coalesce internal state, copy to pci i/o region 0
470
 */
471
static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
472
{
473
    VirtIOBlock *s = to_virtio_blk(vdev);
474
    struct virtio_blk_config blkcfg;
475
    uint64_t capacity;
476
    int cylinders, heads, secs;
477
    int blk_size = s->conf->logical_block_size;
478

    
479
    bdrv_get_geometry(s->bs, &capacity);
480
    bdrv_get_geometry_hint(s->bs, &cylinders, &heads, &secs);
481
    memset(&blkcfg, 0, sizeof(blkcfg));
482
    stq_raw(&blkcfg.capacity, capacity);
483
    stl_raw(&blkcfg.seg_max, 128 - 2);
484
    stw_raw(&blkcfg.cylinders, cylinders);
485
    stl_raw(&blkcfg.blk_size, blk_size);
486
    stw_raw(&blkcfg.min_io_size, s->conf->min_io_size / blk_size);
487
    stw_raw(&blkcfg.opt_io_size, s->conf->opt_io_size / blk_size);
488
    blkcfg.heads = heads;
489
    blkcfg.sectors = secs & ~s->sector_mask;
490
    blkcfg.size_max = 0;
491
    blkcfg.physical_block_exp = get_physical_block_exp(s->conf);
492
    blkcfg.alignment_offset = 0;
493
    memcpy(config, &blkcfg, sizeof(struct virtio_blk_config));
494
}
495

    
496
static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features)
497
{
498
    VirtIOBlock *s = to_virtio_blk(vdev);
499

    
500
    features |= (1 << VIRTIO_BLK_F_SEG_MAX);
501
    features |= (1 << VIRTIO_BLK_F_GEOMETRY);
502
    features |= (1 << VIRTIO_BLK_F_TOPOLOGY);
503
    features |= (1 << VIRTIO_BLK_F_BLK_SIZE);
504

    
505
    if (bdrv_enable_write_cache(s->bs))
506
        features |= (1 << VIRTIO_BLK_F_WCACHE);
507
    
508
    if (bdrv_is_read_only(s->bs))
509
        features |= 1 << VIRTIO_BLK_F_RO;
510

    
511
    return features;
512
}
513

    
514
static void virtio_blk_save(QEMUFile *f, void *opaque)
515
{
516
    VirtIOBlock *s = opaque;
517
    VirtIOBlockReq *req = s->rq;
518

    
519
    virtio_save(&s->vdev, f);
520
    
521
    while (req) {
522
        qemu_put_sbyte(f, 1);
523
        qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
524
        req = req->next;
525
    }
526
    qemu_put_sbyte(f, 0);
527
}
528

    
529
static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
530
{
531
    VirtIOBlock *s = opaque;
532

    
533
    if (version_id != 2)
534
        return -EINVAL;
535

    
536
    virtio_load(&s->vdev, f);
537
    while (qemu_get_sbyte(f)) {
538
        VirtIOBlockReq *req = virtio_blk_alloc_request(s);
539
        qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
540
        req->next = s->rq;
541
        s->rq = req;
542

    
543
        virtqueue_map_sg(req->elem.in_sg, req->elem.in_addr,
544
            req->elem.in_num, 1);
545
        virtqueue_map_sg(req->elem.out_sg, req->elem.out_addr,
546
            req->elem.out_num, 0);
547
    }
548

    
549
    return 0;
550
}
551

    
552
static void virtio_blk_resize(void *opaque)
553
{
554
    VirtIOBlock *s = opaque;
555

    
556
    virtio_notify_config(&s->vdev);
557
}
558

    
559
static const BlockDevOps virtio_block_ops = {
560
    .resize_cb = virtio_blk_resize,
561
};
562

    
563
VirtIODevice *virtio_blk_init(DeviceState *dev, BlockConf *conf,
564
                              char **serial)
565
{
566
    VirtIOBlock *s;
567
    int cylinders, heads, secs;
568
    static int virtio_blk_id;
569
    DriveInfo *dinfo;
570

    
571
    if (!conf->bs) {
572
        error_report("virtio-blk-pci: drive property not set");
573
        return NULL;
574
    }
575
    if (!bdrv_is_inserted(conf->bs)) {
576
        error_report("Device needs media, but drive is empty");
577
        return NULL;
578
    }
579

    
580
    if (!*serial) {
581
        /* try to fall back to value set with legacy -drive serial=... */
582
        dinfo = drive_get_by_blockdev(conf->bs);
583
        if (*dinfo->serial) {
584
            *serial = strdup(dinfo->serial);
585
        }
586
    }
587

    
588
    s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK,
589
                                          sizeof(struct virtio_blk_config),
590
                                          sizeof(VirtIOBlock));
591

    
592
    s->vdev.get_config = virtio_blk_update_config;
593
    s->vdev.get_features = virtio_blk_get_features;
594
    s->vdev.reset = virtio_blk_reset;
595
    s->bs = conf->bs;
596
    s->conf = conf;
597
    s->serial = *serial;
598
    s->rq = NULL;
599
    s->sector_mask = (s->conf->logical_block_size / BDRV_SECTOR_SIZE) - 1;
600
    bdrv_guess_geometry(s->bs, &cylinders, &heads, &secs);
601

    
602
    s->vq = virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output);
603

    
604
    qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
605
    s->qdev = dev;
606
    register_savevm(dev, "virtio-blk", virtio_blk_id++, 2,
607
                    virtio_blk_save, virtio_blk_load, s);
608
    bdrv_set_dev_ops(s->bs, &virtio_block_ops, s);
609
    bdrv_set_buffer_alignment(s->bs, conf->logical_block_size);
610

    
611
    bdrv_iostatus_enable(s->bs);
612
    add_boot_device_path(conf->bootindex, dev, "/disk@0,0");
613

    
614
    return &s->vdev;
615
}
616

    
617
void virtio_blk_exit(VirtIODevice *vdev)
618
{
619
    VirtIOBlock *s = to_virtio_blk(vdev);
620
    unregister_savevm(s->qdev, "virtio-blk", s);
621
    virtio_cleanup(vdev);
622
}