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