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