Commit bfd18d1e authored by Kevin Wolf's avatar Kevin Wolf
Browse files

block: Move enable_write_cache to BB level



Whether a write cache is used or not is a decision that concerns the
user (e.g. the guest device) rather than the backend. It was already
logically part of the BB level as bdrv_move_feature_fields() always kept
it on top of the BDS tree; with this patch, the core of it (the actual
flag and the additional flushes) is also implemented there.

Direct callers of bdrv_open() must pass BDRV_O_CACHE_WB now if bs
doesn't have a BlockBackend attached.

Signed-off-by: default avatarKevin Wolf <kwolf@redhat.com>
Reviewed-by: default avatarMax Reitz <mreitz@redhat.com>
parent 855a6a93
Loading
Loading
Loading
Loading
+17 −9
Original line number Diff line number Diff line
@@ -2038,6 +2038,11 @@ int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue,
            goto error;
        }
    }
    if (!reopen_state->bs->blk && !(reopen_state->flags & BDRV_O_CACHE_WB)) {
        error_setg(errp, "Cannot disable cache.writeback: No BlockBackend");
        ret = -EINVAL;
        goto error;
    }

    /* node-name and driver must be unchanged. Put them back into the QDict, so
     * that they are checked at the end of this function. */
@@ -2138,10 +2143,10 @@ void bdrv_reopen_commit(BDRVReopenState *reopen_state)

    reopen_state->bs->explicit_options   = reopen_state->explicit_options;
    reopen_state->bs->open_flags         = reopen_state->flags;
    reopen_state->bs->enable_write_cache = !!(reopen_state->flags &
                                              BDRV_O_CACHE_WB);
    reopen_state->bs->read_only = !(reopen_state->flags & BDRV_O_RDWR);

    bdrv_set_enable_write_cache(reopen_state->bs,
                                !!(reopen_state->flags & BDRV_O_CACHE_WB));
    bdrv_refresh_limits(reopen_state->bs, NULL);
}

@@ -2271,9 +2276,6 @@ static void bdrv_move_feature_fields(BlockDriverState *bs_dest,
                                     BlockDriverState *bs_src)
{
    /* move some fields that need to stay attached to the device */

    /* dev info */
    bs_dest->enable_write_cache = bs_src->enable_write_cache;
}

static void change_parent_backing_link(BlockDriverState *from,
@@ -2753,12 +2755,18 @@ int bdrv_is_sg(BlockDriverState *bs)

int bdrv_enable_write_cache(BlockDriverState *bs)
{
    return bs->enable_write_cache;
    if (bs->blk) {
        return blk_enable_write_cache(bs->blk);
    } else {
        return true;
    }
}

void bdrv_set_enable_write_cache(BlockDriverState *bs, bool wce)
{
    bs->enable_write_cache = wce;
    if (bs->blk) {
        blk_set_enable_write_cache(bs->blk, wce);
    }

    /* so a reopen() will preserve wce */
    if (wce) {
@@ -3618,8 +3626,8 @@ void bdrv_img_create(const char *filename, const char *fmt,
            }

            /* backing files always opened read-only */
            back_flags =
                flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
            back_flags = flags | BDRV_O_CACHE_WB;
            back_flags &= ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);

            if (backing_fmt) {
                backing_options = qdict_new();
+27 −15
Original line number Diff line number Diff line
@@ -47,6 +47,8 @@ struct BlockBackend {
     * can be used to restore those options in the new BDS on insert) */
    BlockBackendRootState root_state;

    bool enable_write_cache;

    /* I/O stats (display with "info blockstats"). */
    BlockAcctStats stats;

@@ -699,11 +701,17 @@ static int coroutine_fn blk_co_pwritev(BlockBackend *blk, int64_t offset,
                                      unsigned int bytes, QEMUIOVector *qiov,
                                      BdrvRequestFlags flags)
{
    int ret = blk_check_byte_request(blk, offset, bytes);
    int ret;

    ret = blk_check_byte_request(blk, offset, bytes);
    if (ret < 0) {
        return ret;
    }

    if (!blk->enable_write_cache) {
        flags |= BDRV_REQ_FUA;
    }

    return bdrv_co_do_pwritev(blk_bs(blk), offset, bytes, qiov, flags);
}

@@ -1210,26 +1218,19 @@ int blk_is_sg(BlockBackend *blk)

int blk_enable_write_cache(BlockBackend *blk)
{
    BlockDriverState *bs = blk_bs(blk);

    if (bs) {
        return bdrv_enable_write_cache(bs);
    } else {
        return !!(blk->root_state.open_flags & BDRV_O_CACHE_WB);
    }
    return blk->enable_write_cache;
}

void blk_set_enable_write_cache(BlockBackend *blk, bool wce)
{
    BlockDriverState *bs = blk_bs(blk);
    blk->enable_write_cache = wce;

    if (bs) {
        bdrv_set_enable_write_cache(bs, wce);
    } else {
    /* TODO Remove this when BDRV_O_CACHE_WB isn't used any more */
    if (blk->root) {
        if (wce) {
            blk->root_state.open_flags |= BDRV_O_CACHE_WB;
            blk->root->bs->open_flags |= BDRV_O_CACHE_WB;
        } else {
            blk->root_state.open_flags &= ~BDRV_O_CACHE_WB;
            blk->root->bs->open_flags &= ~BDRV_O_CACHE_WB;
        }
    }
}
@@ -1492,11 +1493,22 @@ int blk_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,
                     int64_t pos, int size)
{
    int ret;

    if (!blk_is_available(blk)) {
        return -ENOMEDIUM;
    }

    return bdrv_save_vmstate(blk_bs(blk), buf, pos, size);
    ret = bdrv_save_vmstate(blk_bs(blk), buf, pos, size);
    if (ret < 0) {
        return ret;
    }

    if (ret == size && !blk->enable_write_cache) {
        ret = bdrv_flush(blk_bs(blk));
    }

    return ret < 0 ? ret : size;
}

int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size)
+1 −1
Original line number Diff line number Diff line
@@ -1160,7 +1160,7 @@ static int coroutine_fn bdrv_aligned_pwritev(BlockDriverState *bs,
    }
    bdrv_debug_event(bs, BLKDBG_PWRITEV_DONE);

    if (ret == 0 && !bs->enable_write_cache) {
    if (ret == 0 && (flags & BDRV_REQ_FUA)) {
        ret = bdrv_co_flush(bs);
    }

+1 −1
Original line number Diff line number Diff line
@@ -476,7 +476,7 @@ static int coroutine_fn iscsi_co_writev(BlockDriverState *bs,
    num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
    iscsi_co_init_iscsitask(iscsilun, &iTask);
retry:
    fua = iscsilun->dpofua && !bs->enable_write_cache;
    fua = iscsilun->dpofua && !bdrv_enable_write_cache(bs);
    iTask.force_next_flush = !fua;
    if (iscsilun->use_16_for_rw) {
        iTask.task = iscsi_write16_task(iscsilun->iscsi, iscsilun->lun, lba,
+1 −0
Original line number Diff line number Diff line
@@ -64,6 +64,7 @@ typedef enum {
     */
    BDRV_REQ_MAY_UNMAP          = 0x4,
    BDRV_REQ_NO_SERIALISING     = 0x8,
    BDRV_REQ_FUA                = 0x10,
} BdrvRequestFlags;

typedef struct BlockSizes {
Loading