qcow2: Optimize the refcount-block overlap check
commit7061a078984ba7d08b8b80686ad98c5162e56fbd
authorAlberto Garcia <berto@igalia.com>
Wed, 1 Feb 2017 12:38:28 +0000 (1 14:38 +0200)
committerMax Reitz <mreitz@redhat.com>
Sat, 11 Feb 2017 23:47:43 +0000 (12 00:47 +0100)
tree3d4e6b6144af862c2b1ecbc115938340dc05372b
parentbf68bcb18e65e2fc6aaa55079740feee4c8b474c
qcow2: Optimize the refcount-block overlap check

The metadata overlap checks introduced in a40f1c2add help detect
corruption in the qcow2 image by verifying that data writes don't
overlap with existing metadata sections.

The 'refcount-block' check in particular iterates over the refcount
table in order to get the addresses of all refcount blocks and check
that none of them overlap with the region where we want to write.

The problem with the refcount table is that since it always occupies
complete clusters its size is usually very big. With the default
values of cluster_size=64KB and refcount_bits=16 this table holds 8192
entries, each one of them enough to map 2GB worth of host clusters.

So unless we're using images with several TB of allocated data this
table is going to be mostly empty, and iterating over it is a waste of
CPU. If the storage backend is fast enough this can have an effect on
I/O performance.

This patch keeps the index of the last used (i.e. non-zero) entry in
the refcount table and updates it every time the table changes. The
refcount-block overlap check then uses that index instead of reading
the whole table.

In my tests with a 4GB qcow2 file stored in RAM this doubles the
amount of write IOPS.

Signed-off-by: Alberto Garcia <berto@igalia.com>
Message-id: 20170201123828.4815-1-berto@igalia.com
Reviewed-by: Max Reitz <mreitz@redhat.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
block/qcow2-refcount.c
block/qcow2.c
block/qcow2.h