On 22.12.19 12:36, Alberto Garcia wrote: > If an image has subclusters then there are more copy-on-write > scenarios that we need to consider. Let's say we have a write request > from the middle of subcluster #3 until the end of the cluster: > > - If the cluster is new, then subclusters #0 to #3 from the old > cluster must be copied into the new one. > > - If the cluster is new but the old cluster was unallocated, then > only subcluster #3 needs copy-on-write. #0 to #2 are marked as > unallocated in the bitmap of the new L2 entry. > > - If we are overwriting an old cluster and subcluster #3 is > unallocated or has the all-zeroes bit set then we need > copy-on-write on subcluster #3. > > - If we are overwriting an old cluster and subcluster #3 was > allocated then there is no need to copy-on-write. > > Signed-off-by: Alberto Garcia > --- > block/qcow2-cluster.c | 140 +++++++++++++++++++++++++++++++++--------- > 1 file changed, 110 insertions(+), 30 deletions(-) It’s all a bit tough to wrap my head around. One thing I got particularly hung up is how we ensure that for new clusters the head/tail subcluster bits that do not need COW are initialized to the correct value. Then I realized that we just have to keep them as they are (unallocated or zero, respectively), because this path is only for when we already have L2 entries, it’s just that they point to normal non-COPIED clusters. (So only the L2 offset entry has to be changed, not the bitmap. At least not for the subclusters that aren’t touched by the write.) Reviewed-by: Max Reitz