qcow2: Fix corruption after error in update_refcount
[qemu/aliguori-queue.git] / block / qcow2-refcount.c
blob22b0b45ff7af9a3cccdda70468e843fd470cd4bc
1 /*
2 * Block driver for the QCOW version 2 format
4 * Copyright (c) 2004-2006 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include "qemu-common.h"
26 #include "block_int.h"
27 #include "block/qcow2.h"
29 static int64_t alloc_clusters_noref(BlockDriverState *bs, int64_t size);
30 static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
31 int64_t offset, int64_t length,
32 int addend);
35 static int cache_refcount_updates = 0;
37 static int write_refcount_block(BlockDriverState *bs)
39 BDRVQcowState *s = bs->opaque;
40 size_t size = s->cluster_size;
42 if (s->refcount_block_cache_offset == 0) {
43 return 0;
46 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_UPDATE);
47 if (bdrv_pwrite(bs->file, s->refcount_block_cache_offset,
48 s->refcount_block_cache, size) != size)
50 return -EIO;
53 return 0;
56 /*********************************************************/
57 /* refcount handling */
59 int qcow2_refcount_init(BlockDriverState *bs)
61 BDRVQcowState *s = bs->opaque;
62 int ret, refcount_table_size2, i;
64 s->refcount_block_cache = qemu_malloc(s->cluster_size);
65 refcount_table_size2 = s->refcount_table_size * sizeof(uint64_t);
66 s->refcount_table = qemu_malloc(refcount_table_size2);
67 if (s->refcount_table_size > 0) {
68 BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_LOAD);
69 ret = bdrv_pread(bs->file, s->refcount_table_offset,
70 s->refcount_table, refcount_table_size2);
71 if (ret != refcount_table_size2)
72 goto fail;
73 for(i = 0; i < s->refcount_table_size; i++)
74 be64_to_cpus(&s->refcount_table[i]);
76 return 0;
77 fail:
78 return -ENOMEM;
81 void qcow2_refcount_close(BlockDriverState *bs)
83 BDRVQcowState *s = bs->opaque;
84 qemu_free(s->refcount_block_cache);
85 qemu_free(s->refcount_table);
89 static int load_refcount_block(BlockDriverState *bs,
90 int64_t refcount_block_offset)
92 BDRVQcowState *s = bs->opaque;
93 int ret;
95 if (cache_refcount_updates) {
96 write_refcount_block(bs);
99 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_LOAD);
100 ret = bdrv_pread(bs->file, refcount_block_offset, s->refcount_block_cache,
101 s->cluster_size);
102 if (ret != s->cluster_size)
103 return -EIO;
104 s->refcount_block_cache_offset = refcount_block_offset;
105 return 0;
108 static int get_refcount(BlockDriverState *bs, int64_t cluster_index)
110 BDRVQcowState *s = bs->opaque;
111 int refcount_table_index, block_index;
112 int64_t refcount_block_offset;
114 refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
115 if (refcount_table_index >= s->refcount_table_size)
116 return 0;
117 refcount_block_offset = s->refcount_table[refcount_table_index];
118 if (!refcount_block_offset)
119 return 0;
120 if (refcount_block_offset != s->refcount_block_cache_offset) {
121 /* better than nothing: return allocated if read error */
122 if (load_refcount_block(bs, refcount_block_offset) < 0)
123 return 1;
125 block_index = cluster_index &
126 ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
127 return be16_to_cpu(s->refcount_block_cache[block_index]);
131 * Rounds the refcount table size up to avoid growing the table for each single
132 * refcount block that is allocated.
134 static unsigned int next_refcount_table_size(BDRVQcowState *s,
135 unsigned int min_size)
137 unsigned int min_clusters = (min_size >> (s->cluster_bits - 3)) + 1;
138 unsigned int refcount_table_clusters =
139 MAX(1, s->refcount_table_size >> (s->cluster_bits - 3));
141 while (min_clusters > refcount_table_clusters) {
142 refcount_table_clusters = (refcount_table_clusters * 3 + 1) / 2;
145 return refcount_table_clusters << (s->cluster_bits - 3);
149 /* Checks if two offsets are described by the same refcount block */
150 static int in_same_refcount_block(BDRVQcowState *s, uint64_t offset_a,
151 uint64_t offset_b)
153 uint64_t block_a = offset_a >> (2 * s->cluster_bits - REFCOUNT_SHIFT);
154 uint64_t block_b = offset_b >> (2 * s->cluster_bits - REFCOUNT_SHIFT);
156 return (block_a == block_b);
160 * Loads a refcount block. If it doesn't exist yet, it is allocated first
161 * (including growing the refcount table if needed).
163 * Returns the offset of the refcount block on success or -errno in error case
165 static int64_t alloc_refcount_block(BlockDriverState *bs, int64_t cluster_index)
167 BDRVQcowState *s = bs->opaque;
168 unsigned int refcount_table_index;
169 int ret;
171 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC);
173 /* Find the refcount block for the given cluster */
174 refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
176 if (refcount_table_index < s->refcount_table_size) {
178 uint64_t refcount_block_offset =
179 s->refcount_table[refcount_table_index];
181 /* If it's already there, we're done */
182 if (refcount_block_offset) {
183 if (refcount_block_offset != s->refcount_block_cache_offset) {
184 ret = load_refcount_block(bs, refcount_block_offset);
185 if (ret < 0) {
186 return ret;
189 return refcount_block_offset;
194 * If we came here, we need to allocate something. Something is at least
195 * a cluster for the new refcount block. It may also include a new refcount
196 * table if the old refcount table is too small.
198 * Note that allocating clusters here needs some special care:
200 * - We can't use the normal qcow2_alloc_clusters(), it would try to
201 * increase the refcount and very likely we would end up with an endless
202 * recursion. Instead we must place the refcount blocks in a way that
203 * they can describe them themselves.
205 * - We need to consider that at this point we are inside update_refcounts
206 * and doing the initial refcount increase. This means that some clusters
207 * have already been allocated by the caller, but their refcount isn't
208 * accurate yet. free_cluster_index tells us where this allocation ends
209 * as long as we don't overwrite it by freeing clusters.
211 * - alloc_clusters_noref and qcow2_free_clusters may load a different
212 * refcount block into the cache
215 if (cache_refcount_updates) {
216 ret = write_refcount_block(bs);
217 if (ret < 0) {
218 return ret;
222 /* Allocate the refcount block itself and mark it as used */
223 uint64_t new_block = alloc_clusters_noref(bs, s->cluster_size);
225 #ifdef DEBUG_ALLOC2
226 fprintf(stderr, "qcow2: Allocate refcount block %d for %" PRIx64
227 " at %" PRIx64 "\n",
228 refcount_table_index, cluster_index << s->cluster_bits, new_block);
229 #endif
231 if (in_same_refcount_block(s, new_block, cluster_index << s->cluster_bits)) {
232 /* Zero the new refcount block before updating it */
233 memset(s->refcount_block_cache, 0, s->cluster_size);
234 s->refcount_block_cache_offset = new_block;
236 /* The block describes itself, need to update the cache */
237 int block_index = (new_block >> s->cluster_bits) &
238 ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
239 s->refcount_block_cache[block_index] = cpu_to_be16(1);
240 } else {
241 /* Described somewhere else. This can recurse at most twice before we
242 * arrive at a block that describes itself. */
243 ret = update_refcount(bs, new_block, s->cluster_size, 1);
244 if (ret < 0) {
245 goto fail_block;
248 /* Initialize the new refcount block only after updating its refcount,
249 * update_refcount uses the refcount cache itself */
250 memset(s->refcount_block_cache, 0, s->cluster_size);
251 s->refcount_block_cache_offset = new_block;
254 /* Now the new refcount block needs to be written to disk */
255 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE);
256 ret = bdrv_pwrite(bs->file, new_block, s->refcount_block_cache,
257 s->cluster_size);
258 if (ret < 0) {
259 goto fail_block;
262 /* If the refcount table is big enough, just hook the block up there */
263 if (refcount_table_index < s->refcount_table_size) {
264 uint64_t data64 = cpu_to_be64(new_block);
265 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_HOOKUP);
266 ret = bdrv_pwrite(bs->file,
267 s->refcount_table_offset + refcount_table_index * sizeof(uint64_t),
268 &data64, sizeof(data64));
269 if (ret < 0) {
270 goto fail_block;
273 s->refcount_table[refcount_table_index] = new_block;
274 return new_block;
278 * If we come here, we need to grow the refcount table. Again, a new
279 * refcount table needs some space and we can't simply allocate to avoid
280 * endless recursion.
282 * Therefore let's grab new refcount blocks at the end of the image, which
283 * will describe themselves and the new refcount table. This way we can
284 * reference them only in the new table and do the switch to the new
285 * refcount table at once without producing an inconsistent state in
286 * between.
288 BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_GROW);
290 /* Calculate the number of refcount blocks needed so far */
291 uint64_t refcount_block_clusters = 1 << (s->cluster_bits - REFCOUNT_SHIFT);
292 uint64_t blocks_used = (s->free_cluster_index +
293 refcount_block_clusters - 1) / refcount_block_clusters;
295 /* And now we need at least one block more for the new metadata */
296 uint64_t table_size = next_refcount_table_size(s, blocks_used + 1);
297 uint64_t last_table_size;
298 uint64_t blocks_clusters;
299 do {
300 uint64_t table_clusters = size_to_clusters(s, table_size);
301 blocks_clusters = 1 +
302 ((table_clusters + refcount_block_clusters - 1)
303 / refcount_block_clusters);
304 uint64_t meta_clusters = table_clusters + blocks_clusters;
306 last_table_size = table_size;
307 table_size = next_refcount_table_size(s, blocks_used +
308 ((meta_clusters + refcount_block_clusters - 1)
309 / refcount_block_clusters));
311 } while (last_table_size != table_size);
313 #ifdef DEBUG_ALLOC2
314 fprintf(stderr, "qcow2: Grow refcount table %" PRId32 " => %" PRId64 "\n",
315 s->refcount_table_size, table_size);
316 #endif
318 /* Create the new refcount table and blocks */
319 uint64_t meta_offset = (blocks_used * refcount_block_clusters) *
320 s->cluster_size;
321 uint64_t table_offset = meta_offset + blocks_clusters * s->cluster_size;
322 uint16_t *new_blocks = qemu_mallocz(blocks_clusters * s->cluster_size);
323 uint64_t *new_table = qemu_mallocz(table_size * sizeof(uint64_t));
325 assert(meta_offset >= (s->free_cluster_index * s->cluster_size));
327 /* Fill the new refcount table */
328 memcpy(new_table, s->refcount_table,
329 s->refcount_table_size * sizeof(uint64_t));
330 new_table[refcount_table_index] = new_block;
332 int i;
333 for (i = 0; i < blocks_clusters; i++) {
334 new_table[blocks_used + i] = meta_offset + (i * s->cluster_size);
337 /* Fill the refcount blocks */
338 uint64_t table_clusters = size_to_clusters(s, table_size * sizeof(uint64_t));
339 int block = 0;
340 for (i = 0; i < table_clusters + blocks_clusters; i++) {
341 new_blocks[block++] = cpu_to_be16(1);
344 /* Write refcount blocks to disk */
345 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS);
346 ret = bdrv_pwrite(bs->file, meta_offset, new_blocks,
347 blocks_clusters * s->cluster_size);
348 qemu_free(new_blocks);
349 if (ret < 0) {
350 goto fail_table;
353 /* Write refcount table to disk */
354 for(i = 0; i < table_size; i++) {
355 cpu_to_be64s(&new_table[i]);
358 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE);
359 ret = bdrv_pwrite(bs->file, table_offset, new_table,
360 table_size * sizeof(uint64_t));
361 if (ret < 0) {
362 goto fail_table;
365 for(i = 0; i < table_size; i++) {
366 cpu_to_be64s(&new_table[i]);
369 /* Hook up the new refcount table in the qcow2 header */
370 uint8_t data[12];
371 cpu_to_be64w((uint64_t*)data, table_offset);
372 cpu_to_be32w((uint32_t*)(data + 8), table_clusters);
373 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE);
374 ret = bdrv_pwrite(bs->file, offsetof(QCowHeader, refcount_table_offset),
375 data, sizeof(data));
376 if (ret < 0) {
377 goto fail_table;
380 /* And switch it in memory */
381 uint64_t old_table_offset = s->refcount_table_offset;
382 uint64_t old_table_size = s->refcount_table_size;
384 qemu_free(s->refcount_table);
385 s->refcount_table = new_table;
386 s->refcount_table_size = table_size;
387 s->refcount_table_offset = table_offset;
389 /* Free old table. Remember, we must not change free_cluster_index */
390 uint64_t old_free_cluster_index = s->free_cluster_index;
391 qcow2_free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t));
392 s->free_cluster_index = old_free_cluster_index;
394 ret = load_refcount_block(bs, new_block);
395 if (ret < 0) {
396 goto fail_block;
399 return new_block;
401 fail_table:
402 qemu_free(new_table);
403 fail_block:
404 s->refcount_block_cache_offset = 0;
405 return ret;
408 #define REFCOUNTS_PER_SECTOR (512 >> REFCOUNT_SHIFT)
409 static int write_refcount_block_entries(BlockDriverState *bs,
410 int64_t refcount_block_offset, int first_index, int last_index)
412 BDRVQcowState *s = bs->opaque;
413 size_t size;
414 int ret;
416 if (cache_refcount_updates) {
417 return 0;
420 if (first_index < 0) {
421 return 0;
424 first_index &= ~(REFCOUNTS_PER_SECTOR - 1);
425 last_index = (last_index + REFCOUNTS_PER_SECTOR)
426 & ~(REFCOUNTS_PER_SECTOR - 1);
428 size = (last_index - first_index) << REFCOUNT_SHIFT;
430 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_UPDATE_PART);
431 ret = bdrv_pwrite(bs->file,
432 refcount_block_offset + (first_index << REFCOUNT_SHIFT),
433 &s->refcount_block_cache[first_index], size);
434 if (ret < 0) {
435 return ret;
438 return 0;
441 /* XXX: cache several refcount block clusters ? */
442 static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
443 int64_t offset, int64_t length, int addend)
445 BDRVQcowState *s = bs->opaque;
446 int64_t start, last, cluster_offset;
447 int64_t refcount_block_offset = 0;
448 int64_t table_index = -1, old_table_index;
449 int first_index = -1, last_index = -1;
450 int ret;
452 #ifdef DEBUG_ALLOC2
453 printf("update_refcount: offset=%" PRId64 " size=%" PRId64 " addend=%d\n",
454 offset, length, addend);
455 #endif
456 if (length < 0) {
457 return -EINVAL;
458 } else if (length == 0) {
459 return 0;
462 start = offset & ~(s->cluster_size - 1);
463 last = (offset + length - 1) & ~(s->cluster_size - 1);
464 for(cluster_offset = start; cluster_offset <= last;
465 cluster_offset += s->cluster_size)
467 int block_index, refcount;
468 int64_t cluster_index = cluster_offset >> s->cluster_bits;
469 int64_t new_block;
471 /* Only write refcount block to disk when we are done with it */
472 old_table_index = table_index;
473 table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
474 if ((old_table_index >= 0) && (table_index != old_table_index)) {
476 ret = write_refcount_block_entries(bs, refcount_block_offset,
477 first_index, last_index);
478 if (ret < 0) {
479 return ret;
482 first_index = -1;
483 last_index = -1;
486 /* Load the refcount block and allocate it if needed */
487 new_block = alloc_refcount_block(bs, cluster_index);
488 if (new_block < 0) {
489 ret = new_block;
490 goto fail;
492 refcount_block_offset = new_block;
494 /* we can update the count and save it */
495 block_index = cluster_index &
496 ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
497 if (first_index == -1 || block_index < first_index) {
498 first_index = block_index;
500 if (block_index > last_index) {
501 last_index = block_index;
504 refcount = be16_to_cpu(s->refcount_block_cache[block_index]);
505 refcount += addend;
506 if (refcount < 0 || refcount > 0xffff) {
507 ret = -EINVAL;
508 goto fail;
510 if (refcount == 0 && cluster_index < s->free_cluster_index) {
511 s->free_cluster_index = cluster_index;
513 s->refcount_block_cache[block_index] = cpu_to_be16(refcount);
516 ret = 0;
517 fail:
519 /* Write last changed block to disk */
520 if (refcount_block_offset != 0) {
521 int wret;
522 wret = write_refcount_block_entries(bs, refcount_block_offset,
523 first_index, last_index);
524 if (wret < 0) {
525 return ret < 0 ? ret : wret;
530 * Try do undo any updates if an error is returned (This may succeed in
531 * some cases like ENOSPC for allocating a new refcount block)
533 if (ret < 0) {
534 int dummy;
535 dummy = update_refcount(bs, offset, cluster_offset - offset, -addend);
538 return ret;
541 /* addend must be 1 or -1 */
542 static int update_cluster_refcount(BlockDriverState *bs,
543 int64_t cluster_index,
544 int addend)
546 BDRVQcowState *s = bs->opaque;
547 int ret;
549 ret = update_refcount(bs, cluster_index << s->cluster_bits, 1, addend);
550 if (ret < 0) {
551 return ret;
554 return get_refcount(bs, cluster_index);
559 /*********************************************************/
560 /* cluster allocation functions */
564 /* return < 0 if error */
565 static int64_t alloc_clusters_noref(BlockDriverState *bs, int64_t size)
567 BDRVQcowState *s = bs->opaque;
568 int i, nb_clusters;
570 nb_clusters = size_to_clusters(s, size);
571 retry:
572 for(i = 0; i < nb_clusters; i++) {
573 int64_t next_cluster_index = s->free_cluster_index++;
574 if (get_refcount(bs, next_cluster_index) != 0)
575 goto retry;
577 #ifdef DEBUG_ALLOC2
578 printf("alloc_clusters: size=%" PRId64 " -> %" PRId64 "\n",
579 size,
580 (s->free_cluster_index - nb_clusters) << s->cluster_bits);
581 #endif
582 return (s->free_cluster_index - nb_clusters) << s->cluster_bits;
585 int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size)
587 int64_t offset;
588 int ret;
590 BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC);
591 offset = alloc_clusters_noref(bs, size);
592 ret = update_refcount(bs, offset, size, 1);
593 if (ret < 0) {
594 return ret;
596 return offset;
599 /* only used to allocate compressed sectors. We try to allocate
600 contiguous sectors. size must be <= cluster_size */
601 int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size)
603 BDRVQcowState *s = bs->opaque;
604 int64_t offset, cluster_offset;
605 int free_in_cluster;
607 BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC_BYTES);
608 assert(size > 0 && size <= s->cluster_size);
609 if (s->free_byte_offset == 0) {
610 s->free_byte_offset = qcow2_alloc_clusters(bs, s->cluster_size);
611 if (s->free_byte_offset < 0) {
612 return s->free_byte_offset;
615 redo:
616 free_in_cluster = s->cluster_size -
617 (s->free_byte_offset & (s->cluster_size - 1));
618 if (size <= free_in_cluster) {
619 /* enough space in current cluster */
620 offset = s->free_byte_offset;
621 s->free_byte_offset += size;
622 free_in_cluster -= size;
623 if (free_in_cluster == 0)
624 s->free_byte_offset = 0;
625 if ((offset & (s->cluster_size - 1)) != 0)
626 update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
627 } else {
628 offset = qcow2_alloc_clusters(bs, s->cluster_size);
629 if (offset < 0) {
630 return offset;
632 cluster_offset = s->free_byte_offset & ~(s->cluster_size - 1);
633 if ((cluster_offset + s->cluster_size) == offset) {
634 /* we are lucky: contiguous data */
635 offset = s->free_byte_offset;
636 update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
637 s->free_byte_offset += size;
638 } else {
639 s->free_byte_offset = offset;
640 goto redo;
643 return offset;
646 void qcow2_free_clusters(BlockDriverState *bs,
647 int64_t offset, int64_t size)
649 int ret;
651 BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_FREE);
652 ret = update_refcount(bs, offset, size, -1);
653 if (ret < 0) {
654 fprintf(stderr, "qcow2_free_clusters failed: %s\n", strerror(-ret));
655 /* TODO Remember the clusters to free them later and avoid leaking */
660 * free_any_clusters
662 * free clusters according to its type: compressed or not
666 void qcow2_free_any_clusters(BlockDriverState *bs,
667 uint64_t cluster_offset, int nb_clusters)
669 BDRVQcowState *s = bs->opaque;
671 /* free the cluster */
673 if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
674 int nb_csectors;
675 nb_csectors = ((cluster_offset >> s->csize_shift) &
676 s->csize_mask) + 1;
677 qcow2_free_clusters(bs,
678 (cluster_offset & s->cluster_offset_mask) & ~511,
679 nb_csectors * 512);
680 return;
683 qcow2_free_clusters(bs, cluster_offset, nb_clusters << s->cluster_bits);
685 return;
690 /*********************************************************/
691 /* snapshots and image creation */
695 void qcow2_create_refcount_update(QCowCreateState *s, int64_t offset,
696 int64_t size)
698 int refcount;
699 int64_t start, last, cluster_offset;
700 uint16_t *p;
702 start = offset & ~(s->cluster_size - 1);
703 last = (offset + size - 1) & ~(s->cluster_size - 1);
704 for(cluster_offset = start; cluster_offset <= last;
705 cluster_offset += s->cluster_size) {
706 p = &s->refcount_block[cluster_offset >> s->cluster_bits];
707 refcount = be16_to_cpu(*p);
708 refcount++;
709 *p = cpu_to_be16(refcount);
713 /* update the refcounts of snapshots and the copied flag */
714 int qcow2_update_snapshot_refcount(BlockDriverState *bs,
715 int64_t l1_table_offset, int l1_size, int addend)
717 BDRVQcowState *s = bs->opaque;
718 uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2, l1_allocated;
719 int64_t old_offset, old_l2_offset;
720 int l2_size, i, j, l1_modified, l2_modified, nb_csectors, refcount;
722 qcow2_l2_cache_reset(bs);
723 cache_refcount_updates = 1;
725 l2_table = NULL;
726 l1_table = NULL;
727 l1_size2 = l1_size * sizeof(uint64_t);
728 if (l1_table_offset != s->l1_table_offset) {
729 if (l1_size2 != 0) {
730 l1_table = qemu_mallocz(align_offset(l1_size2, 512));
731 } else {
732 l1_table = NULL;
734 l1_allocated = 1;
735 if (bdrv_pread(bs->file, l1_table_offset,
736 l1_table, l1_size2) != l1_size2)
737 goto fail;
738 for(i = 0;i < l1_size; i++)
739 be64_to_cpus(&l1_table[i]);
740 } else {
741 assert(l1_size == s->l1_size);
742 l1_table = s->l1_table;
743 l1_allocated = 0;
746 l2_size = s->l2_size * sizeof(uint64_t);
747 l2_table = qemu_malloc(l2_size);
748 l1_modified = 0;
749 for(i = 0; i < l1_size; i++) {
750 l2_offset = l1_table[i];
751 if (l2_offset) {
752 old_l2_offset = l2_offset;
753 l2_offset &= ~QCOW_OFLAG_COPIED;
754 l2_modified = 0;
755 if (bdrv_pread(bs->file, l2_offset, l2_table, l2_size) != l2_size)
756 goto fail;
757 for(j = 0; j < s->l2_size; j++) {
758 offset = be64_to_cpu(l2_table[j]);
759 if (offset != 0) {
760 old_offset = offset;
761 offset &= ~QCOW_OFLAG_COPIED;
762 if (offset & QCOW_OFLAG_COMPRESSED) {
763 nb_csectors = ((offset >> s->csize_shift) &
764 s->csize_mask) + 1;
765 if (addend != 0) {
766 int ret;
767 ret = update_refcount(bs,
768 (offset & s->cluster_offset_mask) & ~511,
769 nb_csectors * 512, addend);
770 if (ret < 0) {
771 goto fail;
774 /* compressed clusters are never modified */
775 refcount = 2;
776 } else {
777 if (addend != 0) {
778 refcount = update_cluster_refcount(bs, offset >> s->cluster_bits, addend);
779 } else {
780 refcount = get_refcount(bs, offset >> s->cluster_bits);
784 if (refcount == 1) {
785 offset |= QCOW_OFLAG_COPIED;
787 if (offset != old_offset) {
788 l2_table[j] = cpu_to_be64(offset);
789 l2_modified = 1;
793 if (l2_modified) {
794 if (bdrv_pwrite(bs->file,
795 l2_offset, l2_table, l2_size) != l2_size)
796 goto fail;
799 if (addend != 0) {
800 refcount = update_cluster_refcount(bs, l2_offset >> s->cluster_bits, addend);
801 } else {
802 refcount = get_refcount(bs, l2_offset >> s->cluster_bits);
804 if (refcount == 1) {
805 l2_offset |= QCOW_OFLAG_COPIED;
807 if (l2_offset != old_l2_offset) {
808 l1_table[i] = l2_offset;
809 l1_modified = 1;
813 if (l1_modified) {
814 for(i = 0; i < l1_size; i++)
815 cpu_to_be64s(&l1_table[i]);
816 if (bdrv_pwrite(bs->file, l1_table_offset, l1_table,
817 l1_size2) != l1_size2)
818 goto fail;
819 for(i = 0; i < l1_size; i++)
820 be64_to_cpus(&l1_table[i]);
822 if (l1_allocated)
823 qemu_free(l1_table);
824 qemu_free(l2_table);
825 cache_refcount_updates = 0;
826 write_refcount_block(bs);
827 return 0;
828 fail:
829 if (l1_allocated)
830 qemu_free(l1_table);
831 qemu_free(l2_table);
832 cache_refcount_updates = 0;
833 write_refcount_block(bs);
834 return -EIO;
840 /*********************************************************/
841 /* refcount checking functions */
846 * Increases the refcount for a range of clusters in a given refcount table.
847 * This is used to construct a temporary refcount table out of L1 and L2 tables
848 * which can be compared the the refcount table saved in the image.
850 * Returns the number of errors in the image that were found
852 static int inc_refcounts(BlockDriverState *bs,
853 uint16_t *refcount_table,
854 int refcount_table_size,
855 int64_t offset, int64_t size)
857 BDRVQcowState *s = bs->opaque;
858 int64_t start, last, cluster_offset;
859 int k;
860 int errors = 0;
862 if (size <= 0)
863 return 0;
865 start = offset & ~(s->cluster_size - 1);
866 last = (offset + size - 1) & ~(s->cluster_size - 1);
867 for(cluster_offset = start; cluster_offset <= last;
868 cluster_offset += s->cluster_size) {
869 k = cluster_offset >> s->cluster_bits;
870 if (k < 0 || k >= refcount_table_size) {
871 fprintf(stderr, "ERROR: invalid cluster offset=0x%" PRIx64 "\n",
872 cluster_offset);
873 errors++;
874 } else {
875 if (++refcount_table[k] == 0) {
876 fprintf(stderr, "ERROR: overflow cluster offset=0x%" PRIx64
877 "\n", cluster_offset);
878 errors++;
883 return errors;
887 * Increases the refcount in the given refcount table for the all clusters
888 * referenced in the L2 table. While doing so, performs some checks on L2
889 * entries.
891 * Returns the number of errors found by the checks or -errno if an internal
892 * error occurred.
894 static int check_refcounts_l2(BlockDriverState *bs,
895 uint16_t *refcount_table, int refcount_table_size, int64_t l2_offset,
896 int check_copied)
898 BDRVQcowState *s = bs->opaque;
899 uint64_t *l2_table, offset;
900 int i, l2_size, nb_csectors, refcount;
901 int errors = 0;
903 /* Read L2 table from disk */
904 l2_size = s->l2_size * sizeof(uint64_t);
905 l2_table = qemu_malloc(l2_size);
907 if (bdrv_pread(bs->file, l2_offset, l2_table, l2_size) != l2_size)
908 goto fail;
910 /* Do the actual checks */
911 for(i = 0; i < s->l2_size; i++) {
912 offset = be64_to_cpu(l2_table[i]);
913 if (offset != 0) {
914 if (offset & QCOW_OFLAG_COMPRESSED) {
915 /* Compressed clusters don't have QCOW_OFLAG_COPIED */
916 if (offset & QCOW_OFLAG_COPIED) {
917 fprintf(stderr, "ERROR: cluster %" PRId64 ": "
918 "copied flag must never be set for compressed "
919 "clusters\n", offset >> s->cluster_bits);
920 offset &= ~QCOW_OFLAG_COPIED;
921 errors++;
924 /* Mark cluster as used */
925 nb_csectors = ((offset >> s->csize_shift) &
926 s->csize_mask) + 1;
927 offset &= s->cluster_offset_mask;
928 errors += inc_refcounts(bs, refcount_table,
929 refcount_table_size,
930 offset & ~511, nb_csectors * 512);
931 } else {
932 /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
933 if (check_copied) {
934 uint64_t entry = offset;
935 offset &= ~QCOW_OFLAG_COPIED;
936 refcount = get_refcount(bs, offset >> s->cluster_bits);
937 if ((refcount == 1) != ((entry & QCOW_OFLAG_COPIED) != 0)) {
938 fprintf(stderr, "ERROR OFLAG_COPIED: offset=%"
939 PRIx64 " refcount=%d\n", entry, refcount);
940 errors++;
944 /* Mark cluster as used */
945 offset &= ~QCOW_OFLAG_COPIED;
946 errors += inc_refcounts(bs, refcount_table,
947 refcount_table_size,
948 offset, s->cluster_size);
950 /* Correct offsets are cluster aligned */
951 if (offset & (s->cluster_size - 1)) {
952 fprintf(stderr, "ERROR offset=%" PRIx64 ": Cluster is not "
953 "properly aligned; L2 entry corrupted.\n", offset);
954 errors++;
960 qemu_free(l2_table);
961 return errors;
963 fail:
964 fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
965 qemu_free(l2_table);
966 return -EIO;
970 * Increases the refcount for the L1 table, its L2 tables and all referenced
971 * clusters in the given refcount table. While doing so, performs some checks
972 * on L1 and L2 entries.
974 * Returns the number of errors found by the checks or -errno if an internal
975 * error occurred.
977 static int check_refcounts_l1(BlockDriverState *bs,
978 uint16_t *refcount_table,
979 int refcount_table_size,
980 int64_t l1_table_offset, int l1_size,
981 int check_copied)
983 BDRVQcowState *s = bs->opaque;
984 uint64_t *l1_table, l2_offset, l1_size2;
985 int i, refcount, ret;
986 int errors = 0;
988 l1_size2 = l1_size * sizeof(uint64_t);
990 /* Mark L1 table as used */
991 errors += inc_refcounts(bs, refcount_table, refcount_table_size,
992 l1_table_offset, l1_size2);
994 /* Read L1 table entries from disk */
995 if (l1_size2 == 0) {
996 l1_table = NULL;
997 } else {
998 l1_table = qemu_malloc(l1_size2);
999 if (bdrv_pread(bs->file, l1_table_offset,
1000 l1_table, l1_size2) != l1_size2)
1001 goto fail;
1002 for(i = 0;i < l1_size; i++)
1003 be64_to_cpus(&l1_table[i]);
1006 /* Do the actual checks */
1007 for(i = 0; i < l1_size; i++) {
1008 l2_offset = l1_table[i];
1009 if (l2_offset) {
1010 /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
1011 if (check_copied) {
1012 refcount = get_refcount(bs, (l2_offset & ~QCOW_OFLAG_COPIED)
1013 >> s->cluster_bits);
1014 if ((refcount == 1) != ((l2_offset & QCOW_OFLAG_COPIED) != 0)) {
1015 fprintf(stderr, "ERROR OFLAG_COPIED: l2_offset=%" PRIx64
1016 " refcount=%d\n", l2_offset, refcount);
1017 errors++;
1021 /* Mark L2 table as used */
1022 l2_offset &= ~QCOW_OFLAG_COPIED;
1023 errors += inc_refcounts(bs, refcount_table,
1024 refcount_table_size,
1025 l2_offset,
1026 s->cluster_size);
1028 /* L2 tables are cluster aligned */
1029 if (l2_offset & (s->cluster_size - 1)) {
1030 fprintf(stderr, "ERROR l2_offset=%" PRIx64 ": Table is not "
1031 "cluster aligned; L1 entry corrupted\n", l2_offset);
1032 errors++;
1035 /* Process and check L2 entries */
1036 ret = check_refcounts_l2(bs, refcount_table, refcount_table_size,
1037 l2_offset, check_copied);
1038 if (ret < 0) {
1039 goto fail;
1041 errors += ret;
1044 qemu_free(l1_table);
1045 return errors;
1047 fail:
1048 fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
1049 qemu_free(l1_table);
1050 return -EIO;
1054 * Checks an image for refcount consistency.
1056 * Returns 0 if no errors are found, the number of errors in case the image is
1057 * detected as corrupted, and -errno when an internal error occured.
1059 int qcow2_check_refcounts(BlockDriverState *bs)
1061 BDRVQcowState *s = bs->opaque;
1062 int64_t size;
1063 int nb_clusters, refcount1, refcount2, i;
1064 QCowSnapshot *sn;
1065 uint16_t *refcount_table;
1066 int ret, errors = 0;
1068 size = bdrv_getlength(bs->file);
1069 nb_clusters = size_to_clusters(s, size);
1070 refcount_table = qemu_mallocz(nb_clusters * sizeof(uint16_t));
1072 /* header */
1073 errors += inc_refcounts(bs, refcount_table, nb_clusters,
1074 0, s->cluster_size);
1076 /* current L1 table */
1077 ret = check_refcounts_l1(bs, refcount_table, nb_clusters,
1078 s->l1_table_offset, s->l1_size, 1);
1079 if (ret < 0) {
1080 return ret;
1082 errors += ret;
1084 /* snapshots */
1085 for(i = 0; i < s->nb_snapshots; i++) {
1086 sn = s->snapshots + i;
1087 check_refcounts_l1(bs, refcount_table, nb_clusters,
1088 sn->l1_table_offset, sn->l1_size, 0);
1090 errors += inc_refcounts(bs, refcount_table, nb_clusters,
1091 s->snapshots_offset, s->snapshots_size);
1093 /* refcount data */
1094 errors += inc_refcounts(bs, refcount_table, nb_clusters,
1095 s->refcount_table_offset,
1096 s->refcount_table_size * sizeof(uint64_t));
1097 for(i = 0; i < s->refcount_table_size; i++) {
1098 int64_t offset;
1099 offset = s->refcount_table[i];
1101 /* Refcount blocks are cluster aligned */
1102 if (offset & (s->cluster_size - 1)) {
1103 fprintf(stderr, "ERROR refcount block %d is not "
1104 "cluster aligned; refcount table entry corrupted\n", i);
1105 errors++;
1108 if (offset != 0) {
1109 errors += inc_refcounts(bs, refcount_table, nb_clusters,
1110 offset, s->cluster_size);
1111 if (refcount_table[offset / s->cluster_size] != 1) {
1112 fprintf(stderr, "ERROR refcount block %d refcount=%d\n",
1113 i, refcount_table[offset / s->cluster_size]);
1118 /* compare ref counts */
1119 for(i = 0; i < nb_clusters; i++) {
1120 refcount1 = get_refcount(bs, i);
1121 refcount2 = refcount_table[i];
1122 if (refcount1 != refcount2) {
1123 fprintf(stderr, "ERROR cluster %d refcount=%d reference=%d\n",
1124 i, refcount1, refcount2);
1125 errors++;
1129 qemu_free(refcount_table);
1131 return errors;