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