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