backup: fix skipping unallocated clusters
cover backup: fix skipping unallocated clusters
Hi all!
There is a bug in not yet merged patch
"block/backup: teach TOP to never copy unallocated regions"
in https://github.com/jnsnow/qemu bitmaps. 04 fixes it. So, I propose to put 01-03 somewhere before
"block/backup: teach TOP to never copy unallocated regions"
and squash 04 into "block/backup: teach TOP to never copy unallocated regions"
Based-on: https://github.com/jnsnow/qemu bitmaps
Vladimir Sementsov-Ogievskiy (4):
block/dirty-bitmap: switch _next_dirty_area and _next_zero to int64_t block/dirty-bitmap: add _next_dirty API
block/backup: use bdrv_dirty_bitmap_next_dirty block/backup: fix and improve skipping unallocated in backup_do_cow
include/block/dirty-bitmap.h | 8 ++-- include/qemu/hbitmap.h | 18 +++++++-- block/backup.c | 33 ++++++++++------- block/dirty-bitmap.c | 12 ++++-- block/mirror.c | 4 +- tests/test-hbitmap.c | 32 ++++++++-------- util/hbitmap.c | 72 ++++++++++++++++++++---------------- block/trace-events | 1 - 8 files changed, 107 insertions(+), 73 deletions(-)
Signature · 1 line
2.18.0
PATCH 1/4 block/dirty-bitmap: switch _next_dirty_area and _next_zero to int64_t
It's very uncomfortable that we can't use same variable as result of bdrv_dirty_bitmap_next_zero and parameter of bdrv_dirty_bitmap_next_dirty_area.
--- include/block/dirty-bitmap.h | 6 +++--- include/qemu/hbitmap.h | 5 ++--- block/dirty-bitmap.c | 6 +++--- block/mirror.c | 4 ++-- tests/test-hbitmap.c | 32 ++++++++++++++++---------------- util/hbitmap.c | 15 ++++++++++----- 6 files changed, 36 insertions(+), 32 deletions(-)
include/block/dirty-bitmap.h +3 -3
@@ -108,10 +108,10 @@ bool bdrv_has_changed_persistent_bitmaps(BlockDriverState *bs);BdrvDirtyBitmap *bdrv_dirty_bitmap_next (BlockDriverState *bs,BdrvDirtyBitmap *bitmap);char *bdrv_dirty_bitmap_sha256 (const BdrvDirtyBitmap *bitmap,Error **errp); -int64_t bdrv_dirty_bitmap_next_zero (BdrvDirtyBitmap *bitmap,uint64_t offset, -uint64_t bytes); +int64_t bdrv_dirty_bitmap_next_zero (BdrvDirtyBitmap *bitmap,int64_t offset, +int64_t bytes);bool bdrv_dirty_bitmap_next_dirty_area (BdrvDirtyBitmap *bitmap, -uint64_t *offset,uint64_t *bytes); +int64_t *offset,int64_t *bytes);BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap_locked (BlockDriverState *bs,BdrvDirtyBitmap *bitmap,Error **errp);
include/qemu/hbitmap.h +2 -3
@@ -309,7 +309,7 @@ unsigned long hbitmap_iter_skip_words(HBitmapIter *hbi);* bitmap is looked through. You can use UINT64_MAX as @count to search up to * the bitmap end. */ -int64_t hbitmap_next_zero (const HBitmap *hb,uint64_t start,uint64_t count); +int64_t hbitmap_next_zero (const HBitmap *hb,int64_t start,int64_t count);/* hbitmap_next_dirty_area: * @hb: The HBitmap to operate on
@@ -324,8 +324,7 @@ int64_t hbitmap_next_zero(const HBitmap *hb, uint64_t start, uint64_t count);* @offset and @bytes appropriately. Otherwise returns false and leaves @offset * and @bytes unchanged. */ -bool hbitmap_next_dirty_area (const HBitmap *hb,uint64_t *start, -uint64_t *count); +bool hbitmap_next_dirty_area (const HBitmap *hb,int64_t *start,int64_t *count);/* hbitmap_create_meta: * Create a "meta" hbitmap to track dirtiness of the bits in this HBitmap.
block/dirty-bitmap.c +3 -3
@@ -802,14 +802,14 @@ char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap *bitmap, Error **errp)return hbitmap_sha256 (bitmap->bitmap, errp); } -int64_t bdrv_dirty_bitmap_next_zero (BdrvDirtyBitmap *bitmap,uint64_t offset, -uint64_t bytes) +int64_t bdrv_dirty_bitmap_next_zero (BdrvDirtyBitmap *bitmap,int64_t offset, +int64_t bytes) {return hbitmap_next_zero (bitmap->bitmap, offset, bytes); }bool bdrv_dirty_bitmap_next_dirty_area (BdrvDirtyBitmap *bitmap, -uint64_t *offset,uint64_t *bytes) +int64_t *offset,int64_t *bytes) {return hbitmap_next_dirty_area (bitmap->bitmap, offset, bytes); }
block/mirror.c +2 -2
@@ -1198,8 +1198,8 @@ do_sync_target_write(MirrorBlockJob *job, MirrorMethod method, QEMUIOVector *qiov, int flags) {QEMUIOVector target_qiov; -uint64_t dirty_offset = offset; -uint64_t dirty_bytes; +int64_t dirty_offset = offset; +int64_t dirty_bytes;if (qiov) {qemu_iovec_init (&target_qiov, qiov->niov);
tests/test-hbitmap.c +16 -16
@@ -946,7 +946,7 @@ static void test_hbitmap_next_zero_check_range(TestHBitmapData *data,static void test_hbitmap_next_zero_check (TestHBitmapData *data,int64_t start) { -test_hbitmap_next_zero_check_range (data, start,UINT64_MAX ); +test_hbitmap_next_zero_check_range (data, start,INT64_MAX ); }static void test_hbitmap_next_zero_do (TestHBitmapData *data,int granularity)
@@ -1014,11 +1014,11 @@ static void test_hbitmap_next_zero_after_truncate(TestHBitmapData *data, }static void test_hbitmap_next_dirty_area_check (TestHBitmapData *data, -uint64_t offset, -uint64_t count) +int64_t offset, +int64_t count) { -uint64_t off1, off2; -uint64_t len1 =0 , len2; +int64_t off1, off2; +int64_t len1 =0 , len2;bool ret1, ret2;int64_t end;
@@ -1054,24 +1054,24 @@ static void test_hbitmap_next_dirty_area_do(TestHBitmapData *data, int granularity) {hbitmap_test_init (data,L3 , granularity); -test_hbitmap_next_dirty_area_check (data,0 ,UINT64_MAX ); +test_hbitmap_next_dirty_area_check (data,0 ,INT64_MAX );test_hbitmap_next_dirty_area_check (data,0 ,1 );test_hbitmap_next_dirty_area_check (data,L3 -1 ,1 );hbitmap_set (data->hb,L2 ,1 );test_hbitmap_next_dirty_area_check (data,0 ,1 );test_hbitmap_next_dirty_area_check (data,0 ,L2 ); -test_hbitmap_next_dirty_area_check (data,0 ,UINT64_MAX ); -test_hbitmap_next_dirty_area_check (data,L2 -1 ,UINT64_MAX ); +test_hbitmap_next_dirty_area_check (data,0 ,INT64_MAX ); +test_hbitmap_next_dirty_area_check (data,L2 -1 ,INT64_MAX );test_hbitmap_next_dirty_area_check (data,L2 -1 ,1 );test_hbitmap_next_dirty_area_check (data,L2 -1 ,2 );test_hbitmap_next_dirty_area_check (data,L2 -1 ,3 ); -test_hbitmap_next_dirty_area_check (data,L2 ,UINT64_MAX ); +test_hbitmap_next_dirty_area_check (data,L2 ,INT64_MAX );test_hbitmap_next_dirty_area_check (data,L2 ,1 );test_hbitmap_next_dirty_area_check (data,L2 +1 ,1 );hbitmap_set (data->hb,L2 +5 ,L1 ); -test_hbitmap_next_dirty_area_check (data,0 ,UINT64_MAX ); +test_hbitmap_next_dirty_area_check (data,0 ,INT64_MAX );test_hbitmap_next_dirty_area_check (data,L2 -2 ,8 );test_hbitmap_next_dirty_area_check (data,L2 +1 ,5 );test_hbitmap_next_dirty_area_check (data,L2 +1 ,3 );
@@ -1083,16 +1083,16 @@ static void test_hbitmap_next_dirty_area_do(TestHBitmapData *data,test_hbitmap_next_dirty_area_check (data,L2 +1 ,0 );hbitmap_set (data->hb,L2 *2 ,L3 -L2 *2 ); -test_hbitmap_next_dirty_area_check (data,0 ,UINT64_MAX ); -test_hbitmap_next_dirty_area_check (data,L2 ,UINT64_MAX ); -test_hbitmap_next_dirty_area_check (data,L2 +1 ,UINT64_MAX ); -test_hbitmap_next_dirty_area_check (data,L2 +5 +L1 -1 ,UINT64_MAX ); +test_hbitmap_next_dirty_area_check (data,0 ,INT64_MAX ); +test_hbitmap_next_dirty_area_check (data,L2 ,INT64_MAX ); +test_hbitmap_next_dirty_area_check (data,L2 +1 ,INT64_MAX ); +test_hbitmap_next_dirty_area_check (data,L2 +5 +L1 -1 ,INT64_MAX );test_hbitmap_next_dirty_area_check (data,L2 +5 +L1 ,5 );test_hbitmap_next_dirty_area_check (data,L2 *2 -L1 ,L1 +1 );test_hbitmap_next_dirty_area_check (data,L2 *2 ,L2 );hbitmap_set (data->hb,0 ,L3 ); -test_hbitmap_next_dirty_area_check (data,0 ,UINT64_MAX ); +test_hbitmap_next_dirty_area_check (data,0 ,INT64_MAX ); } static void test_hbitmap_next_dirty_area_0(TestHBitmapData *data,
@@ -1119,7 +1119,7 @@ static void test_hbitmap_next_dirty_area_after_truncate(TestHBitmapData *data,hbitmap_test_init (data,L1 ,0 );hbitmap_test_truncate_impl (data,L1 *2 );hbitmap_set (data->hb,L1 +1 ,1 ); -test_hbitmap_next_dirty_area_check (data,0 ,UINT64_MAX ); +test_hbitmap_next_dirty_area_check (data,0 ,INT64_MAX ); }int main (int argc,char **argv)
util/hbitmap.c +10 -5
@@ -193,7 +193,7 @@ void hbitmap_iter_init(HBitmapIter *hbi, const HBitmap *hb, uint64_t first) } } -int64_t hbitmap_next_zero (const HBitmap *hb,uint64_t start,uint64_t count) +int64_t hbitmap_next_zero (const HBitmap *hb,int64_t start,int64_t count) {size_t pos = (start >> hb->granularity) >>BITS_PER_LEVEL ;unsigned long *last_lev = hb->levels[HBITMAP_LEVELS -1 ];
@@ -202,6 +202,8 @@ int64_t hbitmap_next_zero(const HBitmap *hb, uint64_t start, uint64_t count)uint64_t end_bit, sz;int64_t res; +assert (start >=0 && count >=0 ); +if (start >= hb->orig_size || count ==0 ) {return -1 ; }
@@ -244,14 +246,15 @@ int64_t hbitmap_next_zero(const HBitmap *hb, uint64_t start, uint64_t count)return res; } -bool hbitmap_next_dirty_area (const HBitmap *hb,uint64_t *start, -uint64_t *count) +bool hbitmap_next_dirty_area (const HBitmap *hb,int64_t *start,int64_t *count) {HBitmapIter hbi;int64_t firt_dirty_off, area_end;uint32_t granularity =1UL << hb->granularity;uint64_t end; +assert (*start >=0 && *count >=0 ); +if (*start >= hb->orig_size || *count ==0 ) {return false; }
@@ -704,6 +707,7 @@ HBitmap *hbitmap_alloc(uint64_t size, int granularity) HBitmap *hb = g_new0(struct HBitmap, 1);unsigned i; +assert (size <=INT64_MAX ); hb->orig_size = size;assert (granularity >=0 && granularity <64 );
@@ -734,6 +738,7 @@ void hbitmap_truncate(HBitmap *hb, uint64_t size)uint64_t num_elements = size;uint64_t old; +assert (size <=INT64_MAX ); hb->orig_size = size;/* Size comes in as logical elements, adjust for granularity. */
@@ -791,8 +796,8 @@ bool hbitmap_can_merge(const HBitmap *a, const HBitmap *b)*/ static void hbitmap_sparse_merge (HBitmap *dst,const HBitmap *src) { -uint64_t offset =0 ; -uint64_t count = src->orig_size; +int64_t offset =0 ; +int64_t count = src->orig_size;while (hbitmap_next_dirty_area (src, &offset, &count)) {hbitmap_set (dst, offset, count);
Signature · 1 line
2.18.0
PATCH 2/4 block/dirty-bitmap: add _next_dirty API
In reply to Vladimir Sementsov-OgievskiyWe have bdrv_dirty_bitmap_next_zero, let's add corresponding bdrv_dirty_bitmap_next_dirty, which is more comfortable to use than bitmap iterators in some cases. It will be used in the following commit.
--- include/block/dirty-bitmap.h | 2 ++ include/qemu/hbitmap.h | 13 ++++++++ block/dirty-bitmap.c | 6 ++++ util/hbitmap.c | 61 +++++++++++++++++++----------------- 4 files changed, 54 insertions(+), 28 deletions(-)
include/block/dirty-bitmap.h +2 -0
@@ -108,6 +108,8 @@ bool bdrv_has_changed_persistent_bitmaps(BlockDriverState *bs);BdrvDirtyBitmap *bdrv_dirty_bitmap_next (BlockDriverState *bs,BdrvDirtyBitmap *bitmap);char *bdrv_dirty_bitmap_sha256 (const BdrvDirtyBitmap *bitmap,Error **errp); +int64_t bdrv_dirty_bitmap_next_dirty (BdrvDirtyBitmap *bitmap,int64_t offset, +int64_t bytes);int64_t bdrv_dirty_bitmap_next_zero (BdrvDirtyBitmap *bitmap,int64_t offset,int64_t bytes); bool bdrv_dirty_bitmap_next_dirty_area(BdrvDirtyBitmap *bitmap,
include/qemu/hbitmap.h +13 -0
@@ -299,6 +299,19 @@ void hbitmap_iter_init(HBitmapIter *hbi, const HBitmap *hb, uint64_t first);*/ unsigned long hbitmap_iter_skip_words (HBitmapIter *hbi); +/* +* hbitmap_next_dirty: +* +* Find next dirty bit within selected range. If not found, return -1. +* +* @hb: The HBitmap to operate on +* @start: The bit to start from. +* @count: Number of bits to proceed. If @start+@count > bitmap size, the whole +* bitmap is looked through. You can use UINT64_MAX as @count to search up to +* the bitmap end. +*/ +int64_t hbitmap_next_dirty (const HBitmap *hb,int64_t start,int64_t count); +/* hbitmap_next_zero: * * Find next not dirty bit within selected range. If not found, return -1.
block/dirty-bitmap.c +6 -0
@@ -802,6 +802,12 @@ char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap *bitmap, Error **errp)return hbitmap_sha256 (bitmap->bitmap, errp); } +int64_t bdrv_dirty_bitmap_next_dirty (BdrvDirtyBitmap *bitmap,int64_t offset, +int64_t bytes) +{ +return hbitmap_next_zero (bitmap->bitmap, offset, bytes); +} +int64_t bdrv_dirty_bitmap_next_zero (BdrvDirtyBitmap *bitmap,int64_t offset,int64_t bytes) {
util/hbitmap.c +33 -28
79 lines not on this page · raw message
Signature · 1 line
2.18.0
PATCH 3/4 block/backup: use bdrv_dirty_bitmap_next_dirty
In reply to Vladimir Sementsov-OgievskiyUse more effective search for next dirty byte. Trace point is dropped to not introduce additional variable and logic only for trace point.
--- block/backup.c | 7 +++---- block/trace-events | 1 - 2 files changed, 3 insertions(+), 5 deletions(-)
block/backup.c +3 -4
@@ -272,10 +272,9 @@ static int coroutine_fn backup_do_cow(BackupBlockJob *job,while (start < end) {int64_t dirty_end; -if (!bdrv_dirty_bitmap_get (job->copy_bitmap, start)) { -trace_backup_do_cow_skip (job, start); - start += job->cluster_size; -continue ;/* already copied */ + start =bdrv_dirty_bitmap_next_dirty (job->copy_bitmap, start, end); +if (start <0 ) { +break ; }if (job->initializing_bitmap) {
block/trace-events +0 -1
@@ -40,7 +40,6 @@ mirror_yield_in_flight(void *s, int64_t offset, int in_flight) "s %p offset %" P # backup.c backup_do_cow_enter(void *job, int64_t start, int64_t offset, uint64_t bytes) "job %p start %" PRId64 " offset %" PRId64 " bytes %" PRIu64 backup_do_cow_return(void *job, int64_t offset, uint64_t bytes, int ret) "job %p offset %" PRId64 " bytes %" PRIu64 " ret %d" -backup_do_cow_skip(void *job, int64_t start) "job %p start %"PRId64 backup_do_cow_skip_range(void *job, int64_t start, uint64_t bytes) "job %p start %"PRId64" bytes %"PRId64 backup_do_cow_process(void *job, int64_t start) "job %p start %"PRId64 backup_do_cow_read_fail(void *job, int64_t start, int ret) "job %p start %"PRId64" ret %d"
Signature · 1 line
2.18.0
PATCH 4/4 block/backup: fix and improve skipping unallocated in backup_do_cow
In reply to Vladimir Sementsov-Ogievskiy1. There is a bug: after detection an allocated area of skip_bytes length we ignore skip_bytes variable and my finish up by copying more than skip_bytes.
2. If request area is allocated we call block_status for each cluster on each loop iteration, even if after the first call we know that the whole request area is allocated.
Solve all of this by handling resetting all unallocated bytes from requested area before copying loop.
--- block/backup.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-)
block/backup.c +16 -10
@@ -257,7 +257,6 @@ static int coroutine_fn backup_do_cow(BackupBlockJob *job,int ret =0 ;int64_t start, end;/* bytes */ void *bounce_buffer =NULL ; -int64_t skip_bytes;qemu_co_rwlock_rdlock (&job->flush_rwlock);
@@ -269,6 +268,22 @@ static int coroutine_fn backup_do_cow(BackupBlockJob *job,wait_for_overlapping_requests (job, start, end);cow_request_begin (&cow_request, job, start, end); +if (job->initializing_bitmap) { +int64_t off = start; +int64_t count; + +while (off < end) { + off =bdrv_dirty_bitmap_next_dirty (job->copy_bitmap, + off, end - off); +if (off <0 ) { +break ; + } + +backup_bitmap_reset_unallocated (job, off, &count); + off += count; + } + } +while (start < end) {int64_t dirty_end;
@@ -277,15 +292,6 @@ static int coroutine_fn backup_do_cow(BackupBlockJob *job,break ; } -if (job->initializing_bitmap) { - ret =backup_bitmap_reset_unallocated (job, start, &skip_bytes); -if (ret ==0 ) { -trace_backup_do_cow_skip_range (job, start, skip_bytes); - start += skip_bytes; -continue ; - } - } - dirty_end =bdrv_dirty_bitmap_next_zero (job->copy_bitmap, start, (end - start));if (dirty_end <0 ) {
Signature · 1 line
2.18.0
14 авг. 2019 г. 17:43 пользователь Vladimir Sementsov-Ogievskiy <[email protected]> написал:
Hi all!
There is a bug in not yet merged patch
"block/backup: teach TOP to never copy unallocated regions"
in https://github.com/jnsnow/qemu bitmaps. 04 fixes it. So, I propose to put 01-03 somewhere before
"block/backup: teach TOP to never copy unallocated regions"
and squash 04 into "block/backup: teach TOP to never copy unallocated regions"
Hmm, don't bother with it. Simpler is fix the bug in your commit by just use skip_bytes variable when initializing dirty_end.
Based-on: https://github.com/jnsnow/qemu bitmaps
Vladimir Sementsov-Ogievskiy (4):
block/dirty-bitmap: switch _next_dirty_area and _next_zero to int64_t block/dirty-bitmap: add _next_dirty API
block/backup: use bdrv_dirty_bitmap_next_dirty block/backup: fix and improve skipping unallocated in backup_do_cow
include/block/dirty-bitmap.h | 8 ++-- include/qemu/hbitmap.h | 18 +++++++-- block/backup.c | 33 ++++++++++------- block/dirty-bitmap.c | 12 ++++-- block/mirror.c | 4 +- tests/test-hbitmap.c | 32 ++++++++-------- util/hbitmap.c | 72 ++++++++++++++++++++---------------- block/trace-events | 1 - 8 files changed, 107 insertions(+), 73 deletions(-)
--
2.18.0
Quoted 16 lines · Vladimir Sementsov-Ogievskiy
14 авг. 2019 г. 17:43 пользователь Vladimir Sementsov-Ogievskiy <[email protected]> написал:
Hi all!
There is a bug in not yet merged patch
"block/backup: teach TOP to never copy unallocated regions" in https://github.com/jnsnow/qemu bitmaps. 04 fixes it. So, I propose to put 01-03 somewhere before
"block/backup: teach TOP to never copy unallocated regions" and squash 04 into "block/backup: teach TOP to never copy unallocated regions"
Quoted 3 lines
Hmm, don't bother with it. Simpler is fix the bug in your commit by just use
skip_bytesvariable when initializingdirty_end.
OK, just use Max's fix instead of this entire 4 patch series?
--js
Quoted 19 lines · John Snow
Quoted 16 lines · Vladimir Sementsov-Ogievskiy
14 авг. 2019 г. 17:43 пользователь Vladimir Sementsov-Ogievskiy <[email protected]> написал:
Hi all!
There is a bug in not yet merged patch
"block/backup: teach TOP to never copy unallocated regions" in https://github.com/jnsnow/qemu bitmaps. 04 fixes it. So, I propose to put 01-03 somewhere before
"block/backup: teach TOP to never copy unallocated regions" and squash 04 into "block/backup: teach TOP to never copy unallocated regions"
Quoted 8 lines
Quoted 3 lines
Hmm, don't bother with it. Simpler is fix the bug in your commit by just use
skip_bytesvariable when initializingdirty_end.OK, just use Max's fix instead of this entire 4 patch series?
--js
Yes, I think it's OK
Signature · 2 lines
Best regards, Vladimir
Quoted 24 lines · Vladimir Sementsov-Ogievskiy
Quoted 23 lines · John Snow
Quoted 19 lines · Vladimir Sementsov-Ogievskiy
14 авг. 2019 г. 17:43 пользователь Vladimir Sementsov-Ogievskiy <[email protected]> написал:
Hi all!
There is a bug in not yet merged patch
"block/backup: teach TOP to never copy unallocated regions" in https://github.com/jnsnow/qemu bitmaps. 04 fixes it. So, I propose to put 01-03 somewhere before
"block/backup: teach TOP to never copy unallocated regions" and squash 04 into "block/backup: teach TOP to never copy unallocated regions"Hmm, don't bother with it. Simpler is fix the bug in your commit by just use
skip_bytesvariable when initializingdirty_end.
Quoted 7 lines
Quoted 4 lines
OK, just use Max's fix instead of this entire 4 patch series?
--js
Yes, I think it's OK
ACK, thank you!