xref: /openbmc/qemu/block/qcow2-refcount.c (revision 13b10e05)
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"
26 #include "block/block_int.h"
27 #include "block/qcow2.h"
28 
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);
33 
34 
35 /*********************************************************/
36 /* refcount handling */
37 
38 int qcow2_refcount_init(BlockDriverState *bs)
39 {
40     BDRVQcowState *s = bs->opaque;
41     int ret, refcount_table_size2, i;
42 
43     refcount_table_size2 = s->refcount_table_size * sizeof(uint64_t);
44     s->refcount_table = g_malloc(refcount_table_size2);
45     if (s->refcount_table_size > 0) {
46         BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_LOAD);
47         ret = bdrv_pread(bs->file, s->refcount_table_offset,
48                          s->refcount_table, refcount_table_size2);
49         if (ret != refcount_table_size2)
50             goto fail;
51         for(i = 0; i < s->refcount_table_size; i++)
52             be64_to_cpus(&s->refcount_table[i]);
53     }
54     return 0;
55  fail:
56     return -ENOMEM;
57 }
58 
59 void qcow2_refcount_close(BlockDriverState *bs)
60 {
61     BDRVQcowState *s = bs->opaque;
62     g_free(s->refcount_table);
63 }
64 
65 
66 static int load_refcount_block(BlockDriverState *bs,
67                                int64_t refcount_block_offset,
68                                void **refcount_block)
69 {
70     BDRVQcowState *s = bs->opaque;
71     int ret;
72 
73     BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_LOAD);
74     ret = qcow2_cache_get(bs, s->refcount_block_cache, refcount_block_offset,
75         refcount_block);
76 
77     return ret;
78 }
79 
80 /*
81  * Returns the refcount of the cluster given by its index. Any non-negative
82  * return value is the refcount of the cluster, negative values are -errno
83  * and indicate an error.
84  */
85 static int get_refcount(BlockDriverState *bs, int64_t cluster_index)
86 {
87     BDRVQcowState *s = bs->opaque;
88     int refcount_table_index, block_index;
89     int64_t refcount_block_offset;
90     int ret;
91     uint16_t *refcount_block;
92     uint16_t refcount;
93 
94     refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
95     if (refcount_table_index >= s->refcount_table_size)
96         return 0;
97     refcount_block_offset = s->refcount_table[refcount_table_index];
98     if (!refcount_block_offset)
99         return 0;
100 
101     ret = qcow2_cache_get(bs, s->refcount_block_cache, refcount_block_offset,
102         (void**) &refcount_block);
103     if (ret < 0) {
104         return ret;
105     }
106 
107     block_index = cluster_index &
108         ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
109     refcount = be16_to_cpu(refcount_block[block_index]);
110 
111     ret = qcow2_cache_put(bs, s->refcount_block_cache,
112         (void**) &refcount_block);
113     if (ret < 0) {
114         return ret;
115     }
116 
117     return refcount;
118 }
119 
120 /*
121  * Rounds the refcount table size up to avoid growing the table for each single
122  * refcount block that is allocated.
123  */
124 static unsigned int next_refcount_table_size(BDRVQcowState *s,
125     unsigned int min_size)
126 {
127     unsigned int min_clusters = (min_size >> (s->cluster_bits - 3)) + 1;
128     unsigned int refcount_table_clusters =
129         MAX(1, s->refcount_table_size >> (s->cluster_bits - 3));
130 
131     while (min_clusters > refcount_table_clusters) {
132         refcount_table_clusters = (refcount_table_clusters * 3 + 1) / 2;
133     }
134 
135     return refcount_table_clusters << (s->cluster_bits - 3);
136 }
137 
138 
139 /* Checks if two offsets are described by the same refcount block */
140 static int in_same_refcount_block(BDRVQcowState *s, uint64_t offset_a,
141     uint64_t offset_b)
142 {
143     uint64_t block_a = offset_a >> (2 * s->cluster_bits - REFCOUNT_SHIFT);
144     uint64_t block_b = offset_b >> (2 * s->cluster_bits - REFCOUNT_SHIFT);
145 
146     return (block_a == block_b);
147 }
148 
149 /*
150  * Loads a refcount block. If it doesn't exist yet, it is allocated first
151  * (including growing the refcount table if needed).
152  *
153  * Returns 0 on success or -errno in error case
154  */
155 static int alloc_refcount_block(BlockDriverState *bs,
156     int64_t cluster_index, uint16_t **refcount_block)
157 {
158     BDRVQcowState *s = bs->opaque;
159     unsigned int refcount_table_index;
160     int ret;
161 
162     BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC);
163 
164     /* Find the refcount block for the given cluster */
165     refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
166 
167     if (refcount_table_index < s->refcount_table_size) {
168 
169         uint64_t refcount_block_offset =
170             s->refcount_table[refcount_table_index] & REFT_OFFSET_MASK;
171 
172         /* If it's already there, we're done */
173         if (refcount_block_offset) {
174              return load_refcount_block(bs, refcount_block_offset,
175                  (void**) refcount_block);
176         }
177     }
178 
179     /*
180      * If we came here, we need to allocate something. Something is at least
181      * a cluster for the new refcount block. It may also include a new refcount
182      * table if the old refcount table is too small.
183      *
184      * Note that allocating clusters here needs some special care:
185      *
186      * - We can't use the normal qcow2_alloc_clusters(), it would try to
187      *   increase the refcount and very likely we would end up with an endless
188      *   recursion. Instead we must place the refcount blocks in a way that
189      *   they can describe them themselves.
190      *
191      * - We need to consider that at this point we are inside update_refcounts
192      *   and doing the initial refcount increase. This means that some clusters
193      *   have already been allocated by the caller, but their refcount isn't
194      *   accurate yet. free_cluster_index tells us where this allocation ends
195      *   as long as we don't overwrite it by freeing clusters.
196      *
197      * - alloc_clusters_noref and qcow2_free_clusters may load a different
198      *   refcount block into the cache
199      */
200 
201     *refcount_block = NULL;
202 
203     /* We write to the refcount table, so we might depend on L2 tables */
204     qcow2_cache_flush(bs, s->l2_table_cache);
205 
206     /* Allocate the refcount block itself and mark it as used */
207     int64_t new_block = alloc_clusters_noref(bs, s->cluster_size);
208     if (new_block < 0) {
209         return new_block;
210     }
211 
212 #ifdef DEBUG_ALLOC2
213     fprintf(stderr, "qcow2: Allocate refcount block %d for %" PRIx64
214         " at %" PRIx64 "\n",
215         refcount_table_index, cluster_index << s->cluster_bits, new_block);
216 #endif
217 
218     if (in_same_refcount_block(s, new_block, cluster_index << s->cluster_bits)) {
219         /* Zero the new refcount block before updating it */
220         ret = qcow2_cache_get_empty(bs, s->refcount_block_cache, new_block,
221             (void**) refcount_block);
222         if (ret < 0) {
223             goto fail_block;
224         }
225 
226         memset(*refcount_block, 0, s->cluster_size);
227 
228         /* The block describes itself, need to update the cache */
229         int block_index = (new_block >> s->cluster_bits) &
230             ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
231         (*refcount_block)[block_index] = cpu_to_be16(1);
232     } else {
233         /* Described somewhere else. This can recurse at most twice before we
234          * arrive at a block that describes itself. */
235         ret = update_refcount(bs, new_block, s->cluster_size, 1);
236         if (ret < 0) {
237             goto fail_block;
238         }
239 
240         bdrv_flush(bs->file);
241 
242         /* Initialize the new refcount block only after updating its refcount,
243          * update_refcount uses the refcount cache itself */
244         ret = qcow2_cache_get_empty(bs, s->refcount_block_cache, new_block,
245             (void**) refcount_block);
246         if (ret < 0) {
247             goto fail_block;
248         }
249 
250         memset(*refcount_block, 0, s->cluster_size);
251     }
252 
253     /* Now the new refcount block needs to be written to disk */
254     BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE);
255     qcow2_cache_entry_mark_dirty(s->refcount_block_cache, *refcount_block);
256     ret = qcow2_cache_flush(bs, s->refcount_block_cache);
257     if (ret < 0) {
258         goto fail_block;
259     }
260 
261     /* If the refcount table is big enough, just hook the block up there */
262     if (refcount_table_index < s->refcount_table_size) {
263         uint64_t data64 = cpu_to_be64(new_block);
264         BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_HOOKUP);
265         ret = bdrv_pwrite_sync(bs->file,
266             s->refcount_table_offset + refcount_table_index * sizeof(uint64_t),
267             &data64, sizeof(data64));
268         if (ret < 0) {
269             goto fail_block;
270         }
271 
272         s->refcount_table[refcount_table_index] = new_block;
273         return 0;
274     }
275 
276     ret = qcow2_cache_put(bs, s->refcount_block_cache, (void**) refcount_block);
277     if (ret < 0) {
278         goto fail_block;
279     }
280 
281     /*
282      * If we come here, we need to grow the refcount table. Again, a new
283      * refcount table needs some space and we can't simply allocate to avoid
284      * endless recursion.
285      *
286      * Therefore let's grab new refcount blocks at the end of the image, which
287      * will describe themselves and the new refcount table. This way we can
288      * reference them only in the new table and do the switch to the new
289      * refcount table at once without producing an inconsistent state in
290      * between.
291      */
292     BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_GROW);
293 
294     /* Calculate the number of refcount blocks needed so far */
295     uint64_t refcount_block_clusters = 1 << (s->cluster_bits - REFCOUNT_SHIFT);
296     uint64_t blocks_used = (s->free_cluster_index +
297         refcount_block_clusters - 1) / refcount_block_clusters;
298 
299     /* And now we need at least one block more for the new metadata */
300     uint64_t table_size = next_refcount_table_size(s, blocks_used + 1);
301     uint64_t last_table_size;
302     uint64_t blocks_clusters;
303     do {
304         uint64_t table_clusters =
305             size_to_clusters(s, table_size * sizeof(uint64_t));
306         blocks_clusters = 1 +
307             ((table_clusters + refcount_block_clusters - 1)
308             / refcount_block_clusters);
309         uint64_t meta_clusters = table_clusters + blocks_clusters;
310 
311         last_table_size = table_size;
312         table_size = next_refcount_table_size(s, blocks_used +
313             ((meta_clusters + refcount_block_clusters - 1)
314             / refcount_block_clusters));
315 
316     } while (last_table_size != table_size);
317 
318 #ifdef DEBUG_ALLOC2
319     fprintf(stderr, "qcow2: Grow refcount table %" PRId32 " => %" PRId64 "\n",
320         s->refcount_table_size, table_size);
321 #endif
322 
323     /* Create the new refcount table and blocks */
324     uint64_t meta_offset = (blocks_used * refcount_block_clusters) *
325         s->cluster_size;
326     uint64_t table_offset = meta_offset + blocks_clusters * s->cluster_size;
327     uint16_t *new_blocks = g_malloc0(blocks_clusters * s->cluster_size);
328     uint64_t *new_table = g_malloc0(table_size * sizeof(uint64_t));
329 
330     assert(meta_offset >= (s->free_cluster_index * s->cluster_size));
331 
332     /* Fill the new refcount table */
333     memcpy(new_table, s->refcount_table,
334         s->refcount_table_size * sizeof(uint64_t));
335     new_table[refcount_table_index] = new_block;
336 
337     int i;
338     for (i = 0; i < blocks_clusters; i++) {
339         new_table[blocks_used + i] = meta_offset + (i * s->cluster_size);
340     }
341 
342     /* Fill the refcount blocks */
343     uint64_t table_clusters = size_to_clusters(s, table_size * sizeof(uint64_t));
344     int block = 0;
345     for (i = 0; i < table_clusters + blocks_clusters; i++) {
346         new_blocks[block++] = cpu_to_be16(1);
347     }
348 
349     /* Write refcount blocks to disk */
350     BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS);
351     ret = bdrv_pwrite_sync(bs->file, meta_offset, new_blocks,
352         blocks_clusters * s->cluster_size);
353     g_free(new_blocks);
354     if (ret < 0) {
355         goto fail_table;
356     }
357 
358     /* Write refcount table to disk */
359     for(i = 0; i < table_size; i++) {
360         cpu_to_be64s(&new_table[i]);
361     }
362 
363     BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE);
364     ret = bdrv_pwrite_sync(bs->file, table_offset, new_table,
365         table_size * sizeof(uint64_t));
366     if (ret < 0) {
367         goto fail_table;
368     }
369 
370     for(i = 0; i < table_size; i++) {
371         be64_to_cpus(&new_table[i]);
372     }
373 
374     /* Hook up the new refcount table in the qcow2 header */
375     uint8_t data[12];
376     cpu_to_be64w((uint64_t*)data, table_offset);
377     cpu_to_be32w((uint32_t*)(data + 8), table_clusters);
378     BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE);
379     ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, refcount_table_offset),
380         data, sizeof(data));
381     if (ret < 0) {
382         goto fail_table;
383     }
384 
385     /* And switch it in memory */
386     uint64_t old_table_offset = s->refcount_table_offset;
387     uint64_t old_table_size = s->refcount_table_size;
388 
389     g_free(s->refcount_table);
390     s->refcount_table = new_table;
391     s->refcount_table_size = table_size;
392     s->refcount_table_offset = table_offset;
393 
394     /* Free old table. Remember, we must not change free_cluster_index */
395     uint64_t old_free_cluster_index = s->free_cluster_index;
396     qcow2_free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t));
397     s->free_cluster_index = old_free_cluster_index;
398 
399     ret = load_refcount_block(bs, new_block, (void**) refcount_block);
400     if (ret < 0) {
401         return ret;
402     }
403 
404     return 0;
405 
406 fail_table:
407     g_free(new_table);
408 fail_block:
409     if (*refcount_block != NULL) {
410         qcow2_cache_put(bs, s->refcount_block_cache, (void**) refcount_block);
411     }
412     return ret;
413 }
414 
415 /* XXX: cache several refcount block clusters ? */
416 static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
417     int64_t offset, int64_t length, int addend)
418 {
419     BDRVQcowState *s = bs->opaque;
420     int64_t start, last, cluster_offset;
421     uint16_t *refcount_block = NULL;
422     int64_t old_table_index = -1;
423     int ret;
424 
425 #ifdef DEBUG_ALLOC2
426     fprintf(stderr, "update_refcount: offset=%" PRId64 " size=%" PRId64 " addend=%d\n",
427            offset, length, addend);
428 #endif
429     if (length < 0) {
430         return -EINVAL;
431     } else if (length == 0) {
432         return 0;
433     }
434 
435     if (addend < 0) {
436         qcow2_cache_set_dependency(bs, s->refcount_block_cache,
437             s->l2_table_cache);
438     }
439 
440     start = offset & ~(s->cluster_size - 1);
441     last = (offset + length - 1) & ~(s->cluster_size - 1);
442     for(cluster_offset = start; cluster_offset <= last;
443         cluster_offset += s->cluster_size)
444     {
445         int block_index, refcount;
446         int64_t cluster_index = cluster_offset >> s->cluster_bits;
447         int64_t table_index =
448             cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
449 
450         /* Load the refcount block and allocate it if needed */
451         if (table_index != old_table_index) {
452             if (refcount_block) {
453                 ret = qcow2_cache_put(bs, s->refcount_block_cache,
454                     (void**) &refcount_block);
455                 if (ret < 0) {
456                     goto fail;
457                 }
458             }
459 
460             ret = alloc_refcount_block(bs, cluster_index, &refcount_block);
461             if (ret < 0) {
462                 goto fail;
463             }
464         }
465         old_table_index = table_index;
466 
467         qcow2_cache_entry_mark_dirty(s->refcount_block_cache, refcount_block);
468 
469         /* we can update the count and save it */
470         block_index = cluster_index &
471             ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
472 
473         refcount = be16_to_cpu(refcount_block[block_index]);
474         refcount += addend;
475         if (refcount < 0 || refcount > 0xffff) {
476             ret = -EINVAL;
477             goto fail;
478         }
479         if (refcount == 0 && cluster_index < s->free_cluster_index) {
480             s->free_cluster_index = cluster_index;
481         }
482         refcount_block[block_index] = cpu_to_be16(refcount);
483     }
484 
485     ret = 0;
486 fail:
487     /* Write last changed block to disk */
488     if (refcount_block) {
489         int wret;
490         wret = qcow2_cache_put(bs, s->refcount_block_cache,
491             (void**) &refcount_block);
492         if (wret < 0) {
493             return ret < 0 ? ret : wret;
494         }
495     }
496 
497     /*
498      * Try do undo any updates if an error is returned (This may succeed in
499      * some cases like ENOSPC for allocating a new refcount block)
500      */
501     if (ret < 0) {
502         int dummy;
503         dummy = update_refcount(bs, offset, cluster_offset - offset, -addend);
504         (void)dummy;
505     }
506 
507     return ret;
508 }
509 
510 /*
511  * Increases or decreases the refcount of a given cluster by one.
512  * addend must be 1 or -1.
513  *
514  * If the return value is non-negative, it is the new refcount of the cluster.
515  * If it is negative, it is -errno and indicates an error.
516  */
517 static int update_cluster_refcount(BlockDriverState *bs,
518                                    int64_t cluster_index,
519                                    int addend)
520 {
521     BDRVQcowState *s = bs->opaque;
522     int ret;
523 
524     ret = update_refcount(bs, cluster_index << s->cluster_bits, 1, addend);
525     if (ret < 0) {
526         return ret;
527     }
528 
529     bdrv_flush(bs->file);
530 
531     return get_refcount(bs, cluster_index);
532 }
533 
534 
535 
536 /*********************************************************/
537 /* cluster allocation functions */
538 
539 
540 
541 /* return < 0 if error */
542 static int64_t alloc_clusters_noref(BlockDriverState *bs, int64_t size)
543 {
544     BDRVQcowState *s = bs->opaque;
545     int i, nb_clusters, refcount;
546 
547     nb_clusters = size_to_clusters(s, size);
548 retry:
549     for(i = 0; i < nb_clusters; i++) {
550         int64_t next_cluster_index = s->free_cluster_index++;
551         refcount = get_refcount(bs, next_cluster_index);
552 
553         if (refcount < 0) {
554             return refcount;
555         } else if (refcount != 0) {
556             goto retry;
557         }
558     }
559 #ifdef DEBUG_ALLOC2
560     fprintf(stderr, "alloc_clusters: size=%" PRId64 " -> %" PRId64 "\n",
561             size,
562             (s->free_cluster_index - nb_clusters) << s->cluster_bits);
563 #endif
564     return (s->free_cluster_index - nb_clusters) << s->cluster_bits;
565 }
566 
567 int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size)
568 {
569     int64_t offset;
570     int ret;
571 
572     BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC);
573     offset = alloc_clusters_noref(bs, size);
574     if (offset < 0) {
575         return offset;
576     }
577 
578     ret = update_refcount(bs, offset, size, 1);
579     if (ret < 0) {
580         return ret;
581     }
582 
583     return offset;
584 }
585 
586 int qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
587     int nb_clusters)
588 {
589     BDRVQcowState *s = bs->opaque;
590     uint64_t cluster_index;
591     uint64_t old_free_cluster_index;
592     int i, refcount, ret;
593 
594     /* Check how many clusters there are free */
595     cluster_index = offset >> s->cluster_bits;
596     for(i = 0; i < nb_clusters; i++) {
597         refcount = get_refcount(bs, cluster_index++);
598 
599         if (refcount < 0) {
600             return refcount;
601         } else if (refcount != 0) {
602             break;
603         }
604     }
605 
606     /* And then allocate them */
607     old_free_cluster_index = s->free_cluster_index;
608     s->free_cluster_index = cluster_index + i;
609 
610     ret = update_refcount(bs, offset, i << s->cluster_bits, 1);
611     if (ret < 0) {
612         return ret;
613     }
614 
615     s->free_cluster_index = old_free_cluster_index;
616 
617     return i;
618 }
619 
620 /* only used to allocate compressed sectors. We try to allocate
621    contiguous sectors. size must be <= cluster_size */
622 int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size)
623 {
624     BDRVQcowState *s = bs->opaque;
625     int64_t offset, cluster_offset;
626     int free_in_cluster;
627 
628     BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC_BYTES);
629     assert(size > 0 && size <= s->cluster_size);
630     if (s->free_byte_offset == 0) {
631         offset = qcow2_alloc_clusters(bs, s->cluster_size);
632         if (offset < 0) {
633             return offset;
634         }
635         s->free_byte_offset = offset;
636     }
637  redo:
638     free_in_cluster = s->cluster_size -
639         (s->free_byte_offset & (s->cluster_size - 1));
640     if (size <= free_in_cluster) {
641         /* enough space in current cluster */
642         offset = s->free_byte_offset;
643         s->free_byte_offset += size;
644         free_in_cluster -= size;
645         if (free_in_cluster == 0)
646             s->free_byte_offset = 0;
647         if ((offset & (s->cluster_size - 1)) != 0)
648             update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
649     } else {
650         offset = qcow2_alloc_clusters(bs, s->cluster_size);
651         if (offset < 0) {
652             return offset;
653         }
654         cluster_offset = s->free_byte_offset & ~(s->cluster_size - 1);
655         if ((cluster_offset + s->cluster_size) == offset) {
656             /* we are lucky: contiguous data */
657             offset = s->free_byte_offset;
658             update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
659             s->free_byte_offset += size;
660         } else {
661             s->free_byte_offset = offset;
662             goto redo;
663         }
664     }
665 
666     bdrv_flush(bs->file);
667     return offset;
668 }
669 
670 void qcow2_free_clusters(BlockDriverState *bs,
671                           int64_t offset, int64_t size)
672 {
673     int ret;
674 
675     BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_FREE);
676     ret = update_refcount(bs, offset, size, -1);
677     if (ret < 0) {
678         fprintf(stderr, "qcow2_free_clusters failed: %s\n", strerror(-ret));
679         /* TODO Remember the clusters to free them later and avoid leaking */
680     }
681 }
682 
683 /*
684  * Free a cluster using its L2 entry (handles clusters of all types, e.g.
685  * normal cluster, compressed cluster, etc.)
686  */
687 void qcow2_free_any_clusters(BlockDriverState *bs,
688     uint64_t l2_entry, int nb_clusters)
689 {
690     BDRVQcowState *s = bs->opaque;
691 
692     switch (qcow2_get_cluster_type(l2_entry)) {
693     case QCOW2_CLUSTER_COMPRESSED:
694         {
695             int nb_csectors;
696             nb_csectors = ((l2_entry >> s->csize_shift) &
697                            s->csize_mask) + 1;
698             qcow2_free_clusters(bs,
699                 (l2_entry & s->cluster_offset_mask) & ~511,
700                 nb_csectors * 512);
701         }
702         break;
703     case QCOW2_CLUSTER_NORMAL:
704         qcow2_free_clusters(bs, l2_entry & L2E_OFFSET_MASK,
705                             nb_clusters << s->cluster_bits);
706         break;
707     case QCOW2_CLUSTER_UNALLOCATED:
708     case QCOW2_CLUSTER_ZERO:
709         break;
710     default:
711         abort();
712     }
713 }
714 
715 
716 
717 /*********************************************************/
718 /* snapshots and image creation */
719 
720 
721 
722 /* update the refcounts of snapshots and the copied flag */
723 int qcow2_update_snapshot_refcount(BlockDriverState *bs,
724     int64_t l1_table_offset, int l1_size, int addend)
725 {
726     BDRVQcowState *s = bs->opaque;
727     uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2, l1_allocated;
728     int64_t old_offset, old_l2_offset;
729     int i, j, l1_modified = 0, nb_csectors, refcount;
730     int ret;
731 
732     l2_table = NULL;
733     l1_table = NULL;
734     l1_size2 = l1_size * sizeof(uint64_t);
735 
736     /* WARNING: qcow2_snapshot_goto relies on this function not using the
737      * l1_table_offset when it is the current s->l1_table_offset! Be careful
738      * when changing this! */
739     if (l1_table_offset != s->l1_table_offset) {
740         l1_table = g_malloc0(align_offset(l1_size2, 512));
741         l1_allocated = 1;
742         if (bdrv_pread(bs->file, l1_table_offset,
743                        l1_table, l1_size2) != l1_size2)
744         {
745             ret = -EIO;
746             goto fail;
747         }
748 
749         for(i = 0;i < l1_size; i++)
750             be64_to_cpus(&l1_table[i]);
751     } else {
752         assert(l1_size == s->l1_size);
753         l1_table = s->l1_table;
754         l1_allocated = 0;
755     }
756 
757     for(i = 0; i < l1_size; i++) {
758         l2_offset = l1_table[i];
759         if (l2_offset) {
760             old_l2_offset = l2_offset;
761             l2_offset &= L1E_OFFSET_MASK;
762 
763             ret = qcow2_cache_get(bs, s->l2_table_cache, l2_offset,
764                 (void**) &l2_table);
765             if (ret < 0) {
766                 goto fail;
767             }
768 
769             for(j = 0; j < s->l2_size; j++) {
770                 offset = be64_to_cpu(l2_table[j]);
771                 if (offset != 0) {
772                     old_offset = offset;
773                     offset &= ~QCOW_OFLAG_COPIED;
774                     if (offset & QCOW_OFLAG_COMPRESSED) {
775                         nb_csectors = ((offset >> s->csize_shift) &
776                                        s->csize_mask) + 1;
777                         if (addend != 0) {
778                             int ret;
779                             ret = update_refcount(bs,
780                                 (offset & s->cluster_offset_mask) & ~511,
781                                 nb_csectors * 512, addend);
782                             if (ret < 0) {
783                                 goto fail;
784                             }
785 
786                             /* TODO Flushing once for the whole function should
787                              * be enough */
788                             bdrv_flush(bs->file);
789                         }
790                         /* compressed clusters are never modified */
791                         refcount = 2;
792                     } else {
793                         uint64_t cluster_index = (offset & L2E_OFFSET_MASK) >> s->cluster_bits;
794                         if (addend != 0) {
795                             refcount = update_cluster_refcount(bs, cluster_index, addend);
796                         } else {
797                             refcount = get_refcount(bs, cluster_index);
798                         }
799 
800                         if (refcount < 0) {
801                             ret = -EIO;
802                             goto fail;
803                         }
804                     }
805 
806                     if (refcount == 1) {
807                         offset |= QCOW_OFLAG_COPIED;
808                     }
809                     if (offset != old_offset) {
810                         if (addend > 0) {
811                             qcow2_cache_set_dependency(bs, s->l2_table_cache,
812                                 s->refcount_block_cache);
813                         }
814                         l2_table[j] = cpu_to_be64(offset);
815                         qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table);
816                     }
817                 }
818             }
819 
820             ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
821             if (ret < 0) {
822                 goto fail;
823             }
824 
825 
826             if (addend != 0) {
827                 refcount = update_cluster_refcount(bs, l2_offset >> s->cluster_bits, addend);
828             } else {
829                 refcount = get_refcount(bs, l2_offset >> s->cluster_bits);
830             }
831             if (refcount < 0) {
832                 ret = -EIO;
833                 goto fail;
834             } else if (refcount == 1) {
835                 l2_offset |= QCOW_OFLAG_COPIED;
836             }
837             if (l2_offset != old_l2_offset) {
838                 l1_table[i] = l2_offset;
839                 l1_modified = 1;
840             }
841         }
842     }
843 
844     ret = 0;
845 fail:
846     if (l2_table) {
847         qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
848     }
849 
850     /* Update L1 only if it isn't deleted anyway (addend = -1) */
851     if (addend >= 0 && l1_modified) {
852         for(i = 0; i < l1_size; i++)
853             cpu_to_be64s(&l1_table[i]);
854         if (bdrv_pwrite_sync(bs->file, l1_table_offset, l1_table,
855                         l1_size2) < 0)
856             goto fail;
857         for(i = 0; i < l1_size; i++)
858             be64_to_cpus(&l1_table[i]);
859     }
860     if (l1_allocated)
861         g_free(l1_table);
862     return ret;
863 }
864 
865 
866 
867 
868 /*********************************************************/
869 /* refcount checking functions */
870 
871 
872 
873 /*
874  * Increases the refcount for a range of clusters in a given refcount table.
875  * This is used to construct a temporary refcount table out of L1 and L2 tables
876  * which can be compared the the refcount table saved in the image.
877  *
878  * Modifies the number of errors in res.
879  */
880 static void inc_refcounts(BlockDriverState *bs,
881                           BdrvCheckResult *res,
882                           uint16_t *refcount_table,
883                           int refcount_table_size,
884                           int64_t offset, int64_t size)
885 {
886     BDRVQcowState *s = bs->opaque;
887     int64_t start, last, cluster_offset;
888     int k;
889 
890     if (size <= 0)
891         return;
892 
893     start = offset & ~(s->cluster_size - 1);
894     last = (offset + size - 1) & ~(s->cluster_size - 1);
895     for(cluster_offset = start; cluster_offset <= last;
896         cluster_offset += s->cluster_size) {
897         k = cluster_offset >> s->cluster_bits;
898         if (k < 0) {
899             fprintf(stderr, "ERROR: invalid cluster offset=0x%" PRIx64 "\n",
900                 cluster_offset);
901             res->corruptions++;
902         } else if (k >= refcount_table_size) {
903             fprintf(stderr, "Warning: cluster offset=0x%" PRIx64 " is after "
904                 "the end of the image file, can't properly check refcounts.\n",
905                 cluster_offset);
906             res->check_errors++;
907         } else {
908             if (++refcount_table[k] == 0) {
909                 fprintf(stderr, "ERROR: overflow cluster offset=0x%" PRIx64
910                     "\n", cluster_offset);
911                 res->corruptions++;
912             }
913         }
914     }
915 }
916 
917 /*
918  * Increases the refcount in the given refcount table for the all clusters
919  * referenced in the L2 table. While doing so, performs some checks on L2
920  * entries.
921  *
922  * Returns the number of errors found by the checks or -errno if an internal
923  * error occurred.
924  */
925 static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res,
926     uint16_t *refcount_table, int refcount_table_size, int64_t l2_offset,
927     int check_copied)
928 {
929     BDRVQcowState *s = bs->opaque;
930     uint64_t *l2_table, l2_entry;
931     int i, l2_size, nb_csectors, refcount;
932 
933     /* Read L2 table from disk */
934     l2_size = s->l2_size * sizeof(uint64_t);
935     l2_table = g_malloc(l2_size);
936 
937     if (bdrv_pread(bs->file, l2_offset, l2_table, l2_size) != l2_size)
938         goto fail;
939 
940     /* Do the actual checks */
941     for(i = 0; i < s->l2_size; i++) {
942         l2_entry = be64_to_cpu(l2_table[i]);
943 
944         switch (qcow2_get_cluster_type(l2_entry)) {
945         case QCOW2_CLUSTER_COMPRESSED:
946             /* Compressed clusters don't have QCOW_OFLAG_COPIED */
947             if (l2_entry & QCOW_OFLAG_COPIED) {
948                 fprintf(stderr, "ERROR: cluster %" PRId64 ": "
949                     "copied flag must never be set for compressed "
950                     "clusters\n", l2_entry >> s->cluster_bits);
951                 l2_entry &= ~QCOW_OFLAG_COPIED;
952                 res->corruptions++;
953             }
954 
955             /* Mark cluster as used */
956             nb_csectors = ((l2_entry >> s->csize_shift) &
957                            s->csize_mask) + 1;
958             l2_entry &= s->cluster_offset_mask;
959             inc_refcounts(bs, res, refcount_table, refcount_table_size,
960                 l2_entry & ~511, nb_csectors * 512);
961             break;
962 
963         case QCOW2_CLUSTER_ZERO:
964             if ((l2_entry & L2E_OFFSET_MASK) == 0) {
965                 break;
966             }
967             /* fall through */
968 
969         case QCOW2_CLUSTER_NORMAL:
970         {
971             /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
972             uint64_t offset = l2_entry & L2E_OFFSET_MASK;
973 
974             if (check_copied) {
975                 refcount = get_refcount(bs, offset >> s->cluster_bits);
976                 if (refcount < 0) {
977                     fprintf(stderr, "Can't get refcount for offset %"
978                         PRIx64 ": %s\n", l2_entry, strerror(-refcount));
979                     goto fail;
980                 }
981                 if ((refcount == 1) != ((l2_entry & QCOW_OFLAG_COPIED) != 0)) {
982                     fprintf(stderr, "ERROR OFLAG_COPIED: offset=%"
983                         PRIx64 " refcount=%d\n", l2_entry, refcount);
984                     res->corruptions++;
985                 }
986             }
987 
988             /* Mark cluster as used */
989             inc_refcounts(bs, res, refcount_table,refcount_table_size,
990                 offset, s->cluster_size);
991 
992             /* Correct offsets are cluster aligned */
993             if (offset & (s->cluster_size - 1)) {
994                 fprintf(stderr, "ERROR offset=%" PRIx64 ": Cluster is not "
995                     "properly aligned; L2 entry corrupted.\n", offset);
996                 res->corruptions++;
997             }
998             break;
999         }
1000 
1001         case QCOW2_CLUSTER_UNALLOCATED:
1002             break;
1003 
1004         default:
1005             abort();
1006         }
1007     }
1008 
1009     g_free(l2_table);
1010     return 0;
1011 
1012 fail:
1013     fprintf(stderr, "ERROR: I/O error in check_refcounts_l2\n");
1014     g_free(l2_table);
1015     return -EIO;
1016 }
1017 
1018 /*
1019  * Increases the refcount for the L1 table, its L2 tables and all referenced
1020  * clusters in the given refcount table. While doing so, performs some checks
1021  * on L1 and L2 entries.
1022  *
1023  * Returns the number of errors found by the checks or -errno if an internal
1024  * error occurred.
1025  */
1026 static int check_refcounts_l1(BlockDriverState *bs,
1027                               BdrvCheckResult *res,
1028                               uint16_t *refcount_table,
1029                               int refcount_table_size,
1030                               int64_t l1_table_offset, int l1_size,
1031                               int check_copied)
1032 {
1033     BDRVQcowState *s = bs->opaque;
1034     uint64_t *l1_table, l2_offset, l1_size2;
1035     int i, refcount, ret;
1036 
1037     l1_size2 = l1_size * sizeof(uint64_t);
1038 
1039     /* Mark L1 table as used */
1040     inc_refcounts(bs, res, refcount_table, refcount_table_size,
1041         l1_table_offset, l1_size2);
1042 
1043     /* Read L1 table entries from disk */
1044     if (l1_size2 == 0) {
1045         l1_table = NULL;
1046     } else {
1047         l1_table = g_malloc(l1_size2);
1048         if (bdrv_pread(bs->file, l1_table_offset,
1049                        l1_table, l1_size2) != l1_size2)
1050             goto fail;
1051         for(i = 0;i < l1_size; i++)
1052             be64_to_cpus(&l1_table[i]);
1053     }
1054 
1055     /* Do the actual checks */
1056     for(i = 0; i < l1_size; i++) {
1057         l2_offset = l1_table[i];
1058         if (l2_offset) {
1059             /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
1060             if (check_copied) {
1061                 refcount = get_refcount(bs, (l2_offset & ~QCOW_OFLAG_COPIED)
1062                     >> s->cluster_bits);
1063                 if (refcount < 0) {
1064                     fprintf(stderr, "Can't get refcount for l2_offset %"
1065                         PRIx64 ": %s\n", l2_offset, strerror(-refcount));
1066                     goto fail;
1067                 }
1068                 if ((refcount == 1) != ((l2_offset & QCOW_OFLAG_COPIED) != 0)) {
1069                     fprintf(stderr, "ERROR OFLAG_COPIED: l2_offset=%" PRIx64
1070                         " refcount=%d\n", l2_offset, refcount);
1071                     res->corruptions++;
1072                 }
1073             }
1074 
1075             /* Mark L2 table as used */
1076             l2_offset &= L1E_OFFSET_MASK;
1077             inc_refcounts(bs, res, refcount_table, refcount_table_size,
1078                 l2_offset, s->cluster_size);
1079 
1080             /* L2 tables are cluster aligned */
1081             if (l2_offset & (s->cluster_size - 1)) {
1082                 fprintf(stderr, "ERROR l2_offset=%" PRIx64 ": Table is not "
1083                     "cluster aligned; L1 entry corrupted\n", l2_offset);
1084                 res->corruptions++;
1085             }
1086 
1087             /* Process and check L2 entries */
1088             ret = check_refcounts_l2(bs, res, refcount_table,
1089                 refcount_table_size, l2_offset, check_copied);
1090             if (ret < 0) {
1091                 goto fail;
1092             }
1093         }
1094     }
1095     g_free(l1_table);
1096     return 0;
1097 
1098 fail:
1099     fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
1100     res->check_errors++;
1101     g_free(l1_table);
1102     return -EIO;
1103 }
1104 
1105 /*
1106  * Checks an image for refcount consistency.
1107  *
1108  * Returns 0 if no errors are found, the number of errors in case the image is
1109  * detected as corrupted, and -errno when an internal error occurred.
1110  */
1111 int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
1112                           BdrvCheckMode fix)
1113 {
1114     BDRVQcowState *s = bs->opaque;
1115     int64_t size, i;
1116     int nb_clusters, refcount1, refcount2;
1117     QCowSnapshot *sn;
1118     uint16_t *refcount_table;
1119     int ret;
1120 
1121     size = bdrv_getlength(bs->file);
1122     nb_clusters = size_to_clusters(s, size);
1123     refcount_table = g_malloc0(nb_clusters * sizeof(uint16_t));
1124 
1125     /* header */
1126     inc_refcounts(bs, res, refcount_table, nb_clusters,
1127         0, s->cluster_size);
1128 
1129     /* current L1 table */
1130     ret = check_refcounts_l1(bs, res, refcount_table, nb_clusters,
1131                        s->l1_table_offset, s->l1_size, 1);
1132     if (ret < 0) {
1133         goto fail;
1134     }
1135 
1136     /* snapshots */
1137     for(i = 0; i < s->nb_snapshots; i++) {
1138         sn = s->snapshots + i;
1139         ret = check_refcounts_l1(bs, res, refcount_table, nb_clusters,
1140             sn->l1_table_offset, sn->l1_size, 0);
1141         if (ret < 0) {
1142             goto fail;
1143         }
1144     }
1145     inc_refcounts(bs, res, refcount_table, nb_clusters,
1146         s->snapshots_offset, s->snapshots_size);
1147 
1148     /* refcount data */
1149     inc_refcounts(bs, res, refcount_table, nb_clusters,
1150         s->refcount_table_offset,
1151         s->refcount_table_size * sizeof(uint64_t));
1152 
1153     for(i = 0; i < s->refcount_table_size; i++) {
1154         uint64_t offset, cluster;
1155         offset = s->refcount_table[i];
1156         cluster = offset >> s->cluster_bits;
1157 
1158         /* Refcount blocks are cluster aligned */
1159         if (offset & (s->cluster_size - 1)) {
1160             fprintf(stderr, "ERROR refcount block %" PRId64 " is not "
1161                 "cluster aligned; refcount table entry corrupted\n", i);
1162             res->corruptions++;
1163             continue;
1164         }
1165 
1166         if (cluster >= nb_clusters) {
1167             fprintf(stderr, "ERROR refcount block %" PRId64
1168                     " is outside image\n", i);
1169             res->corruptions++;
1170             continue;
1171         }
1172 
1173         if (offset != 0) {
1174             inc_refcounts(bs, res, refcount_table, nb_clusters,
1175                 offset, s->cluster_size);
1176             if (refcount_table[cluster] != 1) {
1177                 fprintf(stderr, "ERROR refcount block %" PRId64
1178                     " refcount=%d\n",
1179                     i, refcount_table[cluster]);
1180                 res->corruptions++;
1181             }
1182         }
1183     }
1184 
1185     /* compare ref counts */
1186     for(i = 0; i < nb_clusters; i++) {
1187         refcount1 = get_refcount(bs, i);
1188         if (refcount1 < 0) {
1189             fprintf(stderr, "Can't get refcount for cluster %" PRId64 ": %s\n",
1190                 i, strerror(-refcount1));
1191             res->check_errors++;
1192             continue;
1193         }
1194 
1195         refcount2 = refcount_table[i];
1196         if (refcount1 != refcount2) {
1197 
1198             /* Check if we're allowed to fix the mismatch */
1199             int *num_fixed = NULL;
1200             if (refcount1 > refcount2 && (fix & BDRV_FIX_LEAKS)) {
1201                 num_fixed = &res->leaks_fixed;
1202             } else if (refcount1 < refcount2 && (fix & BDRV_FIX_ERRORS)) {
1203                 num_fixed = &res->corruptions_fixed;
1204             }
1205 
1206             fprintf(stderr, "%s cluster %" PRId64 " refcount=%d reference=%d\n",
1207                    num_fixed != NULL     ? "Repairing" :
1208                    refcount1 < refcount2 ? "ERROR" :
1209                                            "Leaked",
1210                    i, refcount1, refcount2);
1211 
1212             if (num_fixed) {
1213                 ret = update_refcount(bs, i << s->cluster_bits, 1,
1214                                       refcount2 - refcount1);
1215                 if (ret >= 0) {
1216                     (*num_fixed)++;
1217                     continue;
1218                 }
1219             }
1220 
1221             /* And if we couldn't, print an error */
1222             if (refcount1 < refcount2) {
1223                 res->corruptions++;
1224             } else {
1225                 res->leaks++;
1226             }
1227         }
1228     }
1229 
1230     ret = 0;
1231 
1232 fail:
1233     g_free(refcount_table);
1234 
1235     return ret;
1236 }
1237 
1238