Alberto Garcia abf754fe40 qcow2: Reset free_cluster_index when allocating a new refcount block
When we try to allocate new clusters we first look for available ones
starting from s->free_cluster_index and once we find them we increase
their reference counts. Before we get to call update_refcount() to do
this last step s->free_cluster_index is already pointing to the next
cluster after the ones we are trying to allocate.

During update_refcount() it may happen however that we also need to
allocate a new refcount block in order to store the refcounts of these
new clusters (and to complicate things further that may also require
us to grow the refcount table). After all this we don't know if the
clusters that we originally tried to allocate are still available, so
we return -EAGAIN to ask the caller to restart the search for free
clusters.

This is what can happen in a common scenario:

  1) We want to allocate a new cluster and we see that cluster N is
     free.

  2) We try to increase N's refcount but all refcount blocks are full,
     so we allocate a new one at N+1 (where s->free_cluster_index was
     pointing at).

  3) Once we're done we return -EAGAIN to look again for a free
     cluster, but now s->free_cluster_index points at N+2, so that's
     the one we allocate. Cluster N remains unallocated and we have a
     hole in the qcow2 file.

This can be reproduced easily:

     qemu-img create -f qcow2 -o cluster_size=512 hd.qcow2 1M
     qemu-io -c 'write 0 124k' hd.qcow2

After this the image has 132608 bytes (256 clusters), and the refcount
block is full. If we write 512 more bytes it should allocate two new
clusters: the data cluster itself and a new refcount block.

     qemu-io -c 'write 124k 512' hd.qcow2

However the image has now three new clusters (259 in total), and the
first one of them is empty (and unallocated):

     dd if=hd.qcow2 bs=512c skip=256 count=1 | hexdump -C

If we write larger amounts of data in the last step instead of the 512
bytes used in this example we can create larger holes in the qcow2
file.

What this patch does is reset s->free_cluster_index to its previous
value when alloc_refcount_block() returns -EAGAIN. This way the caller
will try to allocate again the original clusters if they are still
free.

The output of iotest 026 also needs to be updated because now that
images have no holes some tests fail at a different point and the
number of leaked clusters is different.

Signed-off-by: Alberto Garcia <berto@igalia.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2018-03-26 12:16:00 +02:00
..
020
2018-01-23 12:34:43 +01:00
2018-01-23 12:34:43 +01:00
026
2017-04-27 15:46:16 +02:00
2017-04-27 15:46:16 +02:00
030
2018-03-19 12:01:24 +01:00
033
2018-03-13 15:38:55 -05:00
040
2017-11-14 18:06:25 +01:00
2017-07-11 17:45:02 +02:00
046
2017-05-11 11:08:40 +02:00
2018-01-23 12:34:43 +01:00
2018-01-23 12:34:43 +01:00
055
2018-03-19 12:01:24 +01:00
056
2018-03-19 12:01:24 +01:00
2018-03-19 12:01:24 +01:00
059
2018-03-09 15:40:07 +01:00
2018-02-13 12:27:17 +01:00
063
2017-08-01 18:09:33 +02:00
2015-01-23 12:41:32 -05:00
066
2017-05-11 12:08:24 +02:00
2017-05-11 12:08:24 +02:00
067
2018-01-23 12:34:43 +01:00
2018-01-23 12:34:43 +01:00
068
2017-09-06 15:19:01 +01:00
2017-09-06 15:19:01 +01:00
075
2017-11-27 11:25:41 +01:00
077
2017-11-14 18:06:25 +01:00
078
2017-11-27 11:25:41 +01:00
083
2017-11-14 18:06:25 +01:00
087
2017-11-17 18:21:31 +01:00
089
2018-03-19 14:58:36 -05:00
2018-03-19 14:58:36 -05:00
2016-01-07 21:30:17 +01:00
096
2018-03-09 15:40:07 +01:00
102
2018-01-23 12:34:42 +01:00
2018-01-23 12:34:42 +01:00
106
2017-09-26 15:00:32 +02:00
109
2017-08-08 14:36:59 +02:00
2018-03-19 12:01:24 +01:00
2015-12-18 14:36:17 +01:00
2017-07-10 13:18:05 +02:00
2017-05-11 14:28:07 +02:00
124
2018-03-09 15:40:07 +01:00
125
2017-10-26 15:01:14 +02:00
2017-10-26 15:01:14 +02:00
126
2017-07-11 17:45:00 +02:00
2017-07-11 17:45:00 +02:00
127
2017-10-26 15:01:13 +02:00
129
2018-03-09 15:40:07 +01:00
132
2018-03-09 15:40:07 +01:00
136
2018-03-09 15:40:07 +01:00
2016-05-19 16:45:31 +02:00
139
2018-03-09 15:40:07 +01:00
141
2017-08-01 18:09:33 +02:00
142
2017-07-11 17:45:00 +02:00
148
2018-03-09 15:40:07 +01:00
152
2018-03-09 15:40:07 +01:00
155
2018-02-13 12:27:17 +01:00
156
2017-08-01 18:09:33 +02:00
159
2017-07-11 17:44:59 +02:00
2016-09-20 22:10:57 +02:00
160
2016-09-20 22:10:57 +02:00
2016-09-20 22:10:57 +02:00
163
2018-03-09 15:40:07 +01:00
2017-09-26 15:00:32 +02:00
169
2018-03-13 17:06:26 -04:00
170
2017-07-11 17:44:59 +02:00
2016-09-20 22:10:57 +02:00
171
2017-07-11 17:45:00 +02:00
2017-09-18 19:43:38 -04:00
2017-02-12 00:47:42 +01:00
2017-11-21 14:54:02 +01:00
178
2017-07-11 17:45:01 +02:00
182
2017-11-17 13:35:59 +01:00
183
2017-06-09 11:45:03 +02:00
2017-06-09 11:45:03 +02:00
184
2018-01-23 12:34:43 +01:00
2018-01-23 12:34:43 +01:00
185
2017-08-11 14:44:39 +02:00
191
2018-01-23 12:34:43 +01:00
197
2017-12-22 15:03:41 +01:00
199
2018-03-13 17:06:32 -04:00
200
2018-01-31 22:37:00 -05:00
205
2018-03-09 15:40:07 +01:00
209
2018-03-13 15:44:09 -05:00
210
2018-03-19 12:01:39 +01:00
2018-03-19 12:01:39 +01:00
2018-03-26 12:16:00 +02:00
2018-03-19 12:01:24 +01:00

=== This is the QEMU I/O test suite ===

* Intro

This package contains a simple test suite for the I/O layer of qemu.
It does not require a guest, but only the qemu, qemu-img and qemu-io
binaries.  This does limit it to exercise the low-level I/O path only
but no actual block drivers like ide, scsi or virtio.

* Usage

Just run ./check to run all tests for the raw image format, or ./check
-qcow2 to test the qcow2 image format.  The output of ./check -h explains
additional options to test further image formats or I/O methods.

* Feedback and patches

Please send improvements to the test suite, general feedback or just
reports of failing tests cases to qemu-devel@nongnu.org with a CC:
to qemu-block@nongnu.org.