xref: /openbmc/qemu/block/qcow2-cluster.c (revision 979a8902)
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 "qcow2.h"
30 #include "qemu/bswap.h"
31 #include "trace.h"
32 
33 int qcow2_shrink_l1_table(BlockDriverState *bs, uint64_t exact_size)
34 {
35     BDRVQcow2State *s = bs->opaque;
36     int new_l1_size, i, ret;
37 
38     if (exact_size >= s->l1_size) {
39         return 0;
40     }
41 
42     new_l1_size = exact_size;
43 
44 #ifdef DEBUG_ALLOC2
45     fprintf(stderr, "shrink l1_table from %d to %d\n", s->l1_size, new_l1_size);
46 #endif
47 
48     BLKDBG_EVENT(bs->file, BLKDBG_L1_SHRINK_WRITE_TABLE);
49     ret = bdrv_pwrite_zeroes(bs->file, s->l1_table_offset +
50                                        new_l1_size * sizeof(uint64_t),
51                              (s->l1_size - new_l1_size) * sizeof(uint64_t), 0);
52     if (ret < 0) {
53         goto fail;
54     }
55 
56     ret = bdrv_flush(bs->file->bs);
57     if (ret < 0) {
58         goto fail;
59     }
60 
61     BLKDBG_EVENT(bs->file, BLKDBG_L1_SHRINK_FREE_L2_CLUSTERS);
62     for (i = s->l1_size - 1; i > new_l1_size - 1; i--) {
63         if ((s->l1_table[i] & L1E_OFFSET_MASK) == 0) {
64             continue;
65         }
66         qcow2_free_clusters(bs, s->l1_table[i] & L1E_OFFSET_MASK,
67                             s->cluster_size, QCOW2_DISCARD_ALWAYS);
68         s->l1_table[i] = 0;
69     }
70     return 0;
71 
72 fail:
73     /*
74      * If the write in the l1_table failed the image may contain a partially
75      * overwritten l1_table. In this case it would be better to clear the
76      * l1_table in memory to avoid possible image corruption.
77      */
78     memset(s->l1_table + new_l1_size, 0,
79            (s->l1_size - new_l1_size) * sizeof(uint64_t));
80     return ret;
81 }
82 
83 int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,
84                         bool exact_size)
85 {
86     BDRVQcow2State *s = bs->opaque;
87     int new_l1_size2, ret, i;
88     uint64_t *new_l1_table;
89     int64_t old_l1_table_offset, old_l1_size;
90     int64_t new_l1_table_offset, new_l1_size;
91     uint8_t data[12];
92 
93     if (min_size <= s->l1_size)
94         return 0;
95 
96     /* Do a sanity check on min_size before trying to calculate new_l1_size
97      * (this prevents overflows during the while loop for the calculation of
98      * new_l1_size) */
99     if (min_size > INT_MAX / sizeof(uint64_t)) {
100         return -EFBIG;
101     }
102 
103     if (exact_size) {
104         new_l1_size = min_size;
105     } else {
106         /* Bump size up to reduce the number of times we have to grow */
107         new_l1_size = s->l1_size;
108         if (new_l1_size == 0) {
109             new_l1_size = 1;
110         }
111         while (min_size > new_l1_size) {
112             new_l1_size = DIV_ROUND_UP(new_l1_size * 3, 2);
113         }
114     }
115 
116     QEMU_BUILD_BUG_ON(QCOW_MAX_L1_SIZE > INT_MAX);
117     if (new_l1_size > QCOW_MAX_L1_SIZE / sizeof(uint64_t)) {
118         return -EFBIG;
119     }
120 
121 #ifdef DEBUG_ALLOC2
122     fprintf(stderr, "grow l1_table from %d to %" PRId64 "\n",
123             s->l1_size, new_l1_size);
124 #endif
125 
126     new_l1_size2 = sizeof(uint64_t) * new_l1_size;
127     new_l1_table = qemu_try_blockalign(bs->file->bs, new_l1_size2);
128     if (new_l1_table == NULL) {
129         return -ENOMEM;
130     }
131     memset(new_l1_table, 0, new_l1_size2);
132 
133     if (s->l1_size) {
134         memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t));
135     }
136 
137     /* write new table (align to cluster) */
138     BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_ALLOC_TABLE);
139     new_l1_table_offset = qcow2_alloc_clusters(bs, new_l1_size2);
140     if (new_l1_table_offset < 0) {
141         qemu_vfree(new_l1_table);
142         return new_l1_table_offset;
143     }
144 
145     ret = qcow2_cache_flush(bs, s->refcount_block_cache);
146     if (ret < 0) {
147         goto fail;
148     }
149 
150     /* the L1 position has not yet been updated, so these clusters must
151      * indeed be completely free */
152     ret = qcow2_pre_write_overlap_check(bs, 0, new_l1_table_offset,
153                                         new_l1_size2, false);
154     if (ret < 0) {
155         goto fail;
156     }
157 
158     BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_WRITE_TABLE);
159     for(i = 0; i < s->l1_size; i++)
160         new_l1_table[i] = cpu_to_be64(new_l1_table[i]);
161     ret = bdrv_pwrite_sync(bs->file, new_l1_table_offset,
162                            new_l1_table, new_l1_size2);
163     if (ret < 0)
164         goto fail;
165     for(i = 0; i < s->l1_size; i++)
166         new_l1_table[i] = be64_to_cpu(new_l1_table[i]);
167 
168     /* set new table */
169     BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_ACTIVATE_TABLE);
170     stl_be_p(data, new_l1_size);
171     stq_be_p(data + 4, new_l1_table_offset);
172     ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, l1_size),
173                            data, sizeof(data));
174     if (ret < 0) {
175         goto fail;
176     }
177     qemu_vfree(s->l1_table);
178     old_l1_table_offset = s->l1_table_offset;
179     s->l1_table_offset = new_l1_table_offset;
180     s->l1_table = new_l1_table;
181     old_l1_size = s->l1_size;
182     s->l1_size = new_l1_size;
183     qcow2_free_clusters(bs, old_l1_table_offset, old_l1_size * sizeof(uint64_t),
184                         QCOW2_DISCARD_OTHER);
185     return 0;
186  fail:
187     qemu_vfree(new_l1_table);
188     qcow2_free_clusters(bs, new_l1_table_offset, new_l1_size2,
189                         QCOW2_DISCARD_OTHER);
190     return ret;
191 }
192 
193 /*
194  * l2_load
195  *
196  * @bs: The BlockDriverState
197  * @offset: A guest offset, used to calculate what slice of the L2
198  *          table to load.
199  * @l2_offset: Offset to the L2 table in the image file.
200  * @l2_slice: Location to store the pointer to the L2 slice.
201  *
202  * Loads a L2 slice into memory (L2 slices are the parts of L2 tables
203  * that are loaded by the qcow2 cache). If the slice is in the cache,
204  * the cache is used; otherwise the L2 slice is loaded from the image
205  * file.
206  */
207 static int l2_load(BlockDriverState *bs, uint64_t offset,
208                    uint64_t l2_offset, uint64_t **l2_slice)
209 {
210     BDRVQcow2State *s = bs->opaque;
211     int start_of_slice = sizeof(uint64_t) *
212         (offset_to_l2_index(s, offset) - offset_to_l2_slice_index(s, offset));
213 
214     return qcow2_cache_get(bs, s->l2_table_cache, l2_offset + start_of_slice,
215                            (void **)l2_slice);
216 }
217 
218 /*
219  * Writes an L1 entry to disk (note that depending on the alignment
220  * requirements this function may write more that just one entry in
221  * order to prevent bdrv_pwrite from performing a read-modify-write)
222  */
223 int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index)
224 {
225     BDRVQcow2State *s = bs->opaque;
226     int l1_start_index;
227     int i, ret;
228     int bufsize = MAX(sizeof(uint64_t),
229                       MIN(bs->file->bs->bl.request_alignment, s->cluster_size));
230     int nentries = bufsize / sizeof(uint64_t);
231     g_autofree uint64_t *buf = g_try_new0(uint64_t, nentries);
232 
233     if (buf == NULL) {
234         return -ENOMEM;
235     }
236 
237     l1_start_index = QEMU_ALIGN_DOWN(l1_index, nentries);
238     for (i = 0; i < MIN(nentries, s->l1_size - l1_start_index); i++) {
239         buf[i] = cpu_to_be64(s->l1_table[l1_start_index + i]);
240     }
241 
242     ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L1,
243             s->l1_table_offset + 8 * l1_start_index, bufsize, false);
244     if (ret < 0) {
245         return ret;
246     }
247 
248     BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
249     ret = bdrv_pwrite_sync(bs->file,
250                            s->l1_table_offset + 8 * l1_start_index,
251                            buf, bufsize);
252     if (ret < 0) {
253         return ret;
254     }
255 
256     return 0;
257 }
258 
259 /*
260  * l2_allocate
261  *
262  * Allocate a new l2 entry in the file. If l1_index points to an already
263  * used entry in the L2 table (i.e. we are doing a copy on write for the L2
264  * table) copy the contents of the old L2 table into the newly allocated one.
265  * Otherwise the new table is initialized with zeros.
266  *
267  */
268 
269 static int l2_allocate(BlockDriverState *bs, int l1_index)
270 {
271     BDRVQcow2State *s = bs->opaque;
272     uint64_t old_l2_offset;
273     uint64_t *l2_slice = NULL;
274     unsigned slice, slice_size2, n_slices;
275     int64_t l2_offset;
276     int ret;
277 
278     old_l2_offset = s->l1_table[l1_index];
279 
280     trace_qcow2_l2_allocate(bs, l1_index);
281 
282     /* allocate a new l2 entry */
283 
284     l2_offset = qcow2_alloc_clusters(bs, s->l2_size * sizeof(uint64_t));
285     if (l2_offset < 0) {
286         ret = l2_offset;
287         goto fail;
288     }
289 
290     /* The offset must fit in the offset field of the L1 table entry */
291     assert((l2_offset & L1E_OFFSET_MASK) == l2_offset);
292 
293     /* If we're allocating the table at offset 0 then something is wrong */
294     if (l2_offset == 0) {
295         qcow2_signal_corruption(bs, true, -1, -1, "Preventing invalid "
296                                 "allocation of L2 table at offset 0");
297         ret = -EIO;
298         goto fail;
299     }
300 
301     ret = qcow2_cache_flush(bs, s->refcount_block_cache);
302     if (ret < 0) {
303         goto fail;
304     }
305 
306     /* allocate a new entry in the l2 cache */
307 
308     slice_size2 = s->l2_slice_size * sizeof(uint64_t);
309     n_slices = s->cluster_size / slice_size2;
310 
311     trace_qcow2_l2_allocate_get_empty(bs, l1_index);
312     for (slice = 0; slice < n_slices; slice++) {
313         ret = qcow2_cache_get_empty(bs, s->l2_table_cache,
314                                     l2_offset + slice * slice_size2,
315                                     (void **) &l2_slice);
316         if (ret < 0) {
317             goto fail;
318         }
319 
320         if ((old_l2_offset & L1E_OFFSET_MASK) == 0) {
321             /* if there was no old l2 table, clear the new slice */
322             memset(l2_slice, 0, slice_size2);
323         } else {
324             uint64_t *old_slice;
325             uint64_t old_l2_slice_offset =
326                 (old_l2_offset & L1E_OFFSET_MASK) + slice * slice_size2;
327 
328             /* if there was an old l2 table, read a slice from the disk */
329             BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_COW_READ);
330             ret = qcow2_cache_get(bs, s->l2_table_cache, old_l2_slice_offset,
331                                   (void **) &old_slice);
332             if (ret < 0) {
333                 goto fail;
334             }
335 
336             memcpy(l2_slice, old_slice, slice_size2);
337 
338             qcow2_cache_put(s->l2_table_cache, (void **) &old_slice);
339         }
340 
341         /* write the l2 slice to the file */
342         BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_WRITE);
343 
344         trace_qcow2_l2_allocate_write_l2(bs, l1_index);
345         qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
346         qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
347     }
348 
349     ret = qcow2_cache_flush(bs, s->l2_table_cache);
350     if (ret < 0) {
351         goto fail;
352     }
353 
354     /* update the L1 entry */
355     trace_qcow2_l2_allocate_write_l1(bs, l1_index);
356     s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED;
357     ret = qcow2_write_l1_entry(bs, l1_index);
358     if (ret < 0) {
359         goto fail;
360     }
361 
362     trace_qcow2_l2_allocate_done(bs, l1_index, 0);
363     return 0;
364 
365 fail:
366     trace_qcow2_l2_allocate_done(bs, l1_index, ret);
367     if (l2_slice != NULL) {
368         qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
369     }
370     s->l1_table[l1_index] = old_l2_offset;
371     if (l2_offset > 0) {
372         qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t),
373                             QCOW2_DISCARD_ALWAYS);
374     }
375     return ret;
376 }
377 
378 /*
379  * Checks how many clusters in a given L2 slice are contiguous in the image
380  * file. As soon as one of the flags in the bitmask stop_flags changes compared
381  * to the first cluster, the search is stopped and the cluster is not counted
382  * as contiguous. (This allows it, for example, to stop at the first compressed
383  * cluster which may require a different handling)
384  */
385 static int count_contiguous_clusters(BlockDriverState *bs, int nb_clusters,
386         int cluster_size, uint64_t *l2_slice, uint64_t stop_flags)
387 {
388     int i;
389     QCow2ClusterType first_cluster_type;
390     uint64_t mask = stop_flags | L2E_OFFSET_MASK | QCOW_OFLAG_COMPRESSED;
391     uint64_t first_entry = be64_to_cpu(l2_slice[0]);
392     uint64_t offset = first_entry & mask;
393 
394     first_cluster_type = qcow2_get_cluster_type(bs, first_entry);
395     if (first_cluster_type == QCOW2_CLUSTER_UNALLOCATED) {
396         return 0;
397     }
398 
399     /* must be allocated */
400     assert(first_cluster_type == QCOW2_CLUSTER_NORMAL ||
401            first_cluster_type == QCOW2_CLUSTER_ZERO_ALLOC);
402 
403     for (i = 0; i < nb_clusters; i++) {
404         uint64_t l2_entry = be64_to_cpu(l2_slice[i]) & mask;
405         if (offset + (uint64_t) i * cluster_size != l2_entry) {
406             break;
407         }
408     }
409 
410         return i;
411 }
412 
413 /*
414  * Checks how many consecutive unallocated clusters in a given L2
415  * slice have the same cluster type.
416  */
417 static int count_contiguous_clusters_unallocated(BlockDriverState *bs,
418                                                  int nb_clusters,
419                                                  uint64_t *l2_slice,
420                                                  QCow2ClusterType wanted_type)
421 {
422     int i;
423 
424     assert(wanted_type == QCOW2_CLUSTER_ZERO_PLAIN ||
425            wanted_type == QCOW2_CLUSTER_UNALLOCATED);
426     for (i = 0; i < nb_clusters; i++) {
427         uint64_t entry = be64_to_cpu(l2_slice[i]);
428         QCow2ClusterType type = qcow2_get_cluster_type(bs, entry);
429 
430         if (type != wanted_type) {
431             break;
432         }
433     }
434 
435     return i;
436 }
437 
438 static int coroutine_fn do_perform_cow_read(BlockDriverState *bs,
439                                             uint64_t src_cluster_offset,
440                                             unsigned offset_in_cluster,
441                                             QEMUIOVector *qiov)
442 {
443     int ret;
444 
445     if (qiov->size == 0) {
446         return 0;
447     }
448 
449     BLKDBG_EVENT(bs->file, BLKDBG_COW_READ);
450 
451     if (!bs->drv) {
452         return -ENOMEDIUM;
453     }
454 
455     /* Call .bdrv_co_readv() directly instead of using the public block-layer
456      * interface.  This avoids double I/O throttling and request tracking,
457      * which can lead to deadlock when block layer copy-on-read is enabled.
458      */
459     ret = bs->drv->bdrv_co_preadv_part(bs,
460                                        src_cluster_offset + offset_in_cluster,
461                                        qiov->size, qiov, 0, 0);
462     if (ret < 0) {
463         return ret;
464     }
465 
466     return 0;
467 }
468 
469 static int coroutine_fn do_perform_cow_write(BlockDriverState *bs,
470                                              uint64_t cluster_offset,
471                                              unsigned offset_in_cluster,
472                                              QEMUIOVector *qiov)
473 {
474     BDRVQcow2State *s = bs->opaque;
475     int ret;
476 
477     if (qiov->size == 0) {
478         return 0;
479     }
480 
481     ret = qcow2_pre_write_overlap_check(bs, 0,
482             cluster_offset + offset_in_cluster, qiov->size, true);
483     if (ret < 0) {
484         return ret;
485     }
486 
487     BLKDBG_EVENT(bs->file, BLKDBG_COW_WRITE);
488     ret = bdrv_co_pwritev(s->data_file, cluster_offset + offset_in_cluster,
489                           qiov->size, qiov, 0);
490     if (ret < 0) {
491         return ret;
492     }
493 
494     return 0;
495 }
496 
497 
498 /*
499  * get_cluster_offset
500  *
501  * For a given offset of the virtual disk, find the cluster type and offset in
502  * the qcow2 file. The offset is stored in *cluster_offset.
503  *
504  * On entry, *bytes is the maximum number of contiguous bytes starting at
505  * offset that we are interested in.
506  *
507  * On exit, *bytes is the number of bytes starting at offset that have the same
508  * cluster type and (if applicable) are stored contiguously in the image file.
509  * Compressed clusters are always returned one by one.
510  *
511  * Returns the cluster type (QCOW2_CLUSTER_*) on success, -errno in error
512  * cases.
513  */
514 int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
515                              unsigned int *bytes, uint64_t *cluster_offset)
516 {
517     BDRVQcow2State *s = bs->opaque;
518     unsigned int l2_index;
519     uint64_t l1_index, l2_offset, *l2_slice;
520     int c;
521     unsigned int offset_in_cluster;
522     uint64_t bytes_available, bytes_needed, nb_clusters;
523     QCow2ClusterType type;
524     int ret;
525 
526     offset_in_cluster = offset_into_cluster(s, offset);
527     bytes_needed = (uint64_t) *bytes + offset_in_cluster;
528 
529     /* compute how many bytes there are between the start of the cluster
530      * containing offset and the end of the l2 slice that contains
531      * the entry pointing to it */
532     bytes_available =
533         ((uint64_t) (s->l2_slice_size - offset_to_l2_slice_index(s, offset)))
534         << s->cluster_bits;
535 
536     if (bytes_needed > bytes_available) {
537         bytes_needed = bytes_available;
538     }
539 
540     *cluster_offset = 0;
541 
542     /* seek to the l2 offset in the l1 table */
543 
544     l1_index = offset_to_l1_index(s, offset);
545     if (l1_index >= s->l1_size) {
546         type = QCOW2_CLUSTER_UNALLOCATED;
547         goto out;
548     }
549 
550     l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
551     if (!l2_offset) {
552         type = QCOW2_CLUSTER_UNALLOCATED;
553         goto out;
554     }
555 
556     if (offset_into_cluster(s, l2_offset)) {
557         qcow2_signal_corruption(bs, true, -1, -1, "L2 table offset %#" PRIx64
558                                 " unaligned (L1 index: %#" PRIx64 ")",
559                                 l2_offset, l1_index);
560         return -EIO;
561     }
562 
563     /* load the l2 slice in memory */
564 
565     ret = l2_load(bs, offset, l2_offset, &l2_slice);
566     if (ret < 0) {
567         return ret;
568     }
569 
570     /* find the cluster offset for the given disk offset */
571 
572     l2_index = offset_to_l2_slice_index(s, offset);
573     *cluster_offset = be64_to_cpu(l2_slice[l2_index]);
574 
575     nb_clusters = size_to_clusters(s, bytes_needed);
576     /* bytes_needed <= *bytes + offset_in_cluster, both of which are unsigned
577      * integers; the minimum cluster size is 512, so this assertion is always
578      * true */
579     assert(nb_clusters <= INT_MAX);
580 
581     type = qcow2_get_cluster_type(bs, *cluster_offset);
582     if (s->qcow_version < 3 && (type == QCOW2_CLUSTER_ZERO_PLAIN ||
583                                 type == QCOW2_CLUSTER_ZERO_ALLOC)) {
584         qcow2_signal_corruption(bs, true, -1, -1, "Zero cluster entry found"
585                                 " in pre-v3 image (L2 offset: %#" PRIx64
586                                 ", L2 index: %#x)", l2_offset, l2_index);
587         ret = -EIO;
588         goto fail;
589     }
590     switch (type) {
591     case QCOW2_CLUSTER_COMPRESSED:
592         if (has_data_file(bs)) {
593             qcow2_signal_corruption(bs, true, -1, -1, "Compressed cluster "
594                                     "entry found in image with external data "
595                                     "file (L2 offset: %#" PRIx64 ", L2 index: "
596                                     "%#x)", l2_offset, l2_index);
597             ret = -EIO;
598             goto fail;
599         }
600         /* Compressed clusters can only be processed one by one */
601         c = 1;
602         *cluster_offset &= L2E_COMPRESSED_OFFSET_SIZE_MASK;
603         break;
604     case QCOW2_CLUSTER_ZERO_PLAIN:
605     case QCOW2_CLUSTER_UNALLOCATED:
606         /* how many empty clusters ? */
607         c = count_contiguous_clusters_unallocated(bs, nb_clusters,
608                                                   &l2_slice[l2_index], type);
609         *cluster_offset = 0;
610         break;
611     case QCOW2_CLUSTER_ZERO_ALLOC:
612     case QCOW2_CLUSTER_NORMAL:
613         /* how many allocated clusters ? */
614         c = count_contiguous_clusters(bs, nb_clusters, s->cluster_size,
615                                       &l2_slice[l2_index], QCOW_OFLAG_ZERO);
616         *cluster_offset &= L2E_OFFSET_MASK;
617         if (offset_into_cluster(s, *cluster_offset)) {
618             qcow2_signal_corruption(bs, true, -1, -1,
619                                     "Cluster allocation offset %#"
620                                     PRIx64 " unaligned (L2 offset: %#" PRIx64
621                                     ", L2 index: %#x)", *cluster_offset,
622                                     l2_offset, l2_index);
623             ret = -EIO;
624             goto fail;
625         }
626         if (has_data_file(bs) && *cluster_offset != offset - offset_in_cluster)
627         {
628             qcow2_signal_corruption(bs, true, -1, -1,
629                                     "External data file host cluster offset %#"
630                                     PRIx64 " does not match guest cluster "
631                                     "offset: %#" PRIx64
632                                     ", L2 index: %#x)", *cluster_offset,
633                                     offset - offset_in_cluster, l2_index);
634             ret = -EIO;
635             goto fail;
636         }
637         break;
638     default:
639         abort();
640     }
641 
642     qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
643 
644     bytes_available = (int64_t)c * s->cluster_size;
645 
646 out:
647     if (bytes_available > bytes_needed) {
648         bytes_available = bytes_needed;
649     }
650 
651     /* bytes_available <= bytes_needed <= *bytes + offset_in_cluster;
652      * subtracting offset_in_cluster will therefore definitely yield something
653      * not exceeding UINT_MAX */
654     assert(bytes_available - offset_in_cluster <= UINT_MAX);
655     *bytes = bytes_available - offset_in_cluster;
656 
657     return type;
658 
659 fail:
660     qcow2_cache_put(s->l2_table_cache, (void **)&l2_slice);
661     return ret;
662 }
663 
664 /*
665  * get_cluster_table
666  *
667  * for a given disk offset, load (and allocate if needed)
668  * the appropriate slice of its l2 table.
669  *
670  * the cluster index in the l2 slice is given to the caller.
671  *
672  * Returns 0 on success, -errno in failure case
673  */
674 static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
675                              uint64_t **new_l2_slice,
676                              int *new_l2_index)
677 {
678     BDRVQcow2State *s = bs->opaque;
679     unsigned int l2_index;
680     uint64_t l1_index, l2_offset;
681     uint64_t *l2_slice = NULL;
682     int ret;
683 
684     /* seek to the l2 offset in the l1 table */
685 
686     l1_index = offset_to_l1_index(s, offset);
687     if (l1_index >= s->l1_size) {
688         ret = qcow2_grow_l1_table(bs, l1_index + 1, false);
689         if (ret < 0) {
690             return ret;
691         }
692     }
693 
694     assert(l1_index < s->l1_size);
695     l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
696     if (offset_into_cluster(s, l2_offset)) {
697         qcow2_signal_corruption(bs, true, -1, -1, "L2 table offset %#" PRIx64
698                                 " unaligned (L1 index: %#" PRIx64 ")",
699                                 l2_offset, l1_index);
700         return -EIO;
701     }
702 
703     if (!(s->l1_table[l1_index] & QCOW_OFLAG_COPIED)) {
704         /* First allocate a new L2 table (and do COW if needed) */
705         ret = l2_allocate(bs, l1_index);
706         if (ret < 0) {
707             return ret;
708         }
709 
710         /* Then decrease the refcount of the old table */
711         if (l2_offset) {
712             qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t),
713                                 QCOW2_DISCARD_OTHER);
714         }
715 
716         /* Get the offset of the newly-allocated l2 table */
717         l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
718         assert(offset_into_cluster(s, l2_offset) == 0);
719     }
720 
721     /* load the l2 slice in memory */
722     ret = l2_load(bs, offset, l2_offset, &l2_slice);
723     if (ret < 0) {
724         return ret;
725     }
726 
727     /* find the cluster offset for the given disk offset */
728 
729     l2_index = offset_to_l2_slice_index(s, offset);
730 
731     *new_l2_slice = l2_slice;
732     *new_l2_index = l2_index;
733 
734     return 0;
735 }
736 
737 /*
738  * alloc_compressed_cluster_offset
739  *
740  * For a given offset on the virtual disk, allocate a new compressed cluster
741  * and put the host offset of the cluster into *host_offset. If a cluster is
742  * already allocated at the offset, return an error.
743  *
744  * Return 0 on success and -errno in error cases
745  */
746 int qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
747                                           uint64_t offset,
748                                           int compressed_size,
749                                           uint64_t *host_offset)
750 {
751     BDRVQcow2State *s = bs->opaque;
752     int l2_index, ret;
753     uint64_t *l2_slice;
754     int64_t cluster_offset;
755     int nb_csectors;
756 
757     if (has_data_file(bs)) {
758         return 0;
759     }
760 
761     ret = get_cluster_table(bs, offset, &l2_slice, &l2_index);
762     if (ret < 0) {
763         return ret;
764     }
765 
766     /* Compression can't overwrite anything. Fail if the cluster was already
767      * allocated. */
768     cluster_offset = be64_to_cpu(l2_slice[l2_index]);
769     if (cluster_offset & L2E_OFFSET_MASK) {
770         qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
771         return -EIO;
772     }
773 
774     cluster_offset = qcow2_alloc_bytes(bs, compressed_size);
775     if (cluster_offset < 0) {
776         qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
777         return cluster_offset;
778     }
779 
780     nb_csectors =
781         (cluster_offset + compressed_size - 1) / QCOW2_COMPRESSED_SECTOR_SIZE -
782         (cluster_offset / QCOW2_COMPRESSED_SECTOR_SIZE);
783 
784     /* The offset and size must fit in their fields of the L2 table entry */
785     assert((cluster_offset & s->cluster_offset_mask) == cluster_offset);
786     assert((nb_csectors & s->csize_mask) == nb_csectors);
787 
788     cluster_offset |= QCOW_OFLAG_COMPRESSED |
789                       ((uint64_t)nb_csectors << s->csize_shift);
790 
791     /* update L2 table */
792 
793     /* compressed clusters never have the copied flag */
794 
795     BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE_COMPRESSED);
796     qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
797     l2_slice[l2_index] = cpu_to_be64(cluster_offset);
798     qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
799 
800     *host_offset = cluster_offset & s->cluster_offset_mask;
801     return 0;
802 }
803 
804 static int perform_cow(BlockDriverState *bs, QCowL2Meta *m)
805 {
806     BDRVQcow2State *s = bs->opaque;
807     Qcow2COWRegion *start = &m->cow_start;
808     Qcow2COWRegion *end = &m->cow_end;
809     unsigned buffer_size;
810     unsigned data_bytes = end->offset - (start->offset + start->nb_bytes);
811     bool merge_reads;
812     uint8_t *start_buffer, *end_buffer;
813     QEMUIOVector qiov;
814     int ret;
815 
816     assert(start->nb_bytes <= UINT_MAX - end->nb_bytes);
817     assert(start->nb_bytes + end->nb_bytes <= UINT_MAX - data_bytes);
818     assert(start->offset + start->nb_bytes <= end->offset);
819 
820     if ((start->nb_bytes == 0 && end->nb_bytes == 0) || m->skip_cow) {
821         return 0;
822     }
823 
824     /* If we have to read both the start and end COW regions and the
825      * middle region is not too large then perform just one read
826      * operation */
827     merge_reads = start->nb_bytes && end->nb_bytes && data_bytes <= 16384;
828     if (merge_reads) {
829         buffer_size = start->nb_bytes + data_bytes + end->nb_bytes;
830     } else {
831         /* If we have to do two reads, add some padding in the middle
832          * if necessary to make sure that the end region is optimally
833          * aligned. */
834         size_t align = bdrv_opt_mem_align(bs);
835         assert(align > 0 && align <= UINT_MAX);
836         assert(QEMU_ALIGN_UP(start->nb_bytes, align) <=
837                UINT_MAX - end->nb_bytes);
838         buffer_size = QEMU_ALIGN_UP(start->nb_bytes, align) + end->nb_bytes;
839     }
840 
841     /* Reserve a buffer large enough to store all the data that we're
842      * going to read */
843     start_buffer = qemu_try_blockalign(bs, buffer_size);
844     if (start_buffer == NULL) {
845         return -ENOMEM;
846     }
847     /* The part of the buffer where the end region is located */
848     end_buffer = start_buffer + buffer_size - end->nb_bytes;
849 
850     qemu_iovec_init(&qiov, 2 + (m->data_qiov ?
851                                 qemu_iovec_subvec_niov(m->data_qiov,
852                                                        m->data_qiov_offset,
853                                                        data_bytes)
854                                 : 0));
855 
856     qemu_co_mutex_unlock(&s->lock);
857     /* First we read the existing data from both COW regions. We
858      * either read the whole region in one go, or the start and end
859      * regions separately. */
860     if (merge_reads) {
861         qemu_iovec_add(&qiov, start_buffer, buffer_size);
862         ret = do_perform_cow_read(bs, m->offset, start->offset, &qiov);
863     } else {
864         qemu_iovec_add(&qiov, start_buffer, start->nb_bytes);
865         ret = do_perform_cow_read(bs, m->offset, start->offset, &qiov);
866         if (ret < 0) {
867             goto fail;
868         }
869 
870         qemu_iovec_reset(&qiov);
871         qemu_iovec_add(&qiov, end_buffer, end->nb_bytes);
872         ret = do_perform_cow_read(bs, m->offset, end->offset, &qiov);
873     }
874     if (ret < 0) {
875         goto fail;
876     }
877 
878     /* Encrypt the data if necessary before writing it */
879     if (bs->encrypted) {
880         ret = qcow2_co_encrypt(bs,
881                                m->alloc_offset + start->offset,
882                                m->offset + start->offset,
883                                start_buffer, start->nb_bytes);
884         if (ret < 0) {
885             goto fail;
886         }
887 
888         ret = qcow2_co_encrypt(bs,
889                                m->alloc_offset + end->offset,
890                                m->offset + end->offset,
891                                end_buffer, end->nb_bytes);
892         if (ret < 0) {
893             goto fail;
894         }
895     }
896 
897     /* And now we can write everything. If we have the guest data we
898      * can write everything in one single operation */
899     if (m->data_qiov) {
900         qemu_iovec_reset(&qiov);
901         if (start->nb_bytes) {
902             qemu_iovec_add(&qiov, start_buffer, start->nb_bytes);
903         }
904         qemu_iovec_concat(&qiov, m->data_qiov, m->data_qiov_offset, data_bytes);
905         if (end->nb_bytes) {
906             qemu_iovec_add(&qiov, end_buffer, end->nb_bytes);
907         }
908         /* NOTE: we have a write_aio blkdebug event here followed by
909          * a cow_write one in do_perform_cow_write(), but there's only
910          * one single I/O operation */
911         BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
912         ret = do_perform_cow_write(bs, m->alloc_offset, start->offset, &qiov);
913     } else {
914         /* If there's no guest data then write both COW regions separately */
915         qemu_iovec_reset(&qiov);
916         qemu_iovec_add(&qiov, start_buffer, start->nb_bytes);
917         ret = do_perform_cow_write(bs, m->alloc_offset, start->offset, &qiov);
918         if (ret < 0) {
919             goto fail;
920         }
921 
922         qemu_iovec_reset(&qiov);
923         qemu_iovec_add(&qiov, end_buffer, end->nb_bytes);
924         ret = do_perform_cow_write(bs, m->alloc_offset, end->offset, &qiov);
925     }
926 
927 fail:
928     qemu_co_mutex_lock(&s->lock);
929 
930     /*
931      * Before we update the L2 table to actually point to the new cluster, we
932      * need to be sure that the refcounts have been increased and COW was
933      * handled.
934      */
935     if (ret == 0) {
936         qcow2_cache_depends_on_flush(s->l2_table_cache);
937     }
938 
939     qemu_vfree(start_buffer);
940     qemu_iovec_destroy(&qiov);
941     return ret;
942 }
943 
944 int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)
945 {
946     BDRVQcow2State *s = bs->opaque;
947     int i, j = 0, l2_index, ret;
948     uint64_t *old_cluster, *l2_slice;
949     uint64_t cluster_offset = m->alloc_offset;
950 
951     trace_qcow2_cluster_link_l2(qemu_coroutine_self(), m->nb_clusters);
952     assert(m->nb_clusters > 0);
953 
954     old_cluster = g_try_new(uint64_t, m->nb_clusters);
955     if (old_cluster == NULL) {
956         ret = -ENOMEM;
957         goto err;
958     }
959 
960     /* copy content of unmodified sectors */
961     ret = perform_cow(bs, m);
962     if (ret < 0) {
963         goto err;
964     }
965 
966     /* Update L2 table. */
967     if (s->use_lazy_refcounts) {
968         qcow2_mark_dirty(bs);
969     }
970     if (qcow2_need_accurate_refcounts(s)) {
971         qcow2_cache_set_dependency(bs, s->l2_table_cache,
972                                    s->refcount_block_cache);
973     }
974 
975     ret = get_cluster_table(bs, m->offset, &l2_slice, &l2_index);
976     if (ret < 0) {
977         goto err;
978     }
979     qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
980 
981     assert(l2_index + m->nb_clusters <= s->l2_slice_size);
982     for (i = 0; i < m->nb_clusters; i++) {
983         uint64_t offset = cluster_offset + (i << s->cluster_bits);
984         /* if two concurrent writes happen to the same unallocated cluster
985          * each write allocates separate cluster and writes data concurrently.
986          * The first one to complete updates l2 table with pointer to its
987          * cluster the second one has to do RMW (which is done above by
988          * perform_cow()), update l2 table with its cluster pointer and free
989          * old cluster. This is what this loop does */
990         if (l2_slice[l2_index + i] != 0) {
991             old_cluster[j++] = l2_slice[l2_index + i];
992         }
993 
994         /* The offset must fit in the offset field of the L2 table entry */
995         assert((offset & L2E_OFFSET_MASK) == offset);
996 
997         l2_slice[l2_index + i] = cpu_to_be64(offset | 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     /* Limit total allocation byte count to INT_MAX */
1345     nb_clusters = MIN(nb_clusters, INT_MAX >> s->cluster_bits);
1346 
1347     /* Find L2 entry for the first involved cluster */
1348     ret = get_cluster_table(bs, guest_offset, &l2_slice, &l2_index);
1349     if (ret < 0) {
1350         return ret;
1351     }
1352 
1353     entry = be64_to_cpu(l2_slice[l2_index]);
1354     nb_clusters = count_cow_clusters(bs, nb_clusters, l2_slice, l2_index);
1355 
1356     /* This function is only called when there were no non-COW clusters, so if
1357      * we can't find any unallocated or COW clusters either, something is
1358      * wrong with our code. */
1359     assert(nb_clusters > 0);
1360 
1361     if (qcow2_get_cluster_type(bs, entry) == QCOW2_CLUSTER_ZERO_ALLOC &&
1362         (entry & QCOW_OFLAG_COPIED) &&
1363         (*host_offset == INV_OFFSET ||
1364          start_of_cluster(s, *host_offset) == (entry & L2E_OFFSET_MASK)))
1365     {
1366         int preallocated_nb_clusters;
1367 
1368         if (offset_into_cluster(s, entry & L2E_OFFSET_MASK)) {
1369             qcow2_signal_corruption(bs, true, -1, -1, "Preallocated zero "
1370                                     "cluster offset %#llx unaligned (guest "
1371                                     "offset: %#" PRIx64 ")",
1372                                     entry & L2E_OFFSET_MASK, guest_offset);
1373             ret = -EIO;
1374             goto fail;
1375         }
1376 
1377         /* Try to reuse preallocated zero clusters; contiguous normal clusters
1378          * would be fine, too, but count_cow_clusters() above has limited
1379          * nb_clusters already to a range of COW clusters */
1380         preallocated_nb_clusters =
1381             count_contiguous_clusters(bs, nb_clusters, s->cluster_size,
1382                                       &l2_slice[l2_index], QCOW_OFLAG_COPIED);
1383         assert(preallocated_nb_clusters > 0);
1384 
1385         nb_clusters = preallocated_nb_clusters;
1386         alloc_cluster_offset = entry & L2E_OFFSET_MASK;
1387 
1388         /* We want to reuse these clusters, so qcow2_alloc_cluster_link_l2()
1389          * should not free them. */
1390         keep_old_clusters = true;
1391     }
1392 
1393     qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
1394 
1395     if (alloc_cluster_offset == INV_OFFSET) {
1396         /* Allocate, if necessary at a given offset in the image file */
1397         alloc_cluster_offset = *host_offset == INV_OFFSET ? INV_OFFSET :
1398                                start_of_cluster(s, *host_offset);
1399         ret = do_alloc_cluster_offset(bs, guest_offset, &alloc_cluster_offset,
1400                                       &nb_clusters);
1401         if (ret < 0) {
1402             goto fail;
1403         }
1404 
1405         /* Can't extend contiguous allocation */
1406         if (nb_clusters == 0) {
1407             *bytes = 0;
1408             return 0;
1409         }
1410 
1411         assert(alloc_cluster_offset != INV_OFFSET);
1412     }
1413 
1414     /*
1415      * Save info needed for meta data update.
1416      *
1417      * requested_bytes: Number of bytes from the start of the first
1418      * newly allocated cluster to the end of the (possibly shortened
1419      * before) write request.
1420      *
1421      * avail_bytes: Number of bytes from the start of the first
1422      * newly allocated to the end of the last newly allocated cluster.
1423      *
1424      * nb_bytes: The number of bytes from the start of the first
1425      * newly allocated cluster to the end of the area that the write
1426      * request actually writes to (excluding COW at the end)
1427      */
1428     uint64_t requested_bytes = *bytes + offset_into_cluster(s, guest_offset);
1429     int avail_bytes = nb_clusters << s->cluster_bits;
1430     int nb_bytes = MIN(requested_bytes, avail_bytes);
1431     QCowL2Meta *old_m = *m;
1432 
1433     *m = g_malloc0(sizeof(**m));
1434 
1435     **m = (QCowL2Meta) {
1436         .next           = old_m,
1437 
1438         .alloc_offset   = alloc_cluster_offset,
1439         .offset         = start_of_cluster(s, guest_offset),
1440         .nb_clusters    = nb_clusters,
1441 
1442         .keep_old_clusters  = keep_old_clusters,
1443 
1444         .cow_start = {
1445             .offset     = 0,
1446             .nb_bytes   = offset_into_cluster(s, guest_offset),
1447         },
1448         .cow_end = {
1449             .offset     = nb_bytes,
1450             .nb_bytes   = avail_bytes - nb_bytes,
1451         },
1452     };
1453     qemu_co_queue_init(&(*m)->dependent_requests);
1454     QLIST_INSERT_HEAD(&s->cluster_allocs, *m, next_in_flight);
1455 
1456     *host_offset = alloc_cluster_offset + offset_into_cluster(s, guest_offset);
1457     *bytes = MIN(*bytes, nb_bytes - offset_into_cluster(s, guest_offset));
1458     assert(*bytes != 0);
1459 
1460     return 1;
1461 
1462 fail:
1463     if (*m && (*m)->nb_clusters > 0) {
1464         QLIST_REMOVE(*m, next_in_flight);
1465     }
1466     return ret;
1467 }
1468 
1469 /*
1470  * alloc_cluster_offset
1471  *
1472  * For a given offset on the virtual disk, find the cluster offset in qcow2
1473  * file. If the offset is not found, allocate a new cluster.
1474  *
1475  * If the cluster was already allocated, m->nb_clusters is set to 0 and
1476  * other fields in m are meaningless.
1477  *
1478  * If the cluster is newly allocated, m->nb_clusters is set to the number of
1479  * contiguous clusters that have been allocated. In this case, the other
1480  * fields of m are valid and contain information about the first allocated
1481  * cluster.
1482  *
1483  * If the request conflicts with another write request in flight, the coroutine
1484  * is queued and will be reentered when the dependency has completed.
1485  *
1486  * Return 0 on success and -errno in error cases
1487  */
1488 int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
1489                                unsigned int *bytes, uint64_t *host_offset,
1490                                QCowL2Meta **m)
1491 {
1492     BDRVQcow2State *s = bs->opaque;
1493     uint64_t start, remaining;
1494     uint64_t cluster_offset;
1495     uint64_t cur_bytes;
1496     int ret;
1497 
1498     trace_qcow2_alloc_clusters_offset(qemu_coroutine_self(), offset, *bytes);
1499 
1500 again:
1501     start = offset;
1502     remaining = *bytes;
1503     cluster_offset = INV_OFFSET;
1504     *host_offset = INV_OFFSET;
1505     cur_bytes = 0;
1506     *m = NULL;
1507 
1508     while (true) {
1509 
1510         if (*host_offset == INV_OFFSET && cluster_offset != INV_OFFSET) {
1511             *host_offset = start_of_cluster(s, cluster_offset);
1512         }
1513 
1514         assert(remaining >= cur_bytes);
1515 
1516         start           += cur_bytes;
1517         remaining       -= cur_bytes;
1518 
1519         if (cluster_offset != INV_OFFSET) {
1520             cluster_offset += cur_bytes;
1521         }
1522 
1523         if (remaining == 0) {
1524             break;
1525         }
1526 
1527         cur_bytes = remaining;
1528 
1529         /*
1530          * Now start gathering as many contiguous clusters as possible:
1531          *
1532          * 1. Check for overlaps with in-flight allocations
1533          *
1534          *      a) Overlap not in the first cluster -> shorten this request and
1535          *         let the caller handle the rest in its next loop iteration.
1536          *
1537          *      b) Real overlaps of two requests. Yield and restart the search
1538          *         for contiguous clusters (the situation could have changed
1539          *         while we were sleeping)
1540          *
1541          *      c) TODO: Request starts in the same cluster as the in-flight
1542          *         allocation ends. Shorten the COW of the in-fight allocation,
1543          *         set cluster_offset to write to the same cluster and set up
1544          *         the right synchronisation between the in-flight request and
1545          *         the new one.
1546          */
1547         ret = handle_dependencies(bs, start, &cur_bytes, m);
1548         if (ret == -EAGAIN) {
1549             /* Currently handle_dependencies() doesn't yield if we already had
1550              * an allocation. If it did, we would have to clean up the L2Meta
1551              * structs before starting over. */
1552             assert(*m == NULL);
1553             goto again;
1554         } else if (ret < 0) {
1555             return ret;
1556         } else if (cur_bytes == 0) {
1557             break;
1558         } else {
1559             /* handle_dependencies() may have decreased cur_bytes (shortened
1560              * the allocations below) so that the next dependency is processed
1561              * correctly during the next loop iteration. */
1562         }
1563 
1564         /*
1565          * 2. Count contiguous COPIED clusters.
1566          */
1567         ret = handle_copied(bs, start, &cluster_offset, &cur_bytes, m);
1568         if (ret < 0) {
1569             return ret;
1570         } else if (ret) {
1571             continue;
1572         } else if (cur_bytes == 0) {
1573             break;
1574         }
1575 
1576         /*
1577          * 3. If the request still hasn't completed, allocate new clusters,
1578          *    considering any cluster_offset of steps 1c or 2.
1579          */
1580         ret = handle_alloc(bs, start, &cluster_offset, &cur_bytes, m);
1581         if (ret < 0) {
1582             return ret;
1583         } else if (ret) {
1584             continue;
1585         } else {
1586             assert(cur_bytes == 0);
1587             break;
1588         }
1589     }
1590 
1591     *bytes -= remaining;
1592     assert(*bytes > 0);
1593     assert(*host_offset != INV_OFFSET);
1594 
1595     return 0;
1596 }
1597 
1598 /*
1599  * This discards as many clusters of nb_clusters as possible at once (i.e.
1600  * all clusters in the same L2 slice) and returns the number of discarded
1601  * clusters.
1602  */
1603 static int discard_in_l2_slice(BlockDriverState *bs, uint64_t offset,
1604                                uint64_t nb_clusters,
1605                                enum qcow2_discard_type type, bool full_discard)
1606 {
1607     BDRVQcow2State *s = bs->opaque;
1608     uint64_t *l2_slice;
1609     int l2_index;
1610     int ret;
1611     int i;
1612 
1613     ret = get_cluster_table(bs, offset, &l2_slice, &l2_index);
1614     if (ret < 0) {
1615         return ret;
1616     }
1617 
1618     /* Limit nb_clusters to one L2 slice */
1619     nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);
1620     assert(nb_clusters <= INT_MAX);
1621 
1622     for (i = 0; i < nb_clusters; i++) {
1623         uint64_t old_l2_entry;
1624 
1625         old_l2_entry = be64_to_cpu(l2_slice[l2_index + i]);
1626 
1627         /*
1628          * If full_discard is false, make sure that a discarded area reads back
1629          * as zeroes for v3 images (we cannot do it for v2 without actually
1630          * writing a zero-filled buffer). We can skip the operation if the
1631          * cluster is already marked as zero, or if it's unallocated and we
1632          * don't have a backing file.
1633          *
1634          * TODO We might want to use bdrv_block_status(bs) here, but we're
1635          * holding s->lock, so that doesn't work today.
1636          *
1637          * If full_discard is true, the sector should not read back as zeroes,
1638          * but rather fall through to the backing file.
1639          */
1640         switch (qcow2_get_cluster_type(bs, old_l2_entry)) {
1641         case QCOW2_CLUSTER_UNALLOCATED:
1642             if (full_discard || !bs->backing) {
1643                 continue;
1644             }
1645             break;
1646 
1647         case QCOW2_CLUSTER_ZERO_PLAIN:
1648             if (!full_discard) {
1649                 continue;
1650             }
1651             break;
1652 
1653         case QCOW2_CLUSTER_ZERO_ALLOC:
1654         case QCOW2_CLUSTER_NORMAL:
1655         case QCOW2_CLUSTER_COMPRESSED:
1656             break;
1657 
1658         default:
1659             abort();
1660         }
1661 
1662         /* First remove L2 entries */
1663         qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
1664         if (!full_discard && s->qcow_version >= 3) {
1665             l2_slice[l2_index + i] = cpu_to_be64(QCOW_OFLAG_ZERO);
1666         } else {
1667             l2_slice[l2_index + i] = cpu_to_be64(0);
1668         }
1669 
1670         /* Then decrease the refcount */
1671         qcow2_free_any_clusters(bs, old_l2_entry, 1, type);
1672     }
1673 
1674     qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
1675 
1676     return nb_clusters;
1677 }
1678 
1679 int qcow2_cluster_discard(BlockDriverState *bs, uint64_t offset,
1680                           uint64_t bytes, enum qcow2_discard_type type,
1681                           bool full_discard)
1682 {
1683     BDRVQcow2State *s = bs->opaque;
1684     uint64_t end_offset = offset + bytes;
1685     uint64_t nb_clusters;
1686     int64_t cleared;
1687     int ret;
1688 
1689     /* Caller must pass aligned values, except at image end */
1690     assert(QEMU_IS_ALIGNED(offset, s->cluster_size));
1691     assert(QEMU_IS_ALIGNED(end_offset, s->cluster_size) ||
1692            end_offset == bs->total_sectors << BDRV_SECTOR_BITS);
1693 
1694     nb_clusters = size_to_clusters(s, bytes);
1695 
1696     s->cache_discards = true;
1697 
1698     /* Each L2 slice is handled by its own loop iteration */
1699     while (nb_clusters > 0) {
1700         cleared = discard_in_l2_slice(bs, offset, nb_clusters, type,
1701                                       full_discard);
1702         if (cleared < 0) {
1703             ret = cleared;
1704             goto fail;
1705         }
1706 
1707         nb_clusters -= cleared;
1708         offset += (cleared * s->cluster_size);
1709     }
1710 
1711     ret = 0;
1712 fail:
1713     s->cache_discards = false;
1714     qcow2_process_discards(bs, ret);
1715 
1716     return ret;
1717 }
1718 
1719 /*
1720  * This zeroes as many clusters of nb_clusters as possible at once (i.e.
1721  * all clusters in the same L2 slice) and returns the number of zeroed
1722  * clusters.
1723  */
1724 static int zero_in_l2_slice(BlockDriverState *bs, uint64_t offset,
1725                             uint64_t nb_clusters, int flags)
1726 {
1727     BDRVQcow2State *s = bs->opaque;
1728     uint64_t *l2_slice;
1729     int l2_index;
1730     int ret;
1731     int i;
1732     bool unmap = !!(flags & BDRV_REQ_MAY_UNMAP);
1733 
1734     ret = get_cluster_table(bs, offset, &l2_slice, &l2_index);
1735     if (ret < 0) {
1736         return ret;
1737     }
1738 
1739     /* Limit nb_clusters to one L2 slice */
1740     nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);
1741     assert(nb_clusters <= INT_MAX);
1742 
1743     for (i = 0; i < nb_clusters; i++) {
1744         uint64_t old_offset;
1745         QCow2ClusterType cluster_type;
1746 
1747         old_offset = be64_to_cpu(l2_slice[l2_index + i]);
1748 
1749         /*
1750          * Minimize L2 changes if the cluster already reads back as
1751          * zeroes with correct allocation.
1752          */
1753         cluster_type = qcow2_get_cluster_type(bs, old_offset);
1754         if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN ||
1755             (cluster_type == QCOW2_CLUSTER_ZERO_ALLOC && !unmap)) {
1756             continue;
1757         }
1758 
1759         qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
1760         if (cluster_type == QCOW2_CLUSTER_COMPRESSED || unmap) {
1761             l2_slice[l2_index + i] = cpu_to_be64(QCOW_OFLAG_ZERO);
1762             qcow2_free_any_clusters(bs, old_offset, 1, QCOW2_DISCARD_REQUEST);
1763         } else {
1764             l2_slice[l2_index + i] |= cpu_to_be64(QCOW_OFLAG_ZERO);
1765         }
1766     }
1767 
1768     qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
1769 
1770     return nb_clusters;
1771 }
1772 
1773 int qcow2_cluster_zeroize(BlockDriverState *bs, uint64_t offset,
1774                           uint64_t bytes, int flags)
1775 {
1776     BDRVQcow2State *s = bs->opaque;
1777     uint64_t end_offset = offset + bytes;
1778     uint64_t nb_clusters;
1779     int64_t cleared;
1780     int ret;
1781 
1782     /* If we have to stay in sync with an external data file, zero out
1783      * s->data_file first. */
1784     if (data_file_is_raw(bs)) {
1785         assert(has_data_file(bs));
1786         ret = bdrv_co_pwrite_zeroes(s->data_file, offset, bytes, flags);
1787         if (ret < 0) {
1788             return ret;
1789         }
1790     }
1791 
1792     /* Caller must pass aligned values, except at image end */
1793     assert(QEMU_IS_ALIGNED(offset, s->cluster_size));
1794     assert(QEMU_IS_ALIGNED(end_offset, s->cluster_size) ||
1795            end_offset == bs->total_sectors << BDRV_SECTOR_BITS);
1796 
1797     /* The zero flag is only supported by version 3 and newer */
1798     if (s->qcow_version < 3) {
1799         return -ENOTSUP;
1800     }
1801 
1802     /* Each L2 slice is handled by its own loop iteration */
1803     nb_clusters = size_to_clusters(s, bytes);
1804 
1805     s->cache_discards = true;
1806 
1807     while (nb_clusters > 0) {
1808         cleared = zero_in_l2_slice(bs, offset, nb_clusters, flags);
1809         if (cleared < 0) {
1810             ret = cleared;
1811             goto fail;
1812         }
1813 
1814         nb_clusters -= cleared;
1815         offset += (cleared * s->cluster_size);
1816     }
1817 
1818     ret = 0;
1819 fail:
1820     s->cache_discards = false;
1821     qcow2_process_discards(bs, ret);
1822 
1823     return ret;
1824 }
1825 
1826 /*
1827  * Expands all zero clusters in a specific L1 table (or deallocates them, for
1828  * non-backed non-pre-allocated zero clusters).
1829  *
1830  * l1_entries and *visited_l1_entries are used to keep track of progress for
1831  * status_cb(). l1_entries contains the total number of L1 entries and
1832  * *visited_l1_entries counts all visited L1 entries.
1833  */
1834 static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,
1835                                       int l1_size, int64_t *visited_l1_entries,
1836                                       int64_t l1_entries,
1837                                       BlockDriverAmendStatusCB *status_cb,
1838                                       void *cb_opaque)
1839 {
1840     BDRVQcow2State *s = bs->opaque;
1841     bool is_active_l1 = (l1_table == s->l1_table);
1842     uint64_t *l2_slice = NULL;
1843     unsigned slice, slice_size2, n_slices;
1844     int ret;
1845     int i, j;
1846 
1847     slice_size2 = s->l2_slice_size * sizeof(uint64_t);
1848     n_slices = s->cluster_size / slice_size2;
1849 
1850     if (!is_active_l1) {
1851         /* inactive L2 tables require a buffer to be stored in when loading
1852          * them from disk */
1853         l2_slice = qemu_try_blockalign(bs->file->bs, slice_size2);
1854         if (l2_slice == NULL) {
1855             return -ENOMEM;
1856         }
1857     }
1858 
1859     for (i = 0; i < l1_size; i++) {
1860         uint64_t l2_offset = l1_table[i] & L1E_OFFSET_MASK;
1861         uint64_t l2_refcount;
1862 
1863         if (!l2_offset) {
1864             /* unallocated */
1865             (*visited_l1_entries)++;
1866             if (status_cb) {
1867                 status_cb(bs, *visited_l1_entries, l1_entries, cb_opaque);
1868             }
1869             continue;
1870         }
1871 
1872         if (offset_into_cluster(s, l2_offset)) {
1873             qcow2_signal_corruption(bs, true, -1, -1, "L2 table offset %#"
1874                                     PRIx64 " unaligned (L1 index: %#x)",
1875                                     l2_offset, i);
1876             ret = -EIO;
1877             goto fail;
1878         }
1879 
1880         ret = qcow2_get_refcount(bs, l2_offset >> s->cluster_bits,
1881                                  &l2_refcount);
1882         if (ret < 0) {
1883             goto fail;
1884         }
1885 
1886         for (slice = 0; slice < n_slices; slice++) {
1887             uint64_t slice_offset = l2_offset + slice * slice_size2;
1888             bool l2_dirty = false;
1889             if (is_active_l1) {
1890                 /* get active L2 tables from cache */
1891                 ret = qcow2_cache_get(bs, s->l2_table_cache, slice_offset,
1892                                       (void **)&l2_slice);
1893             } else {
1894                 /* load inactive L2 tables from disk */
1895                 ret = bdrv_pread(bs->file, slice_offset, l2_slice, slice_size2);
1896             }
1897             if (ret < 0) {
1898                 goto fail;
1899             }
1900 
1901             for (j = 0; j < s->l2_slice_size; j++) {
1902                 uint64_t l2_entry = be64_to_cpu(l2_slice[j]);
1903                 int64_t offset = l2_entry & L2E_OFFSET_MASK;
1904                 QCow2ClusterType cluster_type =
1905                     qcow2_get_cluster_type(bs, l2_entry);
1906 
1907                 if (cluster_type != QCOW2_CLUSTER_ZERO_PLAIN &&
1908                     cluster_type != QCOW2_CLUSTER_ZERO_ALLOC) {
1909                     continue;
1910                 }
1911 
1912                 if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN) {
1913                     if (!bs->backing) {
1914                         /* not backed; therefore we can simply deallocate the
1915                          * cluster */
1916                         l2_slice[j] = 0;
1917                         l2_dirty = true;
1918                         continue;
1919                     }
1920 
1921                     offset = qcow2_alloc_clusters(bs, s->cluster_size);
1922                     if (offset < 0) {
1923                         ret = offset;
1924                         goto fail;
1925                     }
1926 
1927                     /* The offset must fit in the offset field */
1928                     assert((offset & L2E_OFFSET_MASK) == offset);
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