From: Dong Xu Wang <wdon...@linux.vnet.ibm.com> To use block-cache.c be common cache interface, we need to add some parameter to identify cache's type.
Define a struct named BlockTableType, pass BlockTableType and table size parameters to block cache initialization function. v17-v18: 1) move struct to source file. 2) cluster_size->table_size. Signed-off-by: Dong Xu Wang <wdon...@linux.vnet.ibm.com> Signed-off-by: Dongxu Wang <wdon...@linux.vnet.ibm.com> --- block/block-cache.c | 75 ++++++++++++++++++++++++++------------------- block/qcow2-cluster.c | 30 +++++++++--------- block/qcow2-refcount.c | 30 +++++++++--------- block/qcow2.c | 7 +++-- block/qcow2.h | 24 ++------------- include/block/block-cache.h | 55 +++++++++++++++++++++++++++++++++ trace-events | 1 + 7 files changed, 139 insertions(+), 83 deletions(-) create mode 100644 include/block/block-cache.h diff --git a/block/block-cache.c b/block/block-cache.c index bc057a8..f5d75d1 100644 --- a/block/block-cache.c +++ b/block/block-cache.c @@ -1,4 +1,8 @@ /* + * QEMU Block Layer Cache + * + * This file is based on qcow2-cache.c, see its copyrights below: + * * L2/refcount table cache for the QCOW2 format * * Copyright (c) 2010 Kevin Wolf <kw...@redhat.com> @@ -24,11 +28,11 @@ #include "block/block_int.h" #include "qemu-common.h" -#include "qcow2.h" #include "trace.h" +#include "block/block-cache.h" typedef struct BlockCachedTable { - void* table; + void *table; int64_t offset; bool dirty; int cache_hits; @@ -36,30 +40,34 @@ typedef struct BlockCachedTable { } BlockCachedTable; struct BlockCache { - BlockCachedTable* entries; - struct BlockCache* depends; - int size; - bool depends_on_flush; + BlockCachedTable *entries; + struct BlockCache *depends; + int size; + size_t table_size; + BlockTableType table_type; + bool depends_on_flush; }; -BlockCache *block_cache_create(BlockDriverState *bs, int num_tables) +BlockCache *block_cache_create(BlockDriverState *bs, int num_tables, + size_t table_size, BlockTableType type) { - BDRVQcowState *s = bs->opaque; BlockCache *c; int i; c = g_malloc0(sizeof(*c)); c->size = num_tables; c->entries = g_malloc0(sizeof(*c->entries) * num_tables); + c->table_type = type; + c->table_size = table_size; for (i = 0; i < c->size; i++) { - c->entries[i].table = qemu_blockalign(bs, s->cluster_size); + c->entries[i].table = qemu_blockalign(bs, table_size); } return c; } -int block_cache_destroy(BlockDriverState* bs, BlockCache *c) +int block_cache_destroy(BlockDriverState *bs, BlockCache *c) { int i; @@ -91,15 +99,13 @@ static int block_cache_flush_dependency(BlockDriverState *bs, BlockCache *c) static int block_cache_entry_flush(BlockDriverState *bs, BlockCache *c, int i) { - BDRVQcowState *s = bs->opaque; int ret = 0; if (!c->entries[i].dirty || !c->entries[i].offset) { return 0; } - trace_block_cache_entry_flush(qemu_coroutine_self(), - c == s->l2_table_cache, i); + trace_block_cache_entry_flush(qemu_coroutine_self(), c->table_type, i); if (c->depends) { ret = block_cache_flush_dependency(bs, c); @@ -114,14 +120,16 @@ static int block_cache_entry_flush(BlockDriverState *bs, BlockCache *c, int i) return ret; } - if (c == s->refcount_block_cache) { + if (c->table_type == BLOCK_TABLE_REF) { BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_UPDATE_PART); - } else if (c == s->l2_table_cache) { + } else if (c->table_type == BLOCK_TABLE_L2) { BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE); + } else if (c->table_type == BLOCK_TABLE_BITMAP) { + BLKDBG_EVENT(bs->file, BLKDBG_COW_WRITE); } - ret = bdrv_pwrite(bs->file, c->entries[i].offset, c->entries[i].table, - s->cluster_size); + ret = bdrv_pwrite(bs->file, c->entries[i].offset, + c->entries[i].table, c->table_size); if (ret < 0) { return ret; } @@ -133,12 +141,11 @@ static int block_cache_entry_flush(BlockDriverState *bs, BlockCache *c, int i) int block_cache_flush(BlockDriverState *bs, BlockCache *c) { - BDRVQcowState *s = bs->opaque; int result = 0; int ret; int i; - trace_block_cache_flush(qemu_coroutine_self(), c == s->l2_table_cache); + trace_block_cache_flush(qemu_coroutine_self(), c->table_type); for (i = 0; i < c->size; i++) { ret = block_cache_entry_flush(bs, c, i); @@ -157,8 +164,9 @@ int block_cache_flush(BlockDriverState *bs, BlockCache *c) return result; } -int block_cache_set_dependency(BlockDriverState *bs, BlockCache *c, - BlockCache *dependency) +int block_cache_set_dependency(BlockDriverState *bs, + BlockCache *c, + BlockCache *dependency) { int ret; @@ -216,13 +224,13 @@ static int block_cache_find_entry_to_replace(BlockCache *c) } static int block_cache_do_get(BlockDriverState *bs, BlockCache *c, - uint64_t offset, void **table, bool read_from_disk) + uint64_t offset, void **table, + bool read_from_disk) { - BDRVQcowState *s = bs->opaque; int i; int ret; - trace_block_cache_get(qemu_coroutine_self(), c == s->l2_table_cache, + trace_block_cache_get(qemu_coroutine_self(), c->table_type, offset, read_from_disk); /* Check if the table is already cached */ @@ -235,7 +243,7 @@ static int block_cache_do_get(BlockDriverState *bs, BlockCache *c, /* If not, write a table back and replace it */ i = block_cache_find_entry_to_replace(c); trace_block_cache_get_replace_entry(qemu_coroutine_self(), - c == s->l2_table_cache, i); + c->table_type, i); if (i < 0) { return i; } @@ -246,14 +254,17 @@ static int block_cache_do_get(BlockDriverState *bs, BlockCache *c, } trace_block_cache_get_read(qemu_coroutine_self(), - c == s->l2_table_cache, i); + c->table_type, i); c->entries[i].offset = 0; if (read_from_disk) { - if (c == s->l2_table_cache) { + if (c->table_type == BLOCK_TABLE_L2) { BLKDBG_EVENT(bs->file, BLKDBG_L2_LOAD); + } else if (c->table_type == BLOCK_TABLE_BITMAP) { + BLKDBG_EVENT(bs->file, BLKDBG_COW_READ); } - ret = bdrv_pread(bs->file, offset, c->entries[i].table, s->cluster_size); + ret = bdrv_pread(bs->file, offset, c->entries[i].table, + c->table_size); if (ret < 0) { return ret; } @@ -271,19 +282,19 @@ found: *table = c->entries[i].table; trace_block_cache_get_done(qemu_coroutine_self(), - c == s->l2_table_cache, i); + c->table_type, i); return 0; } int block_cache_get(BlockDriverState *bs, BlockCache *c, uint64_t offset, - void **table) + void **table) { return block_cache_do_get(bs, c, offset, table, true); } -int block_cache_get_empty(BlockDriverState *bs, BlockCache *c, uint64_t offset, - void **table) +int block_cache_get_empty(BlockDriverState *bs, BlockCache *c, + uint64_t offset, void **table) { return block_cache_do_get(bs, c, offset, table, false); } diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index ede64af..7a01c27 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -125,7 +125,8 @@ static int l2_load(BlockDriverState *bs, uint64_t l2_offset, BDRVQcowState *s = bs->opaque; int ret; - ret = block_cache_get(bs, s->l2_table_cache, l2_offset, (void**) l2_table); + ret = block_cache_get(bs, s->l2_table_cache, l2_offset, + (void **) l2_table); return ret; } @@ -194,7 +195,8 @@ static int l2_allocate(BlockDriverState *bs, int l1_index, uint64_t **table) /* allocate a new entry in the l2 cache */ trace_qcow2_l2_allocate_get_empty(bs, l1_index); - ret = block_cache_get_empty(bs, s->l2_table_cache, l2_offset, (void**) table); + ret = block_cache_get_empty(bs, s->l2_table_cache, l2_offset, + (void **) table); if (ret < 0) { return ret; } @@ -211,14 +213,14 @@ static int l2_allocate(BlockDriverState *bs, int l1_index, uint64_t **table) BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_COW_READ); ret = block_cache_get(bs, s->l2_table_cache, old_l2_offset & L1E_OFFSET_MASK, - (void**) &old_table); + (void **) &old_table); if (ret < 0) { goto fail; } memcpy(l2_table, old_table, s->cluster_size); - ret = block_cache_put(bs, s->l2_table_cache, (void**) &old_table); + ret = block_cache_put(bs, s->l2_table_cache, (void **) &old_table); if (ret < 0) { goto fail; } @@ -248,7 +250,7 @@ static int l2_allocate(BlockDriverState *bs, int l1_index, uint64_t **table) fail: trace_qcow2_l2_allocate_done(bs, l1_index, ret); - block_cache_put(bs, s->l2_table_cache, (void**) table); + block_cache_put(bs, s->l2_table_cache, (void **) table); s->l1_table[l1_index] = old_l2_offset; return ret; } @@ -484,7 +486,7 @@ int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset, abort(); } - block_cache_put(bs, s->l2_table_cache, (void**) &l2_table); + block_cache_put(bs, s->l2_table_cache, (void **) &l2_table); nb_available = (c * s->cluster_sectors); @@ -594,13 +596,13 @@ uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs, * allocated. */ cluster_offset = be64_to_cpu(l2_table[l2_index]); if (cluster_offset & L2E_OFFSET_MASK) { - block_cache_put(bs, s->l2_table_cache, (void**) &l2_table); + block_cache_put(bs, s->l2_table_cache, (void **) &l2_table); return 0; } cluster_offset = qcow2_alloc_bytes(bs, compressed_size); if (cluster_offset < 0) { - block_cache_put(bs, s->l2_table_cache, (void**) &l2_table); + block_cache_put(bs, s->l2_table_cache, (void **) &l2_table); return 0; } @@ -617,7 +619,7 @@ uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs, BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE_COMPRESSED); block_cache_entry_mark_dirty(s->l2_table_cache, l2_table); l2_table[l2_index] = cpu_to_be64(cluster_offset); - ret = block_cache_put(bs, s->l2_table_cache, (void**) &l2_table); + ret = block_cache_put(bs, s->l2_table_cache, (void **) &l2_table); if (ret < 0) { return 0; } @@ -707,7 +709,7 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m) } - ret = block_cache_put(bs, s->l2_table_cache, (void**) &l2_table); + ret = block_cache_put(bs, s->l2_table_cache, (void **) &l2_table); if (ret < 0) { goto err; } @@ -916,7 +918,7 @@ static int handle_copied(BlockDriverState *bs, uint64_t guest_offset, /* Cleanup */ out: - pret = block_cache_put(bs, s->l2_table_cache, (void**) &l2_table); + pret = block_cache_put(bs, s->l2_table_cache, (void **) &l2_table); if (pret < 0) { return pret; } @@ -1044,7 +1046,7 @@ static int handle_alloc(BlockDriverState *bs, uint64_t guest_offset, * wrong with our code. */ assert(nb_clusters > 0); - ret = block_cache_put(bs, s->l2_table_cache, (void**) &l2_table); + ret = block_cache_put(bs, s->l2_table_cache, (void **) &l2_table); if (ret < 0) { return ret; } @@ -1342,7 +1344,7 @@ static int discard_single_l2(BlockDriverState *bs, uint64_t offset, qcow2_free_any_clusters(bs, old_offset, 1); } - ret = block_cache_put(bs, s->l2_table_cache, (void**) &l2_table); + ret = block_cache_put(bs, s->l2_table_cache, (void **) &l2_table); if (ret < 0) { return ret; } @@ -1421,7 +1423,7 @@ static int zero_single_l2(BlockDriverState *bs, uint64_t offset, } } - ret = block_cache_put(bs, s->l2_table_cache, (void**) &l2_table); + ret = block_cache_put(bs, s->l2_table_cache, (void **) &l2_table); if (ret < 0) { return ret; } diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index 332af27..c7427e8 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -99,7 +99,7 @@ static int get_refcount(BlockDriverState *bs, int64_t cluster_index) return 0; ret = block_cache_get(bs, s->refcount_block_cache, refcount_block_offset, - (void**) &refcount_block); + (void **) &refcount_block); if (ret < 0) { return ret; } @@ -109,7 +109,7 @@ static int get_refcount(BlockDriverState *bs, int64_t cluster_index) refcount = be16_to_cpu(refcount_block[block_index]); ret = block_cache_put(bs, s->refcount_block_cache, - (void**) &refcount_block); + (void **) &refcount_block); if (ret < 0) { return ret; } @@ -172,7 +172,7 @@ static int alloc_refcount_block(BlockDriverState *bs, /* If it's already there, we're done */ if (refcount_block_offset) { return load_refcount_block(bs, refcount_block_offset, - (void**) refcount_block); + (void **) refcount_block); } } @@ -221,7 +221,7 @@ static int alloc_refcount_block(BlockDriverState *bs, if (in_same_refcount_block(s, new_block, cluster_index << s->cluster_bits)) { /* Zero the new refcount block before updating it */ ret = block_cache_get_empty(bs, s->refcount_block_cache, new_block, - (void**) refcount_block); + (void **) refcount_block); if (ret < 0) { goto fail_block; } @@ -248,7 +248,7 @@ static int alloc_refcount_block(BlockDriverState *bs, /* Initialize the new refcount block only after updating its refcount, * update_refcount uses the refcount cache itself */ ret = block_cache_get_empty(bs, s->refcount_block_cache, new_block, - (void**) refcount_block); + (void **) refcount_block); if (ret < 0) { goto fail_block; } @@ -279,7 +279,8 @@ static int alloc_refcount_block(BlockDriverState *bs, return 0; } - ret = block_cache_put(bs, s->refcount_block_cache, (void**) refcount_block); + ret = block_cache_put(bs, s->refcount_block_cache, + (void **) refcount_block); if (ret < 0) { goto fail_block; } @@ -402,7 +403,7 @@ static int alloc_refcount_block(BlockDriverState *bs, qcow2_free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t)); s->free_cluster_index = old_free_cluster_index; - ret = load_refcount_block(bs, new_block, (void**) refcount_block); + ret = load_refcount_block(bs, new_block, (void **) refcount_block); if (ret < 0) { return ret; } @@ -413,7 +414,7 @@ fail_table: g_free(new_table); fail_block: if (*refcount_block != NULL) { - block_cache_put(bs, s->refcount_block_cache, (void**) refcount_block); + block_cache_put(bs, s->refcount_block_cache, (void **) refcount_block); } return ret; } @@ -457,7 +458,7 @@ static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs, if (table_index != old_table_index) { if (refcount_block) { ret = block_cache_put(bs, s->refcount_block_cache, - (void**) &refcount_block); + (void **) &refcount_block); if (ret < 0) { goto fail; } @@ -494,7 +495,7 @@ fail: if (refcount_block) { int wret; wret = block_cache_put(bs, s->refcount_block_cache, - (void**) &refcount_block); + (void **) &refcount_block); if (wret < 0) { return ret < 0 ? ret : wret; } @@ -768,7 +769,7 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs, l2_offset &= L1E_OFFSET_MASK; ret = block_cache_get(bs, s->l2_table_cache, l2_offset, - (void**) &l2_table); + (void **) &l2_table); if (ret < 0) { goto fail; } @@ -815,12 +816,13 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs, s->refcount_block_cache); } l2_table[j] = cpu_to_be64(offset); - block_cache_entry_mark_dirty(s->l2_table_cache, l2_table); + block_cache_entry_mark_dirty(s->l2_table_cache, + l2_table); } } } - ret = block_cache_put(bs, s->l2_table_cache, (void**) &l2_table); + ret = block_cache_put(bs, s->l2_table_cache, (void **) &l2_table); if (ret < 0) { goto fail; } @@ -847,7 +849,7 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs, ret = bdrv_flush(bs); fail: if (l2_table) { - block_cache_put(bs, s->l2_table_cache, (void**) &l2_table); + block_cache_put(bs, s->l2_table_cache, (void **) &l2_table); } /* Update L1 only if it isn't deleted anyway (addend = -1) */ diff --git a/block/qcow2.c b/block/qcow2.c index c3340ad..cec1c26 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -454,8 +454,11 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags) } /* alloc L2 table/refcount block cache */ - s->l2_table_cache = block_cache_create(bs, L2_CACHE_SIZE); - s->refcount_block_cache = block_cache_create(bs, REFCOUNT_CACHE_SIZE); + s->l2_table_cache = block_cache_create(bs, L2_CACHE_SIZE, + s->cluster_size, BLOCK_TABLE_L2); + s->refcount_block_cache = block_cache_create(bs, REFCOUNT_CACHE_SIZE, + s->cluster_size, + BLOCK_TABLE_REF); s->cluster_cache = g_malloc(s->cluster_size); /* one more sector for decompressed data alignment */ diff --git a/block/qcow2.h b/block/qcow2.h index 6e63217..7494814 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -27,6 +27,7 @@ #include "qemu/aes.h" #include "block/coroutine.h" +#include "block/block-cache.h" //#define DEBUG_ALLOC //#define DEBUG_ALLOC2 @@ -97,9 +98,6 @@ typedef struct QCowSnapshot { uint64_t vm_clock_nsec; } QCowSnapshot; -struct BlockCache; -typedef struct BlockCache BlockCache; - typedef struct Qcow2UnknownHeaderExtension { uint32_t magic; uint32_t len; @@ -149,8 +147,8 @@ typedef struct BDRVQcowState { uint64_t l1_table_offset; uint64_t *l1_table; - BlockCache* l2_table_cache; - BlockCache* refcount_block_cache; + BlockCache *l2_table_cache; + BlockCache *refcount_block_cache; uint8_t *cluster_cache; uint8_t *cluster_data; @@ -392,20 +390,4 @@ int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name); void qcow2_free_snapshots(BlockDriverState *bs); int qcow2_read_snapshots(BlockDriverState *bs); -/* qcow2-cache.c functions */ -BlockCache *block_cache_create(BlockDriverState *bs, int num_tables); -int block_cache_destroy(BlockDriverState* bs, BlockCache *c); - -void block_cache_entry_mark_dirty(BlockCache *c, void *table); -int block_cache_flush(BlockDriverState *bs, BlockCache *c); -int block_cache_set_dependency(BlockDriverState *bs, BlockCache *c, - BlockCache *dependency); -void block_cache_depends_on_flush(BlockCache *c); - -int block_cache_get(BlockDriverState *bs, BlockCache *c, uint64_t offset, - void **table); -int block_cache_get_empty(BlockDriverState *bs, BlockCache *c, uint64_t offset, - void **table); -int block_cache_put(BlockDriverState *bs, BlockCache *c, void **table); - #endif diff --git a/include/block/block-cache.h b/include/block/block-cache.h new file mode 100644 index 0000000..1992765 --- /dev/null +++ b/include/block/block-cache.h @@ -0,0 +1,55 @@ +/* + * QEMU Block Layer Cache + * + * This file is based on qcow2-cache.c, see its copyrights below: + * + * L2/refcount table cache for the QCOW2 format + * + * Copyright (c) 2010 Kevin Wolf <kw...@redhat.com> + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef BLOCK_CACHE_H +#define BLOCK_CACHE_H + +typedef enum { + BLOCK_TABLE_REF, + BLOCK_TABLE_L2, + BLOCK_TABLE_BITMAP, +} BlockTableType; + +struct BlockCache; +typedef struct BlockCache BlockCache; + +BlockCache *block_cache_create(BlockDriverState *bs, int num_tables, + size_t table_size, BlockTableType type); +int block_cache_destroy(BlockDriverState *bs, BlockCache *c); +int block_cache_flush(BlockDriverState *bs, BlockCache *c); +int block_cache_set_dependency(BlockDriverState *bs, + BlockCache *c, + BlockCache *dependency); +void block_cache_depends_on_flush(BlockCache *c); +int block_cache_get(BlockDriverState *bs, BlockCache *c, uint64_t offset, + void **table); +int block_cache_get_empty(BlockDriverState *bs, BlockCache *c, + uint64_t offset, void **table); +int block_cache_put(BlockDriverState *bs, BlockCache *c, void **table); +void block_cache_entry_mark_dirty(BlockCache *c, void *table); +#endif /* BLOCK_CACHE_H */ diff --git a/trace-events b/trace-events index b7edcc9..b701e01 100644 --- a/trace-events +++ b/trace-events @@ -503,6 +503,7 @@ qcow2_l2_allocate_write_l2(void *bs, int l1_index) "bs %p l1_index %d" qcow2_l2_allocate_write_l1(void *bs, int l1_index) "bs %p l1_index %d" qcow2_l2_allocate_done(void *bs, int l1_index, int ret) "bs %p l1_index %d ret %d" +# block/block-cache.c block_cache_get(void *co, int c, uint64_t offset, bool read_from_disk) "co %p is_l2_cache %d offset %" PRIx64 " read_from_disk %d" block_cache_get_replace_entry(void *co, int c, int i) "co %p is_l2_cache %d index %d" block_cache_get_read(void *co, int c, int i) "co %p is_l2_cache %d index %d" -- 1.7.11.7