aboutsummaryrefslogtreecommitdiff
path: root/block/block-backend.c
diff options
context:
space:
mode:
authorKevin Wolf <kwolf@redhat.com>2016-03-04 14:28:01 +0100
committerKevin Wolf <kwolf@redhat.com>2016-03-30 12:16:02 +0200
commitbfd18d1e0b06608226fcef1982c3a439c49c3366 (patch)
tree86f43754a9f11d01c5521dee96f51660cd440bda /block/block-backend.c
parent855a6a93a12b60b2799eb5999baabd7a85a37c99 (diff)
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: Kevin Wolf <kwolf@redhat.com> Reviewed-by: Max Reitz <mreitz@redhat.com>
Diffstat (limited to 'block/block-backend.c')
-rw-r--r--block/block-backend.c42
1 files changed, 27 insertions, 15 deletions
diff --git a/block/block-backend.c b/block/block-backend.c
index 048f48e935..a263636a58 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;
@@ -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)