qemu/hw/block/virtio-blk.c
<<
>>
Prefs
   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/iov.h"
  16#include "qemu/error-report.h"
  17#include "trace.h"
  18#include "hw/block/block.h"
  19#include "sysemu/block-backend.h"
  20#include "sysemu/blockdev.h"
  21#include "hw/virtio/virtio-blk.h"
  22#include "dataplane/virtio-blk.h"
  23#include "migration/migration.h"
  24#include "block/scsi.h"
  25#ifdef __linux__
  26# include <scsi/sg.h>
  27#endif
  28#include "hw/virtio/virtio-bus.h"
  29#include "hw/virtio/virtio-access.h"
  30
  31VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
  32{
  33    VirtIOBlockReq *req = g_new(VirtIOBlockReq, 1);
  34    req->dev = s;
  35    req->qiov.size = 0;
  36    req->in_len = 0;
  37    req->next = NULL;
  38    req->mr_next = NULL;
  39    return req;
  40}
  41
  42void virtio_blk_free_request(VirtIOBlockReq *req)
  43{
  44    if (req) {
  45        g_free(req);
  46    }
  47}
  48
  49static void virtio_blk_complete_request(VirtIOBlockReq *req,
  50                                        unsigned char status)
  51{
  52    VirtIOBlock *s = req->dev;
  53    VirtIODevice *vdev = VIRTIO_DEVICE(s);
  54
  55    trace_virtio_blk_req_complete(req, status);
  56
  57    stb_p(&req->in->status, status);
  58    virtqueue_push(s->vq, &req->elem, req->in_len);
  59    virtio_notify(vdev, s->vq);
  60}
  61
  62static void virtio_blk_req_complete(VirtIOBlockReq *req, unsigned char status)
  63{
  64    req->dev->complete_request(req, status);
  65}
  66
  67static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
  68    bool is_read)
  69{
  70    BlockErrorAction action = blk_get_error_action(req->dev->blk,
  71                                                   is_read, error);
  72    VirtIOBlock *s = req->dev;
  73
  74    if (action == BLOCK_ERROR_ACTION_STOP) {
  75        /* Break the link as the next request is going to be parsed from the
  76         * ring again. Otherwise we may end up doing a double completion! */
  77        req->mr_next = NULL;
  78        req->next = s->rq;
  79        s->rq = req;
  80    } else if (action == BLOCK_ERROR_ACTION_REPORT) {
  81        virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
  82        block_acct_failed(blk_get_stats(s->blk), &req->acct);
  83        virtio_blk_free_request(req);
  84    }
  85
  86    blk_error_action(s->blk, action, is_read, error);
  87    return action != BLOCK_ERROR_ACTION_IGNORE;
  88}
  89
  90static void virtio_blk_rw_complete(void *opaque, int ret)
  91{
  92    VirtIOBlockReq *next = opaque;
  93
  94    while (next) {
  95        VirtIOBlockReq *req = next;
  96        next = req->mr_next;
  97        trace_virtio_blk_rw_complete(req, ret);
  98
  99        if (req->qiov.nalloc != -1) {
 100            /* If nalloc is != 1 req->qiov is a local copy of the original
 101             * external iovec. It was allocated in submit_merged_requests
 102             * to be able to merge requests. */
 103            qemu_iovec_destroy(&req->qiov);
 104        }
 105
 106        if (ret) {
 107            int p = virtio_ldl_p(VIRTIO_DEVICE(req->dev), &req->out.type);
 108            bool is_read = !(p & VIRTIO_BLK_T_OUT);
 109            /* Note that memory may be dirtied on read failure.  If the
 110             * virtio request is not completed here, as is the case for
 111             * BLOCK_ERROR_ACTION_STOP, the memory may not be copied
 112             * correctly during live migration.  While this is ugly,
 113             * it is acceptable because the device is free to write to
 114             * the memory until the request is completed (which will
 115             * happen on the other side of the migration).
 116             */
 117            if (virtio_blk_handle_rw_error(req, -ret, is_read)) {
 118                continue;
 119            }
 120        }
 121
 122        virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
 123        block_acct_done(blk_get_stats(req->dev->blk), &req->acct);
 124        virtio_blk_free_request(req);
 125    }
 126}
 127
 128static void virtio_blk_flush_complete(void *opaque, int ret)
 129{
 130    VirtIOBlockReq *req = opaque;
 131
 132    if (ret) {
 133        if (virtio_blk_handle_rw_error(req, -ret, 0)) {
 134            return;
 135        }
 136    }
 137
 138    virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
 139    block_acct_done(blk_get_stats(req->dev->blk), &req->acct);
 140    virtio_blk_free_request(req);
 141}
 142
 143#ifdef __linux__
 144
 145typedef struct {
 146    VirtIOBlockReq *req;
 147    struct sg_io_hdr hdr;
 148} VirtIOBlockIoctlReq;
 149
 150static void virtio_blk_ioctl_complete(void *opaque, int status)
 151{
 152    VirtIOBlockIoctlReq *ioctl_req = opaque;
 153    VirtIOBlockReq *req = ioctl_req->req;
 154    VirtIODevice *vdev = VIRTIO_DEVICE(req->dev);
 155    struct virtio_scsi_inhdr *scsi;
 156    struct sg_io_hdr *hdr;
 157
 158    scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base;
 159
 160    if (status) {
 161        status = VIRTIO_BLK_S_UNSUPP;
 162        virtio_stl_p(vdev, &scsi->errors, 255);
 163        goto out;
 164    }
 165
 166    hdr = &ioctl_req->hdr;
 167    /*
 168     * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi)
 169     * clear the masked_status field [hence status gets cleared too, see
 170     * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED
 171     * status has occurred.  However they do set DRIVER_SENSE in driver_status
 172     * field. Also a (sb_len_wr > 0) indicates there is a sense buffer.
 173     */
 174    if (hdr->status == 0 && hdr->sb_len_wr > 0) {
 175        hdr->status = CHECK_CONDITION;
 176    }
 177
 178    virtio_stl_p(vdev, &scsi->errors,
 179                 hdr->status | (hdr->msg_status << 8) |
 180                 (hdr->host_status << 16) | (hdr->driver_status << 24));
 181    virtio_stl_p(vdev, &scsi->residual, hdr->resid);
 182    virtio_stl_p(vdev, &scsi->sense_len, hdr->sb_len_wr);
 183    virtio_stl_p(vdev, &scsi->data_len, hdr->dxfer_len);
 184
 185out:
 186    virtio_blk_req_complete(req, status);
 187    virtio_blk_free_request(req);
 188    g_free(ioctl_req);
 189}
 190
 191#endif
 192
 193static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s)
 194{
 195    VirtIOBlockReq *req = virtio_blk_alloc_request(s);
 196
 197    if (!virtqueue_pop(s->vq, &req->elem)) {
 198        virtio_blk_free_request(req);
 199        return NULL;
 200    }
 201
 202    return req;
 203}
 204
 205static int virtio_blk_handle_scsi_req(VirtIOBlockReq *req)
 206{
 207    int status = VIRTIO_BLK_S_OK;
 208    struct virtio_scsi_inhdr *scsi = NULL;
 209    VirtIODevice *vdev = VIRTIO_DEVICE(req->dev);
 210    VirtQueueElement *elem = &req->elem;
 211    VirtIOBlock *blk = req->dev;
 212
 213#ifdef __linux__
 214    int i;
 215    VirtIOBlockIoctlReq *ioctl_req;
 216    BlockAIOCB *acb;
 217#endif
 218
 219    /*
 220     * We require at least one output segment each for the virtio_blk_outhdr
 221     * and the SCSI command block.
 222     *
 223     * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
 224     * and the sense buffer pointer in the input segments.
 225     */
 226    if (elem->out_num < 2 || elem->in_num < 3) {
 227        status = VIRTIO_BLK_S_IOERR;
 228        goto fail;
 229    }
 230
 231    /*
 232     * The scsi inhdr is placed in the second-to-last input segment, just
 233     * before the regular inhdr.
 234     */
 235    scsi = (void *)elem->in_sg[elem->in_num - 2].iov_base;
 236
 237    if (!blk->conf.scsi) {
 238        status = VIRTIO_BLK_S_UNSUPP;
 239        goto fail;
 240    }
 241
 242    /*
 243     * No support for bidirection commands yet.
 244     */
 245    if (elem->out_num > 2 && elem->in_num > 3) {
 246        status = VIRTIO_BLK_S_UNSUPP;
 247        goto fail;
 248    }
 249
 250#ifdef __linux__
 251    ioctl_req = g_new0(VirtIOBlockIoctlReq, 1);
 252    ioctl_req->req = req;
 253    ioctl_req->hdr.interface_id = 'S';
 254    ioctl_req->hdr.cmd_len = elem->out_sg[1].iov_len;
 255    ioctl_req->hdr.cmdp = elem->out_sg[1].iov_base;
 256    ioctl_req->hdr.dxfer_len = 0;
 257
 258    if (elem->out_num > 2) {
 259        /*
 260         * If there are more than the minimally required 2 output segments
 261         * there is write payload starting from the third iovec.
 262         */
 263        ioctl_req->hdr.dxfer_direction = SG_DXFER_TO_DEV;
 264        ioctl_req->hdr.iovec_count = elem->out_num - 2;
 265
 266        for (i = 0; i < ioctl_req->hdr.iovec_count; i++) {
 267            ioctl_req->hdr.dxfer_len += elem->out_sg[i + 2].iov_len;
 268        }
 269
 270        ioctl_req->hdr.dxferp = elem->out_sg + 2;
 271
 272    } else if (elem->in_num > 3) {
 273        /*
 274         * If we have more than 3 input segments the guest wants to actually
 275         * read data.
 276         */
 277        ioctl_req->hdr.dxfer_direction = SG_DXFER_FROM_DEV;
 278        ioctl_req->hdr.iovec_count = elem->in_num - 3;
 279        for (i = 0; i < ioctl_req->hdr.iovec_count; i++) {
 280            ioctl_req->hdr.dxfer_len += elem->in_sg[i].iov_len;
 281        }
 282
 283        ioctl_req->hdr.dxferp = elem->in_sg;
 284    } else {
 285        /*
 286         * Some SCSI commands don't actually transfer any data.
 287         */
 288        ioctl_req->hdr.dxfer_direction = SG_DXFER_NONE;
 289    }
 290
 291    ioctl_req->hdr.sbp = elem->in_sg[elem->in_num - 3].iov_base;
 292    ioctl_req->hdr.mx_sb_len = elem->in_sg[elem->in_num - 3].iov_len;
 293
 294    acb = blk_aio_ioctl(blk->blk, SG_IO, &ioctl_req->hdr,
 295                        virtio_blk_ioctl_complete, ioctl_req);
 296    if (!acb) {
 297        g_free(ioctl_req);
 298        status = VIRTIO_BLK_S_UNSUPP;
 299        goto fail;
 300    }
 301    return -EINPROGRESS;
 302#else
 303    abort();
 304#endif
 305
 306fail:
 307    /* Just put anything nonzero so that the ioctl fails in the guest.  */
 308    if (scsi) {
 309        virtio_stl_p(vdev, &scsi->errors, 255);
 310    }
 311    return status;
 312}
 313
 314static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
 315{
 316    int status;
 317
 318    status = virtio_blk_handle_scsi_req(req);
 319    if (status != -EINPROGRESS) {
 320        virtio_blk_req_complete(req, status);
 321        virtio_blk_free_request(req);
 322    }
 323}
 324
 325static inline void submit_requests(BlockBackend *blk, MultiReqBuffer *mrb,
 326                                   int start, int num_reqs, int niov)
 327{
 328    QEMUIOVector *qiov = &mrb->reqs[start]->qiov;
 329    int64_t sector_num = mrb->reqs[start]->sector_num;
 330    int nb_sectors = mrb->reqs[start]->qiov.size / BDRV_SECTOR_SIZE;
 331    bool is_write = mrb->is_write;
 332
 333    if (num_reqs > 1) {
 334        int i;
 335        struct iovec *tmp_iov = qiov->iov;
 336        int tmp_niov = qiov->niov;
 337
 338        /* mrb->reqs[start]->qiov was initialized from external so we can't
 339         * modifiy it here. We need to initialize it locally and then add the
 340         * external iovecs. */
 341        qemu_iovec_init(qiov, niov);
 342
 343        for (i = 0; i < tmp_niov; i++) {
 344            qemu_iovec_add(qiov, tmp_iov[i].iov_base, tmp_iov[i].iov_len);
 345        }
 346
 347        for (i = start + 1; i < start + num_reqs; i++) {
 348            qemu_iovec_concat(qiov, &mrb->reqs[i]->qiov, 0,
 349                              mrb->reqs[i]->qiov.size);
 350            mrb->reqs[i - 1]->mr_next = mrb->reqs[i];
 351            nb_sectors += mrb->reqs[i]->qiov.size / BDRV_SECTOR_SIZE;
 352        }
 353        assert(nb_sectors == qiov->size / BDRV_SECTOR_SIZE);
 354
 355        trace_virtio_blk_submit_multireq(mrb, start, num_reqs, sector_num,
 356                                         nb_sectors, is_write);
 357        block_acct_merge_done(blk_get_stats(blk),
 358                              is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ,
 359                              num_reqs - 1);
 360    }
 361
 362    if (is_write) {
 363        blk_aio_writev(blk, sector_num, qiov, nb_sectors,
 364                       virtio_blk_rw_complete, mrb->reqs[start]);
 365    } else {
 366        blk_aio_readv(blk, sector_num, qiov, nb_sectors,
 367                      virtio_blk_rw_complete, mrb->reqs[start]);
 368    }
 369}
 370
 371static int multireq_compare(const void *a, const void *b)
 372{
 373    const VirtIOBlockReq *req1 = *(VirtIOBlockReq **)a,
 374                         *req2 = *(VirtIOBlockReq **)b;
 375
 376    /*
 377     * Note that we can't simply subtract sector_num1 from sector_num2
 378     * here as that could overflow the return value.
 379     */
 380    if (req1->sector_num > req2->sector_num) {
 381        return 1;
 382    } else if (req1->sector_num < req2->sector_num) {
 383        return -1;
 384    } else {
 385        return 0;
 386    }
 387}
 388
 389void virtio_blk_submit_multireq(BlockBackend *blk, MultiReqBuffer *mrb)
 390{
 391    int i = 0, start = 0, num_reqs = 0, niov = 0, nb_sectors = 0;
 392    int max_xfer_len = 0;
 393    int64_t sector_num = 0;
 394
 395    if (mrb->num_reqs == 1) {
 396        submit_requests(blk, mrb, 0, 1, -1);
 397        mrb->num_reqs = 0;
 398        return;
 399    }
 400
 401    max_xfer_len = blk_get_max_transfer_length(mrb->reqs[0]->dev->blk);
 402    max_xfer_len = MIN_NON_ZERO(max_xfer_len, BDRV_REQUEST_MAX_SECTORS);
 403
 404    qsort(mrb->reqs, mrb->num_reqs, sizeof(*mrb->reqs),
 405          &multireq_compare);
 406
 407    for (i = 0; i < mrb->num_reqs; i++) {
 408        VirtIOBlockReq *req = mrb->reqs[i];
 409        if (num_reqs > 0) {
 410            bool merge = true;
 411
 412            /* merge would exceed maximum number of IOVs */
 413            if (niov + req->qiov.niov > IOV_MAX) {
 414                merge = false;
 415            }
 416
 417            /* merge would exceed maximum transfer length of backend device */
 418            if (req->qiov.size / BDRV_SECTOR_SIZE + nb_sectors > max_xfer_len) {
 419                merge = false;
 420            }
 421
 422            /* requests are not sequential */
 423            if (sector_num + nb_sectors != req->sector_num) {
 424                merge = false;
 425            }
 426
 427            if (!merge) {
 428                submit_requests(blk, mrb, start, num_reqs, niov);
 429                num_reqs = 0;
 430            }
 431        }
 432
 433        if (num_reqs == 0) {
 434            sector_num = req->sector_num;
 435            nb_sectors = niov = 0;
 436            start = i;
 437        }
 438
 439        nb_sectors += req->qiov.size / BDRV_SECTOR_SIZE;
 440        niov += req->qiov.niov;
 441        num_reqs++;
 442    }
 443
 444    submit_requests(blk, mrb, start, num_reqs, niov);
 445    mrb->num_reqs = 0;
 446}
 447
 448static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
 449{
 450    block_acct_start(blk_get_stats(req->dev->blk), &req->acct, 0,
 451                     BLOCK_ACCT_FLUSH);
 452
 453    /*
 454     * Make sure all outstanding writes are posted to the backing device.
 455     */
 456    if (mrb->is_write && mrb->num_reqs > 0) {
 457        virtio_blk_submit_multireq(req->dev->blk, mrb);
 458    }
 459    blk_aio_flush(req->dev->blk, virtio_blk_flush_complete, req);
 460}
 461
 462static bool virtio_blk_sect_range_ok(VirtIOBlock *dev,
 463                                     uint64_t sector, size_t size)
 464{
 465    uint64_t nb_sectors = size >> BDRV_SECTOR_BITS;
 466    uint64_t total_sectors;
 467
 468    if (nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
 469        return false;
 470    }
 471    if (sector & dev->sector_mask) {
 472        return false;
 473    }
 474    if (size % dev->conf.conf.logical_block_size) {
 475        return false;
 476    }
 477    blk_get_geometry(dev->blk, &total_sectors);
 478    if (sector > total_sectors || nb_sectors > total_sectors - sector) {
 479        return false;
 480    }
 481    return true;
 482}
 483
 484void virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb)
 485{
 486    uint32_t type;
 487    struct iovec *in_iov = req->elem.in_sg;
 488    struct iovec *iov = req->elem.out_sg;
 489    unsigned in_num = req->elem.in_num;
 490    unsigned out_num = req->elem.out_num;
 491
 492    if (req->elem.out_num < 1 || req->elem.in_num < 1) {
 493        error_report("virtio-blk missing headers");
 494        exit(1);
 495    }
 496
 497    if (unlikely(iov_to_buf(iov, out_num, 0, &req->out,
 498                            sizeof(req->out)) != sizeof(req->out))) {
 499        error_report("virtio-blk request outhdr too short");
 500        exit(1);
 501    }
 502
 503    iov_discard_front(&iov, &out_num, sizeof(req->out));
 504
 505    if (in_iov[in_num - 1].iov_len < sizeof(struct virtio_blk_inhdr)) {
 506        error_report("virtio-blk request inhdr too short");
 507        exit(1);
 508    }
 509
 510    /* We always touch the last byte, so just see how big in_iov is.  */
 511    req->in_len = iov_size(in_iov, in_num);
 512    req->in = (void *)in_iov[in_num - 1].iov_base
 513              + in_iov[in_num - 1].iov_len
 514              - sizeof(struct virtio_blk_inhdr);
 515    iov_discard_back(in_iov, &in_num, sizeof(struct virtio_blk_inhdr));
 516
 517    type = virtio_ldl_p(VIRTIO_DEVICE(req->dev), &req->out.type);
 518
 519    /* VIRTIO_BLK_T_OUT defines the command direction. VIRTIO_BLK_T_BARRIER
 520     * is an optional flag. Although a guest should not send this flag if
 521     * not negotiated we ignored it in the past. So keep ignoring it. */
 522    switch (type & ~(VIRTIO_BLK_T_OUT | VIRTIO_BLK_T_BARRIER)) {
 523    case VIRTIO_BLK_T_IN:
 524    {
 525        bool is_write = type & VIRTIO_BLK_T_OUT;
 526        req->sector_num = virtio_ldq_p(VIRTIO_DEVICE(req->dev),
 527                                       &req->out.sector);
 528
 529        if (is_write) {
 530            qemu_iovec_init_external(&req->qiov, iov, out_num);
 531            trace_virtio_blk_handle_write(req, req->sector_num,
 532                                          req->qiov.size / BDRV_SECTOR_SIZE);
 533        } else {
 534            qemu_iovec_init_external(&req->qiov, in_iov, in_num);
 535            trace_virtio_blk_handle_read(req, req->sector_num,
 536                                         req->qiov.size / BDRV_SECTOR_SIZE);
 537        }
 538
 539        if (!virtio_blk_sect_range_ok(req->dev, req->sector_num,
 540                                      req->qiov.size)) {
 541            virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
 542            block_acct_invalid(blk_get_stats(req->dev->blk),
 543                               is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ);
 544            virtio_blk_free_request(req);
 545            return;
 546        }
 547
 548        block_acct_start(blk_get_stats(req->dev->blk),
 549                         &req->acct, req->qiov.size,
 550                         is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ);
 551
 552        /* merge would exceed maximum number of requests or IO direction
 553         * changes */
 554        if (mrb->num_reqs > 0 && (mrb->num_reqs == VIRTIO_BLK_MAX_MERGE_REQS ||
 555                                  is_write != mrb->is_write ||
 556                                  !req->dev->conf.request_merging)) {
 557            virtio_blk_submit_multireq(req->dev->blk, mrb);
 558        }
 559
 560        assert(mrb->num_reqs < VIRTIO_BLK_MAX_MERGE_REQS);
 561        mrb->reqs[mrb->num_reqs++] = req;
 562        mrb->is_write = is_write;
 563        break;
 564    }
 565    case VIRTIO_BLK_T_FLUSH:
 566        virtio_blk_handle_flush(req, mrb);
 567        break;
 568    case VIRTIO_BLK_T_SCSI_CMD:
 569        virtio_blk_handle_scsi(req);
 570        break;
 571    case VIRTIO_BLK_T_GET_ID:
 572    {
 573        VirtIOBlock *s = req->dev;
 574
 575        /*
 576         * NB: per existing s/n string convention the string is
 577         * terminated by '\0' only when shorter than buffer.
 578         */
 579        const char *serial = s->conf.serial ? s->conf.serial : "";
 580        size_t size = MIN(strlen(serial) + 1,
 581                          MIN(iov_size(in_iov, in_num),
 582                              VIRTIO_BLK_ID_BYTES));
 583        iov_from_buf(in_iov, in_num, 0, serial, size);
 584        virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
 585        virtio_blk_free_request(req);
 586        break;
 587    }
 588    default:
 589        virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
 590        virtio_blk_free_request(req);
 591    }
 592}
 593
 594static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
 595{
 596    VirtIOBlock *s = VIRTIO_BLK(vdev);
 597    VirtIOBlockReq *req;
 598    MultiReqBuffer mrb = {};
 599
 600    /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
 601     * dataplane here instead of waiting for .set_status().
 602     */
 603    if (s->dataplane) {
 604        virtio_blk_data_plane_start(s->dataplane);
 605        return;
 606    }
 607
 608    blk_io_plug(s->blk);
 609
 610    while ((req = virtio_blk_get_request(s))) {
 611        virtio_blk_handle_request(req, &mrb);
 612    }
 613
 614    if (mrb.num_reqs) {
 615        virtio_blk_submit_multireq(s->blk, &mrb);
 616    }
 617
 618    blk_io_unplug(s->blk);
 619}
 620
 621static void virtio_blk_dma_restart_bh(void *opaque)
 622{
 623    VirtIOBlock *s = opaque;
 624    VirtIOBlockReq *req = s->rq;
 625    MultiReqBuffer mrb = {};
 626
 627    qemu_bh_delete(s->bh);
 628    s->bh = NULL;
 629
 630    s->rq = NULL;
 631
 632    while (req) {
 633        VirtIOBlockReq *next = req->next;
 634        virtio_blk_handle_request(req, &mrb);
 635        req = next;
 636    }
 637
 638    if (mrb.num_reqs) {
 639        virtio_blk_submit_multireq(s->blk, &mrb);
 640    }
 641}
 642
 643static void virtio_blk_dma_restart_cb(void *opaque, int running,
 644                                      RunState state)
 645{
 646    VirtIOBlock *s = opaque;
 647
 648    if (!running) {
 649        return;
 650    }
 651
 652    if (!s->bh) {
 653        s->bh = aio_bh_new(blk_get_aio_context(s->conf.conf.blk),
 654                           virtio_blk_dma_restart_bh, s);
 655        qemu_bh_schedule(s->bh);
 656    }
 657}
 658
 659static void virtio_blk_reset(VirtIODevice *vdev)
 660{
 661    VirtIOBlock *s = VIRTIO_BLK(vdev);
 662    AioContext *ctx;
 663
 664    /*
 665     * This should cancel pending requests, but can't do nicely until there
 666     * are per-device request lists.
 667     */
 668    ctx = blk_get_aio_context(s->blk);
 669    aio_context_acquire(ctx);
 670    blk_drain(s->blk);
 671
 672    if (s->dataplane) {
 673        virtio_blk_data_plane_stop(s->dataplane);
 674    }
 675    aio_context_release(ctx);
 676
 677    blk_set_enable_write_cache(s->blk, s->original_wce);
 678}
 679
 680/* coalesce internal state, copy to pci i/o region 0
 681 */
 682static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
 683{
 684    VirtIOBlock *s = VIRTIO_BLK(vdev);
 685    BlockConf *conf = &s->conf.conf;
 686    struct virtio_blk_config blkcfg;
 687    uint64_t capacity;
 688    int blk_size = conf->logical_block_size;
 689
 690    blk_get_geometry(s->blk, &capacity);
 691    memset(&blkcfg, 0, sizeof(blkcfg));
 692    virtio_stq_p(vdev, &blkcfg.capacity, capacity);
 693    virtio_stl_p(vdev, &blkcfg.seg_max, 128 - 2);
 694    virtio_stw_p(vdev, &blkcfg.geometry.cylinders, conf->cyls);
 695    virtio_stl_p(vdev, &blkcfg.blk_size, blk_size);
 696    virtio_stw_p(vdev, &blkcfg.min_io_size, conf->min_io_size / blk_size);
 697    virtio_stw_p(vdev, &blkcfg.opt_io_size, conf->opt_io_size / blk_size);
 698    blkcfg.geometry.heads = conf->heads;
 699    /*
 700     * We must ensure that the block device capacity is a multiple of
 701     * the logical block size. If that is not the case, let's use
 702     * sector_mask to adopt the geometry to have a correct picture.
 703     * For those devices where the capacity is ok for the given geometry
 704     * we don't touch the sector value of the geometry, since some devices
 705     * (like s390 dasd) need a specific value. Here the capacity is already
 706     * cyls*heads*secs*blk_size and the sector value is not block size
 707     * divided by 512 - instead it is the amount of blk_size blocks
 708     * per track (cylinder).
 709     */
 710    if (blk_getlength(s->blk) /  conf->heads / conf->secs % blk_size) {
 711        blkcfg.geometry.sectors = conf->secs & ~s->sector_mask;
 712    } else {
 713        blkcfg.geometry.sectors = conf->secs;
 714    }
 715    blkcfg.size_max = 0;
 716    blkcfg.physical_block_exp = get_physical_block_exp(conf);
 717    blkcfg.alignment_offset = 0;
 718    blkcfg.wce = blk_enable_write_cache(s->blk);
 719    memcpy(config, &blkcfg, sizeof(struct virtio_blk_config));
 720}
 721
 722static void virtio_blk_set_config(VirtIODevice *vdev, const uint8_t *config)
 723{
 724    VirtIOBlock *s = VIRTIO_BLK(vdev);
 725    struct virtio_blk_config blkcfg;
 726
 727    memcpy(&blkcfg, config, sizeof(blkcfg));
 728
 729    aio_context_acquire(blk_get_aio_context(s->blk));
 730    blk_set_enable_write_cache(s->blk, blkcfg.wce != 0);
 731    aio_context_release(blk_get_aio_context(s->blk));
 732}
 733
 734static uint64_t virtio_blk_get_features(VirtIODevice *vdev, uint64_t features,
 735                                        Error **errp)
 736{
 737    VirtIOBlock *s = VIRTIO_BLK(vdev);
 738
 739    virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX);
 740    virtio_add_feature(&features, VIRTIO_BLK_F_GEOMETRY);
 741    virtio_add_feature(&features, VIRTIO_BLK_F_TOPOLOGY);
 742    virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE);
 743    if (virtio_has_feature(features, VIRTIO_F_VERSION_1)) {
 744        if (s->conf.scsi) {
 745            error_setg(errp, "Please set scsi=off for virtio-blk devices in order to use virtio 1.0");
 746            return 0;
 747        }
 748    } else {
 749        virtio_clear_feature(&features, VIRTIO_F_ANY_LAYOUT);
 750        virtio_add_feature(&features, VIRTIO_BLK_F_SCSI);
 751    }
 752
 753    if (s->conf.config_wce) {
 754        virtio_add_feature(&features, VIRTIO_BLK_F_CONFIG_WCE);
 755    }
 756    if (blk_enable_write_cache(s->blk)) {
 757        virtio_add_feature(&features, VIRTIO_BLK_F_WCE);
 758    }
 759    if (blk_is_read_only(s->blk)) {
 760        virtio_add_feature(&features, VIRTIO_BLK_F_RO);
 761    }
 762
 763    return features;
 764}
 765
 766static void virtio_blk_set_status(VirtIODevice *vdev, uint8_t status)
 767{
 768    VirtIOBlock *s = VIRTIO_BLK(vdev);
 769
 770    if (s->dataplane && !(status & (VIRTIO_CONFIG_S_DRIVER |
 771                                    VIRTIO_CONFIG_S_DRIVER_OK))) {
 772        virtio_blk_data_plane_stop(s->dataplane);
 773    }
 774
 775    if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
 776        return;
 777    }
 778
 779    /* A guest that supports VIRTIO_BLK_F_CONFIG_WCE must be able to send
 780     * cache flushes.  Thus, the "auto writethrough" behavior is never
 781     * necessary for guests that support the VIRTIO_BLK_F_CONFIG_WCE feature.
 782     * Leaving it enabled would break the following sequence:
 783     *
 784     *     Guest started with "-drive cache=writethrough"
 785     *     Guest sets status to 0
 786     *     Guest sets DRIVER bit in status field
 787     *     Guest reads host features (WCE=0, CONFIG_WCE=1)
 788     *     Guest writes guest features (WCE=0, CONFIG_WCE=1)
 789     *     Guest writes 1 to the WCE configuration field (writeback mode)
 790     *     Guest sets DRIVER_OK bit in status field
 791     *
 792     * s->blk would erroneously be placed in writethrough mode.
 793     */
 794    if (!virtio_vdev_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE)) {
 795        aio_context_acquire(blk_get_aio_context(s->blk));
 796        blk_set_enable_write_cache(s->blk,
 797                                   virtio_vdev_has_feature(vdev,
 798                                                           VIRTIO_BLK_F_WCE));
 799        aio_context_release(blk_get_aio_context(s->blk));
 800    }
 801}
 802
 803static void virtio_blk_save(QEMUFile *f, void *opaque)
 804{
 805    VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
 806    VirtIOBlock *s = VIRTIO_BLK(vdev);
 807
 808    if (s->dataplane) {
 809        virtio_blk_data_plane_stop(s->dataplane);
 810    }
 811
 812    virtio_save(vdev, f);
 813}
 814    
 815static void virtio_blk_save_device(VirtIODevice *vdev, QEMUFile *f)
 816{
 817    VirtIOBlock *s = VIRTIO_BLK(vdev);
 818    VirtIOBlockReq *req = s->rq;
 819
 820    while (req) {
 821        qemu_put_sbyte(f, 1);
 822        qemu_put_buffer(f, (unsigned char *)&req->elem,
 823                        sizeof(VirtQueueElement));
 824        req = req->next;
 825    }
 826    qemu_put_sbyte(f, 0);
 827}
 828
 829static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
 830{
 831    VirtIOBlock *s = opaque;
 832    VirtIODevice *vdev = VIRTIO_DEVICE(s);
 833
 834    if (version_id != 2)
 835        return -EINVAL;
 836
 837    return virtio_load(vdev, f, version_id);
 838}
 839
 840static int virtio_blk_load_device(VirtIODevice *vdev, QEMUFile *f,
 841                                  int version_id)
 842{
 843    VirtIOBlock *s = VIRTIO_BLK(vdev);
 844
 845    while (qemu_get_sbyte(f)) {
 846        VirtIOBlockReq *req = virtio_blk_alloc_request(s);
 847        qemu_get_buffer(f, (unsigned char *)&req->elem,
 848                        sizeof(VirtQueueElement));
 849        req->next = s->rq;
 850        s->rq = req;
 851
 852        virtqueue_map(&req->elem);
 853    }
 854
 855    return 0;
 856}
 857
 858static void virtio_blk_resize(void *opaque)
 859{
 860    VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
 861
 862    virtio_notify_config(vdev);
 863}
 864
 865static const BlockDevOps virtio_block_ops = {
 866    .resize_cb = virtio_blk_resize,
 867};
 868
 869/* Disable dataplane thread during live migration since it does not
 870 * update the dirty memory bitmap yet.
 871 */
 872static void virtio_blk_migration_state_changed(Notifier *notifier, void *data)
 873{
 874    VirtIOBlock *s = container_of(notifier, VirtIOBlock,
 875                                  migration_state_notifier);
 876    MigrationState *mig = data;
 877    Error *err = NULL;
 878
 879    if (migration_in_setup(mig)) {
 880        if (!s->dataplane) {
 881            return;
 882        }
 883        virtio_blk_data_plane_destroy(s->dataplane);
 884        s->dataplane = NULL;
 885    } else if (migration_has_finished(mig) ||
 886               migration_has_failed(mig)) {
 887        if (s->dataplane) {
 888            return;
 889        }
 890        blk_drain_all(); /* complete in-flight non-dataplane requests */
 891        virtio_blk_data_plane_create(VIRTIO_DEVICE(s), &s->conf,
 892                                     &s->dataplane, &err);
 893        if (err != NULL) {
 894            error_report_err(err);
 895        }
 896    }
 897}
 898
 899static void virtio_blk_device_realize(DeviceState *dev, Error **errp)
 900{
 901    VirtIODevice *vdev = VIRTIO_DEVICE(dev);
 902    VirtIOBlock *s = VIRTIO_BLK(dev);
 903    VirtIOBlkConf *conf = &s->conf;
 904    Error *err = NULL;
 905    static int virtio_blk_id;
 906
 907    if (!conf->conf.blk) {
 908        error_setg(errp, "drive property not set");
 909        return;
 910    }
 911    if (!blk_is_inserted(conf->conf.blk)) {
 912        error_setg(errp, "Device needs media, but drive is empty");
 913        return;
 914    }
 915
 916    blkconf_serial(&conf->conf, &conf->serial);
 917    s->original_wce = blk_enable_write_cache(conf->conf.blk);
 918    blkconf_geometry(&conf->conf, NULL, 65535, 255, 255, &err);
 919    if (err) {
 920        error_propagate(errp, err);
 921        return;
 922    }
 923    blkconf_blocksizes(&conf->conf);
 924
 925    virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK,
 926                sizeof(struct virtio_blk_config));
 927
 928    s->blk = conf->conf.blk;
 929    s->rq = NULL;
 930    s->sector_mask = (s->conf.conf.logical_block_size / BDRV_SECTOR_SIZE) - 1;
 931
 932    s->vq = virtio_add_queue(vdev, 128, virtio_blk_handle_output);
 933    s->complete_request = virtio_blk_complete_request;
 934    virtio_blk_data_plane_create(vdev, conf, &s->dataplane, &err);
 935    if (err != NULL) {
 936        error_propagate(errp, err);
 937        virtio_cleanup(vdev);
 938        return;
 939    }
 940    s->migration_state_notifier.notify = virtio_blk_migration_state_changed;
 941    add_migration_state_change_notifier(&s->migration_state_notifier);
 942
 943    s->change = qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
 944    register_savevm(dev, "virtio-blk", virtio_blk_id++, 2,
 945                    virtio_blk_save, virtio_blk_load, s);
 946    blk_set_dev_ops(s->blk, &virtio_block_ops, s);
 947    blk_set_guest_block_size(s->blk, s->conf.conf.logical_block_size);
 948
 949    blk_iostatus_enable(s->blk);
 950}
 951
 952static void virtio_blk_device_unrealize(DeviceState *dev, Error **errp)
 953{
 954    VirtIODevice *vdev = VIRTIO_DEVICE(dev);
 955    VirtIOBlock *s = VIRTIO_BLK(dev);
 956
 957    remove_migration_state_change_notifier(&s->migration_state_notifier);
 958    virtio_blk_data_plane_destroy(s->dataplane);
 959    s->dataplane = NULL;
 960    qemu_del_vm_change_state_handler(s->change);
 961    unregister_savevm(dev, "virtio-blk", s);
 962    blockdev_mark_auto_del(s->blk);
 963    virtio_cleanup(vdev);
 964}
 965
 966static void virtio_blk_instance_init(Object *obj)
 967{
 968    VirtIOBlock *s = VIRTIO_BLK(obj);
 969
 970    object_property_add_link(obj, "iothread", TYPE_IOTHREAD,
 971                             (Object **)&s->conf.iothread,
 972                             qdev_prop_allow_set_link_before_realize,
 973                             OBJ_PROP_LINK_UNREF_ON_RELEASE, NULL);
 974    device_add_bootindex_property(obj, &s->conf.conf.bootindex,
 975                                  "bootindex", "/disk@0,0",
 976                                  DEVICE(obj), NULL);
 977}
 978
 979static Property virtio_blk_properties[] = {
 980    DEFINE_BLOCK_PROPERTIES(VirtIOBlock, conf.conf),
 981    DEFINE_BLOCK_CHS_PROPERTIES(VirtIOBlock, conf.conf),
 982    DEFINE_PROP_STRING("serial", VirtIOBlock, conf.serial),
 983    DEFINE_PROP_BIT("config-wce", VirtIOBlock, conf.config_wce, 0, true),
 984#ifdef __linux__
 985    DEFINE_PROP_BIT("scsi", VirtIOBlock, conf.scsi, 0, false),
 986#endif
 987    DEFINE_PROP_BIT("request-merging", VirtIOBlock, conf.request_merging, 0,
 988                    true),
 989    DEFINE_PROP_END_OF_LIST(),
 990};
 991
 992static void virtio_blk_class_init(ObjectClass *klass, void *data)
 993{
 994    DeviceClass *dc = DEVICE_CLASS(klass);
 995    VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
 996
 997    dc->props = virtio_blk_properties;
 998    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
 999    vdc->realize = virtio_blk_device_realize;
1000    vdc->unrealize = virtio_blk_device_unrealize;
1001    vdc->get_config = virtio_blk_update_config;
1002    vdc->set_config = virtio_blk_set_config;
1003    vdc->get_features = virtio_blk_get_features;
1004    vdc->set_status = virtio_blk_set_status;
1005    vdc->reset = virtio_blk_reset;
1006    vdc->save = virtio_blk_save_device;
1007    vdc->load = virtio_blk_load_device;
1008}
1009
1010static const TypeInfo virtio_device_info = {
1011    .name = TYPE_VIRTIO_BLK,
1012    .parent = TYPE_VIRTIO_DEVICE,
1013    .instance_size = sizeof(VirtIOBlock),
1014    .instance_init = virtio_blk_instance_init,
1015    .class_init = virtio_blk_class_init,
1016};
1017
1018static void virtio_register_types(void)
1019{
1020    type_register_static(&virtio_device_info);
1021}
1022
1023type_init(virtio_register_types)
1024