xref: /openbmc/qemu/block/qcow2-cluster.c (revision ca693d1c)
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/osdep.h"
26 #include <zlib.h>
27 
28 #include "qapi/error.h"
29 #include "qemu-common.h"
30 #include "qcow2.h"
31 #include "qemu/bswap.h"
32 #include "trace.h"
33 
34 int qcow2_shrink_l1_table(BlockDriverState *bs, uint64_t exact_size)
35 {
36     BDRVQcow2State *s = bs->opaque;
37     int new_l1_size, i, ret;
38 
39     if (exact_size >= s->l1_size) {
40         return 0;
41     }
42 
43     new_l1_size = exact_size;
44 
45 #ifdef DEBUG_ALLOC2
46     fprintf(stderr, "shrink l1_table from %d to %d\n", s->l1_size, new_l1_size);
47 #endif
48 
49     BLKDBG_EVENT(bs->file, BLKDBG_L1_SHRINK_WRITE_TABLE);
50     ret = bdrv_pwrite_zeroes(bs->file, s->l1_table_offset +
51                                        new_l1_size * sizeof(uint64_t),
52                              (s->l1_size - new_l1_size) * sizeof(uint64_t), 0);
53     if (ret < 0) {
54         goto fail;
55     }
56 
57     ret = bdrv_flush(bs->file->bs);
58     if (ret < 0) {
59         goto fail;
60     }
61 
62     BLKDBG_EVENT(bs->file, BLKDBG_L1_SHRINK_FREE_L2_CLUSTERS);
63     for (i = s->l1_size - 1; i > new_l1_size - 1; i--) {
64         if ((s->l1_table[i] & L1E_OFFSET_MASK) == 0) {
65             continue;
66         }
67         qcow2_free_clusters(bs, s->l1_table[i] & L1E_OFFSET_MASK,
68                             s->cluster_size, QCOW2_DISCARD_ALWAYS);
69         s->l1_table[i] = 0;
70     }
71     return 0;
72 
73 fail:
74     /*
75      * If the write in the l1_table failed the image may contain a partially
76      * overwritten l1_table. In this case it would be better to clear the
77      * l1_table in memory to avoid possible image corruption.
78      */
79     memset(s->l1_table + new_l1_size, 0,
80            (s->l1_size - new_l1_size) * sizeof(uint64_t));
81     return ret;
82 }
83 
84 int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,
85                         bool exact_size)
86 {
87     BDRVQcow2State *s = bs->opaque;
88     int new_l1_size2, ret, i;
89     uint64_t *new_l1_table;
90     int64_t old_l1_table_offset, old_l1_size;
91     int64_t new_l1_table_offset, new_l1_size;
92     uint8_t data[12];
93 
94     if (min_size <= s->l1_size)
95         return 0;
96 
97     /* Do a sanity check on min_size before trying to calculate new_l1_size
98      * (this prevents overflows during the while loop for the calculation of
99      * new_l1_size) */
100     if (min_size > INT_MAX / sizeof(uint64_t)) {
101         return -EFBIG;
102     }
103 
104     if (exact_size) {
105         new_l1_size = min_size;
106     } else {
107         /* Bump size up to reduce the number of times we have to grow */
108         new_l1_size = s->l1_size;
109         if (new_l1_size == 0) {
110             new_l1_size = 1;
111         }
112         while (min_size > new_l1_size) {
113             new_l1_size = DIV_ROUND_UP(new_l1_size * 3, 2);
114         }
115     }
116 
117     QEMU_BUILD_BUG_ON(QCOW_MAX_L1_SIZE > INT_MAX);
118     if (new_l1_size > QCOW_MAX_L1_SIZE / sizeof(uint64_t)) {
119         return -EFBIG;
120     }
121 
122 #ifdef DEBUG_ALLOC2
123     fprintf(stderr, "grow l1_table from %d to %" PRId64 "\n",
124             s->l1_size, new_l1_size);
125 #endif
126 
127     new_l1_size2 = sizeof(uint64_t) * new_l1_size;
128     new_l1_table = qemu_try_blockalign(bs->file->bs,
129                                        ROUND_UP(new_l1_size2, 512));
130     if (new_l1_table == NULL) {
131         return -ENOMEM;
132     }
133     memset(new_l1_table, 0, ROUND_UP(new_l1_size2, 512));
134 
135     if (s->l1_size) {
136         memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t));
137     }
138 
139     /* write new table (align to cluster) */
140     BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_ALLOC_TABLE);
141     new_l1_table_offset = qcow2_alloc_clusters(bs, new_l1_size2);
142     if (new_l1_table_offset < 0) {
143         qemu_vfree(new_l1_table);
144         return new_l1_table_offset;
145     }
146 
147     ret = qcow2_cache_flush(bs, s->refcount_block_cache);
148     if (ret < 0) {
149         goto fail;
150     }
151 
152     /* the L1 position has not yet been updated, so these clusters must
153      * indeed be completely free */
154     ret = qcow2_pre_write_overlap_check(bs, 0, new_l1_table_offset,
155                                         new_l1_size2, false);
156     if (ret < 0) {
157         goto fail;
158     }
159 
160     BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_WRITE_TABLE);
161     for(i = 0; i < s->l1_size; i++)
162         new_l1_table[i] = cpu_to_be64(new_l1_table[i]);
163     ret = bdrv_pwrite_sync(bs->file, new_l1_table_offset,
164                            new_l1_table, new_l1_size2);
165     if (ret < 0)
166         goto fail;
167     for(i = 0; i < s->l1_size; i++)
168         new_l1_table[i] = be64_to_cpu(new_l1_table[i]);
169 
170     /* set new table */
171     BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_ACTIVATE_TABLE);
172     stl_be_p(data, new_l1_size);
173     stq_be_p(data + 4, new_l1_table_offset);
174     ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, l1_size),
175                            data, sizeof(data));
176     if (ret < 0) {
177         goto fail;
178     }
179     qemu_vfree(s->l1_table);
180     old_l1_table_offset = s->l1_table_offset;
181     s->l1_table_offset = new_l1_table_offset;
182     s->l1_table = new_l1_table;
183     old_l1_size = s->l1_size;
184     s->l1_size = new_l1_size;
185     qcow2_free_clusters(bs, old_l1_table_offset, old_l1_size * sizeof(uint64_t),
186                         QCOW2_DISCARD_OTHER);
187     return 0;
188  fail:
189     qemu_vfree(new_l1_table);
190     qcow2_free_clusters(bs, new_l1_table_offset, new_l1_size2,
191                         QCOW2_DISCARD_OTHER);
192     return ret;
193 }
194 
195 /*
196  * l2_load
197  *
198  * @bs: The BlockDriverState
199  * @offset: A guest offset, used to calculate what slice of the L2
200  *          table to load.
201  * @l2_offset: Offset to the L2 table in the image file.
202  * @l2_slice: Location to store the pointer to the L2 slice.
203  *
204  * Loads a L2 slice into memory (L2 slices are the parts of L2 tables
205  * that are loaded by the qcow2 cache). If the slice is in the cache,
206  * the cache is used; otherwise the L2 slice is loaded from the image
207  * file.
208  */
209 static int l2_load(BlockDriverState *bs, uint64_t offset,
210                    uint64_t l2_offset, uint64_t **l2_slice)
211 {
212     BDRVQcow2State *s = bs->opaque;
213     int start_of_slice = sizeof(uint64_t) *
214         (offset_to_l2_index(s, offset) - offset_to_l2_slice_index(s, offset));
215 
216     return qcow2_cache_get(bs, s->l2_table_cache, l2_offset + start_of_slice,
217                            (void **)l2_slice);
218 }
219 
220 /*
221  * Writes one sector of the L1 table to the disk (can't update single entries
222  * and we really don't want bdrv_pread to perform a read-modify-write)
223  */
224 #define L1_ENTRIES_PER_SECTOR (512 / 8)
225 int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index)
226 {
227     BDRVQcow2State *s = bs->opaque;
228     uint64_t buf[L1_ENTRIES_PER_SECTOR] = { 0 };
229     int l1_start_index;
230     int i, ret;
231 
232     l1_start_index = l1_index & ~(L1_ENTRIES_PER_SECTOR - 1);
233     for (i = 0; i < L1_ENTRIES_PER_SECTOR && l1_start_index + i < s->l1_size;
234          i++)
235     {
236         buf[i] = cpu_to_be64(s->l1_table[l1_start_index + i]);
237     }
238 
239     ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L1,
240             s->l1_table_offset + 8 * l1_start_index, sizeof(buf), false);
241     if (ret < 0) {
242         return ret;
243     }
244 
245     BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
246     ret = bdrv_pwrite_sync(bs->file,
247                            s->l1_table_offset + 8 * l1_start_index,
248                            buf, sizeof(buf));
249     if (ret < 0) {
250         return ret;
251     }
252 
253     return 0;
254 }
255 
256 /*
257  * l2_allocate
258  *
259  * Allocate a new l2 entry in the file. If l1_index points to an already
260  * used entry in the L2 table (i.e. we are doing a copy on write for the L2
261  * table) copy the contents of the old L2 table into the newly allocated one.
262  * Otherwise the new table is initialized with zeros.
263  *
264  */
265 
266 static int l2_allocate(BlockDriverState *bs, int l1_index)
267 {
268     BDRVQcow2State *s = bs->opaque;
269     uint64_t old_l2_offset;
270     uint64_t *l2_slice = NULL;
271     unsigned slice, slice_size2, n_slices;
272     int64_t l2_offset;
273     int ret;
274 
275     old_l2_offset = s->l1_table[l1_index];
276 
277     trace_qcow2_l2_allocate(bs, l1_index);
278 
279     /* allocate a new l2 entry */
280 
281     l2_offset = qcow2_alloc_clusters(bs, s->l2_size * sizeof(uint64_t));
282     if (l2_offset < 0) {
283         ret = l2_offset;
284         goto fail;
285     }
286 
287     /* The offset must fit in the offset field of the L1 table entry */
288     assert((l2_offset & L1E_OFFSET_MASK) == l2_offset);
289 
290     /* If we're allocating the table at offset 0 then something is wrong */
291     if (l2_offset == 0) {
292         qcow2_signal_corruption(bs, true, -1, -1, "Preventing invalid "
293                                 "allocation of L2 table at offset 0");
294         ret = -EIO;
295         goto fail;
296     }
297 
298     ret = qcow2_cache_flush(bs, s->refcount_block_cache);
299     if (ret < 0) {
300         goto fail;
301     }
302 
303     /* allocate a new entry in the l2 cache */
304 
305     slice_size2 = s->l2_slice_size * sizeof(uint64_t);
306     n_slices = s->cluster_size / slice_size2;
307 
308     trace_qcow2_l2_allocate_get_empty(bs, l1_index);
309     for (slice = 0; slice < n_slices; slice++) {
310         ret = qcow2_cache_get_empty(bs, s->l2_table_cache,
311                                     l2_offset + slice * slice_size2,
312                                     (void **) &l2_slice);
313         if (ret < 0) {
314             goto fail;
315         }
316 
317         if ((old_l2_offset & L1E_OFFSET_MASK) == 0) {
318             /* if there was no old l2 table, clear the new slice */
319             memset(l2_slice, 0, slice_size2);
320         } else {
321             uint64_t *old_slice;
322             uint64_t old_l2_slice_offset =
323                 (old_l2_offset & L1E_OFFSET_MASK) + slice * slice_size2;
324 
325             /* if there was an old l2 table, read a slice from the disk */
326             BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_COW_READ);
327             ret = qcow2_cache_get(bs, s->l2_table_cache, old_l2_slice_offset,
328                                   (void **) &old_slice);
329             if (ret < 0) {
330                 goto fail;
331             }
332 
333             memcpy(l2_slice, old_slice, slice_size2);
334 
335             qcow2_cache_put(s->l2_table_cache, (void **) &old_slice);
336         }
337 
338         /* write the l2 slice to the file */
339         BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_WRITE);
340 
341         trace_qcow2_l2_allocate_write_l2(bs, l1_index);
342         qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
343         qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
344     }
345 
346     ret = qcow2_cache_flush(bs, s->l2_table_cache);
347     if (ret < 0) {
348         goto fail;
349     }
350 
351     /* update the L1 entry */
352     trace_qcow2_l2_allocate_write_l1(bs, l1_index);
353     s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED;
354     ret = qcow2_write_l1_entry(bs, l1_index);
355     if (ret < 0) {
356         goto fail;
357     }
358 
359     trace_qcow2_l2_allocate_done(bs, l1_index, 0);
360     return 0;
361 
362 fail:
363     trace_qcow2_l2_allocate_done(bs, l1_index, ret);
364     if (l2_slice != NULL) {
365         qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
366     }
367     s->l1_table[l1_index] = old_l2_offset;
368     if (l2_offset > 0) {
369         qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t),
370                             QCOW2_DISCARD_ALWAYS);
371     }
372     return ret;
373 }
374 
375 /*
376  * Checks how many clusters in a given L2 slice are contiguous in the image
377  * file. As soon as one of the flags in the bitmask stop_flags changes compared
378  * to the first cluster, the search is stopped and the cluster is not counted
379  * as contiguous. (This allows it, for example, to stop at the first compressed
380  * cluster which may require a different handling)
381  */
382 static int count_contiguous_clusters(BlockDriverState *bs, int nb_clusters,
383         int cluster_size, uint64_t *l2_slice, uint64_t stop_flags)
384 {
385     int i;
386     QCow2ClusterType first_cluster_type;
387     uint64_t mask = stop_flags | L2E_OFFSET_MASK | QCOW_OFLAG_COMPRESSED;
388     uint64_t first_entry = be64_to_cpu(l2_slice[0]);
389     uint64_t offset = first_entry & mask;
390 
391     first_cluster_type = qcow2_get_cluster_type(bs, first_entry);
392     if (first_cluster_type == QCOW2_CLUSTER_UNALLOCATED) {
393         return 0;
394     }
395 
396     /* must be allocated */
397     assert(first_cluster_type == QCOW2_CLUSTER_NORMAL ||
398            first_cluster_type == QCOW2_CLUSTER_ZERO_ALLOC);
399 
400     for (i = 0; i < nb_clusters; i++) {
401         uint64_t l2_entry = be64_to_cpu(l2_slice[i]) & mask;
402         if (offset + (uint64_t) i * cluster_size != l2_entry) {
403             break;
404         }
405     }
406 
407         return i;
408 }
409 
410 /*
411  * Checks how many consecutive unallocated clusters in a given L2
412  * slice have the same cluster type.
413  */
414 static int count_contiguous_clusters_unallocated(BlockDriverState *bs,
415                                                  int nb_clusters,
416                                                  uint64_t *l2_slice,
417                                                  QCow2ClusterType wanted_type)
418 {
419     int i;
420 
421     assert(wanted_type == QCOW2_CLUSTER_ZERO_PLAIN ||
422            wanted_type == QCOW2_CLUSTER_UNALLOCATED);
423     for (i = 0; i < nb_clusters; i++) {
424         uint64_t entry = be64_to_cpu(l2_slice[i]);
425         QCow2ClusterType type = qcow2_get_cluster_type(bs, entry);
426 
427         if (type != wanted_type) {
428             break;
429         }
430     }
431 
432     return i;
433 }
434 
435 static int coroutine_fn do_perform_cow_read(BlockDriverState *bs,
436                                             uint64_t src_cluster_offset,
437                                             unsigned offset_in_cluster,
438                                             QEMUIOVector *qiov)
439 {
440     int ret;
441 
442     if (qiov->size == 0) {
443         return 0;
444     }
445 
446     BLKDBG_EVENT(bs->file, BLKDBG_COW_READ);
447 
448     if (!bs->drv) {
449         return -ENOMEDIUM;
450     }
451 
452     /* Call .bdrv_co_readv() directly instead of using the public block-layer
453      * interface.  This avoids double I/O throttling and request tracking,
454      * which can lead to deadlock when block layer copy-on-read is enabled.
455      */
456     ret = bs->drv->bdrv_co_preadv(bs, src_cluster_offset + offset_in_cluster,
457                                   qiov->size, qiov, 0);
458     if (ret < 0) {
459         return ret;
460     }
461 
462     return 0;
463 }
464 
465 static bool coroutine_fn do_perform_cow_encrypt(BlockDriverState *bs,
466                                                 uint64_t src_cluster_offset,
467                                                 uint64_t cluster_offset,
468                                                 unsigned offset_in_cluster,
469                                                 uint8_t *buffer,
470                                                 unsigned bytes)
471 {
472     if (bytes && bs->encrypted) {
473         BDRVQcow2State *s = bs->opaque;
474         assert((offset_in_cluster & ~BDRV_SECTOR_MASK) == 0);
475         assert((bytes & ~BDRV_SECTOR_MASK) == 0);
476         assert(s->crypto);
477         if (qcow2_co_encrypt(bs, cluster_offset,
478                              src_cluster_offset + offset_in_cluster,
479                              buffer, bytes) < 0) {
480             return false;
481         }
482     }
483     return true;
484 }
485 
486 static int coroutine_fn do_perform_cow_write(BlockDriverState *bs,
487                                              uint64_t cluster_offset,
488                                              unsigned offset_in_cluster,
489                                              QEMUIOVector *qiov)
490 {
491     BDRVQcow2State *s = bs->opaque;
492     int ret;
493 
494     if (qiov->size == 0) {
495         return 0;
496     }
497 
498     ret = qcow2_pre_write_overlap_check(bs, 0,
499             cluster_offset + offset_in_cluster, qiov->size, true);
500     if (ret < 0) {
501         return ret;
502     }
503 
504     BLKDBG_EVENT(bs->file, BLKDBG_COW_WRITE);
505     ret = bdrv_co_pwritev(s->data_file, cluster_offset + offset_in_cluster,
506                           qiov->size, qiov, 0);
507     if (ret < 0) {
508         return ret;
509     }
510 
511     return 0;
512 }
513 
514 
515 /*
516  * get_cluster_offset
517  *
518  * For a given offset of the virtual disk, find the cluster type and offset in
519  * the qcow2 file. The offset is stored in *cluster_offset.
520  *
521  * On entry, *bytes is the maximum number of contiguous bytes starting at
522  * offset that we are interested in.
523  *
524  * On exit, *bytes is the number of bytes starting at offset that have the same
525  * cluster type and (if applicable) are stored contiguously in the image file.
526  * Compressed clusters are always returned one by one.
527  *
528  * Returns the cluster type (QCOW2_CLUSTER_*) on success, -errno in error
529  * cases.
530  */
531 int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
532                              unsigned int *bytes, uint64_t *cluster_offset)
533 {
534     BDRVQcow2State *s = bs->opaque;
535     unsigned int l2_index;
536     uint64_t l1_index, l2_offset, *l2_slice;
537     int c;
538     unsigned int offset_in_cluster;
539     uint64_t bytes_available, bytes_needed, nb_clusters;
540     QCow2ClusterType type;
541     int ret;
542 
543     offset_in_cluster = offset_into_cluster(s, offset);
544     bytes_needed = (uint64_t) *bytes + offset_in_cluster;
545 
546     /* compute how many bytes there are between the start of the cluster
547      * containing offset and the end of the l2 slice that contains
548      * the entry pointing to it */
549     bytes_available =
550         ((uint64_t) (s->l2_slice_size - offset_to_l2_slice_index(s, offset)))
551         << s->cluster_bits;
552 
553     if (bytes_needed > bytes_available) {
554         bytes_needed = bytes_available;
555     }
556 
557     *cluster_offset = 0;
558 
559     /* seek to the l2 offset in the l1 table */
560 
561     l1_index = offset_to_l1_index(s, offset);
562     if (l1_index >= s->l1_size) {
563         type = QCOW2_CLUSTER_UNALLOCATED;
564         goto out;
565     }
566 
567     l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
568     if (!l2_offset) {
569         type = QCOW2_CLUSTER_UNALLOCATED;
570         goto out;
571     }
572 
573     if (offset_into_cluster(s, l2_offset)) {
574         qcow2_signal_corruption(bs, true, -1, -1, "L2 table offset %#" PRIx64
575                                 " unaligned (L1 index: %#" PRIx64 ")",
576                                 l2_offset, l1_index);
577         return -EIO;
578     }
579 
580     /* load the l2 slice in memory */
581 
582     ret = l2_load(bs, offset, l2_offset, &l2_slice);
583     if (ret < 0) {
584         return ret;
585     }
586 
587     /* find the cluster offset for the given disk offset */
588 
589     l2_index = offset_to_l2_slice_index(s, offset);
590     *cluster_offset = be64_to_cpu(l2_slice[l2_index]);
591 
592     nb_clusters = size_to_clusters(s, bytes_needed);
593     /* bytes_needed <= *bytes + offset_in_cluster, both of which are unsigned
594      * integers; the minimum cluster size is 512, so this assertion is always
595      * true */
596     assert(nb_clusters <= INT_MAX);
597 
598     type = qcow2_get_cluster_type(bs, *cluster_offset);
599     if (s->qcow_version < 3 && (type == QCOW2_CLUSTER_ZERO_PLAIN ||
600                                 type == QCOW2_CLUSTER_ZERO_ALLOC)) {
601         qcow2_signal_corruption(bs, true, -1, -1, "Zero cluster entry found"
602                                 " in pre-v3 image (L2 offset: %#" PRIx64
603                                 ", L2 index: %#x)", l2_offset, l2_index);
604         ret = -EIO;
605         goto fail;
606     }
607     switch (type) {
608     case QCOW2_CLUSTER_COMPRESSED:
609         if (has_data_file(bs)) {
610             qcow2_signal_corruption(bs, true, -1, -1, "Compressed cluster "
611                                     "entry found in image with external data "
612                                     "file (L2 offset: %#" PRIx64 ", L2 index: "
613                                     "%#x)", l2_offset, l2_index);
614             ret = -EIO;
615             goto fail;
616         }
617         /* Compressed clusters can only be processed one by one */
618         c = 1;
619         *cluster_offset &= L2E_COMPRESSED_OFFSET_SIZE_MASK;
620         break;
621     case QCOW2_CLUSTER_ZERO_PLAIN:
622     case QCOW2_CLUSTER_UNALLOCATED:
623         /* how many empty clusters ? */
624         c = count_contiguous_clusters_unallocated(bs, nb_clusters,
625                                                   &l2_slice[l2_index], type);
626         *cluster_offset = 0;
627         break;
628     case QCOW2_CLUSTER_ZERO_ALLOC:
629     case QCOW2_CLUSTER_NORMAL:
630         /* how many allocated clusters ? */
631         c = count_contiguous_clusters(bs, nb_clusters, s->cluster_size,
632                                       &l2_slice[l2_index], QCOW_OFLAG_ZERO);
633         *cluster_offset &= L2E_OFFSET_MASK;
634         if (offset_into_cluster(s, *cluster_offset)) {
635             qcow2_signal_corruption(bs, true, -1, -1,
636                                     "Cluster allocation offset %#"
637                                     PRIx64 " unaligned (L2 offset: %#" PRIx64
638                                     ", L2 index: %#x)", *cluster_offset,
639                                     l2_offset, l2_index);
640             ret = -EIO;
641             goto fail;
642         }
643         if (has_data_file(bs) && *cluster_offset != offset - offset_in_cluster)
644         {
645             qcow2_signal_corruption(bs, true, -1, -1,
646                                     "External data file host cluster offset %#"
647                                     PRIx64 " does not match guest cluster "
648                                     "offset: %#" PRIx64
649                                     ", L2 index: %#x)", *cluster_offset,
650                                     offset - offset_in_cluster, l2_index);
651             ret = -EIO;
652             goto fail;
653         }
654         break;
655     default:
656         abort();
657     }
658 
659     qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
660 
661     bytes_available = (int64_t)c * s->cluster_size;
662 
663 out:
664     if (bytes_available > bytes_needed) {
665         bytes_available = bytes_needed;
666     }
667 
668     /* bytes_available <= bytes_needed <= *bytes + offset_in_cluster;
669      * subtracting offset_in_cluster will therefore definitely yield something
670      * not exceeding UINT_MAX */
671     assert(bytes_available - offset_in_cluster <= UINT_MAX);
672     *bytes = bytes_available - offset_in_cluster;
673 
674     return type;
675 
676 fail:
677     qcow2_cache_put(s->l2_table_cache, (void **)&l2_slice);
678     return ret;
679 }
680 
681 /*
682  * get_cluster_table
683  *
684  * for a given disk offset, load (and allocate if needed)
685  * the appropriate slice of its l2 table.
686  *
687  * the cluster index in the l2 slice is given to the caller.
688  *
689  * Returns 0 on success, -errno in failure case
690  */
691 static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
692                              uint64_t **new_l2_slice,
693                              int *new_l2_index)
694 {
695     BDRVQcow2State *s = bs->opaque;
696     unsigned int l2_index;
697     uint64_t l1_index, l2_offset;
698     uint64_t *l2_slice = NULL;
699     int ret;
700 
701     /* seek to the l2 offset in the l1 table */
702 
703     l1_index = offset_to_l1_index(s, offset);
704     if (l1_index >= s->l1_size) {
705         ret = qcow2_grow_l1_table(bs, l1_index + 1, false);
706         if (ret < 0) {
707             return ret;
708         }
709     }
710 
711     assert(l1_index < s->l1_size);
712     l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
713     if (offset_into_cluster(s, l2_offset)) {
714         qcow2_signal_corruption(bs, true, -1, -1, "L2 table offset %#" PRIx64
715                                 " unaligned (L1 index: %#" PRIx64 ")",
716                                 l2_offset, l1_index);
717         return -EIO;
718     }
719 
720     if (!(s->l1_table[l1_index] & QCOW_OFLAG_COPIED)) {
721         /* First allocate a new L2 table (and do COW if needed) */
722         ret = l2_allocate(bs, l1_index);
723         if (ret < 0) {
724             return ret;
725         }
726 
727         /* Then decrease the refcount of the old table */
728         if (l2_offset) {
729             qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t),
730                                 QCOW2_DISCARD_OTHER);
731         }
732 
733         /* Get the offset of the newly-allocated l2 table */
734         l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
735         assert(offset_into_cluster(s, l2_offset) == 0);
736     }
737 
738     /* load the l2 slice in memory */
739     ret = l2_load(bs, offset, l2_offset, &l2_slice);
740     if (ret < 0) {
741         return ret;
742     }
743 
744     /* find the cluster offset for the given disk offset */
745 
746     l2_index = offset_to_l2_slice_index(s, offset);
747 
748     *new_l2_slice = l2_slice;
749     *new_l2_index = l2_index;
750 
751     return 0;
752 }
753 
754 /*
755  * alloc_compressed_cluster_offset
756  *
757  * For a given offset on the virtual disk, allocate a new compressed cluster
758  * and put the host offset of the cluster into *host_offset. If a cluster is
759  * already allocated at the offset, return an error.
760  *
761  * Return 0 on success and -errno in error cases
762  */
763 int qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
764                                           uint64_t offset,
765                                           int compressed_size,
766                                           uint64_t *host_offset)
767 {
768     BDRVQcow2State *s = bs->opaque;
769     int l2_index, ret;
770     uint64_t *l2_slice;
771     int64_t cluster_offset;
772     int nb_csectors;
773 
774     if (has_data_file(bs)) {
775         return 0;
776     }
777 
778     ret = get_cluster_table(bs, offset, &l2_slice, &l2_index);
779     if (ret < 0) {
780         return ret;
781     }
782 
783     /* Compression can't overwrite anything. Fail if the cluster was already
784      * allocated. */
785     cluster_offset = be64_to_cpu(l2_slice[l2_index]);
786     if (cluster_offset & L2E_OFFSET_MASK) {
787         qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
788         return -EIO;
789     }
790 
791     cluster_offset = qcow2_alloc_bytes(bs, compressed_size);
792     if (cluster_offset < 0) {
793         qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
794         return cluster_offset;
795     }
796 
797     nb_csectors =
798         (cluster_offset + compressed_size - 1) / QCOW2_COMPRESSED_SECTOR_SIZE -
799         (cluster_offset / QCOW2_COMPRESSED_SECTOR_SIZE);
800 
801     cluster_offset |= QCOW_OFLAG_COMPRESSED |
802                       ((uint64_t)nb_csectors << s->csize_shift);
803 
804     /* update L2 table */
805 
806     /* compressed clusters never have the copied flag */
807 
808     BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE_COMPRESSED);
809     qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
810     l2_slice[l2_index] = cpu_to_be64(cluster_offset);
811     qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
812 
813     *host_offset = cluster_offset & s->cluster_offset_mask;
814     return 0;
815 }
816 
817 static int perform_cow(BlockDriverState *bs, QCowL2Meta *m)
818 {
819     BDRVQcow2State *s = bs->opaque;
820     Qcow2COWRegion *start = &m->cow_start;
821     Qcow2COWRegion *end = &m->cow_end;
822     unsigned buffer_size;
823     unsigned data_bytes = end->offset - (start->offset + start->nb_bytes);
824     bool merge_reads;
825     uint8_t *start_buffer, *end_buffer;
826     QEMUIOVector qiov;
827     int ret;
828 
829     assert(start->nb_bytes <= UINT_MAX - end->nb_bytes);
830     assert(start->nb_bytes + end->nb_bytes <= UINT_MAX - data_bytes);
831     assert(start->offset + start->nb_bytes <= end->offset);
832     assert(!m->data_qiov || m->data_qiov->size == data_bytes);
833 
834     if ((start->nb_bytes == 0 && end->nb_bytes == 0) || m->skip_cow) {
835         return 0;
836     }
837 
838     /* If we have to read both the start and end COW regions and the
839      * middle region is not too large then perform just one read
840      * operation */
841     merge_reads = start->nb_bytes && end->nb_bytes && data_bytes <= 16384;
842     if (merge_reads) {
843         buffer_size = start->nb_bytes + data_bytes + end->nb_bytes;
844     } else {
845         /* If we have to do two reads, add some padding in the middle
846          * if necessary to make sure that the end region is optimally
847          * aligned. */
848         size_t align = bdrv_opt_mem_align(bs);
849         assert(align > 0 && align <= UINT_MAX);
850         assert(QEMU_ALIGN_UP(start->nb_bytes, align) <=
851                UINT_MAX - end->nb_bytes);
852         buffer_size = QEMU_ALIGN_UP(start->nb_bytes, align) + end->nb_bytes;
853     }
854 
855     /* Reserve a buffer large enough to store all the data that we're
856      * going to read */
857     start_buffer = qemu_try_blockalign(bs, buffer_size);
858     if (start_buffer == NULL) {
859         return -ENOMEM;
860     }
861     /* The part of the buffer where the end region is located */
862     end_buffer = start_buffer + buffer_size - end->nb_bytes;
863 
864     qemu_iovec_init(&qiov, 2 + (m->data_qiov ? m->data_qiov->niov : 0));
865 
866     qemu_co_mutex_unlock(&s->lock);
867     /* First we read the existing data from both COW regions. We
868      * either read the whole region in one go, or the start and end
869      * regions separately. */
870     if (merge_reads) {
871         qemu_iovec_add(&qiov, start_buffer, buffer_size);
872         ret = do_perform_cow_read(bs, m->offset, start->offset, &qiov);
873     } else {
874         qemu_iovec_add(&qiov, start_buffer, start->nb_bytes);
875         ret = do_perform_cow_read(bs, m->offset, start->offset, &qiov);
876         if (ret < 0) {
877             goto fail;
878         }
879 
880         qemu_iovec_reset(&qiov);
881         qemu_iovec_add(&qiov, end_buffer, end->nb_bytes);
882         ret = do_perform_cow_read(bs, m->offset, end->offset, &qiov);
883     }
884     if (ret < 0) {
885         goto fail;
886     }
887 
888     /* Encrypt the data if necessary before writing it */
889     if (bs->encrypted) {
890         if (!do_perform_cow_encrypt(bs, m->offset, m->alloc_offset,
891                                     start->offset, start_buffer,
892                                     start->nb_bytes) ||
893             !do_perform_cow_encrypt(bs, m->offset, m->alloc_offset,
894                                     end->offset, end_buffer, end->nb_bytes)) {
895             ret = -EIO;
896             goto fail;
897         }
898     }
899 
900     /* And now we can write everything. If we have the guest data we
901      * can write everything in one single operation */
902     if (m->data_qiov) {
903         qemu_iovec_reset(&qiov);
904         if (start->nb_bytes) {
905             qemu_iovec_add(&qiov, start_buffer, start->nb_bytes);
906         }
907         qemu_iovec_concat(&qiov, m->data_qiov, 0, data_bytes);
908         if (end->nb_bytes) {
909             qemu_iovec_add(&qiov, end_buffer, end->nb_bytes);
910         }
911         /* NOTE: we have a write_aio blkdebug event here followed by
912          * a cow_write one in do_perform_cow_write(), but there's only
913          * one single I/O operation */
914         BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
915         ret = do_perform_cow_write(bs, m->alloc_offset, start->offset, &qiov);
916     } else {
917         /* If there's no guest data then write both COW regions separately */
918         qemu_iovec_reset(&qiov);
919         qemu_iovec_add(&qiov, start_buffer, start->nb_bytes);
920         ret = do_perform_cow_write(bs, m->alloc_offset, start->offset, &qiov);
921         if (ret < 0) {
922             goto fail;
923         }
924 
925         qemu_iovec_reset(&qiov);
926         qemu_iovec_add(&qiov, end_buffer, end->nb_bytes);
927         ret = do_perform_cow_write(bs, m->alloc_offset, end->offset, &qiov);
928     }
929 
930 fail:
931     qemu_co_mutex_lock(&s->lock);
932 
933     /*
934      * Before we update the L2 table to actually point to the new cluster, we
935      * need to be sure that the refcounts have been increased and COW was
936      * handled.
937      */
938     if (ret == 0) {
939         qcow2_cache_depends_on_flush(s->l2_table_cache);
940     }
941 
942     qemu_vfree(start_buffer);
943     qemu_iovec_destroy(&qiov);
944     return ret;
945 }
946 
947 int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)
948 {
949     BDRVQcow2State *s = bs->opaque;
950     int i, j = 0, l2_index, ret;
951     uint64_t *old_cluster, *l2_slice;
952     uint64_t cluster_offset = m->alloc_offset;
953 
954     trace_qcow2_cluster_link_l2(qemu_coroutine_self(), m->nb_clusters);
955     assert(m->nb_clusters > 0);
956 
957     old_cluster = g_try_new(uint64_t, m->nb_clusters);
958     if (old_cluster == NULL) {
959         ret = -ENOMEM;
960         goto err;
961     }
962 
963     /* copy content of unmodified sectors */
964     ret = perform_cow(bs, m);
965     if (ret < 0) {
966         goto err;
967     }
968 
969     /* Update L2 table. */
970     if (s->use_lazy_refcounts) {
971         qcow2_mark_dirty(bs);
972     }
973     if (qcow2_need_accurate_refcounts(s)) {
974         qcow2_cache_set_dependency(bs, s->l2_table_cache,
975                                    s->refcount_block_cache);
976     }
977 
978     ret = get_cluster_table(bs, m->offset, &l2_slice, &l2_index);
979     if (ret < 0) {
980         goto err;
981     }
982     qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
983 
984     assert(l2_index + m->nb_clusters <= s->l2_slice_size);
985     for (i = 0; i < m->nb_clusters; i++) {
986         /* if two concurrent writes happen to the same unallocated cluster
987          * each write allocates separate cluster and writes data concurrently.
988          * The first one to complete updates l2 table with pointer to its
989          * cluster the second one has to do RMW (which is done above by
990          * perform_cow()), update l2 table with its cluster pointer and free
991          * old cluster. This is what this loop does */
992         if (l2_slice[l2_index + i] != 0) {
993             old_cluster[j++] = l2_slice[l2_index + i];
994         }
995 
996         l2_slice[l2_index + i] = cpu_to_be64((cluster_offset +
997                     (i << s->cluster_bits)) | QCOW_OFLAG_COPIED);
998      }
999 
1000 
1001     qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
1002 
1003     /*
1004      * If this was a COW, we need to decrease the refcount of the old cluster.
1005      *
1006      * Don't discard clusters that reach a refcount of 0 (e.g. compressed
1007      * clusters), the next write will reuse them anyway.
1008      */
1009     if (!m->keep_old_clusters && j != 0) {
1010         for (i = 0; i < j; i++) {
1011             qcow2_free_any_clusters(bs, be64_to_cpu(old_cluster[i]), 1,
1012                                     QCOW2_DISCARD_NEVER);
1013         }
1014     }
1015 
1016     ret = 0;
1017 err:
1018     g_free(old_cluster);
1019     return ret;
1020  }
1021 
1022 /**
1023  * Frees the allocated clusters because the request failed and they won't
1024  * actually be linked.
1025  */
1026 void qcow2_alloc_cluster_abort(BlockDriverState *bs, QCowL2Meta *m)
1027 {
1028     BDRVQcow2State *s = bs->opaque;
1029     qcow2_free_clusters(bs, m->alloc_offset, m->nb_clusters << s->cluster_bits,
1030                         QCOW2_DISCARD_NEVER);
1031 }
1032 
1033 /*
1034  * Returns the number of contiguous clusters that can be used for an allocating
1035  * write, but require COW to be performed (this includes yet unallocated space,
1036  * which must copy from the backing file)
1037  */
1038 static int count_cow_clusters(BlockDriverState *bs, int nb_clusters,
1039     uint64_t *l2_slice, int l2_index)
1040 {
1041     int i;
1042 
1043     for (i = 0; i < nb_clusters; i++) {
1044         uint64_t l2_entry = be64_to_cpu(l2_slice[l2_index + i]);
1045         QCow2ClusterType cluster_type = qcow2_get_cluster_type(bs, l2_entry);
1046 
1047         switch(cluster_type) {
1048         case QCOW2_CLUSTER_NORMAL:
1049             if (l2_entry & QCOW_OFLAG_COPIED) {
1050                 goto out;
1051             }
1052             break;
1053         case QCOW2_CLUSTER_UNALLOCATED:
1054         case QCOW2_CLUSTER_COMPRESSED:
1055         case QCOW2_CLUSTER_ZERO_PLAIN:
1056         case QCOW2_CLUSTER_ZERO_ALLOC:
1057             break;
1058         default:
1059             abort();
1060         }
1061     }
1062 
1063 out:
1064     assert(i <= nb_clusters);
1065     return i;
1066 }
1067 
1068 /*
1069  * Check if there already is an AIO write request in flight which allocates
1070  * the same cluster. In this case we need to wait until the previous
1071  * request has completed and updated the L2 table accordingly.
1072  *
1073  * Returns:
1074  *   0       if there was no dependency. *cur_bytes indicates the number of
1075  *           bytes from guest_offset that can be read before the next
1076  *           dependency must be processed (or the request is complete)
1077  *
1078  *   -EAGAIN if we had to wait for another request, previously gathered
1079  *           information on cluster allocation may be invalid now. The caller
1080  *           must start over anyway, so consider *cur_bytes undefined.
1081  */
1082 static int handle_dependencies(BlockDriverState *bs, uint64_t guest_offset,
1083     uint64_t *cur_bytes, QCowL2Meta **m)
1084 {
1085     BDRVQcow2State *s = bs->opaque;
1086     QCowL2Meta *old_alloc;
1087     uint64_t bytes = *cur_bytes;
1088 
1089     QLIST_FOREACH(old_alloc, &s->cluster_allocs, next_in_flight) {
1090 
1091         uint64_t start = guest_offset;
1092         uint64_t end = start + bytes;
1093         uint64_t old_start = l2meta_cow_start(old_alloc);
1094         uint64_t old_end = l2meta_cow_end(old_alloc);
1095 
1096         if (end <= old_start || start >= old_end) {
1097             /* No intersection */
1098         } else {
1099             if (start < old_start) {
1100                 /* Stop at the start of a running allocation */
1101                 bytes = old_start - start;
1102             } else {
1103                 bytes = 0;
1104             }
1105 
1106             /* Stop if already an l2meta exists. After yielding, it wouldn't
1107              * be valid any more, so we'd have to clean up the old L2Metas
1108              * and deal with requests depending on them before starting to
1109              * gather new ones. Not worth the trouble. */
1110             if (bytes == 0 && *m) {
1111                 *cur_bytes = 0;
1112                 return 0;
1113             }
1114 
1115             if (bytes == 0) {
1116                 /* Wait for the dependency to complete. We need to recheck
1117                  * the free/allocated clusters when we continue. */
1118                 qemu_co_queue_wait(&old_alloc->dependent_requests, &s->lock);
1119                 return -EAGAIN;
1120             }
1121         }
1122     }
1123 
1124     /* Make sure that existing clusters and new allocations are only used up to
1125      * the next dependency if we shortened the request above */
1126     *cur_bytes = bytes;
1127 
1128     return 0;
1129 }
1130 
1131 /*
1132  * Checks how many already allocated clusters that don't require a copy on
1133  * write there are at the given guest_offset (up to *bytes). If *host_offset is
1134  * not INV_OFFSET, only physically contiguous clusters beginning at this host
1135  * offset are counted.
1136  *
1137  * Note that guest_offset may not be cluster aligned. In this case, the
1138  * returned *host_offset points to exact byte referenced by guest_offset and
1139  * therefore isn't cluster aligned as well.
1140  *
1141  * Returns:
1142  *   0:     if no allocated clusters are available at the given offset.
1143  *          *bytes is normally unchanged. It is set to 0 if the cluster
1144  *          is allocated and doesn't need COW, but doesn't have the right
1145  *          physical offset.
1146  *
1147  *   1:     if allocated clusters that don't require a COW are available at
1148  *          the requested offset. *bytes may have decreased and describes
1149  *          the length of the area that can be written to.
1150  *
1151  *  -errno: in error cases
1152  */
1153 static int handle_copied(BlockDriverState *bs, uint64_t guest_offset,
1154     uint64_t *host_offset, uint64_t *bytes, QCowL2Meta **m)
1155 {
1156     BDRVQcow2State *s = bs->opaque;
1157     int l2_index;
1158     uint64_t cluster_offset;
1159     uint64_t *l2_slice;
1160     uint64_t nb_clusters;
1161     unsigned int keep_clusters;
1162     int ret;
1163 
1164     trace_qcow2_handle_copied(qemu_coroutine_self(), guest_offset, *host_offset,
1165                               *bytes);
1166 
1167     assert(*host_offset == INV_OFFSET || offset_into_cluster(s, guest_offset)
1168                                       == offset_into_cluster(s, *host_offset));
1169 
1170     /*
1171      * Calculate the number of clusters to look for. We stop at L2 slice
1172      * boundaries to keep things simple.
1173      */
1174     nb_clusters =
1175         size_to_clusters(s, offset_into_cluster(s, guest_offset) + *bytes);
1176 
1177     l2_index = offset_to_l2_slice_index(s, guest_offset);
1178     nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);
1179     assert(nb_clusters <= INT_MAX);
1180 
1181     /* Find L2 entry for the first involved cluster */
1182     ret = get_cluster_table(bs, guest_offset, &l2_slice, &l2_index);
1183     if (ret < 0) {
1184         return ret;
1185     }
1186 
1187     cluster_offset = be64_to_cpu(l2_slice[l2_index]);
1188 
1189     /* Check how many clusters are already allocated and don't need COW */
1190     if (qcow2_get_cluster_type(bs, cluster_offset) == QCOW2_CLUSTER_NORMAL
1191         && (cluster_offset & QCOW_OFLAG_COPIED))
1192     {
1193         /* If a specific host_offset is required, check it */
1194         bool offset_matches =
1195             (cluster_offset & L2E_OFFSET_MASK) == *host_offset;
1196 
1197         if (offset_into_cluster(s, cluster_offset & L2E_OFFSET_MASK)) {
1198             qcow2_signal_corruption(bs, true, -1, -1, "Data cluster offset "
1199                                     "%#llx unaligned (guest offset: %#" PRIx64
1200                                     ")", cluster_offset & L2E_OFFSET_MASK,
1201                                     guest_offset);
1202             ret = -EIO;
1203             goto out;
1204         }
1205 
1206         if (*host_offset != INV_OFFSET && !offset_matches) {
1207             *bytes = 0;
1208             ret = 0;
1209             goto out;
1210         }
1211 
1212         /* We keep all QCOW_OFLAG_COPIED clusters */
1213         keep_clusters =
1214             count_contiguous_clusters(bs, nb_clusters, s->cluster_size,
1215                                       &l2_slice[l2_index],
1216                                       QCOW_OFLAG_COPIED | QCOW_OFLAG_ZERO);
1217         assert(keep_clusters <= nb_clusters);
1218 
1219         *bytes = MIN(*bytes,
1220                  keep_clusters * s->cluster_size
1221                  - offset_into_cluster(s, guest_offset));
1222 
1223         ret = 1;
1224     } else {
1225         ret = 0;
1226     }
1227 
1228     /* Cleanup */
1229 out:
1230     qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
1231 
1232     /* Only return a host offset if we actually made progress. Otherwise we
1233      * would make requirements for handle_alloc() that it can't fulfill */
1234     if (ret > 0) {
1235         *host_offset = (cluster_offset & L2E_OFFSET_MASK)
1236                      + offset_into_cluster(s, guest_offset);
1237     }
1238 
1239     return ret;
1240 }
1241 
1242 /*
1243  * Allocates new clusters for the given guest_offset.
1244  *
1245  * At most *nb_clusters are allocated, and on return *nb_clusters is updated to
1246  * contain the number of clusters that have been allocated and are contiguous
1247  * in the image file.
1248  *
1249  * If *host_offset is not INV_OFFSET, it specifies the offset in the image file
1250  * at which the new clusters must start. *nb_clusters can be 0 on return in
1251  * this case if the cluster at host_offset is already in use. If *host_offset
1252  * is INV_OFFSET, the clusters can be allocated anywhere in the image file.
1253  *
1254  * *host_offset is updated to contain the offset into the image file at which
1255  * the first allocated cluster starts.
1256  *
1257  * Return 0 on success and -errno in error cases. -EAGAIN means that the
1258  * function has been waiting for another request and the allocation must be
1259  * restarted, but the whole request should not be failed.
1260  */
1261 static int do_alloc_cluster_offset(BlockDriverState *bs, uint64_t guest_offset,
1262                                    uint64_t *host_offset, uint64_t *nb_clusters)
1263 {
1264     BDRVQcow2State *s = bs->opaque;
1265 
1266     trace_qcow2_do_alloc_clusters_offset(qemu_coroutine_self(), guest_offset,
1267                                          *host_offset, *nb_clusters);
1268 
1269     if (has_data_file(bs)) {
1270         assert(*host_offset == INV_OFFSET ||
1271                *host_offset == start_of_cluster(s, guest_offset));
1272         *host_offset = start_of_cluster(s, guest_offset);
1273         return 0;
1274     }
1275 
1276     /* Allocate new clusters */
1277     trace_qcow2_cluster_alloc_phys(qemu_coroutine_self());
1278     if (*host_offset == INV_OFFSET) {
1279         int64_t cluster_offset =
1280             qcow2_alloc_clusters(bs, *nb_clusters * s->cluster_size);
1281         if (cluster_offset < 0) {
1282             return cluster_offset;
1283         }
1284         *host_offset = cluster_offset;
1285         return 0;
1286     } else {
1287         int64_t ret = qcow2_alloc_clusters_at(bs, *host_offset, *nb_clusters);
1288         if (ret < 0) {
1289             return ret;
1290         }
1291         *nb_clusters = ret;
1292         return 0;
1293     }
1294 }
1295 
1296 /*
1297  * Allocates new clusters for an area that either is yet unallocated or needs a
1298  * copy on write. If *host_offset is not INV_OFFSET, clusters are only
1299  * allocated if the new allocation can match the specified host offset.
1300  *
1301  * Note that guest_offset may not be cluster aligned. In this case, the
1302  * returned *host_offset points to exact byte referenced by guest_offset and
1303  * therefore isn't cluster aligned as well.
1304  *
1305  * Returns:
1306  *   0:     if no clusters could be allocated. *bytes is set to 0,
1307  *          *host_offset is left unchanged.
1308  *
1309  *   1:     if new clusters were allocated. *bytes may be decreased if the
1310  *          new allocation doesn't cover all of the requested area.
1311  *          *host_offset is updated to contain the host offset of the first
1312  *          newly allocated cluster.
1313  *
1314  *  -errno: in error cases
1315  */
1316 static int handle_alloc(BlockDriverState *bs, uint64_t guest_offset,
1317     uint64_t *host_offset, uint64_t *bytes, QCowL2Meta **m)
1318 {
1319     BDRVQcow2State *s = bs->opaque;
1320     int l2_index;
1321     uint64_t *l2_slice;
1322     uint64_t entry;
1323     uint64_t nb_clusters;
1324     int ret;
1325     bool keep_old_clusters = false;
1326 
1327     uint64_t alloc_cluster_offset = INV_OFFSET;
1328 
1329     trace_qcow2_handle_alloc(qemu_coroutine_self(), guest_offset, *host_offset,
1330                              *bytes);
1331     assert(*bytes > 0);
1332 
1333     /*
1334      * Calculate the number of clusters to look for. We stop at L2 slice
1335      * boundaries to keep things simple.
1336      */
1337     nb_clusters =
1338         size_to_clusters(s, offset_into_cluster(s, guest_offset) + *bytes);
1339 
1340     l2_index = offset_to_l2_slice_index(s, guest_offset);
1341     nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);
1342     assert(nb_clusters <= INT_MAX);
1343 
1344     /* Find L2 entry for the first involved cluster */
1345     ret = get_cluster_table(bs, guest_offset, &l2_slice, &l2_index);
1346     if (ret < 0) {
1347         return ret;
1348     }
1349 
1350     entry = be64_to_cpu(l2_slice[l2_index]);
1351 
1352     /* For the moment, overwrite compressed clusters one by one */
1353     if (entry & QCOW_OFLAG_COMPRESSED) {
1354         nb_clusters = 1;
1355     } else {
1356         nb_clusters = count_cow_clusters(bs, nb_clusters, l2_slice, l2_index);
1357     }
1358 
1359     /* This function is only called when there were no non-COW clusters, so if
1360      * we can't find any unallocated or COW clusters either, something is
1361      * wrong with our code. */
1362     assert(nb_clusters > 0);
1363 
1364     if (qcow2_get_cluster_type(bs, entry) == QCOW2_CLUSTER_ZERO_ALLOC &&
1365         (entry & QCOW_OFLAG_COPIED) &&
1366         (*host_offset == INV_OFFSET ||
1367          start_of_cluster(s, *host_offset) == (entry & L2E_OFFSET_MASK)))
1368     {
1369         int preallocated_nb_clusters;
1370 
1371         if (offset_into_cluster(s, entry & L2E_OFFSET_MASK)) {
1372             qcow2_signal_corruption(bs, true, -1, -1, "Preallocated zero "
1373                                     "cluster offset %#llx unaligned (guest "
1374                                     "offset: %#" PRIx64 ")",
1375                                     entry & L2E_OFFSET_MASK, guest_offset);
1376             ret = -EIO;
1377             goto fail;
1378         }
1379 
1380         /* Try to reuse preallocated zero clusters; contiguous normal clusters
1381          * would be fine, too, but count_cow_clusters() above has limited
1382          * nb_clusters already to a range of COW clusters */
1383         preallocated_nb_clusters =
1384             count_contiguous_clusters(bs, nb_clusters, s->cluster_size,
1385                                       &l2_slice[l2_index], QCOW_OFLAG_COPIED);
1386         assert(preallocated_nb_clusters > 0);
1387 
1388         nb_clusters = preallocated_nb_clusters;
1389         alloc_cluster_offset = entry & L2E_OFFSET_MASK;
1390 
1391         /* We want to reuse these clusters, so qcow2_alloc_cluster_link_l2()
1392          * should not free them. */
1393         keep_old_clusters = true;
1394     }
1395 
1396     qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
1397 
1398     if (alloc_cluster_offset == INV_OFFSET) {
1399         /* Allocate, if necessary at a given offset in the image file */
1400         alloc_cluster_offset = *host_offset == INV_OFFSET ? INV_OFFSET :
1401                                start_of_cluster(s, *host_offset);
1402         ret = do_alloc_cluster_offset(bs, guest_offset, &alloc_cluster_offset,
1403                                       &nb_clusters);
1404         if (ret < 0) {
1405             goto fail;
1406         }
1407 
1408         /* Can't extend contiguous allocation */
1409         if (nb_clusters == 0) {
1410             *bytes = 0;
1411             return 0;
1412         }
1413 
1414         assert(alloc_cluster_offset != INV_OFFSET);
1415     }
1416 
1417     /*
1418      * Save info needed for meta data update.
1419      *
1420      * requested_bytes: Number of bytes from the start of the first
1421      * newly allocated cluster to the end of the (possibly shortened
1422      * before) write request.
1423      *
1424      * avail_bytes: Number of bytes from the start of the first
1425      * newly allocated to the end of the last newly allocated cluster.
1426      *
1427      * nb_bytes: The number of bytes from the start of the first
1428      * newly allocated cluster to the end of the area that the write
1429      * request actually writes to (excluding COW at the end)
1430      */
1431     uint64_t requested_bytes = *bytes + offset_into_cluster(s, guest_offset);
1432     int avail_bytes = MIN(INT_MAX, nb_clusters << s->cluster_bits);
1433     int nb_bytes = MIN(requested_bytes, avail_bytes);
1434     QCowL2Meta *old_m = *m;
1435 
1436     *m = g_malloc0(sizeof(**m));
1437 
1438     **m = (QCowL2Meta) {
1439         .next           = old_m,
1440 
1441         .alloc_offset   = alloc_cluster_offset,
1442         .offset         = start_of_cluster(s, guest_offset),
1443         .nb_clusters    = nb_clusters,
1444 
1445         .keep_old_clusters  = keep_old_clusters,
1446 
1447         .cow_start = {
1448             .offset     = 0,
1449             .nb_bytes   = offset_into_cluster(s, guest_offset),
1450         },
1451         .cow_end = {
1452             .offset     = nb_bytes,
1453             .nb_bytes   = avail_bytes - nb_bytes,
1454         },
1455     };
1456     qemu_co_queue_init(&(*m)->dependent_requests);
1457     QLIST_INSERT_HEAD(&s->cluster_allocs, *m, next_in_flight);
1458 
1459     *host_offset = alloc_cluster_offset + offset_into_cluster(s, guest_offset);
1460     *bytes = MIN(*bytes, nb_bytes - offset_into_cluster(s, guest_offset));
1461     assert(*bytes != 0);
1462 
1463     return 1;
1464 
1465 fail:
1466     if (*m && (*m)->nb_clusters > 0) {
1467         QLIST_REMOVE(*m, next_in_flight);
1468     }
1469     return ret;
1470 }
1471 
1472 /*
1473  * alloc_cluster_offset
1474  *
1475  * For a given offset on the virtual disk, find the cluster offset in qcow2
1476  * file. If the offset is not found, allocate a new cluster.
1477  *
1478  * If the cluster was already allocated, m->nb_clusters is set to 0 and
1479  * other fields in m are meaningless.
1480  *
1481  * If the cluster is newly allocated, m->nb_clusters is set to the number of
1482  * contiguous clusters that have been allocated. In this case, the other
1483  * fields of m are valid and contain information about the first allocated
1484  * cluster.
1485  *
1486  * If the request conflicts with another write request in flight, the coroutine
1487  * is queued and will be reentered when the dependency has completed.
1488  *
1489  * Return 0 on success and -errno in error cases
1490  */
1491 int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
1492                                unsigned int *bytes, uint64_t *host_offset,
1493                                QCowL2Meta **m)
1494 {
1495     BDRVQcow2State *s = bs->opaque;
1496     uint64_t start, remaining;
1497     uint64_t cluster_offset;
1498     uint64_t cur_bytes;
1499     int ret;
1500 
1501     trace_qcow2_alloc_clusters_offset(qemu_coroutine_self(), offset, *bytes);
1502 
1503 again:
1504     start = offset;
1505     remaining = *bytes;
1506     cluster_offset = INV_OFFSET;
1507     *host_offset = INV_OFFSET;
1508     cur_bytes = 0;
1509     *m = NULL;
1510 
1511     while (true) {
1512 
1513         if (*host_offset == INV_OFFSET && cluster_offset != INV_OFFSET) {
1514             *host_offset = start_of_cluster(s, cluster_offset);
1515         }
1516 
1517         assert(remaining >= cur_bytes);
1518 
1519         start           += cur_bytes;
1520         remaining       -= cur_bytes;
1521 
1522         if (cluster_offset != INV_OFFSET) {
1523             cluster_offset += cur_bytes;
1524         }
1525 
1526         if (remaining == 0) {
1527             break;
1528         }
1529 
1530         cur_bytes = remaining;
1531 
1532         /*
1533          * Now start gathering as many contiguous clusters as possible:
1534          *
1535          * 1. Check for overlaps with in-flight allocations
1536          *
1537          *      a) Overlap not in the first cluster -> shorten this request and
1538          *         let the caller handle the rest in its next loop iteration.
1539          *
1540          *      b) Real overlaps of two requests. Yield and restart the search
1541          *         for contiguous clusters (the situation could have changed
1542          *         while we were sleeping)
1543          *
1544          *      c) TODO: Request starts in the same cluster as the in-flight
1545          *         allocation ends. Shorten the COW of the in-fight allocation,
1546          *         set cluster_offset to write to the same cluster and set up
1547          *         the right synchronisation between the in-flight request and
1548          *         the new one.
1549          */
1550         ret = handle_dependencies(bs, start, &cur_bytes, m);
1551         if (ret == -EAGAIN) {
1552             /* Currently handle_dependencies() doesn't yield if we already had
1553              * an allocation. If it did, we would have to clean up the L2Meta
1554              * structs before starting over. */
1555             assert(*m == NULL);
1556             goto again;
1557         } else if (ret < 0) {
1558             return ret;
1559         } else if (cur_bytes == 0) {
1560             break;
1561         } else {
1562             /* handle_dependencies() may have decreased cur_bytes (shortened
1563              * the allocations below) so that the next dependency is processed
1564              * correctly during the next loop iteration. */
1565         }
1566 
1567         /*
1568          * 2. Count contiguous COPIED clusters.
1569          */
1570         ret = handle_copied(bs, start, &cluster_offset, &cur_bytes, m);
1571         if (ret < 0) {
1572             return ret;
1573         } else if (ret) {
1574             continue;
1575         } else if (cur_bytes == 0) {
1576             break;
1577         }
1578 
1579         /*
1580          * 3. If the request still hasn't completed, allocate new clusters,
1581          *    considering any cluster_offset of steps 1c or 2.
1582          */
1583         ret = handle_alloc(bs, start, &cluster_offset, &cur_bytes, m);
1584         if (ret < 0) {
1585             return ret;
1586         } else if (ret) {
1587             continue;
1588         } else {
1589             assert(cur_bytes == 0);
1590             break;
1591         }
1592     }
1593 
1594     *bytes -= remaining;
1595     assert(*bytes > 0);
1596     assert(*host_offset != INV_OFFSET);
1597 
1598     return 0;
1599 }
1600 
1601 /*
1602  * This discards as many clusters of nb_clusters as possible at once (i.e.
1603  * all clusters in the same L2 slice) and returns the number of discarded
1604  * clusters.
1605  */
1606 static int discard_in_l2_slice(BlockDriverState *bs, uint64_t offset,
1607                                uint64_t nb_clusters,
1608                                enum qcow2_discard_type type, bool full_discard)
1609 {
1610     BDRVQcow2State *s = bs->opaque;
1611     uint64_t *l2_slice;
1612     int l2_index;
1613     int ret;
1614     int i;
1615 
1616     ret = get_cluster_table(bs, offset, &l2_slice, &l2_index);
1617     if (ret < 0) {
1618         return ret;
1619     }
1620 
1621     /* Limit nb_clusters to one L2 slice */
1622     nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);
1623     assert(nb_clusters <= INT_MAX);
1624 
1625     for (i = 0; i < nb_clusters; i++) {
1626         uint64_t old_l2_entry;
1627 
1628         old_l2_entry = be64_to_cpu(l2_slice[l2_index + i]);
1629 
1630         /*
1631          * If full_discard is false, make sure that a discarded area reads back
1632          * as zeroes for v3 images (we cannot do it for v2 without actually
1633          * writing a zero-filled buffer). We can skip the operation if the
1634          * cluster is already marked as zero, or if it's unallocated and we
1635          * don't have a backing file.
1636          *
1637          * TODO We might want to use bdrv_block_status(bs) here, but we're
1638          * holding s->lock, so that doesn't work today.
1639          *
1640          * If full_discard is true, the sector should not read back as zeroes,
1641          * but rather fall through to the backing file.
1642          */
1643         switch (qcow2_get_cluster_type(bs, old_l2_entry)) {
1644         case QCOW2_CLUSTER_UNALLOCATED:
1645             if (full_discard || !bs->backing) {
1646                 continue;
1647             }
1648             break;
1649 
1650         case QCOW2_CLUSTER_ZERO_PLAIN:
1651             if (!full_discard) {
1652                 continue;
1653             }
1654             break;
1655 
1656         case QCOW2_CLUSTER_ZERO_ALLOC:
1657         case QCOW2_CLUSTER_NORMAL:
1658         case QCOW2_CLUSTER_COMPRESSED:
1659             break;
1660 
1661         default:
1662             abort();
1663         }
1664 
1665         /* First remove L2 entries */
1666         qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
1667         if (!full_discard && s->qcow_version >= 3) {
1668             l2_slice[l2_index + i] = cpu_to_be64(QCOW_OFLAG_ZERO);
1669         } else {
1670             l2_slice[l2_index + i] = cpu_to_be64(0);
1671         }
1672 
1673         /* Then decrease the refcount */
1674         qcow2_free_any_clusters(bs, old_l2_entry, 1, type);
1675     }
1676 
1677     qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
1678 
1679     return nb_clusters;
1680 }
1681 
1682 int qcow2_cluster_discard(BlockDriverState *bs, uint64_t offset,
1683                           uint64_t bytes, enum qcow2_discard_type type,
1684                           bool full_discard)
1685 {
1686     BDRVQcow2State *s = bs->opaque;
1687     uint64_t end_offset = offset + bytes;
1688     uint64_t nb_clusters;
1689     int64_t cleared;
1690     int ret;
1691 
1692     /* Caller must pass aligned values, except at image end */
1693     assert(QEMU_IS_ALIGNED(offset, s->cluster_size));
1694     assert(QEMU_IS_ALIGNED(end_offset, s->cluster_size) ||
1695            end_offset == bs->total_sectors << BDRV_SECTOR_BITS);
1696 
1697     nb_clusters = size_to_clusters(s, bytes);
1698 
1699     s->cache_discards = true;
1700 
1701     /* Each L2 slice is handled by its own loop iteration */
1702     while (nb_clusters > 0) {
1703         cleared = discard_in_l2_slice(bs, offset, nb_clusters, type,
1704                                       full_discard);
1705         if (cleared < 0) {
1706             ret = cleared;
1707             goto fail;
1708         }
1709 
1710         nb_clusters -= cleared;
1711         offset += (cleared * s->cluster_size);
1712     }
1713 
1714     ret = 0;
1715 fail:
1716     s->cache_discards = false;
1717     qcow2_process_discards(bs, ret);
1718 
1719     return ret;
1720 }
1721 
1722 /*
1723  * This zeroes as many clusters of nb_clusters as possible at once (i.e.
1724  * all clusters in the same L2 slice) and returns the number of zeroed
1725  * clusters.
1726  */
1727 static int zero_in_l2_slice(BlockDriverState *bs, uint64_t offset,
1728                             uint64_t nb_clusters, int flags)
1729 {
1730     BDRVQcow2State *s = bs->opaque;
1731     uint64_t *l2_slice;
1732     int l2_index;
1733     int ret;
1734     int i;
1735     bool unmap = !!(flags & BDRV_REQ_MAY_UNMAP);
1736 
1737     ret = get_cluster_table(bs, offset, &l2_slice, &l2_index);
1738     if (ret < 0) {
1739         return ret;
1740     }
1741 
1742     /* Limit nb_clusters to one L2 slice */
1743     nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);
1744     assert(nb_clusters <= INT_MAX);
1745 
1746     for (i = 0; i < nb_clusters; i++) {
1747         uint64_t old_offset;
1748         QCow2ClusterType cluster_type;
1749 
1750         old_offset = be64_to_cpu(l2_slice[l2_index + i]);
1751 
1752         /*
1753          * Minimize L2 changes if the cluster already reads back as
1754          * zeroes with correct allocation.
1755          */
1756         cluster_type = qcow2_get_cluster_type(bs, old_offset);
1757         if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN ||
1758             (cluster_type == QCOW2_CLUSTER_ZERO_ALLOC && !unmap)) {
1759             continue;
1760         }
1761 
1762         qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
1763         if (cluster_type == QCOW2_CLUSTER_COMPRESSED || unmap) {
1764             l2_slice[l2_index + i] = cpu_to_be64(QCOW_OFLAG_ZERO);
1765             qcow2_free_any_clusters(bs, old_offset, 1, QCOW2_DISCARD_REQUEST);
1766         } else {
1767             l2_slice[l2_index + i] |= cpu_to_be64(QCOW_OFLAG_ZERO);
1768         }
1769     }
1770 
1771     qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
1772 
1773     return nb_clusters;
1774 }
1775 
1776 int qcow2_cluster_zeroize(BlockDriverState *bs, uint64_t offset,
1777                           uint64_t bytes, int flags)
1778 {
1779     BDRVQcow2State *s = bs->opaque;
1780     uint64_t end_offset = offset + bytes;
1781     uint64_t nb_clusters;
1782     int64_t cleared;
1783     int ret;
1784 
1785     /* If we have to stay in sync with an external data file, zero out
1786      * s->data_file first. */
1787     if (data_file_is_raw(bs)) {
1788         assert(has_data_file(bs));
1789         ret = bdrv_co_pwrite_zeroes(s->data_file, offset, bytes, flags);
1790         if (ret < 0) {
1791             return ret;
1792         }
1793     }
1794 
1795     /* Caller must pass aligned values, except at image end */
1796     assert(QEMU_IS_ALIGNED(offset, s->cluster_size));
1797     assert(QEMU_IS_ALIGNED(end_offset, s->cluster_size) ||
1798            end_offset == bs->total_sectors << BDRV_SECTOR_BITS);
1799 
1800     /* The zero flag is only supported by version 3 and newer */
1801     if (s->qcow_version < 3) {
1802         return -ENOTSUP;
1803     }
1804 
1805     /* Each L2 slice is handled by its own loop iteration */
1806     nb_clusters = size_to_clusters(s, bytes);
1807 
1808     s->cache_discards = true;
1809 
1810     while (nb_clusters > 0) {
1811         cleared = zero_in_l2_slice(bs, offset, nb_clusters, flags);
1812         if (cleared < 0) {
1813             ret = cleared;
1814             goto fail;
1815         }
1816 
1817         nb_clusters -= cleared;
1818         offset += (cleared * s->cluster_size);
1819     }
1820 
1821     ret = 0;
1822 fail:
1823     s->cache_discards = false;
1824     qcow2_process_discards(bs, ret);
1825 
1826     return ret;
1827 }
1828 
1829 /*
1830  * Expands all zero clusters in a specific L1 table (or deallocates them, for
1831  * non-backed non-pre-allocated zero clusters).
1832  *
1833  * l1_entries and *visited_l1_entries are used to keep track of progress for
1834  * status_cb(). l1_entries contains the total number of L1 entries and
1835  * *visited_l1_entries counts all visited L1 entries.
1836  */
1837 static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,
1838                                       int l1_size, int64_t *visited_l1_entries,
1839                                       int64_t l1_entries,
1840                                       BlockDriverAmendStatusCB *status_cb,
1841                                       void *cb_opaque)
1842 {
1843     BDRVQcow2State *s = bs->opaque;
1844     bool is_active_l1 = (l1_table == s->l1_table);
1845     uint64_t *l2_slice = NULL;
1846     unsigned slice, slice_size2, n_slices;
1847     int ret;
1848     int i, j;
1849 
1850     slice_size2 = s->l2_slice_size * sizeof(uint64_t);
1851     n_slices = s->cluster_size / slice_size2;
1852 
1853     if (!is_active_l1) {
1854         /* inactive L2 tables require a buffer to be stored in when loading
1855          * them from disk */
1856         l2_slice = qemu_try_blockalign(bs->file->bs, slice_size2);
1857         if (l2_slice == NULL) {
1858             return -ENOMEM;
1859         }
1860     }
1861 
1862     for (i = 0; i < l1_size; i++) {
1863         uint64_t l2_offset = l1_table[i] & L1E_OFFSET_MASK;
1864         uint64_t l2_refcount;
1865 
1866         if (!l2_offset) {
1867             /* unallocated */
1868             (*visited_l1_entries)++;
1869             if (status_cb) {
1870                 status_cb(bs, *visited_l1_entries, l1_entries, cb_opaque);
1871             }
1872             continue;
1873         }
1874 
1875         if (offset_into_cluster(s, l2_offset)) {
1876             qcow2_signal_corruption(bs, true, -1, -1, "L2 table offset %#"
1877                                     PRIx64 " unaligned (L1 index: %#x)",
1878                                     l2_offset, i);
1879             ret = -EIO;
1880             goto fail;
1881         }
1882 
1883         ret = qcow2_get_refcount(bs, l2_offset >> s->cluster_bits,
1884                                  &l2_refcount);
1885         if (ret < 0) {
1886             goto fail;
1887         }
1888 
1889         for (slice = 0; slice < n_slices; slice++) {
1890             uint64_t slice_offset = l2_offset + slice * slice_size2;
1891             bool l2_dirty = false;
1892             if (is_active_l1) {
1893                 /* get active L2 tables from cache */
1894                 ret = qcow2_cache_get(bs, s->l2_table_cache, slice_offset,
1895                                       (void **)&l2_slice);
1896             } else {
1897                 /* load inactive L2 tables from disk */
1898                 ret = bdrv_pread(bs->file, slice_offset, l2_slice, slice_size2);
1899             }
1900             if (ret < 0) {
1901                 goto fail;
1902             }
1903 
1904             for (j = 0; j < s->l2_slice_size; j++) {
1905                 uint64_t l2_entry = be64_to_cpu(l2_slice[j]);
1906                 int64_t offset = l2_entry & L2E_OFFSET_MASK;
1907                 QCow2ClusterType cluster_type =
1908                     qcow2_get_cluster_type(bs, l2_entry);
1909 
1910                 if (cluster_type != QCOW2_CLUSTER_ZERO_PLAIN &&
1911                     cluster_type != QCOW2_CLUSTER_ZERO_ALLOC) {
1912                     continue;
1913                 }
1914 
1915                 if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN) {
1916                     if (!bs->backing) {
1917                         /* not backed; therefore we can simply deallocate the
1918                          * cluster */
1919                         l2_slice[j] = 0;
1920                         l2_dirty = true;
1921                         continue;
1922                     }
1923 
1924                     offset = qcow2_alloc_clusters(bs, s->cluster_size);
1925                     if (offset < 0) {
1926                         ret = offset;
1927                         goto fail;
1928                     }
1929 
1930                     if (l2_refcount > 1) {
1931                         /* For shared L2 tables, set the refcount accordingly
1932                          * (it is already 1 and needs to be l2_refcount) */
1933                         ret = qcow2_update_cluster_refcount(
1934                             bs, offset >> s->cluster_bits,
1935                             refcount_diff(1, l2_refcount), false,
1936                             QCOW2_DISCARD_OTHER);
1937                         if (ret < 0) {
1938                             qcow2_free_clusters(bs, offset, s->cluster_size,
1939                                                 QCOW2_DISCARD_OTHER);
1940                             goto fail;
1941                         }
1942                     }
1943                 }
1944 
1945                 if (offset_into_cluster(s, offset)) {
1946                     int l2_index = slice * s->l2_slice_size + j;
1947                     qcow2_signal_corruption(
1948                         bs, true, -1, -1,
1949                         "Cluster allocation offset "
1950                         "%#" PRIx64 " unaligned (L2 offset: %#"
1951                         PRIx64 ", L2 index: %#x)", offset,
1952                         l2_offset, l2_index);
1953                     if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN) {
1954                         qcow2_free_clusters(bs, offset, s->cluster_size,
1955                                             QCOW2_DISCARD_ALWAYS);
1956                     }
1957                     ret = -EIO;
1958                     goto fail;
1959                 }
1960 
1961                 ret = qcow2_pre_write_overlap_check(bs, 0, offset,
1962                                                     s->cluster_size, true);
1963                 if (ret < 0) {
1964                     if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN) {
1965                         qcow2_free_clusters(bs, offset, s->cluster_size,
1966                                             QCOW2_DISCARD_ALWAYS);
1967                     }
1968                     goto fail;
1969                 }
1970 
1971                 ret = bdrv_pwrite_zeroes(s->data_file, offset,
1972                                          s->cluster_size, 0);
1973                 if (ret < 0) {
1974                     if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN) {
1975                         qcow2_free_clusters(bs, offset, s->cluster_size,
1976                                             QCOW2_DISCARD_ALWAYS);
1977                     }
1978                     goto fail;
1979                 }
1980 
1981                 if (l2_refcount == 1) {
1982                     l2_slice[j] = cpu_to_be64(offset | QCOW_OFLAG_COPIED);
1983                 } else {
1984                     l2_slice[j] = cpu_to_be64(offset);
1985                 }
1986                 l2_dirty = true;
1987             }
1988 
1989             if (is_active_l1) {
1990                 if (l2_dirty) {
1991                     qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
1992                     qcow2_cache_depends_on_flush(s->l2_table_cache);
1993                 }
1994                 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
1995             } else {
1996                 if (l2_dirty) {
1997                     ret = qcow2_pre_write_overlap_check(
1998                         bs, QCOW2_OL_INACTIVE_L2 | QCOW2_OL_ACTIVE_L2,
1999                         slice_offset, slice_size2, false);
2000                     if (ret < 0) {
2001                         goto fail;
2002                     }
2003 
2004                     ret = bdrv_pwrite(bs->file, slice_offset,
2005                                       l2_slice, slice_size2);
2006                     if (ret < 0) {
2007                         goto fail;
2008                     }
2009                 }
2010             }
2011         }
2012 
2013         (*visited_l1_entries)++;
2014         if (status_cb) {
2015             status_cb(bs, *visited_l1_entries, l1_entries, cb_opaque);
2016         }
2017     }
2018 
2019     ret = 0;
2020 
2021 fail:
2022     if (l2_slice) {
2023         if (!is_active_l1) {
2024             qemu_vfree(l2_slice);
2025         } else {
2026             qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
2027         }
2028     }
2029     return ret;
2030 }
2031 
2032 /*
2033  * For backed images, expands all zero clusters on the image. For non-backed
2034  * images, deallocates all non-pre-allocated zero clusters (and claims the
2035  * allocation for pre-allocated ones). This is important for downgrading to a
2036  * qcow2 version which doesn't yet support metadata zero clusters.
2037  */
2038 int qcow2_expand_zero_clusters(BlockDriverState *bs,
2039                                BlockDriverAmendStatusCB *status_cb,
2040                                void *cb_opaque)
2041 {
2042     BDRVQcow2State *s = bs->opaque;
2043     uint64_t *l1_table = NULL;
2044     int64_t l1_entries = 0, visited_l1_entries = 0;
2045     int ret;
2046     int i, j;
2047 
2048     if (status_cb) {
2049         l1_entries = s->l1_size;
2050         for (i = 0; i < s->nb_snapshots; i++) {
2051             l1_entries += s->snapshots[i].l1_size;
2052         }
2053     }
2054 
2055     ret = expand_zero_clusters_in_l1(bs, s->l1_table, s->l1_size,
2056                                      &visited_l1_entries, l1_entries,
2057                                      status_cb, cb_opaque);
2058     if (ret < 0) {
2059         goto fail;
2060     }
2061 
2062     /* Inactive L1 tables may point to active L2 tables - therefore it is
2063      * necessary to flush the L2 table cache before trying to access the L2
2064      * tables pointed to by inactive L1 entries (else we might try to expand
2065      * zero clusters that have already been expanded); furthermore, it is also
2066      * necessary to empty the L2 table cache, since it may contain tables which
2067      * are now going to be modified directly on disk, bypassing the cache.
2068      * qcow2_cache_empty() does both for us. */
2069     ret = qcow2_cache_empty(bs, s->l2_table_cache);
2070     if (ret < 0) {
2071         goto fail;
2072     }
2073 
2074     for (i = 0; i < s->nb_snapshots; i++) {
2075         int l1_size2;
2076         uint64_t *new_l1_table;
2077         Error *local_err = NULL;
2078 
2079         ret = qcow2_validate_table(bs, s->snapshots[i].l1_table_offset,
2080                                    s->snapshots[i].l1_size, sizeof(uint64_t),
2081                                    QCOW_MAX_L1_SIZE, "Snapshot L1 table",
2082                                    &local_err);
2083         if (ret < 0) {
2084             error_report_err(local_err);
2085             goto fail;
2086         }
2087 
2088         l1_size2 = s->snapshots[i].l1_size * sizeof(uint64_t);
2089         new_l1_table = g_try_realloc(l1_table, l1_size2);
2090 
2091         if (!new_l1_table) {
2092             ret = -ENOMEM;
2093             goto fail;
2094         }
2095 
2096         l1_table = new_l1_table;
2097 
2098         ret = bdrv_pread(bs->file, s->snapshots[i].l1_table_offset,
2099                          l1_table, l1_size2);
2100         if (ret < 0) {
2101             goto fail;
2102         }
2103 
2104         for (j = 0; j < s->snapshots[i].l1_size; j++) {
2105             be64_to_cpus(&l1_table[j]);
2106         }
2107 
2108         ret = expand_zero_clusters_in_l1(bs, l1_table, s->snapshots[i].l1_size,
2109                                          &visited_l1_entries, l1_entries,
2110                                          status_cb, cb_opaque);
2111         if (ret < 0) {
2112             goto fail;
2113         }
2114     }
2115 
2116     ret = 0;
2117 
2118 fail:
2119     g_free(l1_table);
2120     return ret;
2121 }
2122