]> git.proxmox.com Git - mirror_qemu.git/blame - block/qcow2-refcount.c
block: add missing coroutine_fn annotations
[mirror_qemu.git] / block / qcow2-refcount.c
CommitLineData
f7d0fe02
KW
1/*
2 * Block driver for the QCOW version 2 format
3 *
4 * Copyright (c) 2004-2006 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
80c71a24 25#include "qemu/osdep.h"
da34e65c 26#include "qapi/error.h"
0d8c41da 27#include "qcow2.h"
a40f1c2a 28#include "qemu/range.h"
58369e22 29#include "qemu/bswap.h"
46b732cd 30#include "qemu/cutils.h"
5df022cf 31#include "qemu/memalign.h"
1477b6c8 32#include "trace.h"
f7d0fe02 33
77d6a215
EB
34static int64_t alloc_clusters_noref(BlockDriverState *bs, uint64_t size,
35 uint64_t max);
c0840179
MAL
36
37G_GNUC_WARN_UNUSED_RESULT
38static int update_refcount(BlockDriverState *bs,
39 int64_t offset, int64_t length, uint64_t addend,
40 bool decrease, enum qcow2_discard_type type);
f7d0fe02 41
59c0cb78
HR
42static uint64_t get_refcount_ro0(const void *refcount_array, uint64_t index);
43static uint64_t get_refcount_ro1(const void *refcount_array, uint64_t index);
44static uint64_t get_refcount_ro2(const void *refcount_array, uint64_t index);
45static uint64_t get_refcount_ro3(const void *refcount_array, uint64_t index);
7453c96b 46static uint64_t get_refcount_ro4(const void *refcount_array, uint64_t index);
59c0cb78
HR
47static uint64_t get_refcount_ro5(const void *refcount_array, uint64_t index);
48static uint64_t get_refcount_ro6(const void *refcount_array, uint64_t index);
7453c96b 49
59c0cb78
HR
50static void set_refcount_ro0(void *refcount_array, uint64_t index,
51 uint64_t value);
52static void set_refcount_ro1(void *refcount_array, uint64_t index,
53 uint64_t value);
54static void set_refcount_ro2(void *refcount_array, uint64_t index,
55 uint64_t value);
56static void set_refcount_ro3(void *refcount_array, uint64_t index,
57 uint64_t value);
7453c96b
HR
58static void set_refcount_ro4(void *refcount_array, uint64_t index,
59 uint64_t value);
59c0cb78
HR
60static void set_refcount_ro5(void *refcount_array, uint64_t index,
61 uint64_t value);
62static void set_refcount_ro6(void *refcount_array, uint64_t index,
63 uint64_t value);
64
65
66static Qcow2GetRefcountFunc *const get_refcount_funcs[] = {
67 &get_refcount_ro0,
68 &get_refcount_ro1,
69 &get_refcount_ro2,
70 &get_refcount_ro3,
71 &get_refcount_ro4,
72 &get_refcount_ro5,
73 &get_refcount_ro6
74};
75
76static Qcow2SetRefcountFunc *const set_refcount_funcs[] = {
77 &set_refcount_ro0,
78 &set_refcount_ro1,
79 &set_refcount_ro2,
80 &set_refcount_ro3,
81 &set_refcount_ro4,
82 &set_refcount_ro5,
83 &set_refcount_ro6
84};
7453c96b 85
3b88e52b 86
f7d0fe02
KW
87/*********************************************************/
88/* refcount handling */
89
7061a078
AG
90static void update_max_refcount_table_index(BDRVQcow2State *s)
91{
92 unsigned i = s->refcount_table_size - 1;
93 while (i > 0 && (s->refcount_table[i] & REFT_OFFSET_MASK) == 0) {
94 i--;
95 }
96 /* Set s->max_refcount_table_index to the index of the last used entry */
97 s->max_refcount_table_index = i;
98}
99
ed6ccf0f 100int qcow2_refcount_init(BlockDriverState *bs)
f7d0fe02 101{
ff99129a 102 BDRVQcow2State *s = bs->opaque;
5dab2fad
KW
103 unsigned int refcount_table_size2, i;
104 int ret;
f7d0fe02 105
59c0cb78
HR
106 assert(s->refcount_order >= 0 && s->refcount_order <= 6);
107
108 s->get_refcount = get_refcount_funcs[s->refcount_order];
109 s->set_refcount = set_refcount_funcs[s->refcount_order];
7453c96b 110
02b1ecfa
AG
111 assert(s->refcount_table_size <= INT_MAX / REFTABLE_ENTRY_SIZE);
112 refcount_table_size2 = s->refcount_table_size * REFTABLE_ENTRY_SIZE;
de82815d
KW
113 s->refcount_table = g_try_malloc(refcount_table_size2);
114
f7d0fe02 115 if (s->refcount_table_size > 0) {
de82815d 116 if (s->refcount_table == NULL) {
8fcffa98 117 ret = -ENOMEM;
de82815d
KW
118 goto fail;
119 }
66f82cee 120 BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_LOAD);
cf2ab8fc 121 ret = bdrv_pread(bs->file, s->refcount_table_offset,
32cc71de 122 refcount_table_size2, s->refcount_table, 0);
8fcffa98 123 if (ret < 0) {
f7d0fe02 124 goto fail;
8fcffa98 125 }
f7d0fe02
KW
126 for(i = 0; i < s->refcount_table_size; i++)
127 be64_to_cpus(&s->refcount_table[i]);
7061a078 128 update_max_refcount_table_index(s);
f7d0fe02
KW
129 }
130 return 0;
131 fail:
8fcffa98 132 return ret;
f7d0fe02
KW
133}
134
ed6ccf0f 135void qcow2_refcount_close(BlockDriverState *bs)
f7d0fe02 136{
ff99129a 137 BDRVQcow2State *s = bs->opaque;
7267c094 138 g_free(s->refcount_table);
f7d0fe02
KW
139}
140
141
59c0cb78
HR
142static uint64_t get_refcount_ro0(const void *refcount_array, uint64_t index)
143{
144 return (((const uint8_t *)refcount_array)[index / 8] >> (index % 8)) & 0x1;
145}
146
147static void set_refcount_ro0(void *refcount_array, uint64_t index,
148 uint64_t value)
149{
150 assert(!(value >> 1));
151 ((uint8_t *)refcount_array)[index / 8] &= ~(0x1 << (index % 8));
152 ((uint8_t *)refcount_array)[index / 8] |= value << (index % 8);
153}
154
155static uint64_t get_refcount_ro1(const void *refcount_array, uint64_t index)
156{
157 return (((const uint8_t *)refcount_array)[index / 4] >> (2 * (index % 4)))
158 & 0x3;
159}
160
161static void set_refcount_ro1(void *refcount_array, uint64_t index,
162 uint64_t value)
163{
164 assert(!(value >> 2));
165 ((uint8_t *)refcount_array)[index / 4] &= ~(0x3 << (2 * (index % 4)));
166 ((uint8_t *)refcount_array)[index / 4] |= value << (2 * (index % 4));
167}
168
169static uint64_t get_refcount_ro2(const void *refcount_array, uint64_t index)
170{
171 return (((const uint8_t *)refcount_array)[index / 2] >> (4 * (index % 2)))
172 & 0xf;
173}
174
175static void set_refcount_ro2(void *refcount_array, uint64_t index,
176 uint64_t value)
177{
178 assert(!(value >> 4));
179 ((uint8_t *)refcount_array)[index / 2] &= ~(0xf << (4 * (index % 2)));
180 ((uint8_t *)refcount_array)[index / 2] |= value << (4 * (index % 2));
181}
182
183static uint64_t get_refcount_ro3(const void *refcount_array, uint64_t index)
184{
185 return ((const uint8_t *)refcount_array)[index];
186}
187
188static void set_refcount_ro3(void *refcount_array, uint64_t index,
189 uint64_t value)
190{
191 assert(!(value >> 8));
192 ((uint8_t *)refcount_array)[index] = value;
193}
194
7453c96b
HR
195static uint64_t get_refcount_ro4(const void *refcount_array, uint64_t index)
196{
197 return be16_to_cpu(((const uint16_t *)refcount_array)[index]);
198}
199
200static void set_refcount_ro4(void *refcount_array, uint64_t index,
201 uint64_t value)
202{
203 assert(!(value >> 16));
204 ((uint16_t *)refcount_array)[index] = cpu_to_be16(value);
205}
206
59c0cb78
HR
207static uint64_t get_refcount_ro5(const void *refcount_array, uint64_t index)
208{
209 return be32_to_cpu(((const uint32_t *)refcount_array)[index]);
210}
211
212static void set_refcount_ro5(void *refcount_array, uint64_t index,
213 uint64_t value)
214{
215 assert(!(value >> 32));
216 ((uint32_t *)refcount_array)[index] = cpu_to_be32(value);
217}
218
219static uint64_t get_refcount_ro6(const void *refcount_array, uint64_t index)
220{
221 return be64_to_cpu(((const uint64_t *)refcount_array)[index]);
222}
223
224static void set_refcount_ro6(void *refcount_array, uint64_t index,
225 uint64_t value)
226{
227 ((uint64_t *)refcount_array)[index] = cpu_to_be64(value);
228}
229
7453c96b 230
f7d0fe02 231static int load_refcount_block(BlockDriverState *bs,
29c1a730
KW
232 int64_t refcount_block_offset,
233 void **refcount_block)
f7d0fe02 234{
ff99129a 235 BDRVQcow2State *s = bs->opaque;
3b88e52b 236
66f82cee 237 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_LOAD);
9be38598
EH
238 return qcow2_cache_get(bs, s->refcount_block_cache, refcount_block_offset,
239 refcount_block);
f7d0fe02
KW
240}
241
018faafd 242/*
7324c10f
HR
243 * Retrieves the refcount of the cluster given by its index and stores it in
244 * *refcount. Returns 0 on success and -errno on failure.
018faafd 245 */
7324c10f 246int qcow2_get_refcount(BlockDriverState *bs, int64_t cluster_index,
0e06528e 247 uint64_t *refcount)
f7d0fe02 248{
ff99129a 249 BDRVQcow2State *s = bs->opaque;
db8a31d1 250 uint64_t refcount_table_index, block_index;
f7d0fe02 251 int64_t refcount_block_offset;
018faafd 252 int ret;
7453c96b 253 void *refcount_block;
f7d0fe02 254
17bd5f47 255 refcount_table_index = cluster_index >> s->refcount_block_bits;
7324c10f
HR
256 if (refcount_table_index >= s->refcount_table_size) {
257 *refcount = 0;
f7d0fe02 258 return 0;
7324c10f 259 }
26d49c46
HR
260 refcount_block_offset =
261 s->refcount_table[refcount_table_index] & REFT_OFFSET_MASK;
7324c10f
HR
262 if (!refcount_block_offset) {
263 *refcount = 0;
f7d0fe02 264 return 0;
7324c10f 265 }
29c1a730 266
a97c67ee
HR
267 if (offset_into_cluster(s, refcount_block_offset)) {
268 qcow2_signal_corruption(bs, true, -1, -1, "Refblock offset %#" PRIx64
269 " unaligned (reftable index: %#" PRIx64 ")",
270 refcount_block_offset, refcount_table_index);
271 return -EIO;
272 }
273
29c1a730 274 ret = qcow2_cache_get(bs, s->refcount_block_cache, refcount_block_offset,
7453c96b 275 &refcount_block);
29c1a730
KW
276 if (ret < 0) {
277 return ret;
f7d0fe02 278 }
29c1a730 279
17bd5f47 280 block_index = cluster_index & (s->refcount_block_size - 1);
7453c96b 281 *refcount = s->get_refcount(refcount_block, block_index);
29c1a730 282
2013c3d4 283 qcow2_cache_put(s->refcount_block_cache, &refcount_block);
29c1a730 284
7324c10f 285 return 0;
f7d0fe02
KW
286}
287
92dcb59f 288/* Checks if two offsets are described by the same refcount block */
ff99129a 289static int in_same_refcount_block(BDRVQcow2State *s, uint64_t offset_a,
92dcb59f
KW
290 uint64_t offset_b)
291{
17bd5f47
HR
292 uint64_t block_a = offset_a >> (s->cluster_bits + s->refcount_block_bits);
293 uint64_t block_b = offset_b >> (s->cluster_bits + s->refcount_block_bits);
92dcb59f
KW
294
295 return (block_a == block_b);
296}
297
298/*
299 * Loads a refcount block. If it doesn't exist yet, it is allocated first
300 * (including growing the refcount table if needed).
301 *
29c1a730 302 * Returns 0 on success or -errno in error case
92dcb59f 303 */
29c1a730 304static int alloc_refcount_block(BlockDriverState *bs,
7453c96b 305 int64_t cluster_index, void **refcount_block)
f7d0fe02 306{
ff99129a 307 BDRVQcow2State *s = bs->opaque;
92dcb59f 308 unsigned int refcount_table_index;
12cc30a8 309 int64_t ret;
92dcb59f 310
66f82cee 311 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC);
8252278a 312
92dcb59f 313 /* Find the refcount block for the given cluster */
17bd5f47 314 refcount_table_index = cluster_index >> s->refcount_block_bits;
92dcb59f
KW
315
316 if (refcount_table_index < s->refcount_table_size) {
317
318 uint64_t refcount_block_offset =
76dc9e0c 319 s->refcount_table[refcount_table_index] & REFT_OFFSET_MASK;
92dcb59f
KW
320
321 /* If it's already there, we're done */
322 if (refcount_block_offset) {
a97c67ee
HR
323 if (offset_into_cluster(s, refcount_block_offset)) {
324 qcow2_signal_corruption(bs, true, -1, -1, "Refblock offset %#"
325 PRIx64 " unaligned (reftable index: "
326 "%#x)", refcount_block_offset,
327 refcount_table_index);
328 return -EIO;
329 }
330
29c1a730 331 return load_refcount_block(bs, refcount_block_offset,
7453c96b 332 refcount_block);
92dcb59f
KW
333 }
334 }
335
336 /*
337 * If we came here, we need to allocate something. Something is at least
338 * a cluster for the new refcount block. It may also include a new refcount
339 * table if the old refcount table is too small.
340 *
341 * Note that allocating clusters here needs some special care:
342 *
343 * - We can't use the normal qcow2_alloc_clusters(), it would try to
344 * increase the refcount and very likely we would end up with an endless
345 * recursion. Instead we must place the refcount blocks in a way that
346 * they can describe them themselves.
347 *
348 * - We need to consider that at this point we are inside update_refcounts
b106ad91
KW
349 * and potentially doing an initial refcount increase. This means that
350 * some clusters have already been allocated by the caller, but their
351 * refcount isn't accurate yet. If we allocate clusters for metadata, we
352 * need to return -EAGAIN to signal the caller that it needs to restart
353 * the search for free clusters.
92dcb59f
KW
354 *
355 * - alloc_clusters_noref and qcow2_free_clusters may load a different
356 * refcount block into the cache
357 */
358
29c1a730
KW
359 *refcount_block = NULL;
360
361 /* We write to the refcount table, so we might depend on L2 tables */
9991923b
SH
362 ret = qcow2_cache_flush(bs, s->l2_table_cache);
363 if (ret < 0) {
364 return ret;
365 }
92dcb59f
KW
366
367 /* Allocate the refcount block itself and mark it as used */
77d6a215 368 int64_t new_block = alloc_clusters_noref(bs, s->cluster_size, INT64_MAX);
2eaa8f63
KW
369 if (new_block < 0) {
370 return new_block;
371 }
f7d0fe02 372
cdc674c7
AG
373 /* The offset must fit in the offset field of the refcount table entry */
374 assert((new_block & REFT_OFFSET_MASK) == new_block);
375
6bf45d59
AG
376 /* If we're allocating the block at offset 0 then something is wrong */
377 if (new_block == 0) {
378 qcow2_signal_corruption(bs, true, -1, -1, "Preventing invalid "
379 "allocation of refcount block at offset 0");
380 return -EIO;
381 }
382
f7d0fe02 383#ifdef DEBUG_ALLOC2
92dcb59f
KW
384 fprintf(stderr, "qcow2: Allocate refcount block %d for %" PRIx64
385 " at %" PRIx64 "\n",
386 refcount_table_index, cluster_index << s->cluster_bits, new_block);
f7d0fe02 387#endif
92dcb59f
KW
388
389 if (in_same_refcount_block(s, new_block, cluster_index << s->cluster_bits)) {
25408c09 390 /* Zero the new refcount block before updating it */
29c1a730 391 ret = qcow2_cache_get_empty(bs, s->refcount_block_cache, new_block,
7453c96b 392 refcount_block);
29c1a730 393 if (ret < 0) {
60c48a29 394 goto fail;
29c1a730
KW
395 }
396
397 memset(*refcount_block, 0, s->cluster_size);
25408c09 398
92dcb59f
KW
399 /* The block describes itself, need to update the cache */
400 int block_index = (new_block >> s->cluster_bits) &
17bd5f47 401 (s->refcount_block_size - 1);
7453c96b 402 s->set_refcount(*refcount_block, block_index, 1);
92dcb59f
KW
403 } else {
404 /* Described somewhere else. This can recurse at most twice before we
405 * arrive at a block that describes itself. */
2aabe7c7 406 ret = update_refcount(bs, new_block, s->cluster_size, 1, false,
6cfcb9b8 407 QCOW2_DISCARD_NEVER);
92dcb59f 408 if (ret < 0) {
60c48a29 409 goto fail;
92dcb59f 410 }
25408c09 411
9991923b
SH
412 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
413 if (ret < 0) {
60c48a29 414 goto fail;
9991923b 415 }
1c4c2814 416
25408c09
KW
417 /* Initialize the new refcount block only after updating its refcount,
418 * update_refcount uses the refcount cache itself */
29c1a730 419 ret = qcow2_cache_get_empty(bs, s->refcount_block_cache, new_block,
7453c96b 420 refcount_block);
29c1a730 421 if (ret < 0) {
60c48a29 422 goto fail;
29c1a730
KW
423 }
424
425 memset(*refcount_block, 0, s->cluster_size);
92dcb59f
KW
426 }
427
428 /* Now the new refcount block needs to be written to disk */
66f82cee 429 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE);
2d135ee9 430 qcow2_cache_entry_mark_dirty(s->refcount_block_cache, *refcount_block);
29c1a730 431 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
92dcb59f 432 if (ret < 0) {
60c48a29 433 goto fail;
92dcb59f
KW
434 }
435
436 /* If the refcount table is big enough, just hook the block up there */
437 if (refcount_table_index < s->refcount_table_size) {
438 uint64_t data64 = cpu_to_be64(new_block);
66f82cee 439 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_HOOKUP);
02b1ecfa
AG
440 ret = bdrv_pwrite_sync(bs->file, s->refcount_table_offset +
441 refcount_table_index * REFTABLE_ENTRY_SIZE,
32cc71de 442 sizeof(data64), &data64, 0);
92dcb59f 443 if (ret < 0) {
60c48a29 444 goto fail;
92dcb59f
KW
445 }
446
447 s->refcount_table[refcount_table_index] = new_block;
7061a078
AG
448 /* If there's a hole in s->refcount_table then it can happen
449 * that refcount_table_index < s->max_refcount_table_index */
450 s->max_refcount_table_index =
451 MAX(s->max_refcount_table_index, refcount_table_index);
b106ad91
KW
452
453 /* The new refcount block may be where the caller intended to put its
454 * data, so let it restart the search. */
455 return -EAGAIN;
29c1a730
KW
456 }
457
2013c3d4 458 qcow2_cache_put(s->refcount_block_cache, refcount_block);
92dcb59f
KW
459
460 /*
461 * If we come here, we need to grow the refcount table. Again, a new
462 * refcount table needs some space and we can't simply allocate to avoid
463 * endless recursion.
464 *
465 * Therefore let's grab new refcount blocks at the end of the image, which
466 * will describe themselves and the new refcount table. This way we can
467 * reference them only in the new table and do the switch to the new
468 * refcount table at once without producing an inconsistent state in
469 * between.
470 */
66f82cee 471 BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_GROW);
8252278a 472
14a58a4e
HR
473 /* Calculate the number of refcount blocks needed so far; this will be the
474 * basis for calculating the index of the first cluster used for the
475 * self-describing refcount structures which we are about to create.
476 *
477 * Because we reached this point, there cannot be any refcount entries for
478 * cluster_index or higher indices yet. However, because new_block has been
479 * allocated to describe that cluster (and it will assume this role later
480 * on), we cannot use that index; also, new_block may actually have a higher
481 * cluster index than cluster_index, so it needs to be taken into account
482 * here (and 1 needs to be added to its value because that cluster is used).
483 */
484 uint64_t blocks_used = DIV_ROUND_UP(MAX(cluster_index + 1,
485 (new_block >> s->cluster_bits) + 1),
486 s->refcount_block_size);
92dcb59f 487
12cc30a8
HR
488 /* Create the new refcount table and blocks */
489 uint64_t meta_offset = (blocks_used * s->refcount_block_size) *
490 s->cluster_size;
491
492 ret = qcow2_refcount_area(bs, meta_offset, 0, false,
493 refcount_table_index, new_block);
494 if (ret < 0) {
495 return ret;
2b5d5953
KW
496 }
497
12cc30a8
HR
498 ret = load_refcount_block(bs, new_block, refcount_block);
499 if (ret < 0) {
500 return ret;
501 }
92dcb59f 502
12cc30a8
HR
503 /* If we were trying to do the initial refcount update for some cluster
504 * allocation, we might have used the same clusters to store newly
505 * allocated metadata. Make the caller search some new space. */
506 return -EAGAIN;
92dcb59f 507
60c48a29 508fail:
12cc30a8 509 if (*refcount_block != NULL) {
2013c3d4 510 qcow2_cache_put(s->refcount_block_cache, refcount_block);
12cc30a8
HR
511 }
512 return ret;
513}
92dcb59f 514
12cc30a8
HR
515/*
516 * Starting at @start_offset, this function creates new self-covering refcount
517 * structures: A new refcount table and refcount blocks which cover all of
518 * themselves, and a number of @additional_clusters beyond their end.
519 * @start_offset must be at the end of the image file, that is, there must be
520 * only empty space beyond it.
521 * If @exact_size is false, the refcount table will have 50 % more entries than
522 * necessary so it will not need to grow again soon.
523 * If @new_refblock_offset is not zero, it contains the offset of a refcount
524 * block that should be entered into the new refcount table at index
525 * @new_refblock_index.
526 *
527 * Returns: The offset after the new refcount structures (i.e. where the
528 * @additional_clusters may be placed) on success, -errno on error.
529 */
772d1f97
HR
530int64_t qcow2_refcount_area(BlockDriverState *bs, uint64_t start_offset,
531 uint64_t additional_clusters, bool exact_size,
532 int new_refblock_index,
533 uint64_t new_refblock_offset)
12cc30a8
HR
534{
535 BDRVQcow2State *s = bs->opaque;
536 uint64_t total_refblock_count_u64, additional_refblock_count;
537 int total_refblock_count, table_size, area_reftable_index, table_clusters;
538 int i;
539 uint64_t table_offset, block_offset, end_offset;
540 int ret;
541 uint64_t *new_table;
92dcb59f 542
12cc30a8 543 assert(!(start_offset % s->cluster_size));
de82815d 544
12cc30a8
HR
545 qcow2_refcount_metadata_size(start_offset / s->cluster_size +
546 additional_clusters,
547 s->cluster_size, s->refcount_order,
548 !exact_size, &total_refblock_count_u64);
549 if (total_refblock_count_u64 > QCOW_MAX_REFTABLE_SIZE) {
550 return -EFBIG;
551 }
552 total_refblock_count = total_refblock_count_u64;
553
554 /* Index in the refcount table of the first refcount block to cover the area
555 * of refcount structures we are about to create; we know that
556 * @total_refblock_count can cover @start_offset, so this will definitely
557 * fit into an int. */
558 area_reftable_index = (start_offset / s->cluster_size) /
559 s->refcount_block_size;
560
561 if (exact_size) {
562 table_size = total_refblock_count;
563 } else {
564 table_size = total_refblock_count +
565 DIV_ROUND_UP(total_refblock_count, 2);
566 }
567 /* The qcow2 file can only store the reftable size in number of clusters */
02b1ecfa
AG
568 table_size = ROUND_UP(table_size, s->cluster_size / REFTABLE_ENTRY_SIZE);
569 table_clusters = (table_size * REFTABLE_ENTRY_SIZE) / s->cluster_size;
12cc30a8
HR
570
571 if (table_size > QCOW_MAX_REFTABLE_SIZE) {
572 return -EFBIG;
573 }
574
575 new_table = g_try_new0(uint64_t, table_size);
576
577 assert(table_size > 0);
578 if (new_table == NULL) {
de82815d 579 ret = -ENOMEM;
12cc30a8 580 goto fail;
de82815d 581 }
92dcb59f 582
92dcb59f 583 /* Fill the new refcount table */
12cc30a8
HR
584 if (table_size > s->max_refcount_table_index) {
585 /* We're actually growing the reftable */
586 memcpy(new_table, s->refcount_table,
02b1ecfa 587 (s->max_refcount_table_index + 1) * REFTABLE_ENTRY_SIZE);
12cc30a8
HR
588 } else {
589 /* Improbable case: We're shrinking the reftable. However, the caller
590 * has assured us that there is only empty space beyond @start_offset,
591 * so we can simply drop all of the refblocks that won't fit into the
592 * new reftable. */
02b1ecfa 593 memcpy(new_table, s->refcount_table, table_size * REFTABLE_ENTRY_SIZE);
12cc30a8 594 }
92dcb59f 595
12cc30a8
HR
596 if (new_refblock_offset) {
597 assert(new_refblock_index < total_refblock_count);
598 new_table[new_refblock_index] = new_refblock_offset;
599 }
600
601 /* Count how many new refblocks we have to create */
602 additional_refblock_count = 0;
603 for (i = area_reftable_index; i < total_refblock_count; i++) {
604 if (!new_table[i]) {
605 additional_refblock_count++;
606 }
92dcb59f
KW
607 }
608
12cc30a8
HR
609 table_offset = start_offset + additional_refblock_count * s->cluster_size;
610 end_offset = table_offset + table_clusters * s->cluster_size;
611
612 /* Fill the refcount blocks, and create new ones, if necessary */
613 block_offset = start_offset;
614 for (i = area_reftable_index; i < total_refblock_count; i++) {
615 void *refblock_data;
616 uint64_t first_offset_covered;
617
618 /* Reuse an existing refblock if possible, create a new one otherwise */
619 if (new_table[i]) {
620 ret = qcow2_cache_get(bs, s->refcount_block_cache, new_table[i],
621 &refblock_data);
622 if (ret < 0) {
623 goto fail;
624 }
625 } else {
626 ret = qcow2_cache_get_empty(bs, s->refcount_block_cache,
627 block_offset, &refblock_data);
628 if (ret < 0) {
629 goto fail;
630 }
631 memset(refblock_data, 0, s->cluster_size);
2d135ee9 632 qcow2_cache_entry_mark_dirty(s->refcount_block_cache,
12cc30a8
HR
633 refblock_data);
634
635 new_table[i] = block_offset;
636 block_offset += s->cluster_size;
637 }
638
639 /* First host offset covered by this refblock */
640 first_offset_covered = (uint64_t)i * s->refcount_block_size *
641 s->cluster_size;
642 if (first_offset_covered < end_offset) {
643 int j, end_index;
644
645 /* Set the refcount of all of the new refcount structures to 1 */
646
647 if (first_offset_covered < start_offset) {
648 assert(i == area_reftable_index);
649 j = (start_offset - first_offset_covered) / s->cluster_size;
650 assert(j < s->refcount_block_size);
651 } else {
652 j = 0;
653 }
654
655 end_index = MIN((end_offset - first_offset_covered) /
656 s->cluster_size,
657 s->refcount_block_size);
658
659 for (; j < end_index; j++) {
660 /* The caller guaranteed us this space would be empty */
661 assert(s->get_refcount(refblock_data, j) == 0);
662 s->set_refcount(refblock_data, j, 1);
663 }
664
2d135ee9 665 qcow2_cache_entry_mark_dirty(s->refcount_block_cache,
12cc30a8
HR
666 refblock_data);
667 }
668
2013c3d4 669 qcow2_cache_put(s->refcount_block_cache, &refblock_data);
92dcb59f
KW
670 }
671
12cc30a8
HR
672 assert(block_offset == table_offset);
673
92dcb59f 674 /* Write refcount blocks to disk */
66f82cee 675 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS);
12cc30a8 676 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
92dcb59f 677 if (ret < 0) {
12cc30a8 678 goto fail;
92dcb59f
KW
679 }
680
681 /* Write refcount table to disk */
12cc30a8 682 for (i = 0; i < total_refblock_count; i++) {
92dcb59f
KW
683 cpu_to_be64s(&new_table[i]);
684 }
685
66f82cee 686 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE);
32cc71de
AF
687 ret = bdrv_pwrite_sync(bs->file, table_offset,
688 table_size * REFTABLE_ENTRY_SIZE, new_table, 0);
92dcb59f 689 if (ret < 0) {
12cc30a8 690 goto fail;
92dcb59f
KW
691 }
692
12cc30a8 693 for (i = 0; i < total_refblock_count; i++) {
87267753 694 be64_to_cpus(&new_table[i]);
92dcb59f 695 }
f7d0fe02 696
92dcb59f 697 /* Hook up the new refcount table in the qcow2 header */
95334230
JS
698 struct QEMU_PACKED {
699 uint64_t d64;
700 uint32_t d32;
701 } data;
f1f7a1dd
PM
702 data.d64 = cpu_to_be64(table_offset);
703 data.d32 = cpu_to_be32(table_clusters);
66f82cee 704 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE);
d9ca2ea2 705 ret = bdrv_pwrite_sync(bs->file,
32cc71de
AF
706 offsetof(QCowHeader, refcount_table_offset),
707 sizeof(data), &data, 0);
92dcb59f 708 if (ret < 0) {
12cc30a8 709 goto fail;
f2b7c8b3
KW
710 }
711
92dcb59f
KW
712 /* And switch it in memory */
713 uint64_t old_table_offset = s->refcount_table_offset;
714 uint64_t old_table_size = s->refcount_table_size;
715
7267c094 716 g_free(s->refcount_table);
f7d0fe02 717 s->refcount_table = new_table;
92dcb59f 718 s->refcount_table_size = table_size;
f7d0fe02 719 s->refcount_table_offset = table_offset;
7061a078 720 update_max_refcount_table_index(s);
f7d0fe02 721
b106ad91 722 /* Free old table. */
02b1ecfa
AG
723 qcow2_free_clusters(bs, old_table_offset,
724 old_table_size * REFTABLE_ENTRY_SIZE,
6cfcb9b8 725 QCOW2_DISCARD_OTHER);
f7d0fe02 726
12cc30a8 727 return end_offset;
f7d0fe02 728
12cc30a8 729fail:
7267c094 730 g_free(new_table);
29c1a730 731 return ret;
9923e05e
KW
732}
733
0b919fae
KW
734void qcow2_process_discards(BlockDriverState *bs, int ret)
735{
ff99129a 736 BDRVQcow2State *s = bs->opaque;
0b919fae
KW
737 Qcow2DiscardRegion *d, *next;
738
739 QTAILQ_FOREACH_SAFE(d, &s->discards, next, next) {
740 QTAILQ_REMOVE(&s->discards, d, next);
741
742 /* Discard is optional, ignore the return value */
743 if (ret >= 0) {
1477b6c8
VSO
744 int r2 = bdrv_pdiscard(bs->file, d->offset, d->bytes);
745 if (r2 < 0) {
746 trace_qcow2_process_discards_failed_region(d->offset, d->bytes,
747 r2);
748 }
0b919fae
KW
749 }
750
751 g_free(d);
752 }
753}
754
755static void update_refcount_discard(BlockDriverState *bs,
756 uint64_t offset, uint64_t length)
757{
ff99129a 758 BDRVQcow2State *s = bs->opaque;
0b919fae
KW
759 Qcow2DiscardRegion *d, *p, *next;
760
761 QTAILQ_FOREACH(d, &s->discards, next) {
762 uint64_t new_start = MIN(offset, d->offset);
763 uint64_t new_end = MAX(offset + length, d->offset + d->bytes);
764
765 if (new_end - new_start <= length + d->bytes) {
766 /* There can't be any overlap, areas ending up here have no
767 * references any more and therefore shouldn't get freed another
768 * time. */
769 assert(d->bytes + length == new_end - new_start);
770 d->offset = new_start;
771 d->bytes = new_end - new_start;
772 goto found;
773 }
774 }
775
776 d = g_malloc(sizeof(*d));
777 *d = (Qcow2DiscardRegion) {
778 .bs = bs,
779 .offset = offset,
780 .bytes = length,
781 };
782 QTAILQ_INSERT_TAIL(&s->discards, d, next);
783
784found:
785 /* Merge discard requests if they are adjacent now */
786 QTAILQ_FOREACH_SAFE(p, &s->discards, next, next) {
787 if (p == d
788 || p->offset > d->offset + d->bytes
789 || d->offset > p->offset + p->bytes)
790 {
791 continue;
792 }
793
794 /* Still no overlap possible */
795 assert(p->offset == d->offset + d->bytes
796 || d->offset == p->offset + p->bytes);
797
798 QTAILQ_REMOVE(&s->discards, p, next);
799 d->offset = MIN(d->offset, p->offset);
800 d->bytes += p->bytes;
d8bb71b6 801 g_free(p);
0b919fae
KW
802 }
803}
804
f7d0fe02 805/* XXX: cache several refcount block clusters ? */
2aabe7c7
HR
806/* @addend is the absolute value of the addend; if @decrease is set, @addend
807 * will be subtracted from the current refcount, otherwise it will be added */
c0840179
MAL
808static int update_refcount(BlockDriverState *bs,
809 int64_t offset,
810 int64_t length,
811 uint64_t addend,
812 bool decrease,
813 enum qcow2_discard_type type)
f7d0fe02 814{
ff99129a 815 BDRVQcow2State *s = bs->opaque;
f7d0fe02 816 int64_t start, last, cluster_offset;
7453c96b 817 void *refcount_block = NULL;
29c1a730 818 int64_t old_table_index = -1;
09508d13 819 int ret;
f7d0fe02
KW
820
821#ifdef DEBUG_ALLOC2
2aabe7c7 822 fprintf(stderr, "update_refcount: offset=%" PRId64 " size=%" PRId64
0e06528e 823 " addend=%s%" PRIu64 "\n", offset, length, decrease ? "-" : "",
2aabe7c7 824 addend);
f7d0fe02 825#endif
7322afe7 826 if (length < 0) {
f7d0fe02 827 return -EINVAL;
7322afe7
KW
828 } else if (length == 0) {
829 return 0;
830 }
831
2aabe7c7 832 if (decrease) {
29c1a730
KW
833 qcow2_cache_set_dependency(bs, s->refcount_block_cache,
834 s->l2_table_cache);
835 }
836
ac95acdb
HT
837 start = start_of_cluster(s, offset);
838 last = start_of_cluster(s, offset + length - 1);
f7d0fe02
KW
839 for(cluster_offset = start; cluster_offset <= last;
840 cluster_offset += s->cluster_size)
841 {
2aabe7c7 842 int block_index;
0e06528e 843 uint64_t refcount;
f7d0fe02 844 int64_t cluster_index = cluster_offset >> s->cluster_bits;
17bd5f47 845 int64_t table_index = cluster_index >> s->refcount_block_bits;
f7d0fe02 846
29c1a730
KW
847 /* Load the refcount block and allocate it if needed */
848 if (table_index != old_table_index) {
849 if (refcount_block) {
2013c3d4 850 qcow2_cache_put(s->refcount_block_cache, &refcount_block);
29c1a730 851 }
29c1a730 852 ret = alloc_refcount_block(bs, cluster_index, &refcount_block);
abf754fe
AG
853 /* If the caller needs to restart the search for free clusters,
854 * try the same ones first to see if they're still free. */
855 if (ret == -EAGAIN) {
856 if (s->free_cluster_index > (start >> s->cluster_bits)) {
857 s->free_cluster_index = (start >> s->cluster_bits);
858 }
859 }
ed0df867 860 if (ret < 0) {
29c1a730 861 goto fail;
f7d0fe02 862 }
f7d0fe02 863 }
29c1a730 864 old_table_index = table_index;
f7d0fe02 865
2d135ee9 866 qcow2_cache_entry_mark_dirty(s->refcount_block_cache, refcount_block);
f7d0fe02
KW
867
868 /* we can update the count and save it */
17bd5f47 869 block_index = cluster_index & (s->refcount_block_size - 1);
f7d0fe02 870
7453c96b 871 refcount = s->get_refcount(refcount_block, block_index);
0e06528e
HR
872 if (decrease ? (refcount - addend > refcount)
873 : (refcount + addend < refcount ||
874 refcount + addend > s->refcount_max))
2aabe7c7 875 {
09508d13
KW
876 ret = -EINVAL;
877 goto fail;
878 }
2aabe7c7
HR
879 if (decrease) {
880 refcount -= addend;
881 } else {
882 refcount += addend;
883 }
f7d0fe02
KW
884 if (refcount == 0 && cluster_index < s->free_cluster_index) {
885 s->free_cluster_index = cluster_index;
886 }
7453c96b 887 s->set_refcount(refcount_block, block_index, refcount);
0b919fae 888
f71c08ea
PB
889 if (refcount == 0) {
890 void *table;
891
6e6fa760 892 table = qcow2_cache_is_table_offset(s->refcount_block_cache,
f71c08ea
PB
893 offset);
894 if (table != NULL) {
2013c3d4 895 qcow2_cache_put(s->refcount_block_cache, &refcount_block);
dea9052e 896 old_table_index = -1;
77aadd7b 897 qcow2_cache_discard(s->refcount_block_cache, table);
f71c08ea
PB
898 }
899
6e6fa760 900 table = qcow2_cache_is_table_offset(s->l2_table_cache, offset);
f71c08ea 901 if (table != NULL) {
77aadd7b 902 qcow2_cache_discard(s->l2_table_cache, table);
f71c08ea
PB
903 }
904
905 if (s->discard_passthrough[type]) {
906 update_refcount_discard(bs, cluster_offset, s->cluster_size);
907 }
67af674e 908 }
f7d0fe02
KW
909 }
910
09508d13
KW
911 ret = 0;
912fail:
0b919fae
KW
913 if (!s->cache_discards) {
914 qcow2_process_discards(bs, ret);
915 }
916
f7d0fe02 917 /* Write last changed block to disk */
29c1a730 918 if (refcount_block) {
2013c3d4 919 qcow2_cache_put(s->refcount_block_cache, &refcount_block);
f7d0fe02
KW
920 }
921
09508d13
KW
922 /*
923 * Try do undo any updates if an error is returned (This may succeed in
924 * some cases like ENOSPC for allocating a new refcount block)
925 */
926 if (ret < 0) {
927 int dummy;
2aabe7c7
HR
928 dummy = update_refcount(bs, offset, cluster_offset - offset, addend,
929 !decrease, QCOW2_DISCARD_NEVER);
83e3f76c 930 (void)dummy;
09508d13
KW
931 }
932
933 return ret;
f7d0fe02
KW
934}
935
018faafd 936/*
44751917 937 * Increases or decreases the refcount of a given cluster.
018faafd 938 *
2aabe7c7
HR
939 * @addend is the absolute value of the addend; if @decrease is set, @addend
940 * will be subtracted from the current refcount, otherwise it will be added.
941 *
c6e9d8ae 942 * On success 0 is returned; on failure -errno is returned.
018faafd 943 */
32b6444d
HR
944int qcow2_update_cluster_refcount(BlockDriverState *bs,
945 int64_t cluster_index,
0e06528e 946 uint64_t addend, bool decrease,
32b6444d 947 enum qcow2_discard_type type)
f7d0fe02 948{
ff99129a 949 BDRVQcow2State *s = bs->opaque;
f7d0fe02
KW
950 int ret;
951
6cfcb9b8 952 ret = update_refcount(bs, cluster_index << s->cluster_bits, 1, addend,
2aabe7c7 953 decrease, type);
f7d0fe02
KW
954 if (ret < 0) {
955 return ret;
956 }
957
c6e9d8ae 958 return 0;
f7d0fe02
KW
959}
960
961
962
963/*********************************************************/
964/* cluster allocation functions */
965
966
967
968/* return < 0 if error */
77d6a215
EB
969static int64_t alloc_clusters_noref(BlockDriverState *bs, uint64_t size,
970 uint64_t max)
f7d0fe02 971{
ff99129a 972 BDRVQcow2State *s = bs->opaque;
0e06528e 973 uint64_t i, nb_clusters, refcount;
7324c10f 974 int ret;
f7d0fe02 975
ecbda7a2
KW
976 /* We can't allocate clusters if they may still be queued for discard. */
977 if (s->cache_discards) {
978 qcow2_process_discards(bs, 0);
979 }
980
f7d0fe02
KW
981 nb_clusters = size_to_clusters(s, size);
982retry:
983 for(i = 0; i < nb_clusters; i++) {
bb572aef 984 uint64_t next_cluster_index = s->free_cluster_index++;
7324c10f 985 ret = qcow2_get_refcount(bs, next_cluster_index, &refcount);
2eaa8f63 986
7324c10f
HR
987 if (ret < 0) {
988 return ret;
2eaa8f63 989 } else if (refcount != 0) {
f7d0fe02 990 goto retry;
2eaa8f63 991 }
f7d0fe02 992 }
91f827dc
HR
993
994 /* Make sure that all offsets in the "allocated" range are representable
77d6a215 995 * in the requested max */
65f33bc0 996 if (s->free_cluster_index > 0 &&
77d6a215 997 s->free_cluster_index - 1 > (max >> s->cluster_bits))
65f33bc0 998 {
91f827dc
HR
999 return -EFBIG;
1000 }
1001
f7d0fe02 1002#ifdef DEBUG_ALLOC2
35ee5e39 1003 fprintf(stderr, "alloc_clusters: size=%" PRId64 " -> %" PRId64 "\n",
f7d0fe02
KW
1004 size,
1005 (s->free_cluster_index - nb_clusters) << s->cluster_bits);
1006#endif
1007 return (s->free_cluster_index - nb_clusters) << s->cluster_bits;
1008}
1009
bb572aef 1010int64_t qcow2_alloc_clusters(BlockDriverState *bs, uint64_t size)
f7d0fe02
KW
1011{
1012 int64_t offset;
db3a964f 1013 int ret;
f7d0fe02 1014
66f82cee 1015 BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC);
b106ad91 1016 do {
77d6a215 1017 offset = alloc_clusters_noref(bs, size, QCOW_MAX_CLUSTER_OFFSET);
b106ad91
KW
1018 if (offset < 0) {
1019 return offset;
1020 }
1021
2aabe7c7 1022 ret = update_refcount(bs, offset, size, 1, false, QCOW2_DISCARD_NEVER);
b106ad91 1023 } while (ret == -EAGAIN);
2eaa8f63 1024
db3a964f
KW
1025 if (ret < 0) {
1026 return ret;
1027 }
1c4c2814 1028
f7d0fe02
KW
1029 return offset;
1030}
1031
b6d36def
HR
1032int64_t qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
1033 int64_t nb_clusters)
256900b1 1034{
ff99129a 1035 BDRVQcow2State *s = bs->opaque;
0e06528e 1036 uint64_t cluster_index, refcount;
33304ec9 1037 uint64_t i;
7324c10f 1038 int ret;
33304ec9
HT
1039
1040 assert(nb_clusters >= 0);
1041 if (nb_clusters == 0) {
1042 return 0;
1043 }
256900b1 1044
b106ad91
KW
1045 do {
1046 /* Check how many clusters there are free */
1047 cluster_index = offset >> s->cluster_bits;
1048 for(i = 0; i < nb_clusters; i++) {
7324c10f
HR
1049 ret = qcow2_get_refcount(bs, cluster_index++, &refcount);
1050 if (ret < 0) {
1051 return ret;
b106ad91
KW
1052 } else if (refcount != 0) {
1053 break;
1054 }
256900b1 1055 }
256900b1 1056
b106ad91 1057 /* And then allocate them */
2aabe7c7 1058 ret = update_refcount(bs, offset, i << s->cluster_bits, 1, false,
b106ad91
KW
1059 QCOW2_DISCARD_NEVER);
1060 } while (ret == -EAGAIN);
f24423bd 1061
256900b1
KW
1062 if (ret < 0) {
1063 return ret;
1064 }
1065
1066 return i;
1067}
1068
f7d0fe02
KW
1069/* only used to allocate compressed sectors. We try to allocate
1070 contiguous sectors. size must be <= cluster_size */
ed6ccf0f 1071int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size)
f7d0fe02 1072{
ff99129a 1073 BDRVQcow2State *s = bs->opaque;
8c44dfbc
HR
1074 int64_t offset;
1075 size_t free_in_cluster;
1076 int ret;
f7d0fe02 1077
66f82cee 1078 BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC_BYTES);
f7d0fe02 1079 assert(size > 0 && size <= s->cluster_size);
8c44dfbc
HR
1080 assert(!s->free_byte_offset || offset_into_cluster(s, s->free_byte_offset));
1081
1082 offset = s->free_byte_offset;
1083
1084 if (offset) {
0e06528e 1085 uint64_t refcount;
7324c10f
HR
1086 ret = qcow2_get_refcount(bs, offset >> s->cluster_bits, &refcount);
1087 if (ret < 0) {
1088 return ret;
5d757b56 1089 }
8c44dfbc 1090
346a53df 1091 if (refcount == s->refcount_max) {
8c44dfbc 1092 offset = 0;
5d757b56 1093 }
8c44dfbc
HR
1094 }
1095
1096 free_in_cluster = s->cluster_size - offset_into_cluster(s, offset);
3e5feb62
JM
1097 do {
1098 if (!offset || free_in_cluster < size) {
77d6a215
EB
1099 int64_t new_cluster;
1100
1101 new_cluster = alloc_clusters_noref(bs, s->cluster_size,
1102 MIN(s->cluster_offset_mask,
1103 QCOW_MAX_CLUSTER_OFFSET));
3e5feb62
JM
1104 if (new_cluster < 0) {
1105 return new_cluster;
1106 }
8c44dfbc 1107
8aa34834
AG
1108 if (new_cluster == 0) {
1109 qcow2_signal_corruption(bs, true, -1, -1, "Preventing invalid "
1110 "allocation of compressed cluster "
1111 "at offset 0");
1112 return -EIO;
1113 }
1114
3e5feb62
JM
1115 if (!offset || ROUND_UP(offset, s->cluster_size) != new_cluster) {
1116 offset = new_cluster;
2ac01520
HR
1117 free_in_cluster = s->cluster_size;
1118 } else {
1119 free_in_cluster += s->cluster_size;
3e5feb62 1120 }
f7d0fe02 1121 }
29216ed1 1122
3e5feb62
JM
1123 assert(offset);
1124 ret = update_refcount(bs, offset, size, 1, false, QCOW2_DISCARD_NEVER);
2ac01520
HR
1125 if (ret < 0) {
1126 offset = 0;
1127 }
3e5feb62 1128 } while (ret == -EAGAIN);
8c44dfbc
HR
1129 if (ret < 0) {
1130 return ret;
1131 }
1132
1133 /* The cluster refcount was incremented; refcount blocks must be flushed
1134 * before the caller's L2 table updates. */
c1f5bafd 1135 qcow2_cache_set_dependency(bs, s->l2_table_cache, s->refcount_block_cache);
8c44dfbc
HR
1136
1137 s->free_byte_offset = offset + size;
1138 if (!offset_into_cluster(s, s->free_byte_offset)) {
1139 s->free_byte_offset = 0;
1140 }
1141
f7d0fe02
KW
1142 return offset;
1143}
1144
ed6ccf0f 1145void qcow2_free_clusters(BlockDriverState *bs,
6cfcb9b8
KW
1146 int64_t offset, int64_t size,
1147 enum qcow2_discard_type type)
f7d0fe02 1148{
db3a964f
KW
1149 int ret;
1150
66f82cee 1151 BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_FREE);
2aabe7c7 1152 ret = update_refcount(bs, offset, size, 1, true, type);
db3a964f
KW
1153 if (ret < 0) {
1154 fprintf(stderr, "qcow2_free_clusters failed: %s\n", strerror(-ret));
003fad6e 1155 /* TODO Remember the clusters to free them later and avoid leaking */
db3a964f 1156 }
f7d0fe02
KW
1157}
1158
45aba42f 1159/*
c7a4c37a
KW
1160 * Free a cluster using its L2 entry (handles clusters of all types, e.g.
1161 * normal cluster, compressed cluster, etc.)
45aba42f 1162 */
3fec237f
AG
1163void qcow2_free_any_cluster(BlockDriverState *bs, uint64_t l2_entry,
1164 enum qcow2_discard_type type)
45aba42f 1165{
ff99129a 1166 BDRVQcow2State *s = bs->opaque;
966b000f 1167 QCow2ClusterType ctype = qcow2_get_cluster_type(bs, l2_entry);
45aba42f 1168
966b000f
KW
1169 if (has_data_file(bs)) {
1170 if (s->discard_passthrough[type] &&
1171 (ctype == QCOW2_CLUSTER_NORMAL ||
1172 ctype == QCOW2_CLUSTER_ZERO_ALLOC))
1173 {
1174 bdrv_pdiscard(s->data_file, l2_entry & L2E_OFFSET_MASK,
3fec237f 1175 s->cluster_size);
966b000f
KW
1176 }
1177 return;
1178 }
1179
1180 switch (ctype) {
c7a4c37a
KW
1181 case QCOW2_CLUSTER_COMPRESSED:
1182 {
a6e09846
VSO
1183 uint64_t coffset;
1184 int csize;
1185
1186 qcow2_parse_compressed_l2_entry(bs, l2_entry, &coffset, &csize);
1187 qcow2_free_clusters(bs, coffset, csize, type);
c7a4c37a
KW
1188 }
1189 break;
1190 case QCOW2_CLUSTER_NORMAL:
fdfab37d
EB
1191 case QCOW2_CLUSTER_ZERO_ALLOC:
1192 if (offset_into_cluster(s, l2_entry & L2E_OFFSET_MASK)) {
1193 qcow2_signal_corruption(bs, false, -1, -1,
1194 "Cannot free unaligned cluster %#llx",
1195 l2_entry & L2E_OFFSET_MASK);
1196 } else {
1197 qcow2_free_clusters(bs, l2_entry & L2E_OFFSET_MASK,
3fec237f 1198 s->cluster_size, type);
8f730dd2 1199 }
c7a4c37a 1200 break;
fdfab37d 1201 case QCOW2_CLUSTER_ZERO_PLAIN:
c7a4c37a
KW
1202 case QCOW2_CLUSTER_UNALLOCATED:
1203 break;
1204 default:
1205 abort();
45aba42f 1206 }
45aba42f
KW
1207}
1208
8b220eb7
PB
1209int coroutine_fn qcow2_write_caches(BlockDriverState *bs)
1210{
1211 BDRVQcow2State *s = bs->opaque;
1212 int ret;
f7d0fe02 1213
8b220eb7
PB
1214 ret = qcow2_cache_write(bs, s->l2_table_cache);
1215 if (ret < 0) {
1216 return ret;
1217 }
1218
1219 if (qcow2_need_accurate_refcounts(s)) {
1220 ret = qcow2_cache_write(bs, s->refcount_block_cache);
1221 if (ret < 0) {
1222 return ret;
1223 }
1224 }
1225
1226 return 0;
1227}
1228
1229int coroutine_fn qcow2_flush_caches(BlockDriverState *bs)
1230{
1231 int ret = qcow2_write_caches(bs);
1232 if (ret < 0) {
1233 return ret;
1234 }
1235
1236 return bdrv_flush(bs->file->bs);
1237}
f7d0fe02
KW
1238
1239/*********************************************************/
1240/* snapshots and image creation */
1241
1242
1243
f7d0fe02 1244/* update the refcounts of snapshots and the copied flag */
ed6ccf0f
KW
1245int qcow2_update_snapshot_refcount(BlockDriverState *bs,
1246 int64_t l1_table_offset, int l1_size, int addend)
f7d0fe02 1247{
ff99129a 1248 BDRVQcow2State *s = bs->opaque;
83ad165b 1249 uint64_t *l1_table, *l2_slice, l2_offset, entry, l1_size2, refcount;
de82815d 1250 bool l1_allocated = false;
b32cbae1 1251 int64_t old_entry, old_l2_offset;
83ad165b 1252 unsigned slice, slice_size2, n_slices;
a6e09846 1253 int i, j, l1_modified = 0;
29c1a730 1254 int ret;
f7d0fe02 1255
2aabe7c7
HR
1256 assert(addend >= -1 && addend <= 1);
1257
83ad165b 1258 l2_slice = NULL;
f7d0fe02 1259 l1_table = NULL;
02b1ecfa 1260 l1_size2 = l1_size * L1E_SIZE;
c8fd8554 1261 slice_size2 = s->l2_slice_size * l2_entry_size(s);
83ad165b 1262 n_slices = s->cluster_size / slice_size2;
43a0cac4 1263
0b919fae
KW
1264 s->cache_discards = true;
1265
43a0cac4
KW
1266 /* WARNING: qcow2_snapshot_goto relies on this function not using the
1267 * l1_table_offset when it is the current s->l1_table_offset! Be careful
1268 * when changing this! */
f7d0fe02 1269 if (l1_table_offset != s->l1_table_offset) {
ef97d608 1270 l1_table = g_try_malloc0(l1_size2);
de82815d
KW
1271 if (l1_size2 && l1_table == NULL) {
1272 ret = -ENOMEM;
1273 goto fail;
1274 }
1275 l1_allocated = true;
c2bc78b6 1276
32cc71de 1277 ret = bdrv_pread(bs->file, l1_table_offset, l1_size2, l1_table, 0);
c2bc78b6 1278 if (ret < 0) {
f7d0fe02 1279 goto fail;
93913dfd
KW
1280 }
1281
b32cbae1 1282 for (i = 0; i < l1_size; i++) {
f7d0fe02 1283 be64_to_cpus(&l1_table[i]);
b32cbae1 1284 }
f7d0fe02
KW
1285 } else {
1286 assert(l1_size == s->l1_size);
1287 l1_table = s->l1_table;
de82815d 1288 l1_allocated = false;
f7d0fe02
KW
1289 }
1290
b32cbae1 1291 for (i = 0; i < l1_size; i++) {
f7d0fe02
KW
1292 l2_offset = l1_table[i];
1293 if (l2_offset) {
1294 old_l2_offset = l2_offset;
8e37f681 1295 l2_offset &= L1E_OFFSET_MASK;
29c1a730 1296
a97c67ee
HR
1297 if (offset_into_cluster(s, l2_offset)) {
1298 qcow2_signal_corruption(bs, true, -1, -1, "L2 table offset %#"
1299 PRIx64 " unaligned (L1 index: %#x)",
1300 l2_offset, i);
1301 ret = -EIO;
1302 goto fail;
1303 }
1304
83ad165b 1305 for (slice = 0; slice < n_slices; slice++) {
ca62dd5c 1306 ret = qcow2_cache_get(bs, s->l2_table_cache,
83ad165b
AG
1307 l2_offset + slice * slice_size2,
1308 (void **) &l2_slice);
ca62dd5c
AG
1309 if (ret < 0) {
1310 goto fail;
1311 }
29c1a730 1312
83ad165b 1313 for (j = 0; j < s->l2_slice_size; j++) {
ca62dd5c
AG
1314 uint64_t cluster_index;
1315 uint64_t offset;
1316
12c6aebe 1317 entry = get_l2_entry(s, l2_slice, j);
ca62dd5c
AG
1318 old_entry = entry;
1319 entry &= ~QCOW_OFLAG_COPIED;
1320 offset = entry & L2E_OFFSET_MASK;
1321
808c2bb4 1322 switch (qcow2_get_cluster_type(bs, entry)) {
ca62dd5c 1323 case QCOW2_CLUSTER_COMPRESSED:
ca62dd5c 1324 if (addend != 0) {
a6e09846
VSO
1325 uint64_t coffset;
1326 int csize;
1327
1328 qcow2_parse_compressed_l2_entry(bs, entry,
1329 &coffset, &csize);
ca62dd5c 1330 ret = update_refcount(
a6e09846 1331 bs, coffset, csize,
b6c24694 1332 abs(addend), addend < 0,
6cfcb9b8 1333 QCOW2_DISCARD_SNAPSHOT);
ca62dd5c
AG
1334 if (ret < 0) {
1335 goto fail;
1336 }
1337 }
1338 /* compressed clusters are never modified */
1339 refcount = 2;
1340 break;
1341
1342 case QCOW2_CLUSTER_NORMAL:
1343 case QCOW2_CLUSTER_ZERO_ALLOC:
1344 if (offset_into_cluster(s, offset)) {
83ad165b
AG
1345 /* Here l2_index means table (not slice) index */
1346 int l2_index = slice * s->l2_slice_size + j;
ca62dd5c
AG
1347 qcow2_signal_corruption(
1348 bs, true, -1, -1, "Cluster "
1349 "allocation offset %#" PRIx64
1350 " unaligned (L2 offset: %#"
1351 PRIx64 ", L2 index: %#x)",
83ad165b 1352 offset, l2_offset, l2_index);
ca62dd5c 1353 ret = -EIO;
a97c67ee
HR
1354 goto fail;
1355 }
1356
ca62dd5c
AG
1357 cluster_index = offset >> s->cluster_bits;
1358 assert(cluster_index);
1359 if (addend != 0) {
1360 ret = qcow2_update_cluster_refcount(
1361 bs, cluster_index, abs(addend), addend < 0,
1362 QCOW2_DISCARD_SNAPSHOT);
1363 if (ret < 0) {
1364 goto fail;
1365 }
1366 }
1367
1368 ret = qcow2_get_refcount(bs, cluster_index, &refcount);
7324c10f 1369 if (ret < 0) {
018faafd
KW
1370 goto fail;
1371 }
ca62dd5c 1372 break;
bbd995d8 1373
ca62dd5c
AG
1374 case QCOW2_CLUSTER_ZERO_PLAIN:
1375 case QCOW2_CLUSTER_UNALLOCATED:
1376 refcount = 0;
1377 break;
8b81a7b6 1378
ca62dd5c
AG
1379 default:
1380 abort();
1381 }
8b81a7b6 1382
ca62dd5c
AG
1383 if (refcount == 1) {
1384 entry |= QCOW_OFLAG_COPIED;
1385 }
1386 if (entry != old_entry) {
1387 if (addend > 0) {
1388 qcow2_cache_set_dependency(bs, s->l2_table_cache,
1389 s->refcount_block_cache);
1390 }
12c6aebe 1391 set_l2_entry(s, l2_slice, j, entry);
ca62dd5c 1392 qcow2_cache_entry_mark_dirty(s->l2_table_cache,
83ad165b 1393 l2_slice);
f7d0fe02
KW
1394 }
1395 }
29c1a730 1396
83ad165b 1397 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
ca62dd5c 1398 }
29c1a730 1399
f7d0fe02 1400 if (addend != 0) {
c6e9d8ae
HR
1401 ret = qcow2_update_cluster_refcount(bs, l2_offset >>
1402 s->cluster_bits,
2aabe7c7 1403 abs(addend), addend < 0,
c6e9d8ae
HR
1404 QCOW2_DISCARD_SNAPSHOT);
1405 if (ret < 0) {
1406 goto fail;
1407 }
f7d0fe02 1408 }
7324c10f
HR
1409 ret = qcow2_get_refcount(bs, l2_offset >> s->cluster_bits,
1410 &refcount);
1411 if (ret < 0) {
018faafd
KW
1412 goto fail;
1413 } else if (refcount == 1) {
f7d0fe02
KW
1414 l2_offset |= QCOW_OFLAG_COPIED;
1415 }
1416 if (l2_offset != old_l2_offset) {
1417 l1_table[i] = l2_offset;
1418 l1_modified = 1;
1419 }
1420 }
1421 }
93913dfd 1422
2154f24e 1423 ret = bdrv_flush(bs);
93913dfd 1424fail:
83ad165b
AG
1425 if (l2_slice) {
1426 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
93913dfd
KW
1427 }
1428
0b919fae
KW
1429 s->cache_discards = false;
1430 qcow2_process_discards(bs, ret);
1431
43a0cac4 1432 /* Update L1 only if it isn't deleted anyway (addend = -1) */
c2b6ff51
KW
1433 if (ret == 0 && addend >= 0 && l1_modified) {
1434 for (i = 0; i < l1_size; i++) {
f7d0fe02 1435 cpu_to_be64s(&l1_table[i]);
c2b6ff51
KW
1436 }
1437
32cc71de 1438 ret = bdrv_pwrite_sync(bs->file, l1_table_offset, l1_size2, l1_table,
53fb7844 1439 0);
c2b6ff51
KW
1440
1441 for (i = 0; i < l1_size; i++) {
f7d0fe02 1442 be64_to_cpus(&l1_table[i]);
c2b6ff51 1443 }
f7d0fe02
KW
1444 }
1445 if (l1_allocated)
7267c094 1446 g_free(l1_table);
93913dfd 1447 return ret;
f7d0fe02
KW
1448}
1449
1450
1451
1452
1453/*********************************************************/
1454/* refcount checking functions */
1455
1456
c2551b47 1457static uint64_t refcount_array_byte_size(BDRVQcow2State *s, uint64_t entries)
5fee192e
HR
1458{
1459 /* This assertion holds because there is no way we can address more than
1460 * 2^(64 - 9) clusters at once (with cluster size 512 = 2^9, and because
1461 * offsets have to be representable in bytes); due to every cluster
1462 * corresponding to one refcount entry, we are well below that limit */
1463 assert(entries < (UINT64_C(1) << (64 - 9)));
1464
1465 /* Thanks to the assertion this will not overflow, because
1466 * s->refcount_order < 7.
1467 * (note: x << s->refcount_order == x * s->refcount_bits) */
1468 return DIV_ROUND_UP(entries << s->refcount_order, 8);
1469}
1470
1471/**
1472 * Reallocates *array so that it can hold new_size entries. *size must contain
1473 * the current number of entries in *array. If the reallocation fails, *array
1474 * and *size will not be modified and -errno will be returned. If the
1475 * reallocation is successful, *array will be set to the new buffer, *size
1476 * will be set to new_size and 0 will be returned. The size of the reallocated
1477 * refcount array buffer will be aligned to a cluster boundary, and the newly
1478 * allocated area will be zeroed.
1479 */
ff99129a 1480static int realloc_refcount_array(BDRVQcow2State *s, void **array,
5fee192e
HR
1481 int64_t *size, int64_t new_size)
1482{
b6d36def 1483 int64_t old_byte_size, new_byte_size;
7453c96b 1484 void *new_ptr;
5fee192e
HR
1485
1486 /* Round to clusters so the array can be directly written to disk */
1487 old_byte_size = size_to_clusters(s, refcount_array_byte_size(s, *size))
1488 * s->cluster_size;
1489 new_byte_size = size_to_clusters(s, refcount_array_byte_size(s, new_size))
1490 * s->cluster_size;
1491
1492 if (new_byte_size == old_byte_size) {
1493 *size = new_size;
1494 return 0;
1495 }
1496
1497 assert(new_byte_size > 0);
1498
b6d36def
HR
1499 if (new_byte_size > SIZE_MAX) {
1500 return -ENOMEM;
1501 }
1502
5fee192e
HR
1503 new_ptr = g_try_realloc(*array, new_byte_size);
1504 if (!new_ptr) {
1505 return -ENOMEM;
1506 }
1507
1508 if (new_byte_size > old_byte_size) {
b6d36def 1509 memset((char *)new_ptr + old_byte_size, 0,
5fee192e
HR
1510 new_byte_size - old_byte_size);
1511 }
1512
1513 *array = new_ptr;
1514 *size = new_size;
1515
1516 return 0;
1517}
f7d0fe02
KW
1518
1519/*
1520 * Increases the refcount for a range of clusters in a given refcount table.
1521 * This is used to construct a temporary refcount table out of L1 and L2 tables
b6af0975 1522 * which can be compared to the refcount table saved in the image.
f7d0fe02 1523 *
9ac228e0 1524 * Modifies the number of errors in res.
f7d0fe02 1525 */
8a5bb1f1
VSO
1526int qcow2_inc_refcounts_imrt(BlockDriverState *bs, BdrvCheckResult *res,
1527 void **refcount_table,
1528 int64_t *refcount_table_size,
1529 int64_t offset, int64_t size)
f7d0fe02 1530{
ff99129a 1531 BDRVQcow2State *s = bs->opaque;
7453c96b 1532 uint64_t start, last, cluster_offset, k, refcount;
a5fff8d4 1533 int64_t file_len;
5fee192e 1534 int ret;
f7d0fe02 1535
fef4d3d5
HR
1536 if (size <= 0) {
1537 return 0;
1538 }
f7d0fe02 1539
a5fff8d4
VSO
1540 file_len = bdrv_getlength(bs->file->bs);
1541 if (file_len < 0) {
1542 return file_len;
1543 }
1544
1545 /*
1546 * Last cluster of qcow2 image may be semi-allocated, so it may be OK to
1547 * reference some space after file end but it should be less than one
1548 * cluster.
1549 */
1550 if (offset + size - file_len >= s->cluster_size) {
1551 fprintf(stderr, "ERROR: counting reference for region exceeding the "
1552 "end of the file by one cluster or more: offset 0x%" PRIx64
1553 " size 0x%" PRIx64 "\n", offset, size);
1554 res->corruptions++;
1555 return 0;
1556 }
1557
ac95acdb
HT
1558 start = start_of_cluster(s, offset);
1559 last = start_of_cluster(s, offset + size - 1);
f7d0fe02
KW
1560 for(cluster_offset = start; cluster_offset <= last;
1561 cluster_offset += s->cluster_size) {
1562 k = cluster_offset >> s->cluster_bits;
641bb63c 1563 if (k >= *refcount_table_size) {
5fee192e
HR
1564 ret = realloc_refcount_array(s, refcount_table,
1565 refcount_table_size, k + 1);
1566 if (ret < 0) {
641bb63c 1567 res->check_errors++;
5fee192e 1568 return ret;
f7d0fe02 1569 }
641bb63c
HR
1570 }
1571
7453c96b
HR
1572 refcount = s->get_refcount(*refcount_table, k);
1573 if (refcount == s->refcount_max) {
641bb63c
HR
1574 fprintf(stderr, "ERROR: overflow cluster offset=0x%" PRIx64
1575 "\n", cluster_offset);
03bb78ed
HR
1576 fprintf(stderr, "Use qemu-img amend to increase the refcount entry "
1577 "width or qemu-img convert to create a clean copy if the "
1578 "image cannot be opened for writing\n");
641bb63c 1579 res->corruptions++;
7453c96b 1580 continue;
f7d0fe02 1581 }
7453c96b 1582 s->set_refcount(*refcount_table, k, refcount + 1);
f7d0fe02 1583 }
fef4d3d5
HR
1584
1585 return 0;
f7d0fe02
KW
1586}
1587
801f7044
SH
1588/* Flags for check_refcounts_l1() and check_refcounts_l2() */
1589enum {
fba31bae 1590 CHECK_FRAG_INFO = 0x2, /* update BlockFragInfo counters */
801f7044
SH
1591};
1592
a2debf65 1593/*
5c3216c0
VSO
1594 * Fix L2 entry by making it QCOW2_CLUSTER_ZERO_PLAIN (or making all its present
1595 * subclusters QCOW2_SUBCLUSTER_ZERO_PLAIN).
a2debf65
VSO
1596 *
1597 * This function decrements res->corruptions on success, so the caller is
1598 * responsible to increment res->corruptions prior to the call.
1599 *
1600 * On failure in-memory @l2_table may be modified.
1601 */
1602static int fix_l2_entry_by_zero(BlockDriverState *bs, BdrvCheckResult *res,
1603 uint64_t l2_offset,
1604 uint64_t *l2_table, int l2_index, bool active,
1605 bool *metadata_overlap)
1606{
1607 BDRVQcow2State *s = bs->opaque;
1608 int ret;
1609 int idx = l2_index * (l2_entry_size(s) / sizeof(uint64_t));
1610 uint64_t l2e_offset = l2_offset + (uint64_t)l2_index * l2_entry_size(s);
1611 int ign = active ? QCOW2_OL_ACTIVE_L2 : QCOW2_OL_INACTIVE_L2;
a2debf65 1612
5c3216c0
VSO
1613 if (has_subclusters(s)) {
1614 uint64_t l2_bitmap = get_l2_bitmap(s, l2_table, l2_index);
1615
1616 /* Allocated subclusters become zero */
1617 l2_bitmap |= l2_bitmap << 32;
1618 l2_bitmap &= QCOW_L2_BITMAP_ALL_ZEROES;
1619
1620 set_l2_bitmap(s, l2_table, l2_index, l2_bitmap);
1621 set_l2_entry(s, l2_table, l2_index, 0);
1622 } else {
1623 set_l2_entry(s, l2_table, l2_index, QCOW_OFLAG_ZERO);
1624 }
1625
a2debf65
VSO
1626 ret = qcow2_pre_write_overlap_check(bs, ign, l2e_offset, l2_entry_size(s),
1627 false);
1628 if (metadata_overlap) {
1629 *metadata_overlap = ret < 0;
1630 }
1631 if (ret < 0) {
1632 fprintf(stderr, "ERROR: Overlap check failed\n");
1633 goto fail;
1634 }
1635
32cc71de
AF
1636 ret = bdrv_pwrite_sync(bs->file, l2e_offset, l2_entry_size(s),
1637 &l2_table[idx], 0);
a2debf65
VSO
1638 if (ret < 0) {
1639 fprintf(stderr, "ERROR: Failed to overwrite L2 "
1640 "table entry: %s\n", strerror(-ret));
1641 goto fail;
1642 }
1643
1644 res->corruptions--;
1645 res->corruptions_fixed++;
1646 return 0;
1647
1648fail:
1649 res->check_errors++;
1650 return ret;
1651}
1652
f7d0fe02
KW
1653/*
1654 * Increases the refcount in the given refcount table for the all clusters
1655 * referenced in the L2 table. While doing so, performs some checks on L2
1656 * entries.
1657 *
1658 * Returns the number of errors found by the checks or -errno if an internal
1659 * error occurred.
1660 */
9ac228e0 1661static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res,
7453c96b
HR
1662 void **refcount_table,
1663 int64_t *refcount_table_size, int64_t l2_offset,
1ef337b7 1664 int flags, BdrvCheckMode fix, bool active)
f7d0fe02 1665{
ff99129a 1666 BDRVQcow2State *s = bs->opaque;
9631c782 1667 uint64_t l2_entry, l2_bitmap;
fba31bae 1668 uint64_t next_contiguous_offset = 0;
a6e09846 1669 int i, ret;
786c22d9
VSO
1670 size_t l2_size_bytes = s->l2_size * l2_entry_size(s);
1671 g_autofree uint64_t *l2_table = g_malloc(l2_size_bytes);
a2debf65 1672 bool metadata_overlap;
f7d0fe02
KW
1673
1674 /* Read L2 table from disk */
32cc71de 1675 ret = bdrv_pread(bs->file, l2_offset, l2_size_bytes, l2_table, 0);
ad27390c
HR
1676 if (ret < 0) {
1677 fprintf(stderr, "ERROR: I/O error in check_refcounts_l2\n");
1678 res->check_errors++;
786c22d9 1679 return ret;
ad27390c 1680 }
f7d0fe02
KW
1681
1682 /* Do the actual checks */
786c22d9 1683 for (i = 0; i < s->l2_size; i++) {
a6e09846
VSO
1684 uint64_t coffset;
1685 int csize;
289ef5f2
VSO
1686 QCow2ClusterType type;
1687
12c6aebe 1688 l2_entry = get_l2_entry(s, l2_table, i);
9631c782 1689 l2_bitmap = get_l2_bitmap(s, l2_table, i);
289ef5f2
VSO
1690 type = qcow2_get_cluster_type(bs, l2_entry);
1691
1692 if (type != QCOW2_CLUSTER_COMPRESSED) {
1693 /* Check reserved bits of Standard Cluster Descriptor */
1694 if (l2_entry & L2E_STD_RESERVED_MASK) {
1695 fprintf(stderr, "ERROR found l2 entry with reserved bits set: "
1696 "%" PRIx64 "\n", l2_entry);
1697 res->corruptions++;
1698 }
1699 }
afdf0abe 1700
289ef5f2 1701 switch (type) {
afdf0abe
KW
1702 case QCOW2_CLUSTER_COMPRESSED:
1703 /* Compressed clusters don't have QCOW_OFLAG_COPIED */
1704 if (l2_entry & QCOW_OFLAG_COPIED) {
74c44a59 1705 fprintf(stderr, "ERROR: coffset=0x%" PRIx64 ": "
afdf0abe 1706 "copied flag must never be set for compressed "
74c44a59 1707 "clusters\n", l2_entry & s->cluster_offset_mask);
afdf0abe
KW
1708 l2_entry &= ~QCOW_OFLAG_COPIED;
1709 res->corruptions++;
1710 }
f7d0fe02 1711
e9f5b6de
KW
1712 if (has_data_file(bs)) {
1713 fprintf(stderr, "ERROR compressed cluster %d with data file, "
1714 "entry=0x%" PRIx64 "\n", i, l2_entry);
1715 res->corruptions++;
1716 break;
1717 }
1718
9631c782
VSO
1719 if (l2_bitmap) {
1720 fprintf(stderr, "ERROR compressed cluster %d with non-zero "
1721 "subcluster allocation bitmap, entry=0x%" PRIx64 "\n",
1722 i, l2_entry);
1723 res->corruptions++;
1724 break;
1725 }
1726
afdf0abe 1727 /* Mark cluster as used */
a6e09846 1728 qcow2_parse_compressed_l2_entry(bs, l2_entry, &coffset, &csize);
b6c24694 1729 ret = qcow2_inc_refcounts_imrt(
a6e09846 1730 bs, res, refcount_table, refcount_table_size, coffset, csize);
fef4d3d5 1731 if (ret < 0) {
786c22d9 1732 return ret;
fef4d3d5 1733 }
fba31bae
SH
1734
1735 if (flags & CHECK_FRAG_INFO) {
1736 res->bfi.allocated_clusters++;
4db35162 1737 res->bfi.compressed_clusters++;
fba31bae 1738
786c22d9
VSO
1739 /*
1740 * Compressed clusters are fragmented by nature. Since they
fba31bae
SH
1741 * take up sub-sector space but we only have sector granularity
1742 * I/O we need to re-read the same sectors even for adjacent
1743 * compressed clusters.
1744 */
1745 res->bfi.fragmented_clusters++;
1746 }
afdf0abe 1747 break;
f7d0fe02 1748
fdfab37d 1749 case QCOW2_CLUSTER_ZERO_ALLOC:
afdf0abe
KW
1750 case QCOW2_CLUSTER_NORMAL:
1751 {
afdf0abe 1752 uint64_t offset = l2_entry & L2E_OFFSET_MASK;
f7d0fe02 1753
9631c782
VSO
1754 if ((l2_bitmap >> 32) & l2_bitmap) {
1755 res->corruptions++;
1756 fprintf(stderr, "ERROR offset=%" PRIx64 ": Allocated "
1757 "cluster has corrupted subcluster allocation bitmap\n",
1758 offset);
1759 }
1760
ac5b787a
HR
1761 /* Correct offsets are cluster aligned */
1762 if (offset_into_cluster(s, offset)) {
fc2e6528 1763 bool contains_data;
54b10010
VSO
1764 res->corruptions++;
1765
fc2e6528 1766 if (has_subclusters(s)) {
fc2e6528
AG
1767 contains_data = (l2_bitmap & QCOW_L2_BITMAP_ALL_ALLOC);
1768 } else {
1769 contains_data = !(l2_entry & QCOW_OFLAG_ZERO);
1770 }
1771
1772 if (!contains_data) {
1773 fprintf(stderr, "%s offset=%" PRIx64 ": Preallocated "
ac5b787a
HR
1774 "cluster is not properly aligned; L2 entry "
1775 "corrupted.\n",
1776 fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR",
1777 offset);
1778 if (fix & BDRV_FIX_ERRORS) {
a2debf65
VSO
1779 ret = fix_l2_entry_by_zero(bs, res, l2_offset,
1780 l2_table, i, active,
1781 &metadata_overlap);
1782 if (metadata_overlap) {
786c22d9
VSO
1783 /*
1784 * Something is seriously wrong, so abort checking
1785 * this L2 table.
1786 */
1787 return ret;
ac5b787a
HR
1788 }
1789
a2debf65 1790 if (ret == 0) {
786c22d9
VSO
1791 /*
1792 * Skip marking the cluster as used
1793 * (it is unused now).
1794 */
ac5b787a
HR
1795 continue;
1796 }
a2debf65
VSO
1797
1798 /*
1799 * Failed to fix.
1800 * Do not abort, continue checking the rest of this
1801 * L2 table's entries.
1802 */
ac5b787a
HR
1803 }
1804 } else {
1805 fprintf(stderr, "ERROR offset=%" PRIx64 ": Data cluster is "
1806 "not properly aligned; L2 entry corrupted.\n", offset);
ac5b787a
HR
1807 }
1808 }
1809
cbb51e9f
VSO
1810 if (flags & CHECK_FRAG_INFO) {
1811 res->bfi.allocated_clusters++;
1812 if (next_contiguous_offset &&
1813 offset != next_contiguous_offset) {
1814 res->bfi.fragmented_clusters++;
1815 }
1816 next_contiguous_offset = offset + s->cluster_size;
1817 }
1818
afdf0abe 1819 /* Mark cluster as used */
e9f5b6de
KW
1820 if (!has_data_file(bs)) {
1821 ret = qcow2_inc_refcounts_imrt(bs, res, refcount_table,
1822 refcount_table_size,
1823 offset, s->cluster_size);
1824 if (ret < 0) {
786c22d9 1825 return ret;
e9f5b6de 1826 }
fef4d3d5 1827 }
afdf0abe
KW
1828 break;
1829 }
1830
fdfab37d 1831 case QCOW2_CLUSTER_ZERO_PLAIN:
9631c782
VSO
1832 /* Impossible when image has subclusters */
1833 assert(!l2_bitmap);
1834 break;
1835
afdf0abe 1836 case QCOW2_CLUSTER_UNALLOCATED:
9631c782
VSO
1837 if (l2_bitmap & QCOW_L2_BITMAP_ALL_ALLOC) {
1838 res->corruptions++;
1839 fprintf(stderr, "ERROR: Unallocated "
1840 "cluster has non-zero subcluster allocation map\n");
1841 }
afdf0abe
KW
1842 break;
1843
1844 default:
1845 abort();
f7d0fe02
KW
1846 }
1847 }
1848
9ac228e0 1849 return 0;
f7d0fe02
KW
1850}
1851
1852/*
1853 * Increases the refcount for the L1 table, its L2 tables and all referenced
1854 * clusters in the given refcount table. While doing so, performs some checks
1855 * on L1 and L2 entries.
1856 *
1857 * Returns the number of errors found by the checks or -errno if an internal
1858 * error occurred.
1859 */
1860static int check_refcounts_l1(BlockDriverState *bs,
9ac228e0 1861 BdrvCheckResult *res,
7453c96b 1862 void **refcount_table,
641bb63c 1863 int64_t *refcount_table_size,
f7d0fe02 1864 int64_t l1_table_offset, int l1_size,
1ef337b7 1865 int flags, BdrvCheckMode fix, bool active)
f7d0fe02 1866{
ff99129a 1867 BDRVQcow2State *s = bs->opaque;
cd6efd60
VSO
1868 size_t l1_size_bytes = l1_size * L1E_SIZE;
1869 g_autofree uint64_t *l1_table = NULL;
1870 uint64_t l2_offset;
4f6ed88c 1871 int i, ret;
f7d0fe02 1872
cd6efd60
VSO
1873 if (!l1_size) {
1874 return 0;
1875 }
f7d0fe02
KW
1876
1877 /* Mark L1 table as used */
8a5bb1f1 1878 ret = qcow2_inc_refcounts_imrt(bs, res, refcount_table, refcount_table_size,
cd6efd60 1879 l1_table_offset, l1_size_bytes);
fef4d3d5 1880 if (ret < 0) {
cd6efd60
VSO
1881 return ret;
1882 }
1883
1884 l1_table = g_try_malloc(l1_size_bytes);
1885 if (l1_table == NULL) {
1886 res->check_errors++;
1887 return -ENOMEM;
fef4d3d5 1888 }
f7d0fe02
KW
1889
1890 /* Read L1 table entries from disk */
32cc71de 1891 ret = bdrv_pread(bs->file, l1_table_offset, l1_size_bytes, l1_table, 0);
cd6efd60
VSO
1892 if (ret < 0) {
1893 fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
1894 res->check_errors++;
1895 return ret;
1896 }
1897
1898 for (i = 0; i < l1_size; i++) {
1899 be64_to_cpus(&l1_table[i]);
702ef63f 1900 }
f7d0fe02
KW
1901
1902 /* Do the actual checks */
cd6efd60
VSO
1903 for (i = 0; i < l1_size; i++) {
1904 if (!l1_table[i]) {
1905 continue;
1906 }
f7d0fe02 1907
98bc07d6
VSO
1908 if (l1_table[i] & L1E_RESERVED_MASK) {
1909 fprintf(stderr, "ERROR found L1 entry with reserved bits set: "
1910 "%" PRIx64 "\n", l1_table[i]);
1911 res->corruptions++;
1912 }
1913
cd6efd60 1914 l2_offset = l1_table[i] & L1E_OFFSET_MASK;
f7d0fe02 1915
cd6efd60
VSO
1916 /* Mark L2 table as used */
1917 ret = qcow2_inc_refcounts_imrt(bs, res,
1918 refcount_table, refcount_table_size,
1919 l2_offset, s->cluster_size);
1920 if (ret < 0) {
1921 return ret;
1922 }
1923
1924 /* L2 tables are cluster aligned */
1925 if (offset_into_cluster(s, l2_offset)) {
1926 fprintf(stderr, "ERROR l2_offset=%" PRIx64 ": Table is not "
1927 "cluster aligned; L1 entry corrupted\n", l2_offset);
1928 res->corruptions++;
1929 }
1930
1931 /* Process and check L2 entries */
1932 ret = check_refcounts_l2(bs, res, refcount_table,
1933 refcount_table_size, l2_offset, flags,
1934 fix, active);
1935 if (ret < 0) {
1936 return ret;
f7d0fe02
KW
1937 }
1938 }
f7d0fe02 1939
cd6efd60 1940 return 0;
f7d0fe02
KW
1941}
1942
4f6ed88c
HR
1943/*
1944 * Checks the OFLAG_COPIED flag for all L1 and L2 entries.
1945 *
1946 * This function does not print an error message nor does it increment
44751917
HR
1947 * check_errors if qcow2_get_refcount fails (this is because such an error will
1948 * have been already detected and sufficiently signaled by the calling function
4f6ed88c
HR
1949 * (qcow2_check_refcounts) by the time this function is called).
1950 */
e23e400e
HR
1951static int check_oflag_copied(BlockDriverState *bs, BdrvCheckResult *res,
1952 BdrvCheckMode fix)
4f6ed88c 1953{
ff99129a 1954 BDRVQcow2State *s = bs->opaque;
4f6ed88c
HR
1955 uint64_t *l2_table = qemu_blockalign(bs, s->cluster_size);
1956 int ret;
0e06528e 1957 uint64_t refcount;
4f6ed88c 1958 int i, j;
3cce51c9
HR
1959 bool repair;
1960
1961 if (fix & BDRV_FIX_ERRORS) {
1962 /* Always repair */
1963 repair = true;
1964 } else if (fix & BDRV_FIX_LEAKS) {
1965 /* Repair only if that seems safe: This function is always
1966 * called after the refcounts have been fixed, so the refcount
1967 * is accurate if that repair was successful */
1968 repair = !res->check_errors && !res->corruptions && !res->leaks;
1969 } else {
1970 repair = false;
1971 }
4f6ed88c
HR
1972
1973 for (i = 0; i < s->l1_size; i++) {
1974 uint64_t l1_entry = s->l1_table[i];
1975 uint64_t l2_offset = l1_entry & L1E_OFFSET_MASK;
7e3e736c 1976 int l2_dirty = 0;
4f6ed88c
HR
1977
1978 if (!l2_offset) {
1979 continue;
1980 }
1981
7324c10f
HR
1982 ret = qcow2_get_refcount(bs, l2_offset >> s->cluster_bits,
1983 &refcount);
1984 if (ret < 0) {
4f6ed88c
HR
1985 /* don't print message nor increment check_errors */
1986 continue;
1987 }
1988 if ((refcount == 1) != ((l1_entry & QCOW_OFLAG_COPIED) != 0)) {
54b10010 1989 res->corruptions++;
e23e400e 1990 fprintf(stderr, "%s OFLAG_COPIED L2 cluster: l1_index=%d "
0e06528e 1991 "l1_entry=%" PRIx64 " refcount=%" PRIu64 "\n",
3cce51c9
HR
1992 repair ? "Repairing" : "ERROR", i, l1_entry, refcount);
1993 if (repair) {
e23e400e
HR
1994 s->l1_table[i] = refcount == 1
1995 ? l1_entry | QCOW_OFLAG_COPIED
1996 : l1_entry & ~QCOW_OFLAG_COPIED;
1997 ret = qcow2_write_l1_entry(bs, i);
1998 if (ret < 0) {
1999 res->check_errors++;
2000 goto fail;
2001 }
54b10010 2002 res->corruptions--;
e23e400e 2003 res->corruptions_fixed++;
e23e400e 2004 }
4f6ed88c
HR
2005 }
2006
32cc71de
AF
2007 ret = bdrv_pread(bs->file, l2_offset, s->l2_size * l2_entry_size(s),
2008 l2_table, 0);
4f6ed88c
HR
2009 if (ret < 0) {
2010 fprintf(stderr, "ERROR: Could not read L2 table: %s\n",
2011 strerror(-ret));
2012 res->check_errors++;
2013 goto fail;
2014 }
2015
2016 for (j = 0; j < s->l2_size; j++) {
12c6aebe 2017 uint64_t l2_entry = get_l2_entry(s, l2_table, j);
4f6ed88c 2018 uint64_t data_offset = l2_entry & L2E_OFFSET_MASK;
808c2bb4 2019 QCow2ClusterType cluster_type = qcow2_get_cluster_type(bs, l2_entry);
4f6ed88c 2020
fdfab37d
EB
2021 if (cluster_type == QCOW2_CLUSTER_NORMAL ||
2022 cluster_type == QCOW2_CLUSTER_ZERO_ALLOC) {
e9f5b6de
KW
2023 if (has_data_file(bs)) {
2024 refcount = 1;
2025 } else {
2026 ret = qcow2_get_refcount(bs,
2027 data_offset >> s->cluster_bits,
2028 &refcount);
2029 if (ret < 0) {
2030 /* don't print message nor increment check_errors */
2031 continue;
2032 }
4f6ed88c
HR
2033 }
2034 if ((refcount == 1) != ((l2_entry & QCOW_OFLAG_COPIED) != 0)) {
54b10010 2035 res->corruptions++;
e23e400e 2036 fprintf(stderr, "%s OFLAG_COPIED data cluster: "
0e06528e 2037 "l2_entry=%" PRIx64 " refcount=%" PRIu64 "\n",
3cce51c9
HR
2038 repair ? "Repairing" : "ERROR", l2_entry, refcount);
2039 if (repair) {
12c6aebe
AG
2040 set_l2_entry(s, l2_table, j,
2041 refcount == 1 ?
2042 l2_entry | QCOW_OFLAG_COPIED :
2043 l2_entry & ~QCOW_OFLAG_COPIED);
7e3e736c 2044 l2_dirty++;
e23e400e 2045 }
4f6ed88c
HR
2046 }
2047 }
2048 }
e23e400e 2049
7e3e736c 2050 if (l2_dirty > 0) {
231bb267 2051 ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L2,
966b000f
KW
2052 l2_offset, s->cluster_size,
2053 false);
e23e400e
HR
2054 if (ret < 0) {
2055 fprintf(stderr, "ERROR: Could not write L2 table; metadata "
2056 "overlap check failed: %s\n", strerror(-ret));
2057 res->check_errors++;
2058 goto fail;
2059 }
2060
32cc71de 2061 ret = bdrv_pwrite(bs->file, l2_offset, s->cluster_size, l2_table,
53fb7844 2062 0);
e23e400e
HR
2063 if (ret < 0) {
2064 fprintf(stderr, "ERROR: Could not write L2 table: %s\n",
2065 strerror(-ret));
2066 res->check_errors++;
2067 goto fail;
2068 }
54b10010 2069 res->corruptions -= l2_dirty;
7e3e736c 2070 res->corruptions_fixed += l2_dirty;
e23e400e 2071 }
4f6ed88c
HR
2072 }
2073
2074 ret = 0;
2075
2076fail:
2077 qemu_vfree(l2_table);
2078 return ret;
2079}
2080
6ca56bf5
HR
2081/*
2082 * Checks consistency of refblocks and accounts for each refblock in
2083 * *refcount_table.
2084 */
2085static int check_refblocks(BlockDriverState *bs, BdrvCheckResult *res,
f307b255 2086 BdrvCheckMode fix, bool *rebuild,
7453c96b 2087 void **refcount_table, int64_t *nb_clusters)
6ca56bf5 2088{
ff99129a 2089 BDRVQcow2State *s = bs->opaque;
001c158d 2090 int64_t i, size;
fef4d3d5 2091 int ret;
6ca56bf5 2092
f7d0fe02 2093 for(i = 0; i < s->refcount_table_size; i++) {
6882c8fa 2094 uint64_t offset, cluster;
8fba3951 2095 offset = s->refcount_table[i] & REFT_OFFSET_MASK;
6882c8fa 2096 cluster = offset >> s->cluster_bits;
746c3cb5 2097
8fba3951
VSO
2098 if (s->refcount_table[i] & REFT_RESERVED_MASK) {
2099 fprintf(stderr, "ERROR refcount table entry %" PRId64 " has "
2100 "reserved bits set\n", i);
2101 res->corruptions++;
2102 *rebuild = true;
2103 continue;
2104 }
2105
746c3cb5 2106 /* Refcount blocks are cluster aligned */
ac95acdb 2107 if (offset_into_cluster(s, offset)) {
166acf54 2108 fprintf(stderr, "ERROR refcount block %" PRId64 " is not "
746c3cb5 2109 "cluster aligned; refcount table entry corrupted\n", i);
9ac228e0 2110 res->corruptions++;
f307b255 2111 *rebuild = true;
6882c8fa
KW
2112 continue;
2113 }
2114
6ca56bf5 2115 if (cluster >= *nb_clusters) {
54b10010 2116 res->corruptions++;
001c158d
HR
2117 fprintf(stderr, "%s refcount block %" PRId64 " is outside image\n",
2118 fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR", i);
2119
2120 if (fix & BDRV_FIX_ERRORS) {
5fee192e 2121 int64_t new_nb_clusters;
ed3d2ec9 2122 Error *local_err = NULL;
001c158d
HR
2123
2124 if (offset > INT64_MAX - s->cluster_size) {
2125 ret = -EINVAL;
2126 goto resize_fail;
2127 }
2128
c80d8b06 2129 ret = bdrv_truncate(bs->file, offset + s->cluster_size, false,
7b8e4857 2130 PREALLOC_MODE_OFF, 0, &local_err);
001c158d 2131 if (ret < 0) {
ed3d2ec9 2132 error_report_err(local_err);
001c158d
HR
2133 goto resize_fail;
2134 }
9a4f4c31 2135 size = bdrv_getlength(bs->file->bs);
001c158d
HR
2136 if (size < 0) {
2137 ret = size;
2138 goto resize_fail;
2139 }
2140
5fee192e
HR
2141 new_nb_clusters = size_to_clusters(s, size);
2142 assert(new_nb_clusters >= *nb_clusters);
001c158d 2143
5fee192e
HR
2144 ret = realloc_refcount_array(s, refcount_table,
2145 nb_clusters, new_nb_clusters);
2146 if (ret < 0) {
001c158d 2147 res->check_errors++;
5fee192e 2148 return ret;
001c158d 2149 }
001c158d
HR
2150
2151 if (cluster >= *nb_clusters) {
2152 ret = -EINVAL;
2153 goto resize_fail;
2154 }
2155
54b10010 2156 res->corruptions--;
001c158d 2157 res->corruptions_fixed++;
8a5bb1f1
VSO
2158 ret = qcow2_inc_refcounts_imrt(bs, res,
2159 refcount_table, nb_clusters,
2160 offset, s->cluster_size);
001c158d
HR
2161 if (ret < 0) {
2162 return ret;
2163 }
2164 /* No need to check whether the refcount is now greater than 1:
2165 * This area was just allocated and zeroed, so it can only be
8a5bb1f1 2166 * exactly 1 after qcow2_inc_refcounts_imrt() */
001c158d
HR
2167 continue;
2168
2169resize_fail:
f307b255 2170 *rebuild = true;
001c158d
HR
2171 fprintf(stderr, "ERROR could not resize image: %s\n",
2172 strerror(-ret));
001c158d 2173 }
6882c8fa 2174 continue;
746c3cb5
KW
2175 }
2176
f7d0fe02 2177 if (offset != 0) {
8a5bb1f1
VSO
2178 ret = qcow2_inc_refcounts_imrt(bs, res, refcount_table, nb_clusters,
2179 offset, s->cluster_size);
fef4d3d5
HR
2180 if (ret < 0) {
2181 return ret;
2182 }
7453c96b 2183 if (s->get_refcount(*refcount_table, cluster) != 1) {
f307b255 2184 fprintf(stderr, "ERROR refcount block %" PRId64
7453c96b
HR
2185 " refcount=%" PRIu64 "\n", i,
2186 s->get_refcount(*refcount_table, cluster));
f307b255
HR
2187 res->corruptions++;
2188 *rebuild = true;
746c3cb5 2189 }
f7d0fe02
KW
2190 }
2191 }
2192
6ca56bf5
HR
2193 return 0;
2194}
2195
057a3fe5
HR
2196/*
2197 * Calculates an in-memory refcount table.
2198 */
2199static int calculate_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
f307b255 2200 BdrvCheckMode fix, bool *rebuild,
7453c96b 2201 void **refcount_table, int64_t *nb_clusters)
057a3fe5 2202{
ff99129a 2203 BDRVQcow2State *s = bs->opaque;
057a3fe5
HR
2204 int64_t i;
2205 QCowSnapshot *sn;
2206 int ret;
2207
9696df21 2208 if (!*refcount_table) {
5fee192e
HR
2209 int64_t old_size = 0;
2210 ret = realloc_refcount_array(s, refcount_table,
2211 &old_size, *nb_clusters);
2212 if (ret < 0) {
9696df21 2213 res->check_errors++;
5fee192e 2214 return ret;
9696df21 2215 }
057a3fe5
HR
2216 }
2217
2218 /* header */
8a5bb1f1
VSO
2219 ret = qcow2_inc_refcounts_imrt(bs, res, refcount_table, nb_clusters,
2220 0, s->cluster_size);
fef4d3d5
HR
2221 if (ret < 0) {
2222 return ret;
2223 }
057a3fe5
HR
2224
2225 /* current L1 table */
641bb63c 2226 ret = check_refcounts_l1(bs, res, refcount_table, nb_clusters,
ac5b787a 2227 s->l1_table_offset, s->l1_size, CHECK_FRAG_INFO,
1ef337b7 2228 fix, true);
057a3fe5
HR
2229 if (ret < 0) {
2230 return ret;
2231 }
2232
2233 /* snapshots */
e9f5b6de
KW
2234 if (has_data_file(bs) && s->nb_snapshots) {
2235 fprintf(stderr, "ERROR %d snapshots in image with data file\n",
2236 s->nb_snapshots);
2237 res->corruptions++;
2238 }
2239
057a3fe5
HR
2240 for (i = 0; i < s->nb_snapshots; i++) {
2241 sn = s->snapshots + i;
0c2ada81
AG
2242 if (offset_into_cluster(s, sn->l1_table_offset)) {
2243 fprintf(stderr, "ERROR snapshot %s (%s) l1_offset=%#" PRIx64 ": "
2244 "L1 table is not cluster aligned; snapshot table entry "
2245 "corrupted\n", sn->id_str, sn->name, sn->l1_table_offset);
2246 res->corruptions++;
2247 continue;
2248 }
02b1ecfa 2249 if (sn->l1_size > QCOW_MAX_L1_SIZE / L1E_SIZE) {
0c2ada81
AG
2250 fprintf(stderr, "ERROR snapshot %s (%s) l1_size=%#" PRIx32 ": "
2251 "L1 table is too large; snapshot table entry corrupted\n",
2252 sn->id_str, sn->name, sn->l1_size);
2253 res->corruptions++;
2254 continue;
2255 }
641bb63c 2256 ret = check_refcounts_l1(bs, res, refcount_table, nb_clusters,
1ef337b7
VSO
2257 sn->l1_table_offset, sn->l1_size, 0, fix,
2258 false);
057a3fe5
HR
2259 if (ret < 0) {
2260 return ret;
2261 }
2262 }
8a5bb1f1
VSO
2263 ret = qcow2_inc_refcounts_imrt(bs, res, refcount_table, nb_clusters,
2264 s->snapshots_offset, s->snapshots_size);
fef4d3d5
HR
2265 if (ret < 0) {
2266 return ret;
2267 }
057a3fe5
HR
2268
2269 /* refcount data */
8a5bb1f1
VSO
2270 ret = qcow2_inc_refcounts_imrt(bs, res, refcount_table, nb_clusters,
2271 s->refcount_table_offset,
02b1ecfa
AG
2272 s->refcount_table_size *
2273 REFTABLE_ENTRY_SIZE);
fef4d3d5
HR
2274 if (ret < 0) {
2275 return ret;
2276 }
057a3fe5 2277
4652b8f3
DB
2278 /* encryption */
2279 if (s->crypto_header.length) {
8a5bb1f1
VSO
2280 ret = qcow2_inc_refcounts_imrt(bs, res, refcount_table, nb_clusters,
2281 s->crypto_header.offset,
2282 s->crypto_header.length);
4652b8f3
DB
2283 if (ret < 0) {
2284 return ret;
2285 }
2286 }
2287
88ddffae
VSO
2288 /* bitmaps */
2289 ret = qcow2_check_bitmaps_refcounts(bs, res, refcount_table, nb_clusters);
2290 if (ret < 0) {
2291 return ret;
2292 }
2293
f307b255 2294 return check_refblocks(bs, res, fix, rebuild, refcount_table, nb_clusters);
057a3fe5
HR
2295}
2296
6ca56bf5
HR
2297/*
2298 * Compares the actual reference count for each cluster in the image against the
2299 * refcount as reported by the refcount structures on-disk.
2300 */
2301static void compare_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
f307b255
HR
2302 BdrvCheckMode fix, bool *rebuild,
2303 int64_t *highest_cluster,
7453c96b 2304 void *refcount_table, int64_t nb_clusters)
6ca56bf5 2305{
ff99129a 2306 BDRVQcow2State *s = bs->opaque;
6ca56bf5 2307 int64_t i;
0e06528e 2308 uint64_t refcount1, refcount2;
7324c10f 2309 int ret;
6ca56bf5
HR
2310
2311 for (i = 0, *highest_cluster = 0; i < nb_clusters; i++) {
7324c10f
HR
2312 ret = qcow2_get_refcount(bs, i, &refcount1);
2313 if (ret < 0) {
166acf54 2314 fprintf(stderr, "Can't get refcount for cluster %" PRId64 ": %s\n",
7324c10f 2315 i, strerror(-ret));
9ac228e0 2316 res->check_errors++;
f74550fd 2317 continue;
018faafd
KW
2318 }
2319
7453c96b 2320 refcount2 = s->get_refcount(refcount_table, i);
c6bb9ad1
FS
2321
2322 if (refcount1 > 0 || refcount2 > 0) {
6ca56bf5 2323 *highest_cluster = i;
c6bb9ad1
FS
2324 }
2325
f7d0fe02 2326 if (refcount1 != refcount2) {
166acf54
KW
2327 /* Check if we're allowed to fix the mismatch */
2328 int *num_fixed = NULL;
f307b255
HR
2329 if (refcount1 == 0) {
2330 *rebuild = true;
2331 } else if (refcount1 > refcount2 && (fix & BDRV_FIX_LEAKS)) {
166acf54
KW
2332 num_fixed = &res->leaks_fixed;
2333 } else if (refcount1 < refcount2 && (fix & BDRV_FIX_ERRORS)) {
2334 num_fixed = &res->corruptions_fixed;
2335 }
2336
0e06528e
HR
2337 fprintf(stderr, "%s cluster %" PRId64 " refcount=%" PRIu64
2338 " reference=%" PRIu64 "\n",
166acf54
KW
2339 num_fixed != NULL ? "Repairing" :
2340 refcount1 < refcount2 ? "ERROR" :
2341 "Leaked",
f7d0fe02 2342 i, refcount1, refcount2);
166acf54
KW
2343
2344 if (num_fixed) {
2345 ret = update_refcount(bs, i << s->cluster_bits, 1,
2aabe7c7
HR
2346 refcount_diff(refcount1, refcount2),
2347 refcount1 > refcount2,
6cfcb9b8 2348 QCOW2_DISCARD_ALWAYS);
166acf54
KW
2349 if (ret >= 0) {
2350 (*num_fixed)++;
2351 continue;
2352 }
2353 }
2354
2355 /* And if we couldn't, print an error */
9ac228e0
KW
2356 if (refcount1 < refcount2) {
2357 res->corruptions++;
2358 } else {
2359 res->leaks++;
2360 }
f7d0fe02
KW
2361 }
2362 }
6ca56bf5
HR
2363}
2364
c7c0681b
HR
2365/*
2366 * Allocates clusters using an in-memory refcount table (IMRT) in contrast to
2367 * the on-disk refcount structures.
2368 *
2369 * On input, *first_free_cluster tells where to start looking, and need not
2370 * actually be a free cluster; the returned offset will not be before that
2371 * cluster. On output, *first_free_cluster points to the first gap found, even
2372 * if that gap was too small to be used as the returned offset.
2373 *
2374 * Note that *first_free_cluster is a cluster index whereas the return value is
2375 * an offset.
2376 */
2377static int64_t alloc_clusters_imrt(BlockDriverState *bs,
2378 int cluster_count,
7453c96b 2379 void **refcount_table,
c7c0681b
HR
2380 int64_t *imrt_nb_clusters,
2381 int64_t *first_free_cluster)
2382{
ff99129a 2383 BDRVQcow2State *s = bs->opaque;
c7c0681b
HR
2384 int64_t cluster = *first_free_cluster, i;
2385 bool first_gap = true;
2386 int contiguous_free_clusters;
5fee192e 2387 int ret;
c7c0681b
HR
2388
2389 /* Starting at *first_free_cluster, find a range of at least cluster_count
2390 * continuously free clusters */
2391 for (contiguous_free_clusters = 0;
2392 cluster < *imrt_nb_clusters &&
2393 contiguous_free_clusters < cluster_count;
2394 cluster++)
2395 {
7453c96b 2396 if (!s->get_refcount(*refcount_table, cluster)) {
c7c0681b
HR
2397 contiguous_free_clusters++;
2398 if (first_gap) {
2399 /* If this is the first free cluster found, update
2400 * *first_free_cluster accordingly */
2401 *first_free_cluster = cluster;
2402 first_gap = false;
2403 }
2404 } else if (contiguous_free_clusters) {
2405 contiguous_free_clusters = 0;
2406 }
2407 }
2408
2409 /* If contiguous_free_clusters is greater than zero, it contains the number
2410 * of continuously free clusters until the current cluster; the first free
2411 * cluster in the current "gap" is therefore
2412 * cluster - contiguous_free_clusters */
2413
2414 /* If no such range could be found, grow the in-memory refcount table
2415 * accordingly to append free clusters at the end of the image */
2416 if (contiguous_free_clusters < cluster_count) {
c7c0681b
HR
2417 /* contiguous_free_clusters clusters are already empty at the image end;
2418 * we need cluster_count clusters; therefore, we have to allocate
2419 * cluster_count - contiguous_free_clusters new clusters at the end of
2420 * the image (which is the current value of cluster; note that cluster
2421 * may exceed old_imrt_nb_clusters if *first_free_cluster pointed beyond
2422 * the image end) */
5fee192e
HR
2423 ret = realloc_refcount_array(s, refcount_table, imrt_nb_clusters,
2424 cluster + cluster_count
2425 - contiguous_free_clusters);
2426 if (ret < 0) {
2427 return ret;
c7c0681b 2428 }
c7c0681b
HR
2429 }
2430
2431 /* Go back to the first free cluster */
2432 cluster -= contiguous_free_clusters;
2433 for (i = 0; i < cluster_count; i++) {
7453c96b 2434 s->set_refcount(*refcount_table, cluster + i, 1);
c7c0681b
HR
2435 }
2436
2437 return cluster << s->cluster_bits;
2438}
2439
2440/*
a8c07ec2 2441 * Helper function for rebuild_refcount_structure().
c7c0681b 2442 *
a8c07ec2
HR
2443 * Scan the range of clusters [first_cluster, end_cluster) for allocated
2444 * clusters and write all corresponding refblocks to disk. The refblock
2445 * and allocation data is taken from the in-memory refcount table
2446 * *refcount_table[] (of size *nb_clusters), which is basically one big
2447 * (unlimited size) refblock for the whole image.
2448 *
2449 * For these refblocks, clusters are allocated using said in-memory
2450 * refcount table. Care is taken that these allocations are reflected
2451 * in the refblocks written to disk.
2452 *
2453 * The refblocks' offsets are written into a reftable, which is
2454 * *on_disk_reftable_ptr[] (of size *on_disk_reftable_entries_ptr). If
2455 * that reftable is of insufficient size, it will be resized to fit.
2456 * This reftable is not written to disk.
2457 *
2458 * (If *on_disk_reftable_ptr is not NULL, the entries within are assumed
2459 * to point to existing valid refblocks that do not need to be allocated
2460 * again.)
2461 *
2462 * Return whether the on-disk reftable array was resized (true/false),
2463 * or -errno on error.
c7c0681b 2464 */
a8c07ec2
HR
2465static int rebuild_refcounts_write_refblocks(
2466 BlockDriverState *bs, void **refcount_table, int64_t *nb_clusters,
2467 int64_t first_cluster, int64_t end_cluster,
0423f753
HR
2468 uint64_t **on_disk_reftable_ptr, uint32_t *on_disk_reftable_entries_ptr,
2469 Error **errp
a8c07ec2 2470 )
c7c0681b 2471{
ff99129a 2472 BDRVQcow2State *s = bs->opaque;
a8c07ec2 2473 int64_t cluster;
c7c0681b 2474 int64_t refblock_offset, refblock_start, refblock_index;
a8c07ec2
HR
2475 int64_t first_free_cluster = 0;
2476 uint64_t *on_disk_reftable = *on_disk_reftable_ptr;
2477 uint32_t on_disk_reftable_entries = *on_disk_reftable_entries_ptr;
7453c96b 2478 void *on_disk_refblock;
a8c07ec2
HR
2479 bool reftable_grown = false;
2480 int ret;
c7c0681b 2481
a8c07ec2
HR
2482 for (cluster = first_cluster; cluster < end_cluster; cluster++) {
2483 /* Check all clusters to find refblocks that contain non-zero entries */
7453c96b 2484 if (!s->get_refcount(*refcount_table, cluster)) {
c7c0681b
HR
2485 continue;
2486 }
2487
a8c07ec2
HR
2488 /*
2489 * This cluster is allocated, so we need to create a refblock
2490 * for it. The data we will write to disk is just the
2491 * respective slice from *refcount_table, so it will contain
2492 * accurate refcounts for all clusters belonging to this
2493 * refblock. After we have written it, we will therefore skip
2494 * all remaining clusters in this refblock.
2495 */
2496
c7c0681b
HR
2497 refblock_index = cluster >> s->refcount_block_bits;
2498 refblock_start = refblock_index << s->refcount_block_bits;
2499
a8c07ec2
HR
2500 if (on_disk_reftable_entries > refblock_index &&
2501 on_disk_reftable[refblock_index])
2502 {
2503 /*
2504 * We can get here after a `goto write_refblocks`: We have a
2505 * reftable from a previous run, and the refblock is already
2506 * allocated. No need to allocate it again.
2507 */
2508 refblock_offset = on_disk_reftable[refblock_index];
2509 } else {
2510 int64_t refblock_cluster_index;
c7c0681b 2511
a8c07ec2
HR
2512 /* Don't allocate a cluster in a refblock already written to disk */
2513 if (first_free_cluster < refblock_start) {
2514 first_free_cluster = refblock_start;
2515 }
2516 refblock_offset = alloc_clusters_imrt(bs, 1, refcount_table,
2517 nb_clusters,
2518 &first_free_cluster);
2519 if (refblock_offset < 0) {
0423f753
HR
2520 error_setg_errno(errp, -refblock_offset,
2521 "ERROR allocating refblock");
a8c07ec2
HR
2522 return refblock_offset;
2523 }
c7c0681b 2524
a8c07ec2
HR
2525 refblock_cluster_index = refblock_offset / s->cluster_size;
2526 if (refblock_cluster_index >= end_cluster) {
2527 /*
2528 * We must write the refblock that holds this refblock's
2529 * refcount
2530 */
2531 end_cluster = refblock_cluster_index + 1;
c7c0681b 2532 }
c7c0681b 2533
a8c07ec2
HR
2534 if (on_disk_reftable_entries <= refblock_index) {
2535 on_disk_reftable_entries =
2536 ROUND_UP((refblock_index + 1) * REFTABLE_ENTRY_SIZE,
2537 s->cluster_size) / REFTABLE_ENTRY_SIZE;
2538 on_disk_reftable =
2539 g_try_realloc(on_disk_reftable,
2540 on_disk_reftable_entries *
2541 REFTABLE_ENTRY_SIZE);
2542 if (!on_disk_reftable) {
0423f753 2543 error_setg(errp, "ERROR allocating reftable memory");
a8c07ec2
HR
2544 return -ENOMEM;
2545 }
c7c0681b 2546
a8c07ec2
HR
2547 memset(on_disk_reftable + *on_disk_reftable_entries_ptr, 0,
2548 (on_disk_reftable_entries -
2549 *on_disk_reftable_entries_ptr) *
2550 REFTABLE_ENTRY_SIZE);
c7c0681b 2551
a8c07ec2
HR
2552 *on_disk_reftable_ptr = on_disk_reftable;
2553 *on_disk_reftable_entries_ptr = on_disk_reftable_entries;
2554
2555 reftable_grown = true;
2556 } else {
2557 assert(on_disk_reftable);
c7c0681b 2558 }
a8c07ec2 2559 on_disk_reftable[refblock_index] = refblock_offset;
c7c0681b
HR
2560 }
2561
a8c07ec2
HR
2562 /* Refblock is allocated, write it to disk */
2563
c7c0681b 2564 ret = qcow2_pre_write_overlap_check(bs, 0, refblock_offset,
966b000f 2565 s->cluster_size, false);
c7c0681b 2566 if (ret < 0) {
0423f753 2567 error_setg_errno(errp, -ret, "ERROR writing refblock");
a8c07ec2 2568 return ret;
c7c0681b
HR
2569 }
2570
a8c07ec2
HR
2571 /*
2572 * The refblock is simply a slice of *refcount_table.
2573 * Note that the size of *refcount_table is always aligned to
2574 * whole clusters, so the write operation will not result in
2575 * out-of-bounds accesses.
2576 */
7453c96b
HR
2577 on_disk_refblock = (void *)((char *) *refcount_table +
2578 refblock_index * s->cluster_size);
c7c0681b 2579
32cc71de
AF
2580 ret = bdrv_pwrite(bs->file, refblock_offset, s->cluster_size,
2581 on_disk_refblock, 0);
c7c0681b 2582 if (ret < 0) {
0423f753 2583 error_setg_errno(errp, -ret, "ERROR writing refblock");
a8c07ec2 2584 return ret;
c7c0681b
HR
2585 }
2586
a8c07ec2 2587 /* This refblock is done, skip to its end */
c7c0681b
HR
2588 cluster = refblock_start + s->refcount_block_size - 1;
2589 }
2590
a8c07ec2
HR
2591 return reftable_grown;
2592}
2593
2594/*
2595 * Creates a new refcount structure based solely on the in-memory information
2596 * given through *refcount_table (this in-memory information is basically just
2597 * the concatenation of all refblocks). All necessary allocations will be
2598 * reflected in that array.
2599 *
2600 * On success, the old refcount structure is leaked (it will be covered by the
2601 * new refcount structure).
2602 */
2603static int rebuild_refcount_structure(BlockDriverState *bs,
2604 BdrvCheckResult *res,
2605 void **refcount_table,
0423f753
HR
2606 int64_t *nb_clusters,
2607 Error **errp)
a8c07ec2
HR
2608{
2609 BDRVQcow2State *s = bs->opaque;
2610 int64_t reftable_offset = -1;
2611 int64_t reftable_length = 0;
2612 int64_t reftable_clusters;
2613 int64_t refblock_index;
2614 uint32_t on_disk_reftable_entries = 0;
2615 uint64_t *on_disk_reftable = NULL;
2616 int ret = 0;
2617 int reftable_size_changed = 0;
2618 struct {
2619 uint64_t reftable_offset;
2620 uint32_t reftable_clusters;
2621 } QEMU_PACKED reftable_offset_and_clusters;
2622
2623 qcow2_cache_empty(bs, s->refcount_block_cache);
2624
2625 /*
2626 * For each refblock containing entries, we try to allocate a
2627 * cluster (in the in-memory refcount table) and write its offset
2628 * into on_disk_reftable[]. We then write the whole refblock to
2629 * disk (as a slice of the in-memory refcount table).
2630 * This is done by rebuild_refcounts_write_refblocks().
2631 *
2632 * Once we have scanned all clusters, we try to find space for the
2633 * reftable. This will dirty the in-memory refcount table (i.e.
2634 * make it differ from the refblocks we have already written), so we
2635 * need to run rebuild_refcounts_write_refblocks() again for the
2636 * range of clusters where the reftable has been allocated.
2637 *
2638 * This second run might make the reftable grow again, in which case
2639 * we will need to allocate another space for it, which is why we
2640 * repeat all this until the reftable stops growing.
2641 *
2642 * (This loop will terminate, because with every cluster the
2643 * reftable grows, it can accomodate a multitude of more refcounts,
2644 * so that at some point this must be able to cover the reftable
2645 * and all refblocks describing it.)
2646 *
2647 * We then convert the reftable to big-endian and write it to disk.
2648 *
2649 * Note that we never free any reftable allocations. Doing so would
2650 * needlessly complicate the algorithm: The eventual second check
2651 * run we do will clean up all leaks we have caused.
2652 */
2653
2654 reftable_size_changed =
2655 rebuild_refcounts_write_refblocks(bs, refcount_table, nb_clusters,
2656 0, *nb_clusters,
2657 &on_disk_reftable,
0423f753 2658 &on_disk_reftable_entries, errp);
a8c07ec2
HR
2659 if (reftable_size_changed < 0) {
2660 res->check_errors++;
2661 ret = reftable_size_changed;
2662 goto fail;
2663 }
2664
2665 /*
2666 * There was no reftable before, so rebuild_refcounts_write_refblocks()
2667 * must have increased its size (from 0 to something).
2668 */
2669 assert(reftable_size_changed);
2670
2671 do {
2672 int64_t reftable_start_cluster, reftable_end_cluster;
2673 int64_t first_free_cluster = 0;
2674
2675 reftable_length = on_disk_reftable_entries * REFTABLE_ENTRY_SIZE;
2676 reftable_clusters = size_to_clusters(s, reftable_length);
c7c0681b 2677
c7c0681b
HR
2678 reftable_offset = alloc_clusters_imrt(bs, reftable_clusters,
2679 refcount_table, nb_clusters,
2680 &first_free_cluster);
2681 if (reftable_offset < 0) {
0423f753
HR
2682 error_setg_errno(errp, -reftable_offset,
2683 "ERROR allocating reftable");
c7c0681b
HR
2684 res->check_errors++;
2685 ret = reftable_offset;
2686 goto fail;
2687 }
2688
a8c07ec2
HR
2689 /*
2690 * We need to update the affected refblocks, so re-run the
2691 * write_refblocks loop for the reftable's range of clusters.
2692 */
2693 assert(offset_into_cluster(s, reftable_offset) == 0);
2694 reftable_start_cluster = reftable_offset / s->cluster_size;
2695 reftable_end_cluster = reftable_start_cluster + reftable_clusters;
2696 reftable_size_changed =
2697 rebuild_refcounts_write_refblocks(bs, refcount_table, nb_clusters,
2698 reftable_start_cluster,
2699 reftable_end_cluster,
2700 &on_disk_reftable,
0423f753 2701 &on_disk_reftable_entries, errp);
a8c07ec2
HR
2702 if (reftable_size_changed < 0) {
2703 res->check_errors++;
2704 ret = reftable_size_changed;
2705 goto fail;
2706 }
2707
2708 /*
2709 * If the reftable size has changed, we will need to find a new
2710 * allocation, repeating the loop.
2711 */
2712 } while (reftable_size_changed);
c7c0681b 2713
a8c07ec2
HR
2714 /* The above loop must have run at least once */
2715 assert(reftable_offset >= 0);
2716
2717 /*
2718 * All allocations are done, all refblocks are written, convert the
2719 * reftable to big-endian and write it to disk.
2720 */
2721
2722 for (refblock_index = 0; refblock_index < on_disk_reftable_entries;
2723 refblock_index++)
2724 {
c7c0681b
HR
2725 cpu_to_be64s(&on_disk_reftable[refblock_index]);
2726 }
2727
a8c07ec2 2728 ret = qcow2_pre_write_overlap_check(bs, 0, reftable_offset, reftable_length,
966b000f 2729 false);
c7c0681b 2730 if (ret < 0) {
0423f753 2731 error_setg_errno(errp, -ret, "ERROR writing reftable");
c7c0681b
HR
2732 goto fail;
2733 }
2734
a8c07ec2 2735 assert(reftable_length < INT_MAX);
32cc71de
AF
2736 ret = bdrv_pwrite(bs->file, reftable_offset, reftable_length,
2737 on_disk_reftable, 0);
c7c0681b 2738 if (ret < 0) {
0423f753 2739 error_setg_errno(errp, -ret, "ERROR writing reftable");
c7c0681b
HR
2740 goto fail;
2741 }
2742
2743 /* Enter new reftable into the image header */
f1f7a1dd
PM
2744 reftable_offset_and_clusters.reftable_offset = cpu_to_be64(reftable_offset);
2745 reftable_offset_and_clusters.reftable_clusters =
a8c07ec2 2746 cpu_to_be32(reftable_clusters);
d9ca2ea2
KW
2747 ret = bdrv_pwrite_sync(bs->file,
2748 offsetof(QCowHeader, refcount_table_offset),
32cc71de
AF
2749 sizeof(reftable_offset_and_clusters),
2750 &reftable_offset_and_clusters, 0);
c7c0681b 2751 if (ret < 0) {
0423f753 2752 error_setg_errno(errp, -ret, "ERROR setting reftable");
c7c0681b
HR
2753 goto fail;
2754 }
2755
a8c07ec2
HR
2756 for (refblock_index = 0; refblock_index < on_disk_reftable_entries;
2757 refblock_index++)
2758 {
c7c0681b
HR
2759 be64_to_cpus(&on_disk_reftable[refblock_index]);
2760 }
2761 s->refcount_table = on_disk_reftable;
2762 s->refcount_table_offset = reftable_offset;
a8c07ec2 2763 s->refcount_table_size = on_disk_reftable_entries;
7061a078 2764 update_max_refcount_table_index(s);
c7c0681b
HR
2765
2766 return 0;
2767
2768fail:
2769 g_free(on_disk_reftable);
2770 return ret;
2771}
2772
6ca56bf5
HR
2773/*
2774 * Checks an image for refcount consistency.
2775 *
2776 * Returns 0 if no errors are found, the number of errors in case the image is
2777 * detected as corrupted, and -errno when an internal error occurred.
2778 */
2779int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
2780 BdrvCheckMode fix)
2781{
ff99129a 2782 BDRVQcow2State *s = bs->opaque;
c7c0681b 2783 BdrvCheckResult pre_compare_res;
6ca56bf5 2784 int64_t size, highest_cluster, nb_clusters;
7453c96b 2785 void *refcount_table = NULL;
f307b255 2786 bool rebuild = false;
6ca56bf5
HR
2787 int ret;
2788
9a4f4c31 2789 size = bdrv_getlength(bs->file->bs);
6ca56bf5
HR
2790 if (size < 0) {
2791 res->check_errors++;
2792 return size;
2793 }
2794
2795 nb_clusters = size_to_clusters(s, size);
2796 if (nb_clusters > INT_MAX) {
2797 res->check_errors++;
2798 return -EFBIG;
2799 }
2800
2801 res->bfi.total_clusters =
2802 size_to_clusters(s, bs->total_sectors * BDRV_SECTOR_SIZE);
2803
f307b255
HR
2804 ret = calculate_refcounts(bs, res, fix, &rebuild, &refcount_table,
2805 &nb_clusters);
6ca56bf5
HR
2806 if (ret < 0) {
2807 goto fail;
2808 }
2809
c7c0681b
HR
2810 /* In case we don't need to rebuild the refcount structure (but want to fix
2811 * something), this function is immediately called again, in which case the
2812 * result should be ignored */
2813 pre_compare_res = *res;
2814 compare_refcounts(bs, res, 0, &rebuild, &highest_cluster, refcount_table,
6ca56bf5 2815 nb_clusters);
f7d0fe02 2816
c7c0681b 2817 if (rebuild && (fix & BDRV_FIX_ERRORS)) {
791230d8
HR
2818 BdrvCheckResult old_res = *res;
2819 int fresh_leaks = 0;
0423f753 2820 Error *local_err = NULL;
791230d8 2821
c7c0681b
HR
2822 fprintf(stderr, "Rebuilding refcount structure\n");
2823 ret = rebuild_refcount_structure(bs, res, &refcount_table,
0423f753 2824 &nb_clusters, &local_err);
c7c0681b 2825 if (ret < 0) {
0423f753 2826 error_report_err(local_err);
c7c0681b
HR
2827 goto fail;
2828 }
791230d8
HR
2829
2830 res->corruptions = 0;
2831 res->leaks = 0;
2832
2833 /* Because the old reftable has been exchanged for a new one the
2834 * references have to be recalculated */
2835 rebuild = false;
7453c96b 2836 memset(refcount_table, 0, refcount_array_byte_size(s, nb_clusters));
791230d8
HR
2837 ret = calculate_refcounts(bs, res, 0, &rebuild, &refcount_table,
2838 &nb_clusters);
2839 if (ret < 0) {
2840 goto fail;
2841 }
2842
2843 if (fix & BDRV_FIX_LEAKS) {
2844 /* The old refcount structures are now leaked, fix it; the result
2845 * can be ignored, aside from leaks which were introduced by
2846 * rebuild_refcount_structure() that could not be fixed */
2847 BdrvCheckResult saved_res = *res;
2848 *res = (BdrvCheckResult){ 0 };
2849
2850 compare_refcounts(bs, res, BDRV_FIX_LEAKS, &rebuild,
2851 &highest_cluster, refcount_table, nb_clusters);
2852 if (rebuild) {
2853 fprintf(stderr, "ERROR rebuilt refcount structure is still "
2854 "broken\n");
2855 }
2856
2857 /* Any leaks accounted for here were introduced by
2858 * rebuild_refcount_structure() because that function has created a
2859 * new refcount structure from scratch */
2860 fresh_leaks = res->leaks;
2861 *res = saved_res;
2862 }
2863
2864 if (res->corruptions < old_res.corruptions) {
2865 res->corruptions_fixed += old_res.corruptions - res->corruptions;
2866 }
2867 if (res->leaks < old_res.leaks) {
2868 res->leaks_fixed += old_res.leaks - res->leaks;
2869 }
2870 res->leaks += fresh_leaks;
c7c0681b
HR
2871 } else if (fix) {
2872 if (rebuild) {
2873 fprintf(stderr, "ERROR need to rebuild refcount structures\n");
2874 res->check_errors++;
2875 ret = -EIO;
2876 goto fail;
2877 }
2878
2879 if (res->leaks || res->corruptions) {
2880 *res = pre_compare_res;
2881 compare_refcounts(bs, res, fix, &rebuild, &highest_cluster,
2882 refcount_table, nb_clusters);
2883 }
f307b255
HR
2884 }
2885
4f6ed88c 2886 /* check OFLAG_COPIED */
e23e400e 2887 ret = check_oflag_copied(bs, res, fix);
4f6ed88c
HR
2888 if (ret < 0) {
2889 goto fail;
2890 }
2891
c6bb9ad1 2892 res->image_end_offset = (highest_cluster + 1) * s->cluster_size;
80fa3341
KW
2893 ret = 0;
2894
2895fail:
7267c094 2896 g_free(refcount_table);
f7d0fe02 2897
80fa3341 2898 return ret;
f7d0fe02
KW
2899}
2900
a40f1c2a
HR
2901#define overlaps_with(ofs, sz) \
2902 ranges_overlap(offset, size, ofs, sz)
2903
2904/*
2905 * Checks if the given offset into the image file is actually free to use by
2906 * looking for overlaps with important metadata sections (L1/L2 tables etc.),
2907 * i.e. a sanity check without relying on the refcount tables.
2908 *
231bb267
HR
2909 * The ign parameter specifies what checks not to perform (being a bitmask of
2910 * QCow2MetadataOverlap values), i.e., what sections to ignore.
a40f1c2a
HR
2911 *
2912 * Returns:
2913 * - 0 if writing to this offset will not affect the mentioned metadata
2914 * - a positive QCow2MetadataOverlap value indicating one overlapping section
2915 * - a negative value (-errno) indicating an error while performing a check,
f4649069 2916 * e.g. when bdrv_pread failed on QCOW2_OL_INACTIVE_L2
a40f1c2a 2917 */
231bb267 2918int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,
a40f1c2a
HR
2919 int64_t size)
2920{
ff99129a 2921 BDRVQcow2State *s = bs->opaque;
3e355390 2922 int chk = s->overlap_check & ~ign;
a40f1c2a
HR
2923 int i, j;
2924
2925 if (!size) {
2926 return 0;
2927 }
2928
2929 if (chk & QCOW2_OL_MAIN_HEADER) {
2930 if (offset < s->cluster_size) {
2931 return QCOW2_OL_MAIN_HEADER;
2932 }
2933 }
2934
2935 /* align range to test to cluster boundaries */
9e029689 2936 size = ROUND_UP(offset_into_cluster(s, offset) + size, s->cluster_size);
a40f1c2a
HR
2937 offset = start_of_cluster(s, offset);
2938
2939 if ((chk & QCOW2_OL_ACTIVE_L1) && s->l1_size) {
02b1ecfa 2940 if (overlaps_with(s->l1_table_offset, s->l1_size * L1E_SIZE)) {
a40f1c2a
HR
2941 return QCOW2_OL_ACTIVE_L1;
2942 }
2943 }
2944
2945 if ((chk & QCOW2_OL_REFCOUNT_TABLE) && s->refcount_table_size) {
2946 if (overlaps_with(s->refcount_table_offset,
02b1ecfa 2947 s->refcount_table_size * REFTABLE_ENTRY_SIZE)) {
a40f1c2a
HR
2948 return QCOW2_OL_REFCOUNT_TABLE;
2949 }
2950 }
2951
2952 if ((chk & QCOW2_OL_SNAPSHOT_TABLE) && s->snapshots_size) {
2953 if (overlaps_with(s->snapshots_offset, s->snapshots_size)) {
2954 return QCOW2_OL_SNAPSHOT_TABLE;
2955 }
2956 }
2957
2958 if ((chk & QCOW2_OL_INACTIVE_L1) && s->snapshots) {
2959 for (i = 0; i < s->nb_snapshots; i++) {
2960 if (s->snapshots[i].l1_size &&
2961 overlaps_with(s->snapshots[i].l1_table_offset,
02b1ecfa 2962 s->snapshots[i].l1_size * L1E_SIZE)) {
a40f1c2a
HR
2963 return QCOW2_OL_INACTIVE_L1;
2964 }
2965 }
2966 }
2967
2968 if ((chk & QCOW2_OL_ACTIVE_L2) && s->l1_table) {
2969 for (i = 0; i < s->l1_size; i++) {
2970 if ((s->l1_table[i] & L1E_OFFSET_MASK) &&
2971 overlaps_with(s->l1_table[i] & L1E_OFFSET_MASK,
2972 s->cluster_size)) {
2973 return QCOW2_OL_ACTIVE_L2;
2974 }
2975 }
2976 }
2977
2978 if ((chk & QCOW2_OL_REFCOUNT_BLOCK) && s->refcount_table) {
7061a078
AG
2979 unsigned last_entry = s->max_refcount_table_index;
2980 assert(last_entry < s->refcount_table_size);
2981 assert(last_entry + 1 == s->refcount_table_size ||
2982 (s->refcount_table[last_entry + 1] & REFT_OFFSET_MASK) == 0);
2983 for (i = 0; i <= last_entry; i++) {
a40f1c2a
HR
2984 if ((s->refcount_table[i] & REFT_OFFSET_MASK) &&
2985 overlaps_with(s->refcount_table[i] & REFT_OFFSET_MASK,
2986 s->cluster_size)) {
2987 return QCOW2_OL_REFCOUNT_BLOCK;
2988 }
2989 }
2990 }
2991
2992 if ((chk & QCOW2_OL_INACTIVE_L2) && s->snapshots) {
2993 for (i = 0; i < s->nb_snapshots; i++) {
2994 uint64_t l1_ofs = s->snapshots[i].l1_table_offset;
2995 uint32_t l1_sz = s->snapshots[i].l1_size;
02b1ecfa 2996 uint64_t l1_sz2 = l1_sz * L1E_SIZE;
c7a9d81d 2997 uint64_t *l1;
a40f1c2a
HR
2998 int ret;
2999
02b1ecfa 3000 ret = qcow2_validate_table(bs, l1_ofs, l1_sz, L1E_SIZE,
c7a9d81d
AG
3001 QCOW_MAX_L1_SIZE, "", NULL);
3002 if (ret < 0) {
3003 return ret;
3004 }
3005
3006 l1 = g_try_malloc(l1_sz2);
3007
de82815d
KW
3008 if (l1_sz2 && l1 == NULL) {
3009 return -ENOMEM;
3010 }
3011
32cc71de 3012 ret = bdrv_pread(bs->file, l1_ofs, l1_sz2, l1, 0);
a40f1c2a
HR
3013 if (ret < 0) {
3014 g_free(l1);
3015 return ret;
3016 }
3017
3018 for (j = 0; j < l1_sz; j++) {
1e242b55
HR
3019 uint64_t l2_ofs = be64_to_cpu(l1[j]) & L1E_OFFSET_MASK;
3020 if (l2_ofs && overlaps_with(l2_ofs, s->cluster_size)) {
a40f1c2a
HR
3021 g_free(l1);
3022 return QCOW2_OL_INACTIVE_L2;
3023 }
3024 }
3025
3026 g_free(l1);
3027 }
3028 }
3029
0e4e4318
VSO
3030 if ((chk & QCOW2_OL_BITMAP_DIRECTORY) &&
3031 (s->autoclear_features & QCOW2_AUTOCLEAR_BITMAPS))
3032 {
3033 if (overlaps_with(s->bitmap_directory_offset,
3034 s->bitmap_directory_size))
3035 {
3036 return QCOW2_OL_BITMAP_DIRECTORY;
3037 }
3038 }
3039
a40f1c2a
HR
3040 return 0;
3041}
3042
3043static const char *metadata_ol_names[] = {
7cb6d3c9
LM
3044 [QCOW2_OL_MAIN_HEADER_BITNR] = "qcow2_header",
3045 [QCOW2_OL_ACTIVE_L1_BITNR] = "active L1 table",
3046 [QCOW2_OL_ACTIVE_L2_BITNR] = "active L2 table",
3047 [QCOW2_OL_REFCOUNT_TABLE_BITNR] = "refcount table",
3048 [QCOW2_OL_REFCOUNT_BLOCK_BITNR] = "refcount block",
3049 [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = "snapshot table",
3050 [QCOW2_OL_INACTIVE_L1_BITNR] = "inactive L1 table",
3051 [QCOW2_OL_INACTIVE_L2_BITNR] = "inactive L2 table",
3052 [QCOW2_OL_BITMAP_DIRECTORY_BITNR] = "bitmap directory",
a40f1c2a 3053};
7cb6d3c9 3054QEMU_BUILD_BUG_ON(QCOW2_OL_MAX_BITNR != ARRAY_SIZE(metadata_ol_names));
a40f1c2a
HR
3055
3056/*
3057 * First performs a check for metadata overlaps (through
3058 * qcow2_check_metadata_overlap); if that fails with a negative value (error
3059 * while performing a check), that value is returned. If an impending overlap
3060 * is detected, the BDS will be made unusable, the qcow2 file marked corrupt
3061 * and -EIO returned.
3062 *
3063 * Returns 0 if there were neither overlaps nor errors while checking for
3064 * overlaps; or a negative value (-errno) on error.
3065 */
231bb267 3066int qcow2_pre_write_overlap_check(BlockDriverState *bs, int ign, int64_t offset,
966b000f 3067 int64_t size, bool data_file)
a40f1c2a 3068{
966b000f
KW
3069 int ret;
3070
3071 if (data_file && has_data_file(bs)) {
3072 return 0;
3073 }
a40f1c2a 3074
966b000f 3075 ret = qcow2_check_metadata_overlap(bs, ign, offset, size);
a40f1c2a
HR
3076 if (ret < 0) {
3077 return ret;
3078 } else if (ret > 0) {
786a4ea8 3079 int metadata_ol_bitnr = ctz32(ret);
a40f1c2a
HR
3080 assert(metadata_ol_bitnr < QCOW2_OL_MAX_BITNR);
3081
adb43552
HR
3082 qcow2_signal_corruption(bs, true, offset, size, "Preventing invalid "
3083 "write on metadata (overlaps with %s)",
3084 metadata_ol_names[metadata_ol_bitnr]);
a40f1c2a
HR
3085 return -EIO;
3086 }
3087
3088 return 0;
3089}
791c9a00
HR
3090
3091/* A pointer to a function of this type is given to walk_over_reftable(). That
3092 * function will create refblocks and pass them to a RefblockFinishOp once they
3093 * are completed (@refblock). @refblock_empty is set if the refblock is
3094 * completely empty.
3095 *
3096 * Along with the refblock, a corresponding reftable entry is passed, in the
3097 * reftable @reftable (which may be reallocated) at @reftable_index.
3098 *
3099 * @allocated should be set to true if a new cluster has been allocated.
3100 */
3101typedef int (RefblockFinishOp)(BlockDriverState *bs, uint64_t **reftable,
3102 uint64_t reftable_index, uint64_t *reftable_size,
3103 void *refblock, bool refblock_empty,
3104 bool *allocated, Error **errp);
3105
3106/**
3107 * This "operation" for walk_over_reftable() allocates the refblock on disk (if
3108 * it is not empty) and inserts its offset into the new reftable. The size of
3109 * this new reftable is increased as required.
3110 */
3111static int alloc_refblock(BlockDriverState *bs, uint64_t **reftable,
3112 uint64_t reftable_index, uint64_t *reftable_size,
3113 void *refblock, bool refblock_empty, bool *allocated,
3114 Error **errp)
3115{
3116 BDRVQcow2State *s = bs->opaque;
3117 int64_t offset;
3118
3119 if (!refblock_empty && reftable_index >= *reftable_size) {
3120 uint64_t *new_reftable;
3121 uint64_t new_reftable_size;
3122
3123 new_reftable_size = ROUND_UP(reftable_index + 1,
02b1ecfa
AG
3124 s->cluster_size / REFTABLE_ENTRY_SIZE);
3125 if (new_reftable_size > QCOW_MAX_REFTABLE_SIZE / REFTABLE_ENTRY_SIZE) {
791c9a00
HR
3126 error_setg(errp,
3127 "This operation would make the refcount table grow "
3128 "beyond the maximum size supported by QEMU, aborting");
3129 return -ENOTSUP;
3130 }
3131
3132 new_reftable = g_try_realloc(*reftable, new_reftable_size *
02b1ecfa 3133 REFTABLE_ENTRY_SIZE);
791c9a00
HR
3134 if (!new_reftable) {
3135 error_setg(errp, "Failed to increase reftable buffer size");
3136 return -ENOMEM;
3137 }
3138
3139 memset(new_reftable + *reftable_size, 0,
02b1ecfa 3140 (new_reftable_size - *reftable_size) * REFTABLE_ENTRY_SIZE);
791c9a00
HR
3141
3142 *reftable = new_reftable;
3143 *reftable_size = new_reftable_size;
3144 }
3145
3146 if (!refblock_empty && !(*reftable)[reftable_index]) {
3147 offset = qcow2_alloc_clusters(bs, s->cluster_size);
3148 if (offset < 0) {
3149 error_setg_errno(errp, -offset, "Failed to allocate refblock");
3150 return offset;
3151 }
3152 (*reftable)[reftable_index] = offset;
3153 *allocated = true;
3154 }
3155
3156 return 0;
3157}
3158
3159/**
3160 * This "operation" for walk_over_reftable() writes the refblock to disk at the
3161 * offset specified by the new reftable's entry. It does not modify the new
3162 * reftable or change any refcounts.
3163 */
3164static int flush_refblock(BlockDriverState *bs, uint64_t **reftable,
3165 uint64_t reftable_index, uint64_t *reftable_size,
3166 void *refblock, bool refblock_empty, bool *allocated,
3167 Error **errp)
3168{
3169 BDRVQcow2State *s = bs->opaque;
3170 int64_t offset;
3171 int ret;
3172
3173 if (reftable_index < *reftable_size && (*reftable)[reftable_index]) {
3174 offset = (*reftable)[reftable_index];
3175
966b000f
KW
3176 ret = qcow2_pre_write_overlap_check(bs, 0, offset, s->cluster_size,
3177 false);
791c9a00
HR
3178 if (ret < 0) {
3179 error_setg_errno(errp, -ret, "Overlap check failed");
3180 return ret;
3181 }
3182
32cc71de 3183 ret = bdrv_pwrite(bs->file, offset, s->cluster_size, refblock, 0);
791c9a00
HR
3184 if (ret < 0) {
3185 error_setg_errno(errp, -ret, "Failed to write refblock");
3186 return ret;
3187 }
3188 } else {
3189 assert(refblock_empty);
3190 }
3191
3192 return 0;
3193}
3194
3195/**
3196 * This function walks over the existing reftable and every referenced refblock;
3197 * if @new_set_refcount is non-NULL, it is called for every refcount entry to
3198 * create an equal new entry in the passed @new_refblock. Once that
3199 * @new_refblock is completely filled, @operation will be called.
3200 *
3201 * @status_cb and @cb_opaque are used for the amend operation's status callback.
3202 * @index is the index of the walk_over_reftable() calls and @total is the total
3203 * number of walk_over_reftable() calls per amend operation. Both are used for
3204 * calculating the parameters for the status callback.
3205 *
3206 * @allocated is set to true if a new cluster has been allocated.
3207 */
3208static int walk_over_reftable(BlockDriverState *bs, uint64_t **new_reftable,
3209 uint64_t *new_reftable_index,
3210 uint64_t *new_reftable_size,
3211 void *new_refblock, int new_refblock_size,
3212 int new_refcount_bits,
3213 RefblockFinishOp *operation, bool *allocated,
3214 Qcow2SetRefcountFunc *new_set_refcount,
3215 BlockDriverAmendStatusCB *status_cb,
3216 void *cb_opaque, int index, int total,
3217 Error **errp)
3218{
3219 BDRVQcow2State *s = bs->opaque;
3220 uint64_t reftable_index;
3221 bool new_refblock_empty = true;
3222 int refblock_index;
3223 int new_refblock_index = 0;
3224 int ret;
3225
3226 for (reftable_index = 0; reftable_index < s->refcount_table_size;
3227 reftable_index++)
3228 {
3229 uint64_t refblock_offset = s->refcount_table[reftable_index]
3230 & REFT_OFFSET_MASK;
3231
3232 status_cb(bs, (uint64_t)index * s->refcount_table_size + reftable_index,
3233 (uint64_t)total * s->refcount_table_size, cb_opaque);
3234
3235 if (refblock_offset) {
3236 void *refblock;
3237
3238 if (offset_into_cluster(s, refblock_offset)) {
3239 qcow2_signal_corruption(bs, true, -1, -1, "Refblock offset %#"
3240 PRIx64 " unaligned (reftable index: %#"
3241 PRIx64 ")", refblock_offset,
3242 reftable_index);
3243 error_setg(errp,
3244 "Image is corrupt (unaligned refblock offset)");
3245 return -EIO;
3246 }
3247
3248 ret = qcow2_cache_get(bs, s->refcount_block_cache, refblock_offset,
3249 &refblock);
3250 if (ret < 0) {
3251 error_setg_errno(errp, -ret, "Failed to retrieve refblock");
3252 return ret;
3253 }
3254
3255 for (refblock_index = 0; refblock_index < s->refcount_block_size;
3256 refblock_index++)
3257 {
3258 uint64_t refcount;
3259
3260 if (new_refblock_index >= new_refblock_size) {
3261 /* new_refblock is now complete */
3262 ret = operation(bs, new_reftable, *new_reftable_index,
3263 new_reftable_size, new_refblock,
3264 new_refblock_empty, allocated, errp);
3265 if (ret < 0) {
2013c3d4 3266 qcow2_cache_put(s->refcount_block_cache, &refblock);
791c9a00
HR
3267 return ret;
3268 }
3269
3270 (*new_reftable_index)++;
3271 new_refblock_index = 0;
3272 new_refblock_empty = true;
3273 }
3274
3275 refcount = s->get_refcount(refblock, refblock_index);
3276 if (new_refcount_bits < 64 && refcount >> new_refcount_bits) {
3277 uint64_t offset;
3278
2013c3d4 3279 qcow2_cache_put(s->refcount_block_cache, &refblock);
791c9a00
HR
3280
3281 offset = ((reftable_index << s->refcount_block_bits)
3282 + refblock_index) << s->cluster_bits;
3283
3284 error_setg(errp, "Cannot decrease refcount entry width to "
3285 "%i bits: Cluster at offset %#" PRIx64 " has a "
3286 "refcount of %" PRIu64, new_refcount_bits,
3287 offset, refcount);
3288 return -EINVAL;
3289 }
3290
3291 if (new_set_refcount) {
3292 new_set_refcount(new_refblock, new_refblock_index++,
3293 refcount);
3294 } else {
3295 new_refblock_index++;
3296 }
3297 new_refblock_empty = new_refblock_empty && refcount == 0;
3298 }
3299
2013c3d4 3300 qcow2_cache_put(s->refcount_block_cache, &refblock);
791c9a00
HR
3301 } else {
3302 /* No refblock means every refcount is 0 */
3303 for (refblock_index = 0; refblock_index < s->refcount_block_size;
3304 refblock_index++)
3305 {
3306 if (new_refblock_index >= new_refblock_size) {
3307 /* new_refblock is now complete */
3308 ret = operation(bs, new_reftable, *new_reftable_index,
3309 new_reftable_size, new_refblock,
3310 new_refblock_empty, allocated, errp);
3311 if (ret < 0) {
3312 return ret;
3313 }
3314
3315 (*new_reftable_index)++;
3316 new_refblock_index = 0;
3317 new_refblock_empty = true;
3318 }
3319
3320 if (new_set_refcount) {
3321 new_set_refcount(new_refblock, new_refblock_index++, 0);
3322 } else {
3323 new_refblock_index++;
3324 }
3325 }
3326 }
3327 }
3328
3329 if (new_refblock_index > 0) {
3330 /* Complete the potentially existing partially filled final refblock */
3331 if (new_set_refcount) {
3332 for (; new_refblock_index < new_refblock_size;
3333 new_refblock_index++)
3334 {
3335 new_set_refcount(new_refblock, new_refblock_index, 0);
3336 }
3337 }
3338
3339 ret = operation(bs, new_reftable, *new_reftable_index,
3340 new_reftable_size, new_refblock, new_refblock_empty,
3341 allocated, errp);
3342 if (ret < 0) {
3343 return ret;
3344 }
3345
3346 (*new_reftable_index)++;
3347 }
3348
3349 status_cb(bs, (uint64_t)(index + 1) * s->refcount_table_size,
3350 (uint64_t)total * s->refcount_table_size, cb_opaque);
3351
3352 return 0;
3353}
3354
3355int qcow2_change_refcount_order(BlockDriverState *bs, int refcount_order,
3356 BlockDriverAmendStatusCB *status_cb,
3357 void *cb_opaque, Error **errp)
3358{
3359 BDRVQcow2State *s = bs->opaque;
3360 Qcow2GetRefcountFunc *new_get_refcount;
3361 Qcow2SetRefcountFunc *new_set_refcount;
3362 void *new_refblock = qemu_blockalign(bs->file->bs, s->cluster_size);
3363 uint64_t *new_reftable = NULL, new_reftable_size = 0;
3364 uint64_t *old_reftable, old_reftable_size, old_reftable_offset;
3365 uint64_t new_reftable_index = 0;
3366 uint64_t i;
3367 int64_t new_reftable_offset = 0, allocated_reftable_size = 0;
3368 int new_refblock_size, new_refcount_bits = 1 << refcount_order;
3369 int old_refcount_order;
3370 int walk_index = 0;
3371 int ret;
3372 bool new_allocation;
3373
3374 assert(s->qcow_version >= 3);
3375 assert(refcount_order >= 0 && refcount_order <= 6);
3376
3377 /* see qcow2_open() */
3378 new_refblock_size = 1 << (s->cluster_bits - (refcount_order - 3));
3379
3380 new_get_refcount = get_refcount_funcs[refcount_order];
3381 new_set_refcount = set_refcount_funcs[refcount_order];
3382
3383
3384 do {
3385 int total_walks;
3386
3387 new_allocation = false;
3388
3389 /* At least we have to do this walk and the one which writes the
3390 * refblocks; also, at least we have to do this loop here at least
3391 * twice (normally), first to do the allocations, and second to
3392 * determine that everything is correctly allocated, this then makes
3393 * three walks in total */
3394 total_walks = MAX(walk_index + 2, 3);
3395
3396 /* First, allocate the structures so they are present in the refcount
3397 * structures */
3398 ret = walk_over_reftable(bs, &new_reftable, &new_reftable_index,
3399 &new_reftable_size, NULL, new_refblock_size,
3400 new_refcount_bits, &alloc_refblock,
3401 &new_allocation, NULL, status_cb, cb_opaque,
3402 walk_index++, total_walks, errp);
3403 if (ret < 0) {
3404 goto done;
3405 }
3406
3407 new_reftable_index = 0;
3408
3409 if (new_allocation) {
3410 if (new_reftable_offset) {
02b1ecfa
AG
3411 qcow2_free_clusters(
3412 bs, new_reftable_offset,
3413 allocated_reftable_size * REFTABLE_ENTRY_SIZE,
3414 QCOW2_DISCARD_NEVER);
791c9a00
HR
3415 }
3416
3417 new_reftable_offset = qcow2_alloc_clusters(bs, new_reftable_size *
02b1ecfa 3418 REFTABLE_ENTRY_SIZE);
791c9a00
HR
3419 if (new_reftable_offset < 0) {
3420 error_setg_errno(errp, -new_reftable_offset,
3421 "Failed to allocate the new reftable");
3422 ret = new_reftable_offset;
3423 goto done;
3424 }
3425 allocated_reftable_size = new_reftable_size;
3426 }
3427 } while (new_allocation);
3428
3429 /* Second, write the new refblocks */
3430 ret = walk_over_reftable(bs, &new_reftable, &new_reftable_index,
3431 &new_reftable_size, new_refblock,
3432 new_refblock_size, new_refcount_bits,
3433 &flush_refblock, &new_allocation, new_set_refcount,
3434 status_cb, cb_opaque, walk_index, walk_index + 1,
3435 errp);
3436 if (ret < 0) {
3437 goto done;
3438 }
3439 assert(!new_allocation);
3440
3441
3442 /* Write the new reftable */
3443 ret = qcow2_pre_write_overlap_check(bs, 0, new_reftable_offset,
02b1ecfa 3444 new_reftable_size * REFTABLE_ENTRY_SIZE,
966b000f 3445 false);
791c9a00
HR
3446 if (ret < 0) {
3447 error_setg_errno(errp, -ret, "Overlap check failed");
3448 goto done;
3449 }
3450
3451 for (i = 0; i < new_reftable_size; i++) {
3452 cpu_to_be64s(&new_reftable[i]);
3453 }
3454
32cc71de
AF
3455 ret = bdrv_pwrite(bs->file, new_reftable_offset,
3456 new_reftable_size * REFTABLE_ENTRY_SIZE, new_reftable,
3457 0);
791c9a00
HR
3458
3459 for (i = 0; i < new_reftable_size; i++) {
3460 be64_to_cpus(&new_reftable[i]);
3461 }
3462
3463 if (ret < 0) {
3464 error_setg_errno(errp, -ret, "Failed to write the new reftable");
3465 goto done;
3466 }
3467
3468
3469 /* Empty the refcount cache */
3470 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
3471 if (ret < 0) {
3472 error_setg_errno(errp, -ret, "Failed to flush the refblock cache");
3473 goto done;
3474 }
3475
3476 /* Update the image header to point to the new reftable; this only updates
3477 * the fields which are relevant to qcow2_update_header(); other fields
3478 * such as s->refcount_table or s->refcount_bits stay stale for now
3479 * (because we have to restore everything if qcow2_update_header() fails) */
3480 old_refcount_order = s->refcount_order;
3481 old_reftable_size = s->refcount_table_size;
3482 old_reftable_offset = s->refcount_table_offset;
3483
3484 s->refcount_order = refcount_order;
3485 s->refcount_table_size = new_reftable_size;
3486 s->refcount_table_offset = new_reftable_offset;
3487
3488 ret = qcow2_update_header(bs);
3489 if (ret < 0) {
3490 s->refcount_order = old_refcount_order;
3491 s->refcount_table_size = old_reftable_size;
3492 s->refcount_table_offset = old_reftable_offset;
3493 error_setg_errno(errp, -ret, "Failed to update the qcow2 header");
3494 goto done;
3495 }
3496
3497 /* Now update the rest of the in-memory information */
3498 old_reftable = s->refcount_table;
3499 s->refcount_table = new_reftable;
7061a078 3500 update_max_refcount_table_index(s);
791c9a00
HR
3501
3502 s->refcount_bits = 1 << refcount_order;
3503 s->refcount_max = UINT64_C(1) << (s->refcount_bits - 1);
3504 s->refcount_max += s->refcount_max - 1;
3505
3506 s->refcount_block_bits = s->cluster_bits - (refcount_order - 3);
3507 s->refcount_block_size = 1 << s->refcount_block_bits;
3508
3509 s->get_refcount = new_get_refcount;
3510 s->set_refcount = new_set_refcount;
3511
3512 /* For cleaning up all old refblocks and the old reftable below the "done"
3513 * label */
3514 new_reftable = old_reftable;
3515 new_reftable_size = old_reftable_size;
3516 new_reftable_offset = old_reftable_offset;
3517
3518done:
3519 if (new_reftable) {
3520 /* On success, new_reftable actually points to the old reftable (and
3521 * new_reftable_size is the old reftable's size); but that is just
3522 * fine */
3523 for (i = 0; i < new_reftable_size; i++) {
3524 uint64_t offset = new_reftable[i] & REFT_OFFSET_MASK;
3525 if (offset) {
3526 qcow2_free_clusters(bs, offset, s->cluster_size,
3527 QCOW2_DISCARD_OTHER);
3528 }
3529 }
3530 g_free(new_reftable);
3531
3532 if (new_reftable_offset > 0) {
3533 qcow2_free_clusters(bs, new_reftable_offset,
02b1ecfa 3534 new_reftable_size * REFTABLE_ENTRY_SIZE,
791c9a00
HR
3535 QCOW2_DISCARD_OTHER);
3536 }
3537 }
3538
3539 qemu_vfree(new_refblock);
3540 return ret;
3541}
46b732cd 3542
23482f8a
HR
3543static int64_t get_refblock_offset(BlockDriverState *bs, uint64_t offset)
3544{
3545 BDRVQcow2State *s = bs->opaque;
3546 uint32_t index = offset_to_reftable_index(s, offset);
3547 int64_t covering_refblock_offset = 0;
3548
3549 if (index < s->refcount_table_size) {
3550 covering_refblock_offset = s->refcount_table[index] & REFT_OFFSET_MASK;
3551 }
3552 if (!covering_refblock_offset) {
3553 qcow2_signal_corruption(bs, true, -1, -1, "Refblock at %#" PRIx64 " is "
3554 "not covered by the refcount structures",
3555 offset);
3556 return -EIO;
3557 }
3558
3559 return covering_refblock_offset;
3560}
3561
46b732cd
PB
3562static int qcow2_discard_refcount_block(BlockDriverState *bs,
3563 uint64_t discard_block_offs)
3564{
3565 BDRVQcow2State *s = bs->opaque;
23482f8a 3566 int64_t refblock_offs;
46b732cd
PB
3567 uint64_t cluster_index = discard_block_offs >> s->cluster_bits;
3568 uint32_t block_index = cluster_index & (s->refcount_block_size - 1);
3569 void *refblock;
3570 int ret;
3571
23482f8a
HR
3572 refblock_offs = get_refblock_offset(bs, discard_block_offs);
3573 if (refblock_offs < 0) {
3574 return refblock_offs;
3575 }
3576
46b732cd
PB
3577 assert(discard_block_offs != 0);
3578
3579 ret = qcow2_cache_get(bs, s->refcount_block_cache, refblock_offs,
3580 &refblock);
3581 if (ret < 0) {
3582 return ret;
3583 }
3584
3585 if (s->get_refcount(refblock, block_index) != 1) {
3586 qcow2_signal_corruption(bs, true, -1, -1, "Invalid refcount:"
3587 " refblock offset %#" PRIx64
3588 ", reftable index %u"
3589 ", block offset %#" PRIx64
3590 ", refcount %#" PRIx64,
3591 refblock_offs,
3592 offset_to_reftable_index(s, discard_block_offs),
3593 discard_block_offs,
3594 s->get_refcount(refblock, block_index));
2013c3d4 3595 qcow2_cache_put(s->refcount_block_cache, &refblock);
46b732cd
PB
3596 return -EINVAL;
3597 }
3598 s->set_refcount(refblock, block_index, 0);
3599
2d135ee9 3600 qcow2_cache_entry_mark_dirty(s->refcount_block_cache, refblock);
46b732cd 3601
2013c3d4 3602 qcow2_cache_put(s->refcount_block_cache, &refblock);
46b732cd
PB
3603
3604 if (cluster_index < s->free_cluster_index) {
3605 s->free_cluster_index = cluster_index;
3606 }
3607
6e6fa760 3608 refblock = qcow2_cache_is_table_offset(s->refcount_block_cache,
46b732cd
PB
3609 discard_block_offs);
3610 if (refblock) {
3611 /* discard refblock from the cache if refblock is cached */
77aadd7b 3612 qcow2_cache_discard(s->refcount_block_cache, refblock);
46b732cd
PB
3613 }
3614 update_refcount_discard(bs, discard_block_offs, s->cluster_size);
3615
3616 return 0;
3617}
3618
3619int qcow2_shrink_reftable(BlockDriverState *bs)
3620{
3621 BDRVQcow2State *s = bs->opaque;
3622 uint64_t *reftable_tmp =
02b1ecfa 3623 g_malloc(s->refcount_table_size * REFTABLE_ENTRY_SIZE);
46b732cd
PB
3624 int i, ret;
3625
3626 for (i = 0; i < s->refcount_table_size; i++) {
3627 int64_t refblock_offs = s->refcount_table[i] & REFT_OFFSET_MASK;
3628 void *refblock;
3629 bool unused_block;
3630
3631 if (refblock_offs == 0) {
3632 reftable_tmp[i] = 0;
3633 continue;
3634 }
3635 ret = qcow2_cache_get(bs, s->refcount_block_cache, refblock_offs,
3636 &refblock);
3637 if (ret < 0) {
3638 goto out;
3639 }
3640
3641 /* the refblock has own reference */
3642 if (i == offset_to_reftable_index(s, refblock_offs)) {
3643 uint64_t block_index = (refblock_offs >> s->cluster_bits) &
3644 (s->refcount_block_size - 1);
3645 uint64_t refcount = s->get_refcount(refblock, block_index);
3646
3647 s->set_refcount(refblock, block_index, 0);
3648
3649 unused_block = buffer_is_zero(refblock, s->cluster_size);
3650
3651 s->set_refcount(refblock, block_index, refcount);
3652 } else {
3653 unused_block = buffer_is_zero(refblock, s->cluster_size);
3654 }
2013c3d4 3655 qcow2_cache_put(s->refcount_block_cache, &refblock);
46b732cd
PB
3656
3657 reftable_tmp[i] = unused_block ? 0 : cpu_to_be64(s->refcount_table[i]);
3658 }
3659
32cc71de
AF
3660 ret = bdrv_pwrite_sync(bs->file, s->refcount_table_offset,
3661 s->refcount_table_size * REFTABLE_ENTRY_SIZE,
3662 reftable_tmp, 0);
46b732cd
PB
3663 /*
3664 * If the write in the reftable failed the image may contain a partially
3665 * overwritten reftable. In this case it would be better to clear the
3666 * reftable in memory to avoid possible image corruption.
3667 */
3668 for (i = 0; i < s->refcount_table_size; i++) {
3669 if (s->refcount_table[i] && !reftable_tmp[i]) {
3670 if (ret == 0) {
3671 ret = qcow2_discard_refcount_block(bs, s->refcount_table[i] &
3672 REFT_OFFSET_MASK);
3673 }
3674 s->refcount_table[i] = 0;
3675 }
3676 }
3677
3678 if (!s->cache_discards) {
3679 qcow2_process_discards(bs, ret);
3680 }
3681
3682out:
3683 g_free(reftable_tmp);
3684 return ret;
3685}
163bc39d
PB
3686
3687int64_t qcow2_get_last_cluster(BlockDriverState *bs, int64_t size)
3688{
3689 BDRVQcow2State *s = bs->opaque;
3690 int64_t i;
3691
3692 for (i = size_to_clusters(s, size) - 1; i >= 0; i--) {
3693 uint64_t refcount;
3694 int ret = qcow2_get_refcount(bs, i, &refcount);
3695 if (ret < 0) {
3696 fprintf(stderr, "Can't get refcount for cluster %" PRId64 ": %s\n",
3697 i, strerror(-ret));
3698 return ret;
3699 }
3700 if (refcount > 0) {
3701 return i;
3702 }
3703 }
3704 qcow2_signal_corruption(bs, true, -1, -1,
3705 "There are no references in the refcount table.");
3706 return -EIO;
3707}
69f47505
VSO
3708
3709int qcow2_detect_metadata_preallocation(BlockDriverState *bs)
3710{
3711 BDRVQcow2State *s = bs->opaque;
3712 int64_t i, end_cluster, cluster_count = 0, threshold;
3713 int64_t file_length, real_allocation, real_clusters;
3714
5e978550
KW
3715 qemu_co_mutex_assert_locked(&s->lock);
3716
69f47505
VSO
3717 file_length = bdrv_getlength(bs->file->bs);
3718 if (file_length < 0) {
3719 return file_length;
3720 }
3721
3722 real_allocation = bdrv_get_allocated_file_size(bs->file->bs);
3723 if (real_allocation < 0) {
3724 return real_allocation;
3725 }
3726
3727 real_clusters = real_allocation / s->cluster_size;
3728 threshold = MAX(real_clusters * 10 / 9, real_clusters + 2);
3729
3730 end_cluster = size_to_clusters(s, file_length);
3731 for (i = 0; i < end_cluster && cluster_count < threshold; i++) {
3732 uint64_t refcount;
3733 int ret = qcow2_get_refcount(bs, i, &refcount);
3734 if (ret < 0) {
3735 return ret;
3736 }
3737 cluster_count += !!refcount;
3738 }
3739
3740 return cluster_count >= threshold;
3741}