diff options
Diffstat (limited to 'block/block-backend.c')
-rw-r--r-- | block/block-backend.c | 62 |
1 files changed, 25 insertions, 37 deletions
diff --git a/block/block-backend.c b/block/block-backend.c index adf592e867..d74f6701b5 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -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; @@ -160,6 +162,8 @@ BlockBackend *blk_new_open(const char *filename, const char *reference, return NULL; } + blk_set_enable_write_cache(blk, true); + return blk; } @@ -372,23 +376,6 @@ BlockDriverState *blk_bs(BlockBackend *blk) } /* - * Changes the BlockDriverState attached to @blk - */ -void blk_set_bs(BlockBackend *blk, BlockDriverState *bs) -{ - bdrv_ref(bs); - - if (blk->root) { - blk->root->bs->blk = NULL; - bdrv_root_unref_child(blk->root); - } - assert(bs->blk == NULL); - - blk->root = bdrv_root_attach_child(bs, "root", &child_root); - bs->blk = blk; -} - -/* * Return @blk's DriveInfo if any, else null. */ DriveInfo *blk_legacy_dinfo(BlockBackend *blk) @@ -712,11 +699,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); } @@ -1223,28 +1216,12 @@ 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); - - if (bs) { - bdrv_set_enable_write_cache(bs, wce); - } else { - if (wce) { - blk->root_state.open_flags |= BDRV_O_CACHE_WB; - } else { - blk->root_state.open_flags &= ~BDRV_O_CACHE_WB; - } - } + blk->enable_write_cache = wce; } void blk_invalidate_cache(BlockBackend *blk, Error **errp) @@ -1505,11 +1482,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) |