qcow2 metadata cache
Subsystems (2)
v4 cover qcow2 metadata cache
block-queue turned out to be too big effort to be useful for quickly fixing the performance problems that qcow2 got since we introduced the metadata flushes. While I still think the idea is right, it needs more time and qcow2 doesn't have more time. Let's come back to block-queue later when the most urgent qcow2 problems are fixed.
So this is the idea of block-queue applied to the very specific case of qcow2. Whereas block-queue tried to be a generic solution for all kind of things and tried to make all writes asynchronous at the same time, this is only about batching writes to refcount blocks and L2 tables in qcow2 and getting the dependencies right. (Yes, the L1 table and refcount table is left alone. They are almost never written to anyway.)
This should be much easier to understand and review, and I myself feel a bit more confident about it than with block-queue, too.
v1:
- Don't read newly allocated tables from the disk before memsetting them to zero
v2:
- Addressed Stefan's review comments
- Added patch 3 to avoid an unnecessary bdrv_flush after COW
v3:
- Some error path fixes (esp. missing or double qcow2_cache_put)
v4:
- Pay attention to make writethrough performance not even worse in update_refcount
- Add some blkdebug events back so that "failures" in qemu-iotests are kept minimal. Some diff between master and this series is left in test 026, but it's harmless.
Kevin Wolf (3):
qcow2: Add QcowCache
qcow2: Use QcowCache
qcow2: Batch flushes for COW
Makefile.objs | 2 +- block/qcow2-cache.c | 314 ++++++++++++++++++++++++++++++++++++++++++++++++ block/qcow2-cluster.c | 207 +++++++++++--------------------- block/qcow2-refcount.c | 258 ++++++++++++++++----------------------- block/qcow2.c | 48 +++++++- block/qcow2.h | 32 ++++- 6 files changed, 564 insertions(+), 297 deletions(-) create mode 100644 block/qcow2-cache.c
Signature · 1 line
1.7.2.3
PATCH v4 1/3 qcow2: Add QcowCache
This adds some new cache functions to qcow2 which can be used for caching refcount blocks and L2 tables. When used with cache=writethrough they work like the old caching code which is spread all over qcow2, so for this case we have merely a cleanup.
The interesting case is with writeback caching (this includes cache=none) where data isn't written to disk immediately but only kept in cache initially. This leads to some form of metadata write batching which avoids the current "write to refcount block, flush, write to L2 table" pattern for each single request when a lot of cluster allocations happen. Instead, cache entries are only written out if its required to maintain the right order. In the pure cluster allocation case this means that all metadata updates for requests are done in memory initially and on sync, first the refcount blocks are written to disk, then fsync, then L2 tables.
This improves performance of scenarios with lots of cluster allocations noticably (e.g. installation or after taking a snapshot).
--- Makefile.objs | 2 +- block/qcow2-cache.c | 290 +++++++++++++++++++++++++++++++++++++++++++++++++++ block/qcow2.h | 19 ++++ 3 files changed, 310 insertions(+), 1 deletions(-) create mode 100644 block/qcow2-cache.c
Makefile.objs +1 -1
@@ -19,7 +19,7 @@ block-obj-$(CONFIG_POSIX) += posix-aio-compat.o block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o block-nested-y += raw.o cow.o qcow.o vdi.o vmdk.o cloop.o dmg.o bochs.o vpc.o vvfat.o -block-nested-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o +block-nested-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o qcow2-cache.o block-nested-y += qed.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o block-nested-y += qed-check.o block-nested-y += parallels.o nbd.o blkdebug.o sheepdog.o blkverify.o
block/qcow2-cache.c new file +290 -0
290 lines not on this page · raw message
block/qcow2.h +19 -0
@@ -78,6 +78,9 @@ typedef struct QCowSnapshot {uint64_t vm_clock_nsec; } QCowSnapshot; +struct Qcow2Cache ; +typedef struct Qcow2Cache Qcow2Cache ; + typedef struct BDRVQcowState { int cluster_bits; int cluster_size;
@@ -215,4 +218,20 @@ 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 */ +Qcow2Cache *qcow2_cache_create (BlockDriverState *bs,int num_tables, +bool writethrough); +int qcow2_cache_destroy (BlockDriverState * bs,Qcow2Cache *c); + +void qcow2_cache_entry_mark_dirty (Qcow2Cache *c,void *table); +int qcow2_cache_flush (BlockDriverState *bs,Qcow2Cache *c); +int qcow2_cache_set_dependency (BlockDriverState *bs,Qcow2Cache *c, +Qcow2Cache *dependency); + +int qcow2_cache_get (BlockDriverState *bs,Qcow2Cache *c,uint64_t offset, +void **table); +int qcow2_cache_get_empty (BlockDriverState *bs,Qcow2Cache *c,uint64_t offset, +void **table); +int qcow2_cache_put (BlockDriverState *bs,Qcow2Cache *c,void **table); +#endif
Signature · 1 line
1.7.2.3
Re: [Qemu-devel] [PATCH v4 1/3] qcow2: Add QcowCache
Quoted 2 lines · Kevin Wolf
+int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c,
uint64_toffset,
+ void **table)
[...]
block/qcow2-cache.c line 263
Quoted 12 lines
block/qcow2-cache.c new file +12
-0+int qcow2_cache_put (BlockDriverState *bs,Qcow2Cache *c,void **table) +{ + int i; + + for (i = 0; i < c->size; i++) { + if (c->entries[i].table == *table) { + goto found; + } + } + return -ENOENT; + +found:
Using void **table instead of a QCowCacheEntry struct has two disadvantages:
1. The fact that you're holding a reference is not explicit. It makes it unclear whether we're dealing with a cached table or not. In user code, uint64_t *l2_table doesn't tell me whether this table is in the cache or is being managed outside the cache. Therefore it's hard to check that the necessary qcow2_cache_put() calls are being made.
2. qcow2-cache.c needs to scan through the cache linearly looking for void *table on every call. If the user holds an explicit QCowCacheEntry then no lookup is necessary.
Stefan
PATCH v4 2/3 qcow2: Use QcowCache
In reply to Kevin WolfUse the new functions of qcow2-cache.c for everything that works on refcount block and L2 tables.
--- block/qcow2-cache.c | 10 ++ block/qcow2-cluster.c | 207 +++++++++++++------------------------- block/qcow2-refcount.c | 258 ++++++++++++++++++++---------------------------- block/qcow2.c | 48 ++++++++- block/qcow2.h | 12 ++- 5 files changed, 239 insertions(+), 296 deletions(-)
block/qcow2-cache.c +10 -0
@@ -104,6 +104,12 @@ static int qcow2_cache_entry_flush(BlockDriverState *bs, Qcow2Cache *c, int i) } } +if (c == s->refcount_block_cache) { +BLKDBG_EVENT (bs->file,BLKDBG_REFBLOCK_UPDATE_PART ); + }else if (c == s->l2_table_cache) { +BLKDBG_EVENT (bs->file,BLKDBG_L2_UPDATE ); + } + ret =bdrv_pwrite (bs->file, c->entries[i].offset, c->entries[i].table, s->cluster_size);if (ret <0 ) {
@@ -218,6 +224,10 @@ static int qcow2_cache_do_get(BlockDriverState *bs, Qcow2Cache *c, c->entries[i].offset =0 ;if (read_from_disk) { +if (c == s->l2_table_cache) { +BLKDBG_EVENT (bs->file,BLKDBG_L2_LOAD ); + } + ret =bdrv_pread (bs->file, offset, c->entries[i].table, s->cluster_size);if (ret <0 ) {return ret;
block/qcow2-cluster.c +72 -135
355 lines not on this page · raw message
block/qcow2-refcount.c +106 -152
444 lines not on this page · raw message
block/qcow2.c +44 -4
84 lines not on this page · raw message
block/qcow2.h +7 -5
@@ -51,6 +51,9 @@#define L2_CACHE_SIZE 16 +/* Must be at least 4 to cover all cases of refcount table growth */ +#define REFCOUNT_CACHE_SIZE 4 + typedef struct QCowHeader { uint32_t magic; uint32_t version;
@@ -94,9 +97,10 @@ typedef struct BDRVQcowState {uint64_t cluster_offset_mask;uint64_t l1_table_offset;uint64_t *l1_table; -uint64_t *l2_cache; -uint64_t l2_cache_offsets[L2_CACHE_SIZE ]; -uint32_t l2_cache_counts[L2_CACHE_SIZE ]; + +Qcow2Cache * l2_table_cache; +Qcow2Cache * refcount_block_cache; +uint8_t *cluster_cache;uint8_t *cluster_data;uint64_t cluster_cache_offset;
@@ -105,8 +109,6 @@ typedef struct BDRVQcowState {uint64_t *refcount_table;uint64_t refcount_table_offset;uint32_t refcount_table_size; -uint64_t refcount_block_cache_offset; -uint16_t *refcount_block_cache;int64_t free_cluster_index;int64_t free_byte_offset;
Signature · 1 line
1.7.2.3
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [Qemu-devel] [PATCH v4 2/3] qcow2: Use QcowCache
[ Re-adding qemu-devel to CC ]
Am 24.01.2011 15:34, schrieb Stefan Hajnoczi:
Quoted 12 lines · Kevin Wolf
Quoted 11 lines
block/qcow2-cluster.c +1
-0@@ -702,17 +622,30 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)if (m->nb_available & (s->cluster_sectors -1 )) {uint64_t end = m->nb_available & ~(uint64_t )(s->cluster_sectors -1 ); + cow = true; ret =copy_sectors (bs, start_sect + end, cluster_offset + (end <<9 ), m->nb_available - end, s->cluster_sectors);if (ret <0 ) goto err; }
Quoted 12 lines
block/qcow2-cluster.c int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m) · line 640
Quoted 10 lines
block/qcow2-cluster.c +9
-1-/* update L2 table */ +/* +* Update L2 table. +* +* Before we update the L2 table to actually point to the new cluster, we +* need to be sure that the refcounts have been increased and COW was +* handled. +*/ +if (cow) { +bdrv_flush (bs->file);Just
bdrv_flush(bs->file)and not a refcounts cache flush?
Refcounts and data need not to be ordered against each other. They only must both be on disk when we write the L2 table.
Quoted 44 lines
Quoted 19 lines
block/qcow2-cluster.c +4
-4+ } + +qcow2_cache_set_dependency (bs, s->l2_table_cache, s->refcount_block_cache); ret =get_cluster_table (bs, m->offset, &l2_table, &l2_offset, &l2_index);if (ret <0 ) { goto err; } +qcow2_cache_entry_mark_dirty (s->l2_table_cache, l2_table);for (i =0 ; i < m->nb_clusters; i++) {/* if two concurrent writes happen to the same unallocated cluster @@ -728,16 +661,9 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m) (i << s->cluster_bits)) | QCOW_OFLAG_COPIED); } -/* -* Before we update the L2 table to actually point to the new cluster, we -* need to be sure that the refcounts have been increased and COW was -* handled. block/qcow2-cluster.c int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m) · line 669
Quoted 10 lines
block/qcow2-cluster.c +1
-4-*/ -bdrv_flush (bs->file); - ret =write_l2_entries (bs, l2_table, l2_offset, l2_index, m->nb_clusters); + ret =qcow2_cache_put (bs, s->l2_table_cache, (void **) &l2_table);if (ret <0 ) { -qcow2_l2_cache_reset (bs); goto err; }The function continues like this:
/*
* If this was a COW, we need to decrease the refcount of the old cluster.
* Also flushbs->fileto get the right order for L2 and refcount update. */if (j != 0) { bdrv_flush(bs->file); for (i = 0; i < j; i++) { qcow2_free_any_clusters(bs, be64_to_cpu(old_cluster[i]) & ~QCOW_OFLAG_COPIED, 1); } }
Quoted 3 lines
Does
bdrv_flush(bs->file)"get the right order for L2 and refcount update"? We've just put an L2 table, should this be an L2 table flush?
I agree, this looks wrong. What we need is a dependency to ensure that first L2 is flushed and then the refcount block. qcow2_free_any_clusters() calls update_refcount() indirectly, which takes care of setting this dependency.
So in the end I think it's just an unnecessary bdrv_flush. Makes sense?
Kevin
Re: [Qemu-devel] [PATCH v4 2/3] qcow2: Use QcowCache
Quoted 28 lines · Kevin Wolf
[ Re-adding qemu-devel to CC ]
Am 24.01.2011 15:34, schrieb Stefan Hajnoczi:
Quoted 12 lines · Kevin Wolf
Quoted 11 lines
block/qcow2-cluster.c +1
-0@@ -702,17 +622,30 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m) if (m->nb_available & (s->cluster_sectors - 1)) { uint64_t end = m->nb_available & ~(uint64_t)(s->cluster_sectors - 1); + cow = true; ret = copy_sectors(bs, start_sect + end, cluster_offset + (end << 9), m->nb_available - end, s->cluster_sectors); if (ret < 0) goto err; }Quoted 12 lines
block/qcow2-cluster.c int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m) · line 640
Quoted 10 lines
block/qcow2-cluster.c +9
-1- /* update L2 table */ + /* + * Update L2 table. + * + * Before we update the L2 table to actually point to the new cluster, we + * need to be sure that the refcounts have been increased and COW was + * handled. + */ + if (cow) { + bdrv_flush(bs->file);Just
bdrv_flush(bs->file)and not a refcounts cache flush?
Quoted 2 lines
Refcounts and data need not to be ordered against each other. They only must both be on disk when we write the L2 table.
Have I missed where refcounts actually get flushed from the cache out to the disk because bdrv_flush(bs->file) only syncs the file but doesn't write out dirty data held in cache.
Quoted 48 lines
Quoted 44 lines
Quoted 19 lines
block/qcow2-cluster.c +4
-4+ } + + qcow2_cache_set_dependency(bs, s->l2_table_cache, s->refcount_block_cache); ret = get_cluster_table(bs, m->offset, &l2_table, &l2_offset, &l2_index); if (ret < 0) { goto err; } + qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table); for (i = 0; i < m->nb_clusters; i++) { /* if two concurrent writes happen to the same unallocated cluster@@ -728,16 +661,9 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m) (i << s->cluster_bits)) | QCOW_OFLAG_COPIED); } - /* -* Before we update the L2 table to actually point to the new cluster, we -* need to be sure that the refcounts have been increased and COW was -* handled. block/qcow2-cluster.c int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m) · line 669
Quoted 10 lines
block/qcow2-cluster.c +1
-4-*/ - bdrv_flush(bs->file); - ret = write_l2_entries(bs, l2_table, l2_offset, l2_index, m->nb_clusters); + ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table); if (ret < 0) { - qcow2_l2_cache_reset(bs); goto err; }The function continues like this:
/*
* If this was a COW, we need to decrease the refcount of the old cluster.
* Also flushbs->fileto get the right order for L2 and refcount update. */
if (j != 0) {
bdrv_flush(bs->file);
for (i = 0; i < j; i++) {
qcow2_free_any_clusters(bs, be64_to_cpu(old_cluster[i]) & ~QCOW_OFLAG_COPIED, 1); }
}Quoted 3 lines
Does
bdrv_flush(bs->file)"get the right order for L2 and refcount update"? We've just put an L2 table, should this be an L2 table flush?
Quoted 6 lines
I agree, this looks wrong. What we need is a dependency to ensure that first L2 is flushed and then the refcount block.
qcow2_free_any_clusters()callsupdate_refcount()indirectly, which takes care of setting this dependency.So in the end I think it's just an unnecessary
bdrv_flush. Makes sense?
I don't understand this fully. I've noticed that the cache isn't the only mechanism for making changes to tables - there are functions like write_l2_entries() that directly write out parts of tables without honoring dependencies or using the dirty bit on the cache entry. I probably need to look at this more carefully to fully understand whether or not it is correct.
Stefan
Re: [Qemu-devel] [PATCH v4 2/3] qcow2: Use QcowCache
Am 24.01.2011 16:26, schrieb Stefan Hajnoczi:
Quoted 29 lines · Kevin Wolf
Quoted 28 lines
[ Re-adding qemu-devel to CC ]
Am 24.01.2011 15:34, schrieb Stefan Hajnoczi:
Quoted 24 lines · Kevin Wolf
Quoted 11 lines
block/qcow2-cluster.c +1
-0@@ -702,17 +622,30 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)if (m->nb_available & (s->cluster_sectors -1 )) {uint64_t end = m->nb_available & ~(uint64_t )(s->cluster_sectors -1 ); + cow = true; ret =copy_sectors (bs, start_sect + end, cluster_offset + (end <<9 ), m->nb_available - end, s->cluster_sectors);if (ret <0 ) goto err; }block/qcow2-cluster.c int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m) · line 640
Quoted 10 lines
block/qcow2-cluster.c +9
-1-/* update L2 table */ +/* +* Update L2 table. +* +* Before we update the L2 table to actually point to the new cluster, we +* need to be sure that the refcounts have been increased and COW was +* handled. +*/ +if (cow) { +bdrv_flush (bs->file);Just
bdrv_flush(bs->file)and not a refcounts cache flush?
Quoted 6 lines
Quoted 2 lines
Refcounts and data need not to be ordered against each other. They only must both be on disk when we write the L2 table.
Have I missed where refcounts actually get flushed from the cache out to the disk because
bdrv_flush(bs->file)only syncs the file but doesn't write out dirty data held in cache.
The bdrv_flush isn't supposed to flush the refcounts, but the data copied during COW (what you call pre/postfill in QED)
The refcounts are handled by the qcow2_cache_set_dependency below, so that before writing the L2 tables we always write the refcounts first.
Quoted 53 lines
Quoted 53 lines
Quoted 44 lines
Quoted 19 lines
block/qcow2-cluster.c +4
-4+ } + +qcow2_cache_set_dependency (bs, s->l2_table_cache, s->refcount_block_cache); ret =get_cluster_table (bs, m->offset, &l2_table, &l2_offset, &l2_index);if (ret <0 ) { goto err; } +qcow2_cache_entry_mark_dirty (s->l2_table_cache, l2_table);for (i =0 ; i < m->nb_clusters; i++) {/* if two concurrent writes happen to the same unallocated cluster @@ -728,16 +661,9 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m) (i << s->cluster_bits)) | QCOW_OFLAG_COPIED); } -/* -* Before we update the L2 table to actually point to the new cluster, we -* need to be sure that the refcounts have been increased and COW was -* handled. block/qcow2-cluster.c int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m) · line 669
Quoted 10 lines
block/qcow2-cluster.c +1
-4-*/ -bdrv_flush (bs->file); - ret =write_l2_entries (bs, l2_table, l2_offset, l2_index, m->nb_clusters); + ret =qcow2_cache_put (bs, s->l2_table_cache, (void **) &l2_table);if (ret <0 ) { -qcow2_l2_cache_reset (bs); goto err; }The function continues like this:
/*
* If this was a COW, we need to decrease the refcount of the old cluster.
* Also flushbs->fileto get the right order for L2 and refcount update. */if (j != 0) { bdrv_flush(bs->file); for (i = 0; i < j; i++) { qcow2_free_any_clusters(bs, be64_to_cpu(old_cluster[i]) & ~QCOW_OFLAG_COPIED, 1); } }Quoted 3 lines
Does
bdrv_flush(bs->file)"get the right order for L2 and refcount update"? We've just put an L2 table, should this be an L2 table flush?I agree, this looks wrong. What we need is a dependency to ensure that first L2 is flushed and then the refcount block.
qcow2_free_any_clusters()callsupdate_refcount()indirectly, which takes care of setting this dependency.
Quoted 8 lines
Quoted 1 line
So in the end I think it's just an unnecessary
bdrv_flush. Makes sense?I don't understand this fully. I've noticed that the cache isn't the only mechanism for making changes to tables - there are functions like
write_l2_entries()that directly write out parts of tables without honoring dependencies or using the dirty bit on the cache entry. I probably need to look at this more carefully to fully understand whether or not it is correct.
No, the cache should be the only mechanism that is used for accessing L2 tables and refcount blocks. write_l2_entries() is an old function that is removed by the patch.
Direct accesses should only be left for L1 tables and refcount tables.
Kevin
Re: [Qemu-devel] [PATCH v4 2/3] qcow2: Use QcowCache
Quoted 105 lines · Kevin Wolf
Am 24.01.2011 16:26, schrieb Stefan Hajnoczi:
Quoted 29 lines · Kevin Wolf
Quoted 28 lines
[ Re-adding qemu-devel to CC ]
Am 24.01.2011 15:34, schrieb Stefan Hajnoczi:
Quoted 24 lines · Kevin Wolf
Quoted 11 lines
block/qcow2-cluster.c +1
-0@@ -702,17 +622,30 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m) if (m->nb_available & (s->cluster_sectors - 1)) { uint64_t end = m->nb_available & ~(uint64_t)(s->cluster_sectors - 1); + cow = true; ret = copy_sectors(bs, start_sect + end, cluster_offset + (end << 9), m->nb_available - end, s->cluster_sectors); if (ret < 0) goto err; }block/qcow2-cluster.c int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m) · line 640
Quoted 10 lines
block/qcow2-cluster.c +9
-1- /* update L2 table */ + /* + * Update L2 table. + * + * Before we update the L2 table to actually point to the new cluster, we + * need to be sure that the refcounts have been increased and COW was + * handled. + */ + if (cow) { + bdrv_flush(bs->file);Just
bdrv_flush(bs->file)and not a refcounts cache flush?Quoted 6 lines
Quoted 2 lines
Refcounts and data need not to be ordered against each other. They only must both be on disk when we write the L2 table.
Have I missed where refcounts actually get flushed from the cache out to the disk because
bdrv_flush(bs->file)only syncs the file but doesn't write out dirty data held in cache.The
bdrv_flushisn't supposed to flush the refcounts, but the data copied during COW (what you call pre/postfill in QED)The refcounts are handled by the
qcow2_cache_set_dependencybelow, so that before writing the L2 tables we always write the refcounts first.Quoted 53 lines
Quoted 53 lines
Quoted 44 lines
Quoted 19 lines
block/qcow2-cluster.c +4
-4+ } + + qcow2_cache_set_dependency(bs, s->l2_table_cache, s->refcount_block_cache); ret = get_cluster_table(bs, m->offset, &l2_table, &l2_offset, &l2_index); if (ret < 0) { goto err; } + qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table); for (i = 0; i < m->nb_clusters; i++) { /* if two concurrent writes happen to the same unallocated cluster@@ -728,16 +661,9 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m) (i << s->cluster_bits)) | QCOW_OFLAG_COPIED); } - /* -* Before we update the L2 table to actually point to the new cluster, we -* need to be sure that the refcounts have been increased and COW was -* handled. block/qcow2-cluster.c int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m) · line 669
Quoted 10 lines
block/qcow2-cluster.c +1
-4-*/ - bdrv_flush(bs->file); - ret = write_l2_entries(bs, l2_table, l2_offset, l2_index, m->nb_clusters); + ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table); if (ret < 0) { - qcow2_l2_cache_reset(bs); goto err; }The function continues like this:
/*
* If this was a COW, we need to decrease the refcount of the old cluster.
* Also flushbs->fileto get the right order for L2 and refcount update. */
if (j != 0) {
bdrv_flush(bs->file);
for (i = 0; i < j; i++) {
qcow2_free_any_clusters(bs, be64_to_cpu(old_cluster[i]) & ~QCOW_OFLAG_COPIED, 1); }
}Quoted 3 lines
Does
bdrv_flush(bs->file)"get the right order for L2 and refcount update"? We've just put an L2 table, should this be an L2 table flush?I agree, this looks wrong. What we need is a dependency to ensure that first L2 is flushed and then the refcount block.
qcow2_free_any_clusters()callsupdate_refcount()indirectly, which takes care of setting this dependency.Quoted 8 lines
Quoted 1 line
So in the end I think it's just an unnecessary
bdrv_flush. Makes sense?I don't understand this fully. I've noticed that the cache isn't the only mechanism for making changes to tables - there are functions like
write_l2_entries()that directly write out parts of tables without honoring dependencies or using the dirty bit on the cache entry. I probably need to look at this more carefully to fully understand whether or not it is correct.
Quoted 5 lines
No, the cache should be the only mechanism that is used for accessing L2 tables and refcount blocks.
write_l2_entries()is an old function that is removed by the patch.Direct accesses should only be left for L1 tables and refcount tables.
Agreed. I was switching between branches and had looked at a non-qcowcache world!
Stefan
Re: [Qemu-devel] [PATCH v4 2/3] qcow2: Use QcowCache
In reply to Kevin WolfQuoted 3 lines · Kevin Wolf
Use the new functions of
qcow2-cache.cfor everything that works on refcount block and L2 tables.
While autotesting qemu-kvm's stable-0.14 branch, this commit causes failures. The scenario is:
- install OS
- reboot
- hang at GRUB prompt
- qemu-img reports hundreds of leaked clusters
The actual commit I am testing is in qemu-kvm.git qcow2-qcowcache-failure (tag). The test runs with -drive ,cache=unsafe.
Quoted 5 lines
block/qcow2.c +0
-1@@ -719,7 +727,13 @@ static void qcow2_close(BlockDriverState *bs) {BDRVQcowState *s = bs->opaque;qemu_free (s->l1_table); -qemu_free (s->l2_cache);
block/qcow2.c static void qcow2_close(BlockDriverState *bs) · line 739
Quoted 10 lines
block/qcow2.c +7
-0+ +qcow2_cache_flush (bs, s->l2_table_cache); +qcow2_cache_flush (bs, s->refcount_block_cache); + +qcow2_cache_destroy (bs, s->l2_table_cache); +qcow2_cache_destroy (bs, s->refcount_block_cache); +qemu_free (s->cluster_cache);qemu_free (s->cluster_data);qcow2_refcount_close (bs);
This is of course the immediate suspect, but it appears correct. Perhaps it isn't called, or maybe in the wrong place?
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [Qemu-devel] [PATCH v4 2/3] qcow2: Use QcowCache
Quoted 5 lines · Avi Kivity
Quoted 3 lines · Kevin Wolf
Use the new functions of
qcow2-cache.cfor everything that works on refcount block and L2 tables.
Quoted 8 lines
While autotesting qemu-kvm's stable-0.14 branch, this commit causes failures. The scenario is:
- install OS
- reboot
- hang at GRUB prompt
- qemu-img reports hundreds of leaked clustersThe actual commit I am testing is in qemu-kvm.git qcow2-qcowcache-failure (tag). The test runs with -drive ,cache=unsafe.
Got it - it's specific to qemu-kvm; the call to bdrv_close_all() wasn't replicated in qemu-kvm's main loop.
Signature · 1 line
error compiling committee.c: too many arguments to function
PATCH v4 3/3 qcow2: Batch flushes for COW
In reply to Kevin Wolfqcow2 calls bdrv_flush() after performing COW in order to ensure that the L2 table change is never written before the copy is safe on disk. Now that the L2 table is cached, we can wait with flushing until we write out the next L2 table.
--- block/qcow2-cache.c | 20 +++++++++++++++++--- block/qcow2-cluster.c | 2 +- block/qcow2.h | 1 + 3 files changed, 19 insertions(+), 4 deletions(-)
block/qcow2-cache.c +17 -3
@@ -38,6 +38,7 @@ struct Qcow2Cache {int size;Qcow2CachedTable * entries;struct Qcow2Cache * depends; +bool depends_on_flush;bool writethrough; };
@@ -85,13 +86,15 @@ static int qcow2_cache_flush_dependency(BlockDriverState *bs, Qcow2Cache *c) } c->depends =NULL ; + c->depends_on_flush = false; +return 0 ; }static int qcow2_cache_entry_flush (BlockDriverState *bs,Qcow2Cache *c,int i) {BDRVQcowState *s = bs->opaque; -int ret; +int ret =0 ;if (!c->entries[i].dirty || !c->entries[i].offset) {return 0 ;
@@ -99,11 +102,17 @@ static int qcow2_cache_entry_flush(BlockDriverState *bs, Qcow2Cache *c, int i)if (c->depends) { ret =qcow2_cache_flush_dependency (bs, c); -if (ret <0 ) { -return ret; + }else if (c->depends_on_flush) { + ret =bdrv_flush (bs->file); +if (ret >=0 ) { + c->depends_on_flush = false; } } +if (ret <0 ) { +return ret; + } +if (c == s->refcount_block_cache) {BLKDBG_EVENT (bs->file,BLKDBG_REFBLOCK_UPDATE_PART ); }else if (c == s->l2_table_cache) {
@@ -167,6 +176,11 @@ int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,return 0 ; } +void qcow2_cache_depends_on_flush (Qcow2Cache *c) +{ + c->depends_on_flush = true; +} +static int qcow2_cache_find_entry_to_replace (Qcow2Cache *c) {int i;
block/qcow2-cluster.c +1 -1
@@ -637,7 +637,7 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)* handled. */ if (cow) { -bdrv_flush (bs->file); +qcow2_cache_depends_on_flush (s->l2_table_cache); }qcow2_cache_set_dependency (bs, s->l2_table_cache, s->refcount_block_cache);
block/qcow2.h +1 -0
@@ -229,6 +229,7 @@ void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table);int qcow2_cache_flush (BlockDriverState *bs,Qcow2Cache *c);int qcow2_cache_set_dependency (BlockDriverState *bs,Qcow2Cache *c,Qcow2Cache *dependency); +void qcow2_cache_depends_on_flush (Qcow2Cache *c);int qcow2_cache_get (BlockDriverState *bs,Qcow2Cache *c,uint64_t offset,void **table);
Signature · 1 line
1.7.2.3