]> Git Repo - qemu.git/blame - block/qcow2-refcount.c
target-i386: fix pcmpxstrx equal-ordered (strstr) mode
[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
25#include "qemu-common.h"
737e150e 26#include "block/block_int.h"
f7d0fe02 27#include "block/qcow2.h"
a40f1c2a 28#include "qemu/range.h"
f7d0fe02 29
bb572aef 30static int64_t alloc_clusters_noref(BlockDriverState *bs, uint64_t size);
92dcb59f 31static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
0e06528e 32 int64_t offset, int64_t length, uint64_t addend,
2aabe7c7 33 bool decrease, enum qcow2_discard_type type);
f7d0fe02 34
59c0cb78
HR
35static uint64_t get_refcount_ro0(const void *refcount_array, uint64_t index);
36static uint64_t get_refcount_ro1(const void *refcount_array, uint64_t index);
37static uint64_t get_refcount_ro2(const void *refcount_array, uint64_t index);
38static uint64_t get_refcount_ro3(const void *refcount_array, uint64_t index);
7453c96b 39static uint64_t get_refcount_ro4(const void *refcount_array, uint64_t index);
59c0cb78
HR
40static uint64_t get_refcount_ro5(const void *refcount_array, uint64_t index);
41static uint64_t get_refcount_ro6(const void *refcount_array, uint64_t index);
7453c96b 42
59c0cb78
HR
43static void set_refcount_ro0(void *refcount_array, uint64_t index,
44 uint64_t value);
45static void set_refcount_ro1(void *refcount_array, uint64_t index,
46 uint64_t value);
47static void set_refcount_ro2(void *refcount_array, uint64_t index,
48 uint64_t value);
49static void set_refcount_ro3(void *refcount_array, uint64_t index,
50 uint64_t value);
7453c96b
HR
51static void set_refcount_ro4(void *refcount_array, uint64_t index,
52 uint64_t value);
59c0cb78
HR
53static void set_refcount_ro5(void *refcount_array, uint64_t index,
54 uint64_t value);
55static void set_refcount_ro6(void *refcount_array, uint64_t index,
56 uint64_t value);
57
58
59static Qcow2GetRefcountFunc *const get_refcount_funcs[] = {
60 &get_refcount_ro0,
61 &get_refcount_ro1,
62 &get_refcount_ro2,
63 &get_refcount_ro3,
64 &get_refcount_ro4,
65 &get_refcount_ro5,
66 &get_refcount_ro6
67};
68
69static Qcow2SetRefcountFunc *const set_refcount_funcs[] = {
70 &set_refcount_ro0,
71 &set_refcount_ro1,
72 &set_refcount_ro2,
73 &set_refcount_ro3,
74 &set_refcount_ro4,
75 &set_refcount_ro5,
76 &set_refcount_ro6
77};
7453c96b 78
3b88e52b 79
f7d0fe02
KW
80/*********************************************************/
81/* refcount handling */
82
ed6ccf0f 83int qcow2_refcount_init(BlockDriverState *bs)
f7d0fe02 84{
ff99129a 85 BDRVQcow2State *s = bs->opaque;
5dab2fad
KW
86 unsigned int refcount_table_size2, i;
87 int ret;
f7d0fe02 88
59c0cb78
HR
89 assert(s->refcount_order >= 0 && s->refcount_order <= 6);
90
91 s->get_refcount = get_refcount_funcs[s->refcount_order];
92 s->set_refcount = set_refcount_funcs[s->refcount_order];
7453c96b 93
5dab2fad 94 assert(s->refcount_table_size <= INT_MAX / sizeof(uint64_t));
f7d0fe02 95 refcount_table_size2 = s->refcount_table_size * sizeof(uint64_t);
de82815d
KW
96 s->refcount_table = g_try_malloc(refcount_table_size2);
97
f7d0fe02 98 if (s->refcount_table_size > 0) {
de82815d 99 if (s->refcount_table == NULL) {
8fcffa98 100 ret = -ENOMEM;
de82815d
KW
101 goto fail;
102 }
66f82cee 103 BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_LOAD);
9a4f4c31 104 ret = bdrv_pread(bs->file->bs, s->refcount_table_offset,
f7d0fe02 105 s->refcount_table, refcount_table_size2);
8fcffa98 106 if (ret < 0) {
f7d0fe02 107 goto fail;
8fcffa98 108 }
f7d0fe02
KW
109 for(i = 0; i < s->refcount_table_size; i++)
110 be64_to_cpus(&s->refcount_table[i]);
111 }
112 return 0;
113 fail:
8fcffa98 114 return ret;
f7d0fe02
KW
115}
116
ed6ccf0f 117void qcow2_refcount_close(BlockDriverState *bs)
f7d0fe02 118{
ff99129a 119 BDRVQcow2State *s = bs->opaque;
7267c094 120 g_free(s->refcount_table);
f7d0fe02
KW
121}
122
123
59c0cb78
HR
124static uint64_t get_refcount_ro0(const void *refcount_array, uint64_t index)
125{
126 return (((const uint8_t *)refcount_array)[index / 8] >> (index % 8)) & 0x1;
127}
128
129static void set_refcount_ro0(void *refcount_array, uint64_t index,
130 uint64_t value)
131{
132 assert(!(value >> 1));
133 ((uint8_t *)refcount_array)[index / 8] &= ~(0x1 << (index % 8));
134 ((uint8_t *)refcount_array)[index / 8] |= value << (index % 8);
135}
136
137static uint64_t get_refcount_ro1(const void *refcount_array, uint64_t index)
138{
139 return (((const uint8_t *)refcount_array)[index / 4] >> (2 * (index % 4)))
140 & 0x3;
141}
142
143static void set_refcount_ro1(void *refcount_array, uint64_t index,
144 uint64_t value)
145{
146 assert(!(value >> 2));
147 ((uint8_t *)refcount_array)[index / 4] &= ~(0x3 << (2 * (index % 4)));
148 ((uint8_t *)refcount_array)[index / 4] |= value << (2 * (index % 4));
149}
150
151static uint64_t get_refcount_ro2(const void *refcount_array, uint64_t index)
152{
153 return (((const uint8_t *)refcount_array)[index / 2] >> (4 * (index % 2)))
154 & 0xf;
155}
156
157static void set_refcount_ro2(void *refcount_array, uint64_t index,
158 uint64_t value)
159{
160 assert(!(value >> 4));
161 ((uint8_t *)refcount_array)[index / 2] &= ~(0xf << (4 * (index % 2)));
162 ((uint8_t *)refcount_array)[index / 2] |= value << (4 * (index % 2));
163}
164
165static uint64_t get_refcount_ro3(const void *refcount_array, uint64_t index)
166{
167 return ((const uint8_t *)refcount_array)[index];
168}
169
170static void set_refcount_ro3(void *refcount_array, uint64_t index,
171 uint64_t value)
172{
173 assert(!(value >> 8));
174 ((uint8_t *)refcount_array)[index] = value;
175}
176
7453c96b
HR
177static uint64_t get_refcount_ro4(const void *refcount_array, uint64_t index)
178{
179 return be16_to_cpu(((const uint16_t *)refcount_array)[index]);
180}
181
182static void set_refcount_ro4(void *refcount_array, uint64_t index,
183 uint64_t value)
184{
185 assert(!(value >> 16));
186 ((uint16_t *)refcount_array)[index] = cpu_to_be16(value);
187}
188
59c0cb78
HR
189static uint64_t get_refcount_ro5(const void *refcount_array, uint64_t index)
190{
191 return be32_to_cpu(((const uint32_t *)refcount_array)[index]);
192}
193
194static void set_refcount_ro5(void *refcount_array, uint64_t index,
195 uint64_t value)
196{
197 assert(!(value >> 32));
198 ((uint32_t *)refcount_array)[index] = cpu_to_be32(value);
199}
200
201static uint64_t get_refcount_ro6(const void *refcount_array, uint64_t index)
202{
203 return be64_to_cpu(((const uint64_t *)refcount_array)[index]);
204}
205
206static void set_refcount_ro6(void *refcount_array, uint64_t index,
207 uint64_t value)
208{
209 ((uint64_t *)refcount_array)[index] = cpu_to_be64(value);
210}
211
7453c96b 212
f7d0fe02 213static int load_refcount_block(BlockDriverState *bs,
29c1a730
KW
214 int64_t refcount_block_offset,
215 void **refcount_block)
f7d0fe02 216{
ff99129a 217 BDRVQcow2State *s = bs->opaque;
f7d0fe02 218 int ret;
3b88e52b 219
66f82cee 220 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_LOAD);
29c1a730
KW
221 ret = qcow2_cache_get(bs, s->refcount_block_cache, refcount_block_offset,
222 refcount_block);
e14e8ba5 223
29c1a730 224 return ret;
f7d0fe02
KW
225}
226
018faafd 227/*
7324c10f
HR
228 * Retrieves the refcount of the cluster given by its index and stores it in
229 * *refcount. Returns 0 on success and -errno on failure.
018faafd 230 */
7324c10f 231int qcow2_get_refcount(BlockDriverState *bs, int64_t cluster_index,
0e06528e 232 uint64_t *refcount)
f7d0fe02 233{
ff99129a 234 BDRVQcow2State *s = bs->opaque;
db8a31d1 235 uint64_t refcount_table_index, block_index;
f7d0fe02 236 int64_t refcount_block_offset;
018faafd 237 int ret;
7453c96b 238 void *refcount_block;
f7d0fe02 239
17bd5f47 240 refcount_table_index = cluster_index >> s->refcount_block_bits;
7324c10f
HR
241 if (refcount_table_index >= s->refcount_table_size) {
242 *refcount = 0;
f7d0fe02 243 return 0;
7324c10f 244 }
26d49c46
HR
245 refcount_block_offset =
246 s->refcount_table[refcount_table_index] & REFT_OFFSET_MASK;
7324c10f
HR
247 if (!refcount_block_offset) {
248 *refcount = 0;
f7d0fe02 249 return 0;
7324c10f 250 }
29c1a730 251
a97c67ee
HR
252 if (offset_into_cluster(s, refcount_block_offset)) {
253 qcow2_signal_corruption(bs, true, -1, -1, "Refblock offset %#" PRIx64
254 " unaligned (reftable index: %#" PRIx64 ")",
255 refcount_block_offset, refcount_table_index);
256 return -EIO;
257 }
258
29c1a730 259 ret = qcow2_cache_get(bs, s->refcount_block_cache, refcount_block_offset,
7453c96b 260 &refcount_block);
29c1a730
KW
261 if (ret < 0) {
262 return ret;
f7d0fe02 263 }
29c1a730 264
17bd5f47 265 block_index = cluster_index & (s->refcount_block_size - 1);
7453c96b 266 *refcount = s->get_refcount(refcount_block, block_index);
29c1a730 267
a3f1afb4 268 qcow2_cache_put(bs, s->refcount_block_cache, &refcount_block);
29c1a730 269
7324c10f 270 return 0;
f7d0fe02
KW
271}
272
05121aed
KW
273/*
274 * Rounds the refcount table size up to avoid growing the table for each single
275 * refcount block that is allocated.
276 */
ff99129a 277static unsigned int next_refcount_table_size(BDRVQcow2State *s,
05121aed
KW
278 unsigned int min_size)
279{
280 unsigned int min_clusters = (min_size >> (s->cluster_bits - 3)) + 1;
281 unsigned int refcount_table_clusters =
282 MAX(1, s->refcount_table_size >> (s->cluster_bits - 3));
283
284 while (min_clusters > refcount_table_clusters) {
285 refcount_table_clusters = (refcount_table_clusters * 3 + 1) / 2;
286 }
287
288 return refcount_table_clusters << (s->cluster_bits - 3);
289}
290
92dcb59f
KW
291
292/* Checks if two offsets are described by the same refcount block */
ff99129a 293static int in_same_refcount_block(BDRVQcow2State *s, uint64_t offset_a,
92dcb59f
KW
294 uint64_t offset_b)
295{
17bd5f47
HR
296 uint64_t block_a = offset_a >> (s->cluster_bits + s->refcount_block_bits);
297 uint64_t block_b = offset_b >> (s->cluster_bits + s->refcount_block_bits);
92dcb59f
KW
298
299 return (block_a == block_b);
300}
301
302/*
303 * Loads a refcount block. If it doesn't exist yet, it is allocated first
304 * (including growing the refcount table if needed).
305 *
29c1a730 306 * Returns 0 on success or -errno in error case
92dcb59f 307 */
29c1a730 308static int alloc_refcount_block(BlockDriverState *bs,
7453c96b 309 int64_t cluster_index, void **refcount_block)
f7d0fe02 310{
ff99129a 311 BDRVQcow2State *s = bs->opaque;
92dcb59f
KW
312 unsigned int refcount_table_index;
313 int ret;
314
66f82cee 315 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC);
8252278a 316
92dcb59f 317 /* Find the refcount block for the given cluster */
17bd5f47 318 refcount_table_index = cluster_index >> s->refcount_block_bits;
92dcb59f
KW
319
320 if (refcount_table_index < s->refcount_table_size) {
321
322 uint64_t refcount_block_offset =
76dc9e0c 323 s->refcount_table[refcount_table_index] & REFT_OFFSET_MASK;
92dcb59f
KW
324
325 /* If it's already there, we're done */
326 if (refcount_block_offset) {
a97c67ee
HR
327 if (offset_into_cluster(s, refcount_block_offset)) {
328 qcow2_signal_corruption(bs, true, -1, -1, "Refblock offset %#"
329 PRIx64 " unaligned (reftable index: "
330 "%#x)", refcount_block_offset,
331 refcount_table_index);
332 return -EIO;
333 }
334
29c1a730 335 return load_refcount_block(bs, refcount_block_offset,
7453c96b 336 refcount_block);
92dcb59f
KW
337 }
338 }
339
340 /*
341 * If we came here, we need to allocate something. Something is at least
342 * a cluster for the new refcount block. It may also include a new refcount
343 * table if the old refcount table is too small.
344 *
345 * Note that allocating clusters here needs some special care:
346 *
347 * - We can't use the normal qcow2_alloc_clusters(), it would try to
348 * increase the refcount and very likely we would end up with an endless
349 * recursion. Instead we must place the refcount blocks in a way that
350 * they can describe them themselves.
351 *
352 * - We need to consider that at this point we are inside update_refcounts
b106ad91
KW
353 * and potentially doing an initial refcount increase. This means that
354 * some clusters have already been allocated by the caller, but their
355 * refcount isn't accurate yet. If we allocate clusters for metadata, we
356 * need to return -EAGAIN to signal the caller that it needs to restart
357 * the search for free clusters.
92dcb59f
KW
358 *
359 * - alloc_clusters_noref and qcow2_free_clusters may load a different
360 * refcount block into the cache
361 */
362
29c1a730
KW
363 *refcount_block = NULL;
364
365 /* We write to the refcount table, so we might depend on L2 tables */
9991923b
SH
366 ret = qcow2_cache_flush(bs, s->l2_table_cache);
367 if (ret < 0) {
368 return ret;
369 }
92dcb59f
KW
370
371 /* Allocate the refcount block itself and mark it as used */
2eaa8f63
KW
372 int64_t new_block = alloc_clusters_noref(bs, s->cluster_size);
373 if (new_block < 0) {
374 return new_block;
375 }
f7d0fe02 376
f7d0fe02 377#ifdef DEBUG_ALLOC2
92dcb59f
KW
378 fprintf(stderr, "qcow2: Allocate refcount block %d for %" PRIx64
379 " at %" PRIx64 "\n",
380 refcount_table_index, cluster_index << s->cluster_bits, new_block);
f7d0fe02 381#endif
92dcb59f
KW
382
383 if (in_same_refcount_block(s, new_block, cluster_index << s->cluster_bits)) {
25408c09 384 /* Zero the new refcount block before updating it */
29c1a730 385 ret = qcow2_cache_get_empty(bs, s->refcount_block_cache, new_block,
7453c96b 386 refcount_block);
29c1a730
KW
387 if (ret < 0) {
388 goto fail_block;
389 }
390
391 memset(*refcount_block, 0, s->cluster_size);
25408c09 392
92dcb59f
KW
393 /* The block describes itself, need to update the cache */
394 int block_index = (new_block >> s->cluster_bits) &
17bd5f47 395 (s->refcount_block_size - 1);
7453c96b 396 s->set_refcount(*refcount_block, block_index, 1);
92dcb59f
KW
397 } else {
398 /* Described somewhere else. This can recurse at most twice before we
399 * arrive at a block that describes itself. */
2aabe7c7 400 ret = update_refcount(bs, new_block, s->cluster_size, 1, false,
6cfcb9b8 401 QCOW2_DISCARD_NEVER);
92dcb59f
KW
402 if (ret < 0) {
403 goto fail_block;
404 }
25408c09 405
9991923b
SH
406 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
407 if (ret < 0) {
408 goto fail_block;
409 }
1c4c2814 410
25408c09
KW
411 /* Initialize the new refcount block only after updating its refcount,
412 * update_refcount uses the refcount cache itself */
29c1a730 413 ret = qcow2_cache_get_empty(bs, s->refcount_block_cache, new_block,
7453c96b 414 refcount_block);
29c1a730
KW
415 if (ret < 0) {
416 goto fail_block;
417 }
418
419 memset(*refcount_block, 0, s->cluster_size);
92dcb59f
KW
420 }
421
422 /* Now the new refcount block needs to be written to disk */
66f82cee 423 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE);
72e80b89 424 qcow2_cache_entry_mark_dirty(bs, s->refcount_block_cache, *refcount_block);
29c1a730 425 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
92dcb59f
KW
426 if (ret < 0) {
427 goto fail_block;
428 }
429
430 /* If the refcount table is big enough, just hook the block up there */
431 if (refcount_table_index < s->refcount_table_size) {
432 uint64_t data64 = cpu_to_be64(new_block);
66f82cee 433 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_HOOKUP);
9a4f4c31 434 ret = bdrv_pwrite_sync(bs->file->bs,
92dcb59f
KW
435 s->refcount_table_offset + refcount_table_index * sizeof(uint64_t),
436 &data64, sizeof(data64));
437 if (ret < 0) {
438 goto fail_block;
439 }
440
441 s->refcount_table[refcount_table_index] = new_block;
b106ad91
KW
442
443 /* The new refcount block may be where the caller intended to put its
444 * data, so let it restart the search. */
445 return -EAGAIN;
29c1a730
KW
446 }
447
a3f1afb4 448 qcow2_cache_put(bs, s->refcount_block_cache, refcount_block);
92dcb59f
KW
449
450 /*
451 * If we come here, we need to grow the refcount table. Again, a new
452 * refcount table needs some space and we can't simply allocate to avoid
453 * endless recursion.
454 *
455 * Therefore let's grab new refcount blocks at the end of the image, which
456 * will describe themselves and the new refcount table. This way we can
457 * reference them only in the new table and do the switch to the new
458 * refcount table at once without producing an inconsistent state in
459 * between.
460 */
66f82cee 461 BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_GROW);
8252278a 462
14a58a4e
HR
463 /* Calculate the number of refcount blocks needed so far; this will be the
464 * basis for calculating the index of the first cluster used for the
465 * self-describing refcount structures which we are about to create.
466 *
467 * Because we reached this point, there cannot be any refcount entries for
468 * cluster_index or higher indices yet. However, because new_block has been
469 * allocated to describe that cluster (and it will assume this role later
470 * on), we cannot use that index; also, new_block may actually have a higher
471 * cluster index than cluster_index, so it needs to be taken into account
472 * here (and 1 needs to be added to its value because that cluster is used).
473 */
474 uint64_t blocks_used = DIV_ROUND_UP(MAX(cluster_index + 1,
475 (new_block >> s->cluster_bits) + 1),
476 s->refcount_block_size);
92dcb59f 477
2b5d5953
KW
478 if (blocks_used > QCOW_MAX_REFTABLE_SIZE / sizeof(uint64_t)) {
479 return -EFBIG;
480 }
481
92dcb59f
KW
482 /* And now we need at least one block more for the new metadata */
483 uint64_t table_size = next_refcount_table_size(s, blocks_used + 1);
484 uint64_t last_table_size;
485 uint64_t blocks_clusters;
486 do {
a3548077
KW
487 uint64_t table_clusters =
488 size_to_clusters(s, table_size * sizeof(uint64_t));
92dcb59f 489 blocks_clusters = 1 +
17bd5f47
HR
490 ((table_clusters + s->refcount_block_size - 1)
491 / s->refcount_block_size);
92dcb59f
KW
492 uint64_t meta_clusters = table_clusters + blocks_clusters;
493
494 last_table_size = table_size;
495 table_size = next_refcount_table_size(s, blocks_used +
17bd5f47
HR
496 ((meta_clusters + s->refcount_block_size - 1)
497 / s->refcount_block_size));
92dcb59f
KW
498
499 } while (last_table_size != table_size);
500
501#ifdef DEBUG_ALLOC2
502 fprintf(stderr, "qcow2: Grow refcount table %" PRId32 " => %" PRId64 "\n",
503 s->refcount_table_size, table_size);
504#endif
505
506 /* Create the new refcount table and blocks */
17bd5f47 507 uint64_t meta_offset = (blocks_used * s->refcount_block_size) *
92dcb59f
KW
508 s->cluster_size;
509 uint64_t table_offset = meta_offset + blocks_clusters * s->cluster_size;
5839e53b 510 uint64_t *new_table = g_try_new0(uint64_t, table_size);
7453c96b 511 void *new_blocks = g_try_malloc0(blocks_clusters * s->cluster_size);
de82815d
KW
512
513 assert(table_size > 0 && blocks_clusters > 0);
514 if (new_table == NULL || new_blocks == NULL) {
515 ret = -ENOMEM;
516 goto fail_table;
517 }
92dcb59f 518
92dcb59f 519 /* Fill the new refcount table */
f7d0fe02 520 memcpy(new_table, s->refcount_table,
92dcb59f
KW
521 s->refcount_table_size * sizeof(uint64_t));
522 new_table[refcount_table_index] = new_block;
523
524 int i;
525 for (i = 0; i < blocks_clusters; i++) {
526 new_table[blocks_used + i] = meta_offset + (i * s->cluster_size);
527 }
528
529 /* Fill the refcount blocks */
530 uint64_t table_clusters = size_to_clusters(s, table_size * sizeof(uint64_t));
531 int block = 0;
532 for (i = 0; i < table_clusters + blocks_clusters; i++) {
7453c96b 533 s->set_refcount(new_blocks, block++, 1);
92dcb59f
KW
534 }
535
536 /* Write refcount blocks to disk */
66f82cee 537 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS);
9a4f4c31 538 ret = bdrv_pwrite_sync(bs->file->bs, meta_offset, new_blocks,
92dcb59f 539 blocks_clusters * s->cluster_size);
7267c094 540 g_free(new_blocks);
39ba3bf6 541 new_blocks = NULL;
92dcb59f
KW
542 if (ret < 0) {
543 goto fail_table;
544 }
545
546 /* Write refcount table to disk */
547 for(i = 0; i < table_size; i++) {
548 cpu_to_be64s(&new_table[i]);
549 }
550
66f82cee 551 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE);
9a4f4c31 552 ret = bdrv_pwrite_sync(bs->file->bs, table_offset, new_table,
92dcb59f
KW
553 table_size * sizeof(uint64_t));
554 if (ret < 0) {
555 goto fail_table;
556 }
557
558 for(i = 0; i < table_size; i++) {
87267753 559 be64_to_cpus(&new_table[i]);
92dcb59f 560 }
f7d0fe02 561
92dcb59f
KW
562 /* Hook up the new refcount table in the qcow2 header */
563 uint8_t data[12];
f7d0fe02 564 cpu_to_be64w((uint64_t*)data, table_offset);
92dcb59f 565 cpu_to_be32w((uint32_t*)(data + 8), table_clusters);
66f82cee 566 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE);
9a4f4c31
KW
567 ret = bdrv_pwrite_sync(bs->file->bs,
568 offsetof(QCowHeader, refcount_table_offset),
569 data, sizeof(data));
92dcb59f
KW
570 if (ret < 0) {
571 goto fail_table;
f2b7c8b3
KW
572 }
573
92dcb59f
KW
574 /* And switch it in memory */
575 uint64_t old_table_offset = s->refcount_table_offset;
576 uint64_t old_table_size = s->refcount_table_size;
577
7267c094 578 g_free(s->refcount_table);
f7d0fe02 579 s->refcount_table = new_table;
92dcb59f 580 s->refcount_table_size = table_size;
f7d0fe02
KW
581 s->refcount_table_offset = table_offset;
582
b106ad91 583 /* Free old table. */
6cfcb9b8
KW
584 qcow2_free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t),
585 QCOW2_DISCARD_OTHER);
f7d0fe02 586
7453c96b 587 ret = load_refcount_block(bs, new_block, refcount_block);
92dcb59f 588 if (ret < 0) {
29c1a730 589 return ret;
f7d0fe02
KW
590 }
591
b106ad91
KW
592 /* If we were trying to do the initial refcount update for some cluster
593 * allocation, we might have used the same clusters to store newly
594 * allocated metadata. Make the caller search some new space. */
595 return -EAGAIN;
f7d0fe02 596
92dcb59f 597fail_table:
de82815d 598 g_free(new_blocks);
7267c094 599 g_free(new_table);
92dcb59f 600fail_block:
29c1a730 601 if (*refcount_block != NULL) {
7453c96b 602 qcow2_cache_put(bs, s->refcount_block_cache, refcount_block);
3b88e52b 603 }
29c1a730 604 return ret;
9923e05e
KW
605}
606
0b919fae
KW
607void qcow2_process_discards(BlockDriverState *bs, int ret)
608{
ff99129a 609 BDRVQcow2State *s = bs->opaque;
0b919fae
KW
610 Qcow2DiscardRegion *d, *next;
611
612 QTAILQ_FOREACH_SAFE(d, &s->discards, next, next) {
613 QTAILQ_REMOVE(&s->discards, d, next);
614
615 /* Discard is optional, ignore the return value */
616 if (ret >= 0) {
9a4f4c31 617 bdrv_discard(bs->file->bs,
0b919fae
KW
618 d->offset >> BDRV_SECTOR_BITS,
619 d->bytes >> BDRV_SECTOR_BITS);
620 }
621
622 g_free(d);
623 }
624}
625
626static void update_refcount_discard(BlockDriverState *bs,
627 uint64_t offset, uint64_t length)
628{
ff99129a 629 BDRVQcow2State *s = bs->opaque;
0b919fae
KW
630 Qcow2DiscardRegion *d, *p, *next;
631
632 QTAILQ_FOREACH(d, &s->discards, next) {
633 uint64_t new_start = MIN(offset, d->offset);
634 uint64_t new_end = MAX(offset + length, d->offset + d->bytes);
635
636 if (new_end - new_start <= length + d->bytes) {
637 /* There can't be any overlap, areas ending up here have no
638 * references any more and therefore shouldn't get freed another
639 * time. */
640 assert(d->bytes + length == new_end - new_start);
641 d->offset = new_start;
642 d->bytes = new_end - new_start;
643 goto found;
644 }
645 }
646
647 d = g_malloc(sizeof(*d));
648 *d = (Qcow2DiscardRegion) {
649 .bs = bs,
650 .offset = offset,
651 .bytes = length,
652 };
653 QTAILQ_INSERT_TAIL(&s->discards, d, next);
654
655found:
656 /* Merge discard requests if they are adjacent now */
657 QTAILQ_FOREACH_SAFE(p, &s->discards, next, next) {
658 if (p == d
659 || p->offset > d->offset + d->bytes
660 || d->offset > p->offset + p->bytes)
661 {
662 continue;
663 }
664
665 /* Still no overlap possible */
666 assert(p->offset == d->offset + d->bytes
667 || d->offset == p->offset + p->bytes);
668
669 QTAILQ_REMOVE(&s->discards, p, next);
670 d->offset = MIN(d->offset, p->offset);
671 d->bytes += p->bytes;
d8bb71b6 672 g_free(p);
0b919fae
KW
673 }
674}
675
f7d0fe02 676/* XXX: cache several refcount block clusters ? */
2aabe7c7
HR
677/* @addend is the absolute value of the addend; if @decrease is set, @addend
678 * will be subtracted from the current refcount, otherwise it will be added */
db3a964f 679static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
2aabe7c7
HR
680 int64_t offset,
681 int64_t length,
0e06528e 682 uint64_t addend,
2aabe7c7
HR
683 bool decrease,
684 enum qcow2_discard_type type)
f7d0fe02 685{
ff99129a 686 BDRVQcow2State *s = bs->opaque;
f7d0fe02 687 int64_t start, last, cluster_offset;
7453c96b 688 void *refcount_block = NULL;
29c1a730 689 int64_t old_table_index = -1;
09508d13 690 int ret;
f7d0fe02
KW
691
692#ifdef DEBUG_ALLOC2
2aabe7c7 693 fprintf(stderr, "update_refcount: offset=%" PRId64 " size=%" PRId64
0e06528e 694 " addend=%s%" PRIu64 "\n", offset, length, decrease ? "-" : "",
2aabe7c7 695 addend);
f7d0fe02 696#endif
7322afe7 697 if (length < 0) {
f7d0fe02 698 return -EINVAL;
7322afe7
KW
699 } else if (length == 0) {
700 return 0;
701 }
702
2aabe7c7 703 if (decrease) {
29c1a730
KW
704 qcow2_cache_set_dependency(bs, s->refcount_block_cache,
705 s->l2_table_cache);
706 }
707
ac95acdb
HT
708 start = start_of_cluster(s, offset);
709 last = start_of_cluster(s, offset + length - 1);
f7d0fe02
KW
710 for(cluster_offset = start; cluster_offset <= last;
711 cluster_offset += s->cluster_size)
712 {
2aabe7c7 713 int block_index;
0e06528e 714 uint64_t refcount;
f7d0fe02 715 int64_t cluster_index = cluster_offset >> s->cluster_bits;
17bd5f47 716 int64_t table_index = cluster_index >> s->refcount_block_bits;
f7d0fe02 717
29c1a730
KW
718 /* Load the refcount block and allocate it if needed */
719 if (table_index != old_table_index) {
720 if (refcount_block) {
a3f1afb4 721 qcow2_cache_put(bs, s->refcount_block_cache, &refcount_block);
29c1a730 722 }
29c1a730 723 ret = alloc_refcount_block(bs, cluster_index, &refcount_block);
ed0df867 724 if (ret < 0) {
29c1a730 725 goto fail;
f7d0fe02 726 }
f7d0fe02 727 }
29c1a730 728 old_table_index = table_index;
f7d0fe02 729
72e80b89
AG
730 qcow2_cache_entry_mark_dirty(bs, s->refcount_block_cache,
731 refcount_block);
f7d0fe02
KW
732
733 /* we can update the count and save it */
17bd5f47 734 block_index = cluster_index & (s->refcount_block_size - 1);
f7d0fe02 735
7453c96b 736 refcount = s->get_refcount(refcount_block, block_index);
0e06528e
HR
737 if (decrease ? (refcount - addend > refcount)
738 : (refcount + addend < refcount ||
739 refcount + addend > s->refcount_max))
2aabe7c7 740 {
09508d13
KW
741 ret = -EINVAL;
742 goto fail;
743 }
2aabe7c7
HR
744 if (decrease) {
745 refcount -= addend;
746 } else {
747 refcount += addend;
748 }
f7d0fe02
KW
749 if (refcount == 0 && cluster_index < s->free_cluster_index) {
750 s->free_cluster_index = cluster_index;
751 }
7453c96b 752 s->set_refcount(refcount_block, block_index, refcount);
0b919fae 753
67af674e 754 if (refcount == 0 && s->discard_passthrough[type]) {
0b919fae 755 update_refcount_discard(bs, cluster_offset, s->cluster_size);
67af674e 756 }
f7d0fe02
KW
757 }
758
09508d13
KW
759 ret = 0;
760fail:
0b919fae
KW
761 if (!s->cache_discards) {
762 qcow2_process_discards(bs, ret);
763 }
764
f7d0fe02 765 /* Write last changed block to disk */
29c1a730 766 if (refcount_block) {
a3f1afb4 767 qcow2_cache_put(bs, s->refcount_block_cache, &refcount_block);
f7d0fe02
KW
768 }
769
09508d13
KW
770 /*
771 * Try do undo any updates if an error is returned (This may succeed in
772 * some cases like ENOSPC for allocating a new refcount block)
773 */
774 if (ret < 0) {
775 int dummy;
2aabe7c7
HR
776 dummy = update_refcount(bs, offset, cluster_offset - offset, addend,
777 !decrease, QCOW2_DISCARD_NEVER);
83e3f76c 778 (void)dummy;
09508d13
KW
779 }
780
781 return ret;
f7d0fe02
KW
782}
783
018faafd 784/*
44751917 785 * Increases or decreases the refcount of a given cluster.
018faafd 786 *
2aabe7c7
HR
787 * @addend is the absolute value of the addend; if @decrease is set, @addend
788 * will be subtracted from the current refcount, otherwise it will be added.
789 *
c6e9d8ae 790 * On success 0 is returned; on failure -errno is returned.
018faafd 791 */
32b6444d
HR
792int qcow2_update_cluster_refcount(BlockDriverState *bs,
793 int64_t cluster_index,
0e06528e 794 uint64_t addend, bool decrease,
32b6444d 795 enum qcow2_discard_type type)
f7d0fe02 796{
ff99129a 797 BDRVQcow2State *s = bs->opaque;
f7d0fe02
KW
798 int ret;
799
6cfcb9b8 800 ret = update_refcount(bs, cluster_index << s->cluster_bits, 1, addend,
2aabe7c7 801 decrease, type);
f7d0fe02
KW
802 if (ret < 0) {
803 return ret;
804 }
805
c6e9d8ae 806 return 0;
f7d0fe02
KW
807}
808
809
810
811/*********************************************************/
812/* cluster allocation functions */
813
814
815
816/* return < 0 if error */
bb572aef 817static int64_t alloc_clusters_noref(BlockDriverState *bs, uint64_t size)
f7d0fe02 818{
ff99129a 819 BDRVQcow2State *s = bs->opaque;
0e06528e 820 uint64_t i, nb_clusters, refcount;
7324c10f 821 int ret;
f7d0fe02 822
ecbda7a2
KW
823 /* We can't allocate clusters if they may still be queued for discard. */
824 if (s->cache_discards) {
825 qcow2_process_discards(bs, 0);
826 }
827
f7d0fe02
KW
828 nb_clusters = size_to_clusters(s, size);
829retry:
830 for(i = 0; i < nb_clusters; i++) {
bb572aef 831 uint64_t next_cluster_index = s->free_cluster_index++;
7324c10f 832 ret = qcow2_get_refcount(bs, next_cluster_index, &refcount);
2eaa8f63 833
7324c10f
HR
834 if (ret < 0) {
835 return ret;
2eaa8f63 836 } else if (refcount != 0) {
f7d0fe02 837 goto retry;
2eaa8f63 838 }
f7d0fe02 839 }
91f827dc
HR
840
841 /* Make sure that all offsets in the "allocated" range are representable
842 * in an int64_t */
65f33bc0
HR
843 if (s->free_cluster_index > 0 &&
844 s->free_cluster_index - 1 > (INT64_MAX >> s->cluster_bits))
845 {
91f827dc
HR
846 return -EFBIG;
847 }
848
f7d0fe02 849#ifdef DEBUG_ALLOC2
35ee5e39 850 fprintf(stderr, "alloc_clusters: size=%" PRId64 " -> %" PRId64 "\n",
f7d0fe02
KW
851 size,
852 (s->free_cluster_index - nb_clusters) << s->cluster_bits);
853#endif
854 return (s->free_cluster_index - nb_clusters) << s->cluster_bits;
855}
856
bb572aef 857int64_t qcow2_alloc_clusters(BlockDriverState *bs, uint64_t size)
f7d0fe02
KW
858{
859 int64_t offset;
db3a964f 860 int ret;
f7d0fe02 861
66f82cee 862 BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC);
b106ad91
KW
863 do {
864 offset = alloc_clusters_noref(bs, size);
865 if (offset < 0) {
866 return offset;
867 }
868
2aabe7c7 869 ret = update_refcount(bs, offset, size, 1, false, QCOW2_DISCARD_NEVER);
b106ad91 870 } while (ret == -EAGAIN);
2eaa8f63 871
db3a964f
KW
872 if (ret < 0) {
873 return ret;
874 }
1c4c2814 875
f7d0fe02
KW
876 return offset;
877}
878
b6d36def
HR
879int64_t qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
880 int64_t nb_clusters)
256900b1 881{
ff99129a 882 BDRVQcow2State *s = bs->opaque;
0e06528e 883 uint64_t cluster_index, refcount;
33304ec9 884 uint64_t i;
7324c10f 885 int ret;
33304ec9
HT
886
887 assert(nb_clusters >= 0);
888 if (nb_clusters == 0) {
889 return 0;
890 }
256900b1 891
b106ad91
KW
892 do {
893 /* Check how many clusters there are free */
894 cluster_index = offset >> s->cluster_bits;
895 for(i = 0; i < nb_clusters; i++) {
7324c10f
HR
896 ret = qcow2_get_refcount(bs, cluster_index++, &refcount);
897 if (ret < 0) {
898 return ret;
b106ad91
KW
899 } else if (refcount != 0) {
900 break;
901 }
256900b1 902 }
256900b1 903
b106ad91 904 /* And then allocate them */
2aabe7c7 905 ret = update_refcount(bs, offset, i << s->cluster_bits, 1, false,
b106ad91
KW
906 QCOW2_DISCARD_NEVER);
907 } while (ret == -EAGAIN);
f24423bd 908
256900b1
KW
909 if (ret < 0) {
910 return ret;
911 }
912
913 return i;
914}
915
f7d0fe02
KW
916/* only used to allocate compressed sectors. We try to allocate
917 contiguous sectors. size must be <= cluster_size */
ed6ccf0f 918int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size)
f7d0fe02 919{
ff99129a 920 BDRVQcow2State *s = bs->opaque;
8c44dfbc
HR
921 int64_t offset;
922 size_t free_in_cluster;
923 int ret;
f7d0fe02 924
66f82cee 925 BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC_BYTES);
f7d0fe02 926 assert(size > 0 && size <= s->cluster_size);
8c44dfbc
HR
927 assert(!s->free_byte_offset || offset_into_cluster(s, s->free_byte_offset));
928
929 offset = s->free_byte_offset;
930
931 if (offset) {
0e06528e 932 uint64_t refcount;
7324c10f
HR
933 ret = qcow2_get_refcount(bs, offset >> s->cluster_bits, &refcount);
934 if (ret < 0) {
935 return ret;
5d757b56 936 }
8c44dfbc 937
346a53df 938 if (refcount == s->refcount_max) {
8c44dfbc 939 offset = 0;
5d757b56 940 }
8c44dfbc
HR
941 }
942
943 free_in_cluster = s->cluster_size - offset_into_cluster(s, offset);
3e5feb62
JM
944 do {
945 if (!offset || free_in_cluster < size) {
946 int64_t new_cluster = alloc_clusters_noref(bs, s->cluster_size);
947 if (new_cluster < 0) {
948 return new_cluster;
949 }
8c44dfbc 950
3e5feb62
JM
951 if (!offset || ROUND_UP(offset, s->cluster_size) != new_cluster) {
952 offset = new_cluster;
2ac01520
HR
953 free_in_cluster = s->cluster_size;
954 } else {
955 free_in_cluster += s->cluster_size;
3e5feb62 956 }
f7d0fe02 957 }
29216ed1 958
3e5feb62
JM
959 assert(offset);
960 ret = update_refcount(bs, offset, size, 1, false, QCOW2_DISCARD_NEVER);
2ac01520
HR
961 if (ret < 0) {
962 offset = 0;
963 }
3e5feb62 964 } while (ret == -EAGAIN);
8c44dfbc
HR
965 if (ret < 0) {
966 return ret;
967 }
968
969 /* The cluster refcount was incremented; refcount blocks must be flushed
970 * before the caller's L2 table updates. */
c1f5bafd 971 qcow2_cache_set_dependency(bs, s->l2_table_cache, s->refcount_block_cache);
8c44dfbc
HR
972
973 s->free_byte_offset = offset + size;
974 if (!offset_into_cluster(s, s->free_byte_offset)) {
975 s->free_byte_offset = 0;
976 }
977
f7d0fe02
KW
978 return offset;
979}
980
ed6ccf0f 981void qcow2_free_clusters(BlockDriverState *bs,
6cfcb9b8
KW
982 int64_t offset, int64_t size,
983 enum qcow2_discard_type type)
f7d0fe02 984{
db3a964f
KW
985 int ret;
986
66f82cee 987 BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_FREE);
2aabe7c7 988 ret = update_refcount(bs, offset, size, 1, true, type);
db3a964f
KW
989 if (ret < 0) {
990 fprintf(stderr, "qcow2_free_clusters failed: %s\n", strerror(-ret));
003fad6e 991 /* TODO Remember the clusters to free them later and avoid leaking */
db3a964f 992 }
f7d0fe02
KW
993}
994
45aba42f 995/*
c7a4c37a
KW
996 * Free a cluster using its L2 entry (handles clusters of all types, e.g.
997 * normal cluster, compressed cluster, etc.)
45aba42f 998 */
6cfcb9b8
KW
999void qcow2_free_any_clusters(BlockDriverState *bs, uint64_t l2_entry,
1000 int nb_clusters, enum qcow2_discard_type type)
45aba42f 1001{
ff99129a 1002 BDRVQcow2State *s = bs->opaque;
45aba42f 1003
c7a4c37a
KW
1004 switch (qcow2_get_cluster_type(l2_entry)) {
1005 case QCOW2_CLUSTER_COMPRESSED:
1006 {
1007 int nb_csectors;
1008 nb_csectors = ((l2_entry >> s->csize_shift) &
1009 s->csize_mask) + 1;
1010 qcow2_free_clusters(bs,
1011 (l2_entry & s->cluster_offset_mask) & ~511,
6cfcb9b8 1012 nb_csectors * 512, type);
c7a4c37a
KW
1013 }
1014 break;
1015 case QCOW2_CLUSTER_NORMAL:
8f730dd2
HR
1016 case QCOW2_CLUSTER_ZERO:
1017 if (l2_entry & L2E_OFFSET_MASK) {
a97c67ee
HR
1018 if (offset_into_cluster(s, l2_entry & L2E_OFFSET_MASK)) {
1019 qcow2_signal_corruption(bs, false, -1, -1,
1020 "Cannot free unaligned cluster %#llx",
1021 l2_entry & L2E_OFFSET_MASK);
1022 } else {
1023 qcow2_free_clusters(bs, l2_entry & L2E_OFFSET_MASK,
1024 nb_clusters << s->cluster_bits, type);
1025 }
8f730dd2 1026 }
c7a4c37a
KW
1027 break;
1028 case QCOW2_CLUSTER_UNALLOCATED:
1029 break;
1030 default:
1031 abort();
45aba42f 1032 }
45aba42f
KW
1033}
1034
f7d0fe02
KW
1035
1036
1037/*********************************************************/
1038/* snapshots and image creation */
1039
1040
1041
f7d0fe02 1042/* update the refcounts of snapshots and the copied flag */
ed6ccf0f
KW
1043int qcow2_update_snapshot_refcount(BlockDriverState *bs,
1044 int64_t l1_table_offset, int l1_size, int addend)
f7d0fe02 1045{
ff99129a 1046 BDRVQcow2State *s = bs->opaque;
0e06528e 1047 uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2, refcount;
de82815d 1048 bool l1_allocated = false;
f7d0fe02 1049 int64_t old_offset, old_l2_offset;
7324c10f 1050 int i, j, l1_modified = 0, nb_csectors;
29c1a730 1051 int ret;
f7d0fe02 1052
2aabe7c7
HR
1053 assert(addend >= -1 && addend <= 1);
1054
f7d0fe02
KW
1055 l2_table = NULL;
1056 l1_table = NULL;
1057 l1_size2 = l1_size * sizeof(uint64_t);
43a0cac4 1058
0b919fae
KW
1059 s->cache_discards = true;
1060
43a0cac4
KW
1061 /* WARNING: qcow2_snapshot_goto relies on this function not using the
1062 * l1_table_offset when it is the current s->l1_table_offset! Be careful
1063 * when changing this! */
f7d0fe02 1064 if (l1_table_offset != s->l1_table_offset) {
de82815d
KW
1065 l1_table = g_try_malloc0(align_offset(l1_size2, 512));
1066 if (l1_size2 && l1_table == NULL) {
1067 ret = -ENOMEM;
1068 goto fail;
1069 }
1070 l1_allocated = true;
c2bc78b6 1071
9a4f4c31 1072 ret = bdrv_pread(bs->file->bs, l1_table_offset, l1_table, l1_size2);
c2bc78b6 1073 if (ret < 0) {
f7d0fe02 1074 goto fail;
93913dfd
KW
1075 }
1076
f7d0fe02
KW
1077 for(i = 0;i < l1_size; i++)
1078 be64_to_cpus(&l1_table[i]);
1079 } else {
1080 assert(l1_size == s->l1_size);
1081 l1_table = s->l1_table;
de82815d 1082 l1_allocated = false;
f7d0fe02
KW
1083 }
1084
f7d0fe02
KW
1085 for(i = 0; i < l1_size; i++) {
1086 l2_offset = l1_table[i];
1087 if (l2_offset) {
1088 old_l2_offset = l2_offset;
8e37f681 1089 l2_offset &= L1E_OFFSET_MASK;
29c1a730 1090
a97c67ee
HR
1091 if (offset_into_cluster(s, l2_offset)) {
1092 qcow2_signal_corruption(bs, true, -1, -1, "L2 table offset %#"
1093 PRIx64 " unaligned (L1 index: %#x)",
1094 l2_offset, i);
1095 ret = -EIO;
1096 goto fail;
1097 }
1098
29c1a730
KW
1099 ret = qcow2_cache_get(bs, s->l2_table_cache, l2_offset,
1100 (void**) &l2_table);
1101 if (ret < 0) {
f7d0fe02 1102 goto fail;
29c1a730
KW
1103 }
1104
f7d0fe02 1105 for(j = 0; j < s->l2_size; j++) {
8b81a7b6
HR
1106 uint64_t cluster_index;
1107
f7d0fe02 1108 offset = be64_to_cpu(l2_table[j]);
8b81a7b6
HR
1109 old_offset = offset;
1110 offset &= ~QCOW_OFLAG_COPIED;
1111
1112 switch (qcow2_get_cluster_type(offset)) {
1113 case QCOW2_CLUSTER_COMPRESSED:
f7d0fe02
KW
1114 nb_csectors = ((offset >> s->csize_shift) &
1115 s->csize_mask) + 1;
db3a964f 1116 if (addend != 0) {
db3a964f
KW
1117 ret = update_refcount(bs,
1118 (offset & s->cluster_offset_mask) & ~511,
2aabe7c7 1119 nb_csectors * 512, abs(addend), addend < 0,
6cfcb9b8 1120 QCOW2_DISCARD_SNAPSHOT);
db3a964f
KW
1121 if (ret < 0) {
1122 goto fail;
1123 }
1124 }
f7d0fe02
KW
1125 /* compressed clusters are never modified */
1126 refcount = 2;
8b81a7b6
HR
1127 break;
1128
1129 case QCOW2_CLUSTER_NORMAL:
1130 case QCOW2_CLUSTER_ZERO:
a97c67ee
HR
1131 if (offset_into_cluster(s, offset & L2E_OFFSET_MASK)) {
1132 qcow2_signal_corruption(bs, true, -1, -1, "Data "
1133 "cluster offset %#llx "
1134 "unaligned (L2 offset: %#"
1135 PRIx64 ", L2 index: %#x)",
1136 offset & L2E_OFFSET_MASK,
1137 l2_offset, j);
1138 ret = -EIO;
1139 goto fail;
1140 }
1141
8b81a7b6
HR
1142 cluster_index = (offset & L2E_OFFSET_MASK) >> s->cluster_bits;
1143 if (!cluster_index) {
1144 /* unallocated */
1145 refcount = 0;
1146 break;
1147 }
f7d0fe02 1148 if (addend != 0) {
c6e9d8ae 1149 ret = qcow2_update_cluster_refcount(bs,
2aabe7c7 1150 cluster_index, abs(addend), addend < 0,
32b6444d 1151 QCOW2_DISCARD_SNAPSHOT);
c6e9d8ae
HR
1152 if (ret < 0) {
1153 goto fail;
1154 }
f7d0fe02 1155 }
018faafd 1156
7324c10f
HR
1157 ret = qcow2_get_refcount(bs, cluster_index, &refcount);
1158 if (ret < 0) {
018faafd
KW
1159 goto fail;
1160 }
8b81a7b6 1161 break;
f7d0fe02 1162
8b81a7b6
HR
1163 case QCOW2_CLUSTER_UNALLOCATED:
1164 refcount = 0;
1165 break;
1166
1167 default:
1168 abort();
1169 }
1170
1171 if (refcount == 1) {
1172 offset |= QCOW_OFLAG_COPIED;
1173 }
1174 if (offset != old_offset) {
1175 if (addend > 0) {
1176 qcow2_cache_set_dependency(bs, s->l2_table_cache,
1177 s->refcount_block_cache);
f7d0fe02 1178 }
8b81a7b6 1179 l2_table[j] = cpu_to_be64(offset);
72e80b89
AG
1180 qcow2_cache_entry_mark_dirty(bs, s->l2_table_cache,
1181 l2_table);
f7d0fe02
KW
1182 }
1183 }
29c1a730 1184
a3f1afb4 1185 qcow2_cache_put(bs, s->l2_table_cache, (void **) &l2_table);
29c1a730 1186
f7d0fe02 1187 if (addend != 0) {
c6e9d8ae
HR
1188 ret = qcow2_update_cluster_refcount(bs, l2_offset >>
1189 s->cluster_bits,
2aabe7c7 1190 abs(addend), addend < 0,
c6e9d8ae
HR
1191 QCOW2_DISCARD_SNAPSHOT);
1192 if (ret < 0) {
1193 goto fail;
1194 }
f7d0fe02 1195 }
7324c10f
HR
1196 ret = qcow2_get_refcount(bs, l2_offset >> s->cluster_bits,
1197 &refcount);
1198 if (ret < 0) {
018faafd
KW
1199 goto fail;
1200 } else if (refcount == 1) {
f7d0fe02
KW
1201 l2_offset |= QCOW_OFLAG_COPIED;
1202 }
1203 if (l2_offset != old_l2_offset) {
1204 l1_table[i] = l2_offset;
1205 l1_modified = 1;
1206 }
1207 }
1208 }
93913dfd 1209
2154f24e 1210 ret = bdrv_flush(bs);
93913dfd
KW
1211fail:
1212 if (l2_table) {
1213 qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
1214 }
1215
0b919fae
KW
1216 s->cache_discards = false;
1217 qcow2_process_discards(bs, ret);
1218
43a0cac4 1219 /* Update L1 only if it isn't deleted anyway (addend = -1) */
c2b6ff51
KW
1220 if (ret == 0 && addend >= 0 && l1_modified) {
1221 for (i = 0; i < l1_size; i++) {
f7d0fe02 1222 cpu_to_be64s(&l1_table[i]);
c2b6ff51
KW
1223 }
1224
9a4f4c31
KW
1225 ret = bdrv_pwrite_sync(bs->file->bs, l1_table_offset,
1226 l1_table, l1_size2);
c2b6ff51
KW
1227
1228 for (i = 0; i < l1_size; i++) {
f7d0fe02 1229 be64_to_cpus(&l1_table[i]);
c2b6ff51 1230 }
f7d0fe02
KW
1231 }
1232 if (l1_allocated)
7267c094 1233 g_free(l1_table);
93913dfd 1234 return ret;
f7d0fe02
KW
1235}
1236
1237
1238
1239
1240/*********************************************************/
1241/* refcount checking functions */
1242
1243
ff99129a 1244static size_t refcount_array_byte_size(BDRVQcow2State *s, uint64_t entries)
5fee192e
HR
1245{
1246 /* This assertion holds because there is no way we can address more than
1247 * 2^(64 - 9) clusters at once (with cluster size 512 = 2^9, and because
1248 * offsets have to be representable in bytes); due to every cluster
1249 * corresponding to one refcount entry, we are well below that limit */
1250 assert(entries < (UINT64_C(1) << (64 - 9)));
1251
1252 /* Thanks to the assertion this will not overflow, because
1253 * s->refcount_order < 7.
1254 * (note: x << s->refcount_order == x * s->refcount_bits) */
1255 return DIV_ROUND_UP(entries << s->refcount_order, 8);
1256}
1257
1258/**
1259 * Reallocates *array so that it can hold new_size entries. *size must contain
1260 * the current number of entries in *array. If the reallocation fails, *array
1261 * and *size will not be modified and -errno will be returned. If the
1262 * reallocation is successful, *array will be set to the new buffer, *size
1263 * will be set to new_size and 0 will be returned. The size of the reallocated
1264 * refcount array buffer will be aligned to a cluster boundary, and the newly
1265 * allocated area will be zeroed.
1266 */
ff99129a 1267static int realloc_refcount_array(BDRVQcow2State *s, void **array,
5fee192e
HR
1268 int64_t *size, int64_t new_size)
1269{
b6d36def 1270 int64_t old_byte_size, new_byte_size;
7453c96b 1271 void *new_ptr;
5fee192e
HR
1272
1273 /* Round to clusters so the array can be directly written to disk */
1274 old_byte_size = size_to_clusters(s, refcount_array_byte_size(s, *size))
1275 * s->cluster_size;
1276 new_byte_size = size_to_clusters(s, refcount_array_byte_size(s, new_size))
1277 * s->cluster_size;
1278
1279 if (new_byte_size == old_byte_size) {
1280 *size = new_size;
1281 return 0;
1282 }
1283
1284 assert(new_byte_size > 0);
1285
b6d36def
HR
1286 if (new_byte_size > SIZE_MAX) {
1287 return -ENOMEM;
1288 }
1289
5fee192e
HR
1290 new_ptr = g_try_realloc(*array, new_byte_size);
1291 if (!new_ptr) {
1292 return -ENOMEM;
1293 }
1294
1295 if (new_byte_size > old_byte_size) {
b6d36def 1296 memset((char *)new_ptr + old_byte_size, 0,
5fee192e
HR
1297 new_byte_size - old_byte_size);
1298 }
1299
1300 *array = new_ptr;
1301 *size = new_size;
1302
1303 return 0;
1304}
f7d0fe02
KW
1305
1306/*
1307 * Increases the refcount for a range of clusters in a given refcount table.
1308 * This is used to construct a temporary refcount table out of L1 and L2 tables
b6af0975 1309 * which can be compared to the refcount table saved in the image.
f7d0fe02 1310 *
9ac228e0 1311 * Modifies the number of errors in res.
f7d0fe02 1312 */
fef4d3d5
HR
1313static int inc_refcounts(BlockDriverState *bs,
1314 BdrvCheckResult *res,
7453c96b 1315 void **refcount_table,
641bb63c 1316 int64_t *refcount_table_size,
fef4d3d5 1317 int64_t offset, int64_t size)
f7d0fe02 1318{
ff99129a 1319 BDRVQcow2State *s = bs->opaque;
7453c96b 1320 uint64_t start, last, cluster_offset, k, refcount;
5fee192e 1321 int ret;
f7d0fe02 1322
fef4d3d5
HR
1323 if (size <= 0) {
1324 return 0;
1325 }
f7d0fe02 1326
ac95acdb
HT
1327 start = start_of_cluster(s, offset);
1328 last = start_of_cluster(s, offset + size - 1);
f7d0fe02
KW
1329 for(cluster_offset = start; cluster_offset <= last;
1330 cluster_offset += s->cluster_size) {
1331 k = cluster_offset >> s->cluster_bits;
641bb63c 1332 if (k >= *refcount_table_size) {
5fee192e
HR
1333 ret = realloc_refcount_array(s, refcount_table,
1334 refcount_table_size, k + 1);
1335 if (ret < 0) {
641bb63c 1336 res->check_errors++;
5fee192e 1337 return ret;
f7d0fe02 1338 }
641bb63c
HR
1339 }
1340
7453c96b
HR
1341 refcount = s->get_refcount(*refcount_table, k);
1342 if (refcount == s->refcount_max) {
641bb63c
HR
1343 fprintf(stderr, "ERROR: overflow cluster offset=0x%" PRIx64
1344 "\n", cluster_offset);
1345 res->corruptions++;
7453c96b 1346 continue;
f7d0fe02 1347 }
7453c96b 1348 s->set_refcount(*refcount_table, k, refcount + 1);
f7d0fe02 1349 }
fef4d3d5
HR
1350
1351 return 0;
f7d0fe02
KW
1352}
1353
801f7044
SH
1354/* Flags for check_refcounts_l1() and check_refcounts_l2() */
1355enum {
fba31bae 1356 CHECK_FRAG_INFO = 0x2, /* update BlockFragInfo counters */
801f7044
SH
1357};
1358
f7d0fe02
KW
1359/*
1360 * Increases the refcount in the given refcount table for the all clusters
1361 * referenced in the L2 table. While doing so, performs some checks on L2
1362 * entries.
1363 *
1364 * Returns the number of errors found by the checks or -errno if an internal
1365 * error occurred.
1366 */
9ac228e0 1367static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res,
7453c96b
HR
1368 void **refcount_table,
1369 int64_t *refcount_table_size, int64_t l2_offset,
1370 int flags)
f7d0fe02 1371{
ff99129a 1372 BDRVQcow2State *s = bs->opaque;
afdf0abe 1373 uint64_t *l2_table, l2_entry;
fba31bae 1374 uint64_t next_contiguous_offset = 0;
ad27390c 1375 int i, l2_size, nb_csectors, ret;
f7d0fe02
KW
1376
1377 /* Read L2 table from disk */
1378 l2_size = s->l2_size * sizeof(uint64_t);
7267c094 1379 l2_table = g_malloc(l2_size);
f7d0fe02 1380
9a4f4c31 1381 ret = bdrv_pread(bs->file->bs, l2_offset, l2_table, l2_size);
ad27390c
HR
1382 if (ret < 0) {
1383 fprintf(stderr, "ERROR: I/O error in check_refcounts_l2\n");
1384 res->check_errors++;
f7d0fe02 1385 goto fail;
ad27390c 1386 }
f7d0fe02
KW
1387
1388 /* Do the actual checks */
1389 for(i = 0; i < s->l2_size; i++) {
afdf0abe
KW
1390 l2_entry = be64_to_cpu(l2_table[i]);
1391
1392 switch (qcow2_get_cluster_type(l2_entry)) {
1393 case QCOW2_CLUSTER_COMPRESSED:
1394 /* Compressed clusters don't have QCOW_OFLAG_COPIED */
1395 if (l2_entry & QCOW_OFLAG_COPIED) {
1396 fprintf(stderr, "ERROR: cluster %" PRId64 ": "
1397 "copied flag must never be set for compressed "
1398 "clusters\n", l2_entry >> s->cluster_bits);
1399 l2_entry &= ~QCOW_OFLAG_COPIED;
1400 res->corruptions++;
1401 }
f7d0fe02 1402
afdf0abe
KW
1403 /* Mark cluster as used */
1404 nb_csectors = ((l2_entry >> s->csize_shift) &
1405 s->csize_mask) + 1;
1406 l2_entry &= s->cluster_offset_mask;
fef4d3d5
HR
1407 ret = inc_refcounts(bs, res, refcount_table, refcount_table_size,
1408 l2_entry & ~511, nb_csectors * 512);
1409 if (ret < 0) {
1410 goto fail;
1411 }
fba31bae
SH
1412
1413 if (flags & CHECK_FRAG_INFO) {
1414 res->bfi.allocated_clusters++;
4db35162 1415 res->bfi.compressed_clusters++;
fba31bae
SH
1416
1417 /* Compressed clusters are fragmented by nature. Since they
1418 * take up sub-sector space but we only have sector granularity
1419 * I/O we need to re-read the same sectors even for adjacent
1420 * compressed clusters.
1421 */
1422 res->bfi.fragmented_clusters++;
1423 }
afdf0abe 1424 break;
f7d0fe02 1425
6377af48
KW
1426 case QCOW2_CLUSTER_ZERO:
1427 if ((l2_entry & L2E_OFFSET_MASK) == 0) {
1428 break;
1429 }
1430 /* fall through */
1431
afdf0abe
KW
1432 case QCOW2_CLUSTER_NORMAL:
1433 {
afdf0abe 1434 uint64_t offset = l2_entry & L2E_OFFSET_MASK;
f7d0fe02 1435
fba31bae
SH
1436 if (flags & CHECK_FRAG_INFO) {
1437 res->bfi.allocated_clusters++;
1438 if (next_contiguous_offset &&
1439 offset != next_contiguous_offset) {
1440 res->bfi.fragmented_clusters++;
1441 }
1442 next_contiguous_offset = offset + s->cluster_size;
1443 }
1444
afdf0abe 1445 /* Mark cluster as used */
fef4d3d5
HR
1446 ret = inc_refcounts(bs, res, refcount_table, refcount_table_size,
1447 offset, s->cluster_size);
1448 if (ret < 0) {
1449 goto fail;
1450 }
afdf0abe
KW
1451
1452 /* Correct offsets are cluster aligned */
ac95acdb 1453 if (offset_into_cluster(s, offset)) {
afdf0abe
KW
1454 fprintf(stderr, "ERROR offset=%" PRIx64 ": Cluster is not "
1455 "properly aligned; L2 entry corrupted.\n", offset);
1456 res->corruptions++;
1457 }
1458 break;
1459 }
1460
1461 case QCOW2_CLUSTER_UNALLOCATED:
1462 break;
1463
1464 default:
1465 abort();
f7d0fe02
KW
1466 }
1467 }
1468
7267c094 1469 g_free(l2_table);
9ac228e0 1470 return 0;
f7d0fe02
KW
1471
1472fail:
7267c094 1473 g_free(l2_table);
ad27390c 1474 return ret;
f7d0fe02
KW
1475}
1476
1477/*
1478 * Increases the refcount for the L1 table, its L2 tables and all referenced
1479 * clusters in the given refcount table. While doing so, performs some checks
1480 * on L1 and L2 entries.
1481 *
1482 * Returns the number of errors found by the checks or -errno if an internal
1483 * error occurred.
1484 */
1485static int check_refcounts_l1(BlockDriverState *bs,
9ac228e0 1486 BdrvCheckResult *res,
7453c96b 1487 void **refcount_table,
641bb63c 1488 int64_t *refcount_table_size,
f7d0fe02 1489 int64_t l1_table_offset, int l1_size,
801f7044 1490 int flags)
f7d0fe02 1491{
ff99129a 1492 BDRVQcow2State *s = bs->opaque;
fef4d3d5 1493 uint64_t *l1_table = NULL, l2_offset, l1_size2;
4f6ed88c 1494 int i, ret;
f7d0fe02
KW
1495
1496 l1_size2 = l1_size * sizeof(uint64_t);
1497
1498 /* Mark L1 table as used */
fef4d3d5
HR
1499 ret = inc_refcounts(bs, res, refcount_table, refcount_table_size,
1500 l1_table_offset, l1_size2);
1501 if (ret < 0) {
1502 goto fail;
1503 }
f7d0fe02
KW
1504
1505 /* Read L1 table entries from disk */
fef4d3d5 1506 if (l1_size2 > 0) {
de82815d
KW
1507 l1_table = g_try_malloc(l1_size2);
1508 if (l1_table == NULL) {
1509 ret = -ENOMEM;
ad27390c 1510 res->check_errors++;
de82815d
KW
1511 goto fail;
1512 }
9a4f4c31 1513 ret = bdrv_pread(bs->file->bs, l1_table_offset, l1_table, l1_size2);
ad27390c
HR
1514 if (ret < 0) {
1515 fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
1516 res->check_errors++;
702ef63f 1517 goto fail;
ad27390c 1518 }
702ef63f
KW
1519 for(i = 0;i < l1_size; i++)
1520 be64_to_cpus(&l1_table[i]);
1521 }
f7d0fe02
KW
1522
1523 /* Do the actual checks */
1524 for(i = 0; i < l1_size; i++) {
1525 l2_offset = l1_table[i];
1526 if (l2_offset) {
f7d0fe02 1527 /* Mark L2 table as used */
afdf0abe 1528 l2_offset &= L1E_OFFSET_MASK;
fef4d3d5
HR
1529 ret = inc_refcounts(bs, res, refcount_table, refcount_table_size,
1530 l2_offset, s->cluster_size);
1531 if (ret < 0) {
1532 goto fail;
1533 }
f7d0fe02
KW
1534
1535 /* L2 tables are cluster aligned */
ac95acdb 1536 if (offset_into_cluster(s, l2_offset)) {
f7d0fe02
KW
1537 fprintf(stderr, "ERROR l2_offset=%" PRIx64 ": Table is not "
1538 "cluster aligned; L1 entry corrupted\n", l2_offset);
9ac228e0 1539 res->corruptions++;
f7d0fe02
KW
1540 }
1541
1542 /* Process and check L2 entries */
9ac228e0 1543 ret = check_refcounts_l2(bs, res, refcount_table,
801f7044 1544 refcount_table_size, l2_offset, flags);
f7d0fe02
KW
1545 if (ret < 0) {
1546 goto fail;
1547 }
f7d0fe02
KW
1548 }
1549 }
7267c094 1550 g_free(l1_table);
9ac228e0 1551 return 0;
f7d0fe02
KW
1552
1553fail:
7267c094 1554 g_free(l1_table);
ad27390c 1555 return ret;
f7d0fe02
KW
1556}
1557
4f6ed88c
HR
1558/*
1559 * Checks the OFLAG_COPIED flag for all L1 and L2 entries.
1560 *
1561 * This function does not print an error message nor does it increment
44751917
HR
1562 * check_errors if qcow2_get_refcount fails (this is because such an error will
1563 * have been already detected and sufficiently signaled by the calling function
4f6ed88c
HR
1564 * (qcow2_check_refcounts) by the time this function is called).
1565 */
e23e400e
HR
1566static int check_oflag_copied(BlockDriverState *bs, BdrvCheckResult *res,
1567 BdrvCheckMode fix)
4f6ed88c 1568{
ff99129a 1569 BDRVQcow2State *s = bs->opaque;
4f6ed88c
HR
1570 uint64_t *l2_table = qemu_blockalign(bs, s->cluster_size);
1571 int ret;
0e06528e 1572 uint64_t refcount;
4f6ed88c
HR
1573 int i, j;
1574
1575 for (i = 0; i < s->l1_size; i++) {
1576 uint64_t l1_entry = s->l1_table[i];
1577 uint64_t l2_offset = l1_entry & L1E_OFFSET_MASK;
e23e400e 1578 bool l2_dirty = false;
4f6ed88c
HR
1579
1580 if (!l2_offset) {
1581 continue;
1582 }
1583
7324c10f
HR
1584 ret = qcow2_get_refcount(bs, l2_offset >> s->cluster_bits,
1585 &refcount);
1586 if (ret < 0) {
4f6ed88c
HR
1587 /* don't print message nor increment check_errors */
1588 continue;
1589 }
1590 if ((refcount == 1) != ((l1_entry & QCOW_OFLAG_COPIED) != 0)) {
e23e400e 1591 fprintf(stderr, "%s OFLAG_COPIED L2 cluster: l1_index=%d "
0e06528e 1592 "l1_entry=%" PRIx64 " refcount=%" PRIu64 "\n",
e23e400e
HR
1593 fix & BDRV_FIX_ERRORS ? "Repairing" :
1594 "ERROR",
4f6ed88c 1595 i, l1_entry, refcount);
e23e400e
HR
1596 if (fix & BDRV_FIX_ERRORS) {
1597 s->l1_table[i] = refcount == 1
1598 ? l1_entry | QCOW_OFLAG_COPIED
1599 : l1_entry & ~QCOW_OFLAG_COPIED;
1600 ret = qcow2_write_l1_entry(bs, i);
1601 if (ret < 0) {
1602 res->check_errors++;
1603 goto fail;
1604 }
1605 res->corruptions_fixed++;
1606 } else {
1607 res->corruptions++;
1608 }
4f6ed88c
HR
1609 }
1610
9a4f4c31 1611 ret = bdrv_pread(bs->file->bs, l2_offset, l2_table,
4f6ed88c
HR
1612 s->l2_size * sizeof(uint64_t));
1613 if (ret < 0) {
1614 fprintf(stderr, "ERROR: Could not read L2 table: %s\n",
1615 strerror(-ret));
1616 res->check_errors++;
1617 goto fail;
1618 }
1619
1620 for (j = 0; j < s->l2_size; j++) {
1621 uint64_t l2_entry = be64_to_cpu(l2_table[j]);
1622 uint64_t data_offset = l2_entry & L2E_OFFSET_MASK;
1623 int cluster_type = qcow2_get_cluster_type(l2_entry);
1624
1625 if ((cluster_type == QCOW2_CLUSTER_NORMAL) ||
1626 ((cluster_type == QCOW2_CLUSTER_ZERO) && (data_offset != 0))) {
7324c10f
HR
1627 ret = qcow2_get_refcount(bs,
1628 data_offset >> s->cluster_bits,
1629 &refcount);
1630 if (ret < 0) {
4f6ed88c
HR
1631 /* don't print message nor increment check_errors */
1632 continue;
1633 }
1634 if ((refcount == 1) != ((l2_entry & QCOW_OFLAG_COPIED) != 0)) {
e23e400e 1635 fprintf(stderr, "%s OFLAG_COPIED data cluster: "
0e06528e 1636 "l2_entry=%" PRIx64 " refcount=%" PRIu64 "\n",
e23e400e
HR
1637 fix & BDRV_FIX_ERRORS ? "Repairing" :
1638 "ERROR",
4f6ed88c 1639 l2_entry, refcount);
e23e400e
HR
1640 if (fix & BDRV_FIX_ERRORS) {
1641 l2_table[j] = cpu_to_be64(refcount == 1
1642 ? l2_entry | QCOW_OFLAG_COPIED
1643 : l2_entry & ~QCOW_OFLAG_COPIED);
1644 l2_dirty = true;
1645 res->corruptions_fixed++;
1646 } else {
1647 res->corruptions++;
1648 }
4f6ed88c
HR
1649 }
1650 }
1651 }
e23e400e
HR
1652
1653 if (l2_dirty) {
231bb267
HR
1654 ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L2,
1655 l2_offset, s->cluster_size);
e23e400e
HR
1656 if (ret < 0) {
1657 fprintf(stderr, "ERROR: Could not write L2 table; metadata "
1658 "overlap check failed: %s\n", strerror(-ret));
1659 res->check_errors++;
1660 goto fail;
1661 }
1662
9a4f4c31
KW
1663 ret = bdrv_pwrite(bs->file->bs, l2_offset, l2_table,
1664 s->cluster_size);
e23e400e
HR
1665 if (ret < 0) {
1666 fprintf(stderr, "ERROR: Could not write L2 table: %s\n",
1667 strerror(-ret));
1668 res->check_errors++;
1669 goto fail;
1670 }
1671 }
4f6ed88c
HR
1672 }
1673
1674 ret = 0;
1675
1676fail:
1677 qemu_vfree(l2_table);
1678 return ret;
1679}
1680
6ca56bf5
HR
1681/*
1682 * Checks consistency of refblocks and accounts for each refblock in
1683 * *refcount_table.
1684 */
1685static int check_refblocks(BlockDriverState *bs, BdrvCheckResult *res,
f307b255 1686 BdrvCheckMode fix, bool *rebuild,
7453c96b 1687 void **refcount_table, int64_t *nb_clusters)
6ca56bf5 1688{
ff99129a 1689 BDRVQcow2State *s = bs->opaque;
001c158d 1690 int64_t i, size;
fef4d3d5 1691 int ret;
6ca56bf5 1692
f7d0fe02 1693 for(i = 0; i < s->refcount_table_size; i++) {
6882c8fa 1694 uint64_t offset, cluster;
f7d0fe02 1695 offset = s->refcount_table[i];
6882c8fa 1696 cluster = offset >> s->cluster_bits;
746c3cb5
KW
1697
1698 /* Refcount blocks are cluster aligned */
ac95acdb 1699 if (offset_into_cluster(s, offset)) {
166acf54 1700 fprintf(stderr, "ERROR refcount block %" PRId64 " is not "
746c3cb5 1701 "cluster aligned; refcount table entry corrupted\n", i);
9ac228e0 1702 res->corruptions++;
f307b255 1703 *rebuild = true;
6882c8fa
KW
1704 continue;
1705 }
1706
6ca56bf5 1707 if (cluster >= *nb_clusters) {
001c158d
HR
1708 fprintf(stderr, "%s refcount block %" PRId64 " is outside image\n",
1709 fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR", i);
1710
1711 if (fix & BDRV_FIX_ERRORS) {
5fee192e 1712 int64_t new_nb_clusters;
001c158d
HR
1713
1714 if (offset > INT64_MAX - s->cluster_size) {
1715 ret = -EINVAL;
1716 goto resize_fail;
1717 }
1718
9a4f4c31 1719 ret = bdrv_truncate(bs->file->bs, offset + s->cluster_size);
001c158d
HR
1720 if (ret < 0) {
1721 goto resize_fail;
1722 }
9a4f4c31 1723 size = bdrv_getlength(bs->file->bs);
001c158d
HR
1724 if (size < 0) {
1725 ret = size;
1726 goto resize_fail;
1727 }
1728
5fee192e
HR
1729 new_nb_clusters = size_to_clusters(s, size);
1730 assert(new_nb_clusters >= *nb_clusters);
001c158d 1731
5fee192e
HR
1732 ret = realloc_refcount_array(s, refcount_table,
1733 nb_clusters, new_nb_clusters);
1734 if (ret < 0) {
001c158d 1735 res->check_errors++;
5fee192e 1736 return ret;
001c158d 1737 }
001c158d
HR
1738
1739 if (cluster >= *nb_clusters) {
1740 ret = -EINVAL;
1741 goto resize_fail;
1742 }
1743
1744 res->corruptions_fixed++;
1745 ret = inc_refcounts(bs, res, refcount_table, nb_clusters,
1746 offset, s->cluster_size);
1747 if (ret < 0) {
1748 return ret;
1749 }
1750 /* No need to check whether the refcount is now greater than 1:
1751 * This area was just allocated and zeroed, so it can only be
1752 * exactly 1 after inc_refcounts() */
1753 continue;
1754
1755resize_fail:
1756 res->corruptions++;
f307b255 1757 *rebuild = true;
001c158d
HR
1758 fprintf(stderr, "ERROR could not resize image: %s\n",
1759 strerror(-ret));
1760 } else {
1761 res->corruptions++;
1762 }
6882c8fa 1763 continue;
746c3cb5
KW
1764 }
1765
f7d0fe02 1766 if (offset != 0) {
641bb63c 1767 ret = inc_refcounts(bs, res, refcount_table, nb_clusters,
fef4d3d5
HR
1768 offset, s->cluster_size);
1769 if (ret < 0) {
1770 return ret;
1771 }
7453c96b 1772 if (s->get_refcount(*refcount_table, cluster) != 1) {
f307b255 1773 fprintf(stderr, "ERROR refcount block %" PRId64
7453c96b
HR
1774 " refcount=%" PRIu64 "\n", i,
1775 s->get_refcount(*refcount_table, cluster));
f307b255
HR
1776 res->corruptions++;
1777 *rebuild = true;
746c3cb5 1778 }
f7d0fe02
KW
1779 }
1780 }
1781
6ca56bf5
HR
1782 return 0;
1783}
1784
057a3fe5
HR
1785/*
1786 * Calculates an in-memory refcount table.
1787 */
1788static int calculate_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
f307b255 1789 BdrvCheckMode fix, bool *rebuild,
7453c96b 1790 void **refcount_table, int64_t *nb_clusters)
057a3fe5 1791{
ff99129a 1792 BDRVQcow2State *s = bs->opaque;
057a3fe5
HR
1793 int64_t i;
1794 QCowSnapshot *sn;
1795 int ret;
1796
9696df21 1797 if (!*refcount_table) {
5fee192e
HR
1798 int64_t old_size = 0;
1799 ret = realloc_refcount_array(s, refcount_table,
1800 &old_size, *nb_clusters);
1801 if (ret < 0) {
9696df21 1802 res->check_errors++;
5fee192e 1803 return ret;
9696df21 1804 }
057a3fe5
HR
1805 }
1806
1807 /* header */
641bb63c 1808 ret = inc_refcounts(bs, res, refcount_table, nb_clusters,
fef4d3d5
HR
1809 0, s->cluster_size);
1810 if (ret < 0) {
1811 return ret;
1812 }
057a3fe5
HR
1813
1814 /* current L1 table */
641bb63c 1815 ret = check_refcounts_l1(bs, res, refcount_table, nb_clusters,
057a3fe5
HR
1816 s->l1_table_offset, s->l1_size, CHECK_FRAG_INFO);
1817 if (ret < 0) {
1818 return ret;
1819 }
1820
1821 /* snapshots */
1822 for (i = 0; i < s->nb_snapshots; i++) {
1823 sn = s->snapshots + i;
641bb63c 1824 ret = check_refcounts_l1(bs, res, refcount_table, nb_clusters,
fef4d3d5 1825 sn->l1_table_offset, sn->l1_size, 0);
057a3fe5
HR
1826 if (ret < 0) {
1827 return ret;
1828 }
1829 }
641bb63c 1830 ret = inc_refcounts(bs, res, refcount_table, nb_clusters,
fef4d3d5
HR
1831 s->snapshots_offset, s->snapshots_size);
1832 if (ret < 0) {
1833 return ret;
1834 }
057a3fe5
HR
1835
1836 /* refcount data */
641bb63c 1837 ret = inc_refcounts(bs, res, refcount_table, nb_clusters,
fef4d3d5
HR
1838 s->refcount_table_offset,
1839 s->refcount_table_size * sizeof(uint64_t));
1840 if (ret < 0) {
1841 return ret;
1842 }
057a3fe5 1843
f307b255 1844 return check_refblocks(bs, res, fix, rebuild, refcount_table, nb_clusters);
057a3fe5
HR
1845}
1846
6ca56bf5
HR
1847/*
1848 * Compares the actual reference count for each cluster in the image against the
1849 * refcount as reported by the refcount structures on-disk.
1850 */
1851static void compare_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
f307b255
HR
1852 BdrvCheckMode fix, bool *rebuild,
1853 int64_t *highest_cluster,
7453c96b 1854 void *refcount_table, int64_t nb_clusters)
6ca56bf5 1855{
ff99129a 1856 BDRVQcow2State *s = bs->opaque;
6ca56bf5 1857 int64_t i;
0e06528e 1858 uint64_t refcount1, refcount2;
7324c10f 1859 int ret;
6ca56bf5
HR
1860
1861 for (i = 0, *highest_cluster = 0; i < nb_clusters; i++) {
7324c10f
HR
1862 ret = qcow2_get_refcount(bs, i, &refcount1);
1863 if (ret < 0) {
166acf54 1864 fprintf(stderr, "Can't get refcount for cluster %" PRId64 ": %s\n",
7324c10f 1865 i, strerror(-ret));
9ac228e0 1866 res->check_errors++;
f74550fd 1867 continue;
018faafd
KW
1868 }
1869
7453c96b 1870 refcount2 = s->get_refcount(refcount_table, i);
c6bb9ad1
FS
1871
1872 if (refcount1 > 0 || refcount2 > 0) {
6ca56bf5 1873 *highest_cluster = i;
c6bb9ad1
FS
1874 }
1875
f7d0fe02 1876 if (refcount1 != refcount2) {
166acf54
KW
1877 /* Check if we're allowed to fix the mismatch */
1878 int *num_fixed = NULL;
f307b255
HR
1879 if (refcount1 == 0) {
1880 *rebuild = true;
1881 } else if (refcount1 > refcount2 && (fix & BDRV_FIX_LEAKS)) {
166acf54
KW
1882 num_fixed = &res->leaks_fixed;
1883 } else if (refcount1 < refcount2 && (fix & BDRV_FIX_ERRORS)) {
1884 num_fixed = &res->corruptions_fixed;
1885 }
1886
0e06528e
HR
1887 fprintf(stderr, "%s cluster %" PRId64 " refcount=%" PRIu64
1888 " reference=%" PRIu64 "\n",
166acf54
KW
1889 num_fixed != NULL ? "Repairing" :
1890 refcount1 < refcount2 ? "ERROR" :
1891 "Leaked",
f7d0fe02 1892 i, refcount1, refcount2);
166acf54
KW
1893
1894 if (num_fixed) {
1895 ret = update_refcount(bs, i << s->cluster_bits, 1,
2aabe7c7
HR
1896 refcount_diff(refcount1, refcount2),
1897 refcount1 > refcount2,
6cfcb9b8 1898 QCOW2_DISCARD_ALWAYS);
166acf54
KW
1899 if (ret >= 0) {
1900 (*num_fixed)++;
1901 continue;
1902 }
1903 }
1904
1905 /* And if we couldn't, print an error */
9ac228e0
KW
1906 if (refcount1 < refcount2) {
1907 res->corruptions++;
1908 } else {
1909 res->leaks++;
1910 }
f7d0fe02
KW
1911 }
1912 }
6ca56bf5
HR
1913}
1914
c7c0681b
HR
1915/*
1916 * Allocates clusters using an in-memory refcount table (IMRT) in contrast to
1917 * the on-disk refcount structures.
1918 *
1919 * On input, *first_free_cluster tells where to start looking, and need not
1920 * actually be a free cluster; the returned offset will not be before that
1921 * cluster. On output, *first_free_cluster points to the first gap found, even
1922 * if that gap was too small to be used as the returned offset.
1923 *
1924 * Note that *first_free_cluster is a cluster index whereas the return value is
1925 * an offset.
1926 */
1927static int64_t alloc_clusters_imrt(BlockDriverState *bs,
1928 int cluster_count,
7453c96b 1929 void **refcount_table,
c7c0681b
HR
1930 int64_t *imrt_nb_clusters,
1931 int64_t *first_free_cluster)
1932{
ff99129a 1933 BDRVQcow2State *s = bs->opaque;
c7c0681b
HR
1934 int64_t cluster = *first_free_cluster, i;
1935 bool first_gap = true;
1936 int contiguous_free_clusters;
5fee192e 1937 int ret;
c7c0681b
HR
1938
1939 /* Starting at *first_free_cluster, find a range of at least cluster_count
1940 * continuously free clusters */
1941 for (contiguous_free_clusters = 0;
1942 cluster < *imrt_nb_clusters &&
1943 contiguous_free_clusters < cluster_count;
1944 cluster++)
1945 {
7453c96b 1946 if (!s->get_refcount(*refcount_table, cluster)) {
c7c0681b
HR
1947 contiguous_free_clusters++;
1948 if (first_gap) {
1949 /* If this is the first free cluster found, update
1950 * *first_free_cluster accordingly */
1951 *first_free_cluster = cluster;
1952 first_gap = false;
1953 }
1954 } else if (contiguous_free_clusters) {
1955 contiguous_free_clusters = 0;
1956 }
1957 }
1958
1959 /* If contiguous_free_clusters is greater than zero, it contains the number
1960 * of continuously free clusters until the current cluster; the first free
1961 * cluster in the current "gap" is therefore
1962 * cluster - contiguous_free_clusters */
1963
1964 /* If no such range could be found, grow the in-memory refcount table
1965 * accordingly to append free clusters at the end of the image */
1966 if (contiguous_free_clusters < cluster_count) {
c7c0681b
HR
1967 /* contiguous_free_clusters clusters are already empty at the image end;
1968 * we need cluster_count clusters; therefore, we have to allocate
1969 * cluster_count - contiguous_free_clusters new clusters at the end of
1970 * the image (which is the current value of cluster; note that cluster
1971 * may exceed old_imrt_nb_clusters if *first_free_cluster pointed beyond
1972 * the image end) */
5fee192e
HR
1973 ret = realloc_refcount_array(s, refcount_table, imrt_nb_clusters,
1974 cluster + cluster_count
1975 - contiguous_free_clusters);
1976 if (ret < 0) {
1977 return ret;
c7c0681b 1978 }
c7c0681b
HR
1979 }
1980
1981 /* Go back to the first free cluster */
1982 cluster -= contiguous_free_clusters;
1983 for (i = 0; i < cluster_count; i++) {
7453c96b 1984 s->set_refcount(*refcount_table, cluster + i, 1);
c7c0681b
HR
1985 }
1986
1987 return cluster << s->cluster_bits;
1988}
1989
1990/*
1991 * Creates a new refcount structure based solely on the in-memory information
1992 * given through *refcount_table. All necessary allocations will be reflected
1993 * in that array.
1994 *
1995 * On success, the old refcount structure is leaked (it will be covered by the
1996 * new refcount structure).
1997 */
1998static int rebuild_refcount_structure(BlockDriverState *bs,
1999 BdrvCheckResult *res,
7453c96b 2000 void **refcount_table,
c7c0681b
HR
2001 int64_t *nb_clusters)
2002{
ff99129a 2003 BDRVQcow2State *s = bs->opaque;
c7c0681b
HR
2004 int64_t first_free_cluster = 0, reftable_offset = -1, cluster = 0;
2005 int64_t refblock_offset, refblock_start, refblock_index;
2006 uint32_t reftable_size = 0;
2007 uint64_t *on_disk_reftable = NULL;
7453c96b
HR
2008 void *on_disk_refblock;
2009 int ret = 0;
c7c0681b
HR
2010 struct {
2011 uint64_t reftable_offset;
2012 uint32_t reftable_clusters;
2013 } QEMU_PACKED reftable_offset_and_clusters;
2014
2015 qcow2_cache_empty(bs, s->refcount_block_cache);
2016
2017write_refblocks:
2018 for (; cluster < *nb_clusters; cluster++) {
7453c96b 2019 if (!s->get_refcount(*refcount_table, cluster)) {
c7c0681b
HR
2020 continue;
2021 }
2022
2023 refblock_index = cluster >> s->refcount_block_bits;
2024 refblock_start = refblock_index << s->refcount_block_bits;
2025
2026 /* Don't allocate a cluster in a refblock already written to disk */
2027 if (first_free_cluster < refblock_start) {
2028 first_free_cluster = refblock_start;
2029 }
2030 refblock_offset = alloc_clusters_imrt(bs, 1, refcount_table,
2031 nb_clusters, &first_free_cluster);
2032 if (refblock_offset < 0) {
2033 fprintf(stderr, "ERROR allocating refblock: %s\n",
2034 strerror(-refblock_offset));
2035 res->check_errors++;
2036 ret = refblock_offset;
2037 goto fail;
2038 }
2039
2040 if (reftable_size <= refblock_index) {
2041 uint32_t old_reftable_size = reftable_size;
2042 uint64_t *new_on_disk_reftable;
2043
2044 reftable_size = ROUND_UP((refblock_index + 1) * sizeof(uint64_t),
2045 s->cluster_size) / sizeof(uint64_t);
2046 new_on_disk_reftable = g_try_realloc(on_disk_reftable,
2047 reftable_size *
2048 sizeof(uint64_t));
2049 if (!new_on_disk_reftable) {
2050 res->check_errors++;
2051 ret = -ENOMEM;
2052 goto fail;
2053 }
2054 on_disk_reftable = new_on_disk_reftable;
2055
2056 memset(on_disk_reftable + old_reftable_size, 0,
2057 (reftable_size - old_reftable_size) * sizeof(uint64_t));
2058
2059 /* The offset we have for the reftable is now no longer valid;
2060 * this will leak that range, but we can easily fix that by running
2061 * a leak-fixing check after this rebuild operation */
2062 reftable_offset = -1;
2063 }
2064 on_disk_reftable[refblock_index] = refblock_offset;
2065
2066 /* If this is apparently the last refblock (for now), try to squeeze the
2067 * reftable in */
2068 if (refblock_index == (*nb_clusters - 1) >> s->refcount_block_bits &&
2069 reftable_offset < 0)
2070 {
2071 uint64_t reftable_clusters = size_to_clusters(s, reftable_size *
2072 sizeof(uint64_t));
2073 reftable_offset = alloc_clusters_imrt(bs, reftable_clusters,
2074 refcount_table, nb_clusters,
2075 &first_free_cluster);
2076 if (reftable_offset < 0) {
2077 fprintf(stderr, "ERROR allocating reftable: %s\n",
2078 strerror(-reftable_offset));
2079 res->check_errors++;
2080 ret = reftable_offset;
2081 goto fail;
2082 }
2083 }
2084
2085 ret = qcow2_pre_write_overlap_check(bs, 0, refblock_offset,
2086 s->cluster_size);
2087 if (ret < 0) {
2088 fprintf(stderr, "ERROR writing refblock: %s\n", strerror(-ret));
2089 goto fail;
2090 }
2091
7453c96b
HR
2092 /* The size of *refcount_table is always cluster-aligned, therefore the
2093 * write operation will not overflow */
2094 on_disk_refblock = (void *)((char *) *refcount_table +
2095 refblock_index * s->cluster_size);
c7c0681b 2096
9a4f4c31 2097 ret = bdrv_write(bs->file->bs, refblock_offset / BDRV_SECTOR_SIZE,
7453c96b 2098 on_disk_refblock, s->cluster_sectors);
c7c0681b
HR
2099 if (ret < 0) {
2100 fprintf(stderr, "ERROR writing refblock: %s\n", strerror(-ret));
2101 goto fail;
2102 }
2103
2104 /* Go to the end of this refblock */
2105 cluster = refblock_start + s->refcount_block_size - 1;
2106 }
2107
2108 if (reftable_offset < 0) {
2109 uint64_t post_refblock_start, reftable_clusters;
2110
2111 post_refblock_start = ROUND_UP(*nb_clusters, s->refcount_block_size);
2112 reftable_clusters = size_to_clusters(s,
2113 reftable_size * sizeof(uint64_t));
2114 /* Not pretty but simple */
2115 if (first_free_cluster < post_refblock_start) {
2116 first_free_cluster = post_refblock_start;
2117 }
2118 reftable_offset = alloc_clusters_imrt(bs, reftable_clusters,
2119 refcount_table, nb_clusters,
2120 &first_free_cluster);
2121 if (reftable_offset < 0) {
2122 fprintf(stderr, "ERROR allocating reftable: %s\n",
2123 strerror(-reftable_offset));
2124 res->check_errors++;
2125 ret = reftable_offset;
2126 goto fail;
2127 }
2128
2129 goto write_refblocks;
2130 }
2131
2132 assert(on_disk_reftable);
2133
2134 for (refblock_index = 0; refblock_index < reftable_size; refblock_index++) {
2135 cpu_to_be64s(&on_disk_reftable[refblock_index]);
2136 }
2137
2138 ret = qcow2_pre_write_overlap_check(bs, 0, reftable_offset,
2139 reftable_size * sizeof(uint64_t));
2140 if (ret < 0) {
2141 fprintf(stderr, "ERROR writing reftable: %s\n", strerror(-ret));
2142 goto fail;
2143 }
2144
2145 assert(reftable_size < INT_MAX / sizeof(uint64_t));
9a4f4c31 2146 ret = bdrv_pwrite(bs->file->bs, reftable_offset, on_disk_reftable,
c7c0681b
HR
2147 reftable_size * sizeof(uint64_t));
2148 if (ret < 0) {
2149 fprintf(stderr, "ERROR writing reftable: %s\n", strerror(-ret));
2150 goto fail;
2151 }
2152
2153 /* Enter new reftable into the image header */
2154 cpu_to_be64w(&reftable_offset_and_clusters.reftable_offset,
2155 reftable_offset);
2156 cpu_to_be32w(&reftable_offset_and_clusters.reftable_clusters,
2157 size_to_clusters(s, reftable_size * sizeof(uint64_t)));
9a4f4c31
KW
2158 ret = bdrv_pwrite_sync(bs->file->bs, offsetof(QCowHeader,
2159 refcount_table_offset),
c7c0681b
HR
2160 &reftable_offset_and_clusters,
2161 sizeof(reftable_offset_and_clusters));
2162 if (ret < 0) {
2163 fprintf(stderr, "ERROR setting reftable: %s\n", strerror(-ret));
2164 goto fail;
2165 }
2166
2167 for (refblock_index = 0; refblock_index < reftable_size; refblock_index++) {
2168 be64_to_cpus(&on_disk_reftable[refblock_index]);
2169 }
2170 s->refcount_table = on_disk_reftable;
2171 s->refcount_table_offset = reftable_offset;
2172 s->refcount_table_size = reftable_size;
2173
2174 return 0;
2175
2176fail:
2177 g_free(on_disk_reftable);
2178 return ret;
2179}
2180
6ca56bf5
HR
2181/*
2182 * Checks an image for refcount consistency.
2183 *
2184 * Returns 0 if no errors are found, the number of errors in case the image is
2185 * detected as corrupted, and -errno when an internal error occurred.
2186 */
2187int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
2188 BdrvCheckMode fix)
2189{
ff99129a 2190 BDRVQcow2State *s = bs->opaque;
c7c0681b 2191 BdrvCheckResult pre_compare_res;
6ca56bf5 2192 int64_t size, highest_cluster, nb_clusters;
7453c96b 2193 void *refcount_table = NULL;
f307b255 2194 bool rebuild = false;
6ca56bf5
HR
2195 int ret;
2196
9a4f4c31 2197 size = bdrv_getlength(bs->file->bs);
6ca56bf5
HR
2198 if (size < 0) {
2199 res->check_errors++;
2200 return size;
2201 }
2202
2203 nb_clusters = size_to_clusters(s, size);
2204 if (nb_clusters > INT_MAX) {
2205 res->check_errors++;
2206 return -EFBIG;
2207 }
2208
2209 res->bfi.total_clusters =
2210 size_to_clusters(s, bs->total_sectors * BDRV_SECTOR_SIZE);
2211
f307b255
HR
2212 ret = calculate_refcounts(bs, res, fix, &rebuild, &refcount_table,
2213 &nb_clusters);
6ca56bf5
HR
2214 if (ret < 0) {
2215 goto fail;
2216 }
2217
c7c0681b
HR
2218 /* In case we don't need to rebuild the refcount structure (but want to fix
2219 * something), this function is immediately called again, in which case the
2220 * result should be ignored */
2221 pre_compare_res = *res;
2222 compare_refcounts(bs, res, 0, &rebuild, &highest_cluster, refcount_table,
6ca56bf5 2223 nb_clusters);
f7d0fe02 2224
c7c0681b 2225 if (rebuild && (fix & BDRV_FIX_ERRORS)) {
791230d8
HR
2226 BdrvCheckResult old_res = *res;
2227 int fresh_leaks = 0;
2228
c7c0681b
HR
2229 fprintf(stderr, "Rebuilding refcount structure\n");
2230 ret = rebuild_refcount_structure(bs, res, &refcount_table,
2231 &nb_clusters);
2232 if (ret < 0) {
2233 goto fail;
2234 }
791230d8
HR
2235
2236 res->corruptions = 0;
2237 res->leaks = 0;
2238
2239 /* Because the old reftable has been exchanged for a new one the
2240 * references have to be recalculated */
2241 rebuild = false;
7453c96b 2242 memset(refcount_table, 0, refcount_array_byte_size(s, nb_clusters));
791230d8
HR
2243 ret = calculate_refcounts(bs, res, 0, &rebuild, &refcount_table,
2244 &nb_clusters);
2245 if (ret < 0) {
2246 goto fail;
2247 }
2248
2249 if (fix & BDRV_FIX_LEAKS) {
2250 /* The old refcount structures are now leaked, fix it; the result
2251 * can be ignored, aside from leaks which were introduced by
2252 * rebuild_refcount_structure() that could not be fixed */
2253 BdrvCheckResult saved_res = *res;
2254 *res = (BdrvCheckResult){ 0 };
2255
2256 compare_refcounts(bs, res, BDRV_FIX_LEAKS, &rebuild,
2257 &highest_cluster, refcount_table, nb_clusters);
2258 if (rebuild) {
2259 fprintf(stderr, "ERROR rebuilt refcount structure is still "
2260 "broken\n");
2261 }
2262
2263 /* Any leaks accounted for here were introduced by
2264 * rebuild_refcount_structure() because that function has created a
2265 * new refcount structure from scratch */
2266 fresh_leaks = res->leaks;
2267 *res = saved_res;
2268 }
2269
2270 if (res->corruptions < old_res.corruptions) {
2271 res->corruptions_fixed += old_res.corruptions - res->corruptions;
2272 }
2273 if (res->leaks < old_res.leaks) {
2274 res->leaks_fixed += old_res.leaks - res->leaks;
2275 }
2276 res->leaks += fresh_leaks;
c7c0681b
HR
2277 } else if (fix) {
2278 if (rebuild) {
2279 fprintf(stderr, "ERROR need to rebuild refcount structures\n");
2280 res->check_errors++;
2281 ret = -EIO;
2282 goto fail;
2283 }
2284
2285 if (res->leaks || res->corruptions) {
2286 *res = pre_compare_res;
2287 compare_refcounts(bs, res, fix, &rebuild, &highest_cluster,
2288 refcount_table, nb_clusters);
2289 }
f307b255
HR
2290 }
2291
4f6ed88c 2292 /* check OFLAG_COPIED */
e23e400e 2293 ret = check_oflag_copied(bs, res, fix);
4f6ed88c
HR
2294 if (ret < 0) {
2295 goto fail;
2296 }
2297
c6bb9ad1 2298 res->image_end_offset = (highest_cluster + 1) * s->cluster_size;
80fa3341
KW
2299 ret = 0;
2300
2301fail:
7267c094 2302 g_free(refcount_table);
f7d0fe02 2303
80fa3341 2304 return ret;
f7d0fe02
KW
2305}
2306
a40f1c2a
HR
2307#define overlaps_with(ofs, sz) \
2308 ranges_overlap(offset, size, ofs, sz)
2309
2310/*
2311 * Checks if the given offset into the image file is actually free to use by
2312 * looking for overlaps with important metadata sections (L1/L2 tables etc.),
2313 * i.e. a sanity check without relying on the refcount tables.
2314 *
231bb267
HR
2315 * The ign parameter specifies what checks not to perform (being a bitmask of
2316 * QCow2MetadataOverlap values), i.e., what sections to ignore.
a40f1c2a
HR
2317 *
2318 * Returns:
2319 * - 0 if writing to this offset will not affect the mentioned metadata
2320 * - a positive QCow2MetadataOverlap value indicating one overlapping section
2321 * - a negative value (-errno) indicating an error while performing a check,
2322 * e.g. when bdrv_read failed on QCOW2_OL_INACTIVE_L2
2323 */
231bb267 2324int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,
a40f1c2a
HR
2325 int64_t size)
2326{
ff99129a 2327 BDRVQcow2State *s = bs->opaque;
3e355390 2328 int chk = s->overlap_check & ~ign;
a40f1c2a
HR
2329 int i, j;
2330
2331 if (!size) {
2332 return 0;
2333 }
2334
2335 if (chk & QCOW2_OL_MAIN_HEADER) {
2336 if (offset < s->cluster_size) {
2337 return QCOW2_OL_MAIN_HEADER;
2338 }
2339 }
2340
2341 /* align range to test to cluster boundaries */
2342 size = align_offset(offset_into_cluster(s, offset) + size, s->cluster_size);
2343 offset = start_of_cluster(s, offset);
2344
2345 if ((chk & QCOW2_OL_ACTIVE_L1) && s->l1_size) {
2346 if (overlaps_with(s->l1_table_offset, s->l1_size * sizeof(uint64_t))) {
2347 return QCOW2_OL_ACTIVE_L1;
2348 }
2349 }
2350
2351 if ((chk & QCOW2_OL_REFCOUNT_TABLE) && s->refcount_table_size) {
2352 if (overlaps_with(s->refcount_table_offset,
2353 s->refcount_table_size * sizeof(uint64_t))) {
2354 return QCOW2_OL_REFCOUNT_TABLE;
2355 }
2356 }
2357
2358 if ((chk & QCOW2_OL_SNAPSHOT_TABLE) && s->snapshots_size) {
2359 if (overlaps_with(s->snapshots_offset, s->snapshots_size)) {
2360 return QCOW2_OL_SNAPSHOT_TABLE;
2361 }
2362 }
2363
2364 if ((chk & QCOW2_OL_INACTIVE_L1) && s->snapshots) {
2365 for (i = 0; i < s->nb_snapshots; i++) {
2366 if (s->snapshots[i].l1_size &&
2367 overlaps_with(s->snapshots[i].l1_table_offset,
2368 s->snapshots[i].l1_size * sizeof(uint64_t))) {
2369 return QCOW2_OL_INACTIVE_L1;
2370 }
2371 }
2372 }
2373
2374 if ((chk & QCOW2_OL_ACTIVE_L2) && s->l1_table) {
2375 for (i = 0; i < s->l1_size; i++) {
2376 if ((s->l1_table[i] & L1E_OFFSET_MASK) &&
2377 overlaps_with(s->l1_table[i] & L1E_OFFSET_MASK,
2378 s->cluster_size)) {
2379 return QCOW2_OL_ACTIVE_L2;
2380 }
2381 }
2382 }
2383
2384 if ((chk & QCOW2_OL_REFCOUNT_BLOCK) && s->refcount_table) {
2385 for (i = 0; i < s->refcount_table_size; i++) {
2386 if ((s->refcount_table[i] & REFT_OFFSET_MASK) &&
2387 overlaps_with(s->refcount_table[i] & REFT_OFFSET_MASK,
2388 s->cluster_size)) {
2389 return QCOW2_OL_REFCOUNT_BLOCK;
2390 }
2391 }
2392 }
2393
2394 if ((chk & QCOW2_OL_INACTIVE_L2) && s->snapshots) {
2395 for (i = 0; i < s->nb_snapshots; i++) {
2396 uint64_t l1_ofs = s->snapshots[i].l1_table_offset;
2397 uint32_t l1_sz = s->snapshots[i].l1_size;
998b959c 2398 uint64_t l1_sz2 = l1_sz * sizeof(uint64_t);
de82815d 2399 uint64_t *l1 = g_try_malloc(l1_sz2);
a40f1c2a
HR
2400 int ret;
2401
de82815d
KW
2402 if (l1_sz2 && l1 == NULL) {
2403 return -ENOMEM;
2404 }
2405
9a4f4c31 2406 ret = bdrv_pread(bs->file->bs, l1_ofs, l1, l1_sz2);
a40f1c2a
HR
2407 if (ret < 0) {
2408 g_free(l1);
2409 return ret;
2410 }
2411
2412 for (j = 0; j < l1_sz; j++) {
1e242b55
HR
2413 uint64_t l2_ofs = be64_to_cpu(l1[j]) & L1E_OFFSET_MASK;
2414 if (l2_ofs && overlaps_with(l2_ofs, s->cluster_size)) {
a40f1c2a
HR
2415 g_free(l1);
2416 return QCOW2_OL_INACTIVE_L2;
2417 }
2418 }
2419
2420 g_free(l1);
2421 }
2422 }
2423
2424 return 0;
2425}
2426
2427static const char *metadata_ol_names[] = {
2428 [QCOW2_OL_MAIN_HEADER_BITNR] = "qcow2_header",
2429 [QCOW2_OL_ACTIVE_L1_BITNR] = "active L1 table",
2430 [QCOW2_OL_ACTIVE_L2_BITNR] = "active L2 table",
2431 [QCOW2_OL_REFCOUNT_TABLE_BITNR] = "refcount table",
2432 [QCOW2_OL_REFCOUNT_BLOCK_BITNR] = "refcount block",
2433 [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = "snapshot table",
2434 [QCOW2_OL_INACTIVE_L1_BITNR] = "inactive L1 table",
2435 [QCOW2_OL_INACTIVE_L2_BITNR] = "inactive L2 table",
2436};
2437
2438/*
2439 * First performs a check for metadata overlaps (through
2440 * qcow2_check_metadata_overlap); if that fails with a negative value (error
2441 * while performing a check), that value is returned. If an impending overlap
2442 * is detected, the BDS will be made unusable, the qcow2 file marked corrupt
2443 * and -EIO returned.
2444 *
2445 * Returns 0 if there were neither overlaps nor errors while checking for
2446 * overlaps; or a negative value (-errno) on error.
2447 */
231bb267 2448int qcow2_pre_write_overlap_check(BlockDriverState *bs, int ign, int64_t offset,
a40f1c2a
HR
2449 int64_t size)
2450{
231bb267 2451 int ret = qcow2_check_metadata_overlap(bs, ign, offset, size);
a40f1c2a
HR
2452
2453 if (ret < 0) {
2454 return ret;
2455 } else if (ret > 0) {
786a4ea8 2456 int metadata_ol_bitnr = ctz32(ret);
a40f1c2a
HR
2457 assert(metadata_ol_bitnr < QCOW2_OL_MAX_BITNR);
2458
adb43552
HR
2459 qcow2_signal_corruption(bs, true, offset, size, "Preventing invalid "
2460 "write on metadata (overlaps with %s)",
2461 metadata_ol_names[metadata_ol_bitnr]);
a40f1c2a
HR
2462 return -EIO;
2463 }
2464
2465 return 0;
2466}
This page took 0.833281 seconds and 4 git commands to generate.