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