The new testcase did not find Kevin's bug (which is actually not there), but I still did the change he asked to the logic; it is clearer that way.
And the testcase *did* find a related problem. Here is the relevant diff from v2: if (backing_filename[0] && !s->target->backing_hd) { bdrv_get_info(s->target, &bdi); - if (s->buf_size < bdi.cluster_size) { - s->buf_size = bdi.cluster_size; + if (s->granularity < bdi.cluster_size) { + s->buf_size = MAX(s->buf_size, bdi.cluster_size); s->cow_bitmap = bitmap_new(length); } } In deciding whether to do COW, the cluster size was tested against the wrong value. Following Kevin's suggestion, I also renamed all variables dealing with dirty-bitmap-granularity-sized blocks to call them "chunks", not "clusters". This makes the interdiff quite large, but there are no semantic changes other than the movement of bitmap_set(s->cow_bitmap) and the bugfix. Patches 1-4 are identical. For the others, see the individual patches. Paolo Paolo Bonzini (12): host-utils: add ffsl add hierarchical bitmap data type and test cases block: implement dirty bitmap using HBitmap block: make round_to_clusters public mirror: perform COW if the cluster size is bigger than the granularity block: return count of dirty sectors, not chunks block: allow customizing the granularity of the dirty bitmap mirror: allow customizing the granularity mirror: switch mirror_iteration to AIO mirror: add buf-size argument to drive-mirror mirror: support more than one in-flight AIO operation mirror: support arbitrarily-sized iterations block-migration.c | 7 +- block.c | 124 ++++---------- block/mirror.c | 379 ++++++++++++++++++++++++++++++++++------- blockdev.c | 37 ++-- hmp.c | 2 +- include/block/block.h | 11 +- include/block/block_int.h | 10 +- include/qemu/hbitmap.h | 207 +++++++++++++++++++++++ include/qemu/host-utils.h | 26 +++ qapi-schema.json | 15 +- qmp-commands.hx | 10 +- roms/seabios | 2 +- tests/Makefile | 3 + tests/qemu-iotests/041 | 81 +++++++++ tests/qemu-iotests/041.out | 4 +- tests/test-hbitmap.c | 408 +++++++++++++++++++++++++++++++++++++++++++++ trace-events | 12 ++ util/Makefile.objs | 2 +- util/hbitmap.c | 400 ++++++++++++++++++++++++++++++++++++++++++++ 19 files changed, 1556 insertions(+), 184 deletions(-) create mode 100644 include/qemu/hbitmap.h create mode 100644 tests/test-hbitmap.c create mode 100644 util/hbitmap.c -- 1.8.1