block/qcow2: Fix corruption introduced by commit 8ac0f15f335
[qemu/ar7.git] / block / qcow2-cluster.c
blobcac0b6c7ba2576b032a2d7a50295c36107ad0165
1 /*
2 * Block driver for the QCOW version 2 format
4 * Copyright (c) 2004-2006 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include <zlib.h>
28 #include "qapi/error.h"
29 #include "qcow2.h"
30 #include "qemu/bswap.h"
31 #include "trace.h"
33 int qcow2_shrink_l1_table(BlockDriverState *bs, uint64_t exact_size)
35 BDRVQcow2State *s = bs->opaque;
36 int new_l1_size, i, ret;
38 if (exact_size >= s->l1_size) {
39 return 0;
42 new_l1_size = exact_size;
44 #ifdef DEBUG_ALLOC2
45 fprintf(stderr, "shrink l1_table from %d to %d\n", s->l1_size, new_l1_size);
46 #endif
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;
56 ret = bdrv_flush(bs->file->bs);
57 if (ret < 0) {
58 goto fail;
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;
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;
70 return 0;
72 fail:
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.
78 memset(s->l1_table + new_l1_size, 0,
79 (s->l1_size - new_l1_size) * sizeof(uint64_t));
80 return ret;
83 int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,
84 bool exact_size)
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];
93 if (min_size <= s->l1_size)
94 return 0;
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;
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;
111 while (min_size > new_l1_size) {
112 new_l1_size = DIV_ROUND_UP(new_l1_size * 3, 2);
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;
121 #ifdef DEBUG_ALLOC2
122 fprintf(stderr, "grow l1_table from %d to %" PRId64 "\n",
123 s->l1_size, new_l1_size);
124 #endif
126 new_l1_size2 = sizeof(uint64_t) * new_l1_size;
127 new_l1_table = qemu_try_blockalign(bs->file->bs,
128 ROUND_UP(new_l1_size2, 512));
129 if (new_l1_table == NULL) {
130 return -ENOMEM;
132 memset(new_l1_table, 0, ROUND_UP(new_l1_size2, 512));
134 if (s->l1_size) {
135 memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t));
138 /* write new table (align to cluster) */
139 BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_ALLOC_TABLE);
140 new_l1_table_offset = qcow2_alloc_clusters(bs, new_l1_size2);
141 if (new_l1_table_offset < 0) {
142 qemu_vfree(new_l1_table);
143 return new_l1_table_offset;
146 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
147 if (ret < 0) {
148 goto fail;
151 /* the L1 position has not yet been updated, so these clusters must
152 * indeed be completely free */
153 ret = qcow2_pre_write_overlap_check(bs, 0, new_l1_table_offset,
154 new_l1_size2, false);
155 if (ret < 0) {
156 goto fail;
159 BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_WRITE_TABLE);
160 for(i = 0; i < s->l1_size; i++)
161 new_l1_table[i] = cpu_to_be64(new_l1_table[i]);
162 ret = bdrv_pwrite_sync(bs->file, new_l1_table_offset,
163 new_l1_table, new_l1_size2);
164 if (ret < 0)
165 goto fail;
166 for(i = 0; i < s->l1_size; i++)
167 new_l1_table[i] = be64_to_cpu(new_l1_table[i]);
169 /* set new table */
170 BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_ACTIVATE_TABLE);
171 stl_be_p(data, new_l1_size);
172 stq_be_p(data + 4, new_l1_table_offset);
173 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, l1_size),
174 data, sizeof(data));
175 if (ret < 0) {
176 goto fail;
178 qemu_vfree(s->l1_table);
179 old_l1_table_offset = s->l1_table_offset;
180 s->l1_table_offset = new_l1_table_offset;
181 s->l1_table = new_l1_table;
182 old_l1_size = s->l1_size;
183 s->l1_size = new_l1_size;
184 qcow2_free_clusters(bs, old_l1_table_offset, old_l1_size * sizeof(uint64_t),
185 QCOW2_DISCARD_OTHER);
186 return 0;
187 fail:
188 qemu_vfree(new_l1_table);
189 qcow2_free_clusters(bs, new_l1_table_offset, new_l1_size2,
190 QCOW2_DISCARD_OTHER);
191 return ret;
195 * l2_load
197 * @bs: The BlockDriverState
198 * @offset: A guest offset, used to calculate what slice of the L2
199 * table to load.
200 * @l2_offset: Offset to the L2 table in the image file.
201 * @l2_slice: Location to store the pointer to the L2 slice.
203 * Loads a L2 slice into memory (L2 slices are the parts of L2 tables
204 * that are loaded by the qcow2 cache). If the slice is in the cache,
205 * the cache is used; otherwise the L2 slice is loaded from the image
206 * file.
208 static int l2_load(BlockDriverState *bs, uint64_t offset,
209 uint64_t l2_offset, uint64_t **l2_slice)
211 BDRVQcow2State *s = bs->opaque;
212 int start_of_slice = sizeof(uint64_t) *
213 (offset_to_l2_index(s, offset) - offset_to_l2_slice_index(s, offset));
215 return qcow2_cache_get(bs, s->l2_table_cache, l2_offset + start_of_slice,
216 (void **)l2_slice);
220 * Writes one sector of the L1 table to the disk (can't update single entries
221 * and we really don't want bdrv_pread to perform a read-modify-write)
223 #define L1_ENTRIES_PER_SECTOR (512 / 8)
224 int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index)
226 BDRVQcow2State *s = bs->opaque;
227 uint64_t buf[L1_ENTRIES_PER_SECTOR] = { 0 };
228 int l1_start_index;
229 int i, ret;
231 l1_start_index = l1_index & ~(L1_ENTRIES_PER_SECTOR - 1);
232 for (i = 0; i < L1_ENTRIES_PER_SECTOR && l1_start_index + i < s->l1_size;
233 i++)
235 buf[i] = cpu_to_be64(s->l1_table[l1_start_index + i]);
238 ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L1,
239 s->l1_table_offset + 8 * l1_start_index, sizeof(buf), false);
240 if (ret < 0) {
241 return ret;
244 BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
245 ret = bdrv_pwrite_sync(bs->file,
246 s->l1_table_offset + 8 * l1_start_index,
247 buf, sizeof(buf));
248 if (ret < 0) {
249 return ret;
252 return 0;
256 * l2_allocate
258 * Allocate a new l2 entry in the file. If l1_index points to an already
259 * used entry in the L2 table (i.e. we are doing a copy on write for the L2
260 * table) copy the contents of the old L2 table into the newly allocated one.
261 * Otherwise the new table is initialized with zeros.
265 static int l2_allocate(BlockDriverState *bs, int l1_index)
267 BDRVQcow2State *s = bs->opaque;
268 uint64_t old_l2_offset;
269 uint64_t *l2_slice = NULL;
270 unsigned slice, slice_size2, n_slices;
271 int64_t l2_offset;
272 int ret;
274 old_l2_offset = s->l1_table[l1_index];
276 trace_qcow2_l2_allocate(bs, l1_index);
278 /* allocate a new l2 entry */
280 l2_offset = qcow2_alloc_clusters(bs, s->l2_size * sizeof(uint64_t));
281 if (l2_offset < 0) {
282 ret = l2_offset;
283 goto fail;
286 /* The offset must fit in the offset field of the L1 table entry */
287 assert((l2_offset & L1E_OFFSET_MASK) == l2_offset);
289 /* If we're allocating the table at offset 0 then something is wrong */
290 if (l2_offset == 0) {
291 qcow2_signal_corruption(bs, true, -1, -1, "Preventing invalid "
292 "allocation of L2 table at offset 0");
293 ret = -EIO;
294 goto fail;
297 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
298 if (ret < 0) {
299 goto fail;
302 /* allocate a new entry in the l2 cache */
304 slice_size2 = s->l2_slice_size * sizeof(uint64_t);
305 n_slices = s->cluster_size / slice_size2;
307 trace_qcow2_l2_allocate_get_empty(bs, l1_index);
308 for (slice = 0; slice < n_slices; slice++) {
309 ret = qcow2_cache_get_empty(bs, s->l2_table_cache,
310 l2_offset + slice * slice_size2,
311 (void **) &l2_slice);
312 if (ret < 0) {
313 goto fail;
316 if ((old_l2_offset & L1E_OFFSET_MASK) == 0) {
317 /* if there was no old l2 table, clear the new slice */
318 memset(l2_slice, 0, slice_size2);
319 } else {
320 uint64_t *old_slice;
321 uint64_t old_l2_slice_offset =
322 (old_l2_offset & L1E_OFFSET_MASK) + slice * slice_size2;
324 /* if there was an old l2 table, read a slice from the disk */
325 BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_COW_READ);
326 ret = qcow2_cache_get(bs, s->l2_table_cache, old_l2_slice_offset,
327 (void **) &old_slice);
328 if (ret < 0) {
329 goto fail;
332 memcpy(l2_slice, old_slice, slice_size2);
334 qcow2_cache_put(s->l2_table_cache, (void **) &old_slice);
337 /* write the l2 slice to the file */
338 BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_WRITE);
340 trace_qcow2_l2_allocate_write_l2(bs, l1_index);
341 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
342 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
345 ret = qcow2_cache_flush(bs, s->l2_table_cache);
346 if (ret < 0) {
347 goto fail;
350 /* update the L1 entry */
351 trace_qcow2_l2_allocate_write_l1(bs, l1_index);
352 s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED;
353 ret = qcow2_write_l1_entry(bs, l1_index);
354 if (ret < 0) {
355 goto fail;
358 trace_qcow2_l2_allocate_done(bs, l1_index, 0);
359 return 0;
361 fail:
362 trace_qcow2_l2_allocate_done(bs, l1_index, ret);
363 if (l2_slice != NULL) {
364 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
366 s->l1_table[l1_index] = old_l2_offset;
367 if (l2_offset > 0) {
368 qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t),
369 QCOW2_DISCARD_ALWAYS);
371 return ret;
375 * Checks how many clusters in a given L2 slice are contiguous in the image
376 * file. As soon as one of the flags in the bitmask stop_flags changes compared
377 * to the first cluster, the search is stopped and the cluster is not counted
378 * as contiguous. (This allows it, for example, to stop at the first compressed
379 * cluster which may require a different handling)
381 static int count_contiguous_clusters(BlockDriverState *bs, int nb_clusters,
382 int cluster_size, uint64_t *l2_slice, uint64_t stop_flags)
384 int i;
385 QCow2ClusterType first_cluster_type;
386 uint64_t mask = stop_flags | L2E_OFFSET_MASK | QCOW_OFLAG_COMPRESSED;
387 uint64_t first_entry = be64_to_cpu(l2_slice[0]);
388 uint64_t offset = first_entry & mask;
390 first_cluster_type = qcow2_get_cluster_type(bs, first_entry);
391 if (first_cluster_type == QCOW2_CLUSTER_UNALLOCATED) {
392 return 0;
395 /* must be allocated */
396 assert(first_cluster_type == QCOW2_CLUSTER_NORMAL ||
397 first_cluster_type == QCOW2_CLUSTER_ZERO_ALLOC);
399 for (i = 0; i < nb_clusters; i++) {
400 uint64_t l2_entry = be64_to_cpu(l2_slice[i]) & mask;
401 if (offset + (uint64_t) i * cluster_size != l2_entry) {
402 break;
406 return i;
410 * Checks how many consecutive unallocated clusters in a given L2
411 * slice have the same cluster type.
413 static int count_contiguous_clusters_unallocated(BlockDriverState *bs,
414 int nb_clusters,
415 uint64_t *l2_slice,
416 QCow2ClusterType wanted_type)
418 int i;
420 assert(wanted_type == QCOW2_CLUSTER_ZERO_PLAIN ||
421 wanted_type == QCOW2_CLUSTER_UNALLOCATED);
422 for (i = 0; i < nb_clusters; i++) {
423 uint64_t entry = be64_to_cpu(l2_slice[i]);
424 QCow2ClusterType type = qcow2_get_cluster_type(bs, entry);
426 if (type != wanted_type) {
427 break;
431 return i;
434 static int coroutine_fn do_perform_cow_read(BlockDriverState *bs,
435 uint64_t src_cluster_offset,
436 unsigned offset_in_cluster,
437 QEMUIOVector *qiov)
439 int ret;
441 if (qiov->size == 0) {
442 return 0;
445 BLKDBG_EVENT(bs->file, BLKDBG_COW_READ);
447 if (!bs->drv) {
448 return -ENOMEDIUM;
451 /* Call .bdrv_co_readv() directly instead of using the public block-layer
452 * interface. This avoids double I/O throttling and request tracking,
453 * which can lead to deadlock when block layer copy-on-read is enabled.
455 ret = bs->drv->bdrv_co_preadv_part(bs,
456 src_cluster_offset + offset_in_cluster,
457 qiov->size, qiov, 0, 0);
458 if (ret < 0) {
459 return ret;
462 return 0;
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)
472 if (bytes && bs->encrypted) {
473 BDRVQcow2State *s = bs->opaque;
474 assert(QEMU_IS_ALIGNED(offset_in_cluster, BDRV_SECTOR_SIZE));
475 assert(QEMU_IS_ALIGNED(bytes, BDRV_SECTOR_SIZE));
476 assert(s->crypto);
477 if (qcow2_co_encrypt(bs,
478 start_of_cluster(s, cluster_offset + offset_in_cluster),
479 src_cluster_offset + offset_in_cluster,
480 buffer, bytes) < 0) {
481 return false;
484 return true;
487 static int coroutine_fn do_perform_cow_write(BlockDriverState *bs,
488 uint64_t cluster_offset,
489 unsigned offset_in_cluster,
490 QEMUIOVector *qiov)
492 BDRVQcow2State *s = bs->opaque;
493 int ret;
495 if (qiov->size == 0) {
496 return 0;
499 ret = qcow2_pre_write_overlap_check(bs, 0,
500 cluster_offset + offset_in_cluster, qiov->size, true);
501 if (ret < 0) {
502 return ret;
505 BLKDBG_EVENT(bs->file, BLKDBG_COW_WRITE);
506 ret = bdrv_co_pwritev(s->data_file, cluster_offset + offset_in_cluster,
507 qiov->size, qiov, 0);
508 if (ret < 0) {
509 return ret;
512 return 0;
517 * get_cluster_offset
519 * For a given offset of the virtual disk, find the cluster type and offset in
520 * the qcow2 file. The offset is stored in *cluster_offset.
522 * On entry, *bytes is the maximum number of contiguous bytes starting at
523 * offset that we are interested in.
525 * On exit, *bytes is the number of bytes starting at offset that have the same
526 * cluster type and (if applicable) are stored contiguously in the image file.
527 * Compressed clusters are always returned one by one.
529 * Returns the cluster type (QCOW2_CLUSTER_*) on success, -errno in error
530 * cases.
532 int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
533 unsigned int *bytes, uint64_t *cluster_offset)
535 BDRVQcow2State *s = bs->opaque;
536 unsigned int l2_index;
537 uint64_t l1_index, l2_offset, *l2_slice;
538 int c;
539 unsigned int offset_in_cluster;
540 uint64_t bytes_available, bytes_needed, nb_clusters;
541 QCow2ClusterType type;
542 int ret;
544 offset_in_cluster = offset_into_cluster(s, offset);
545 bytes_needed = (uint64_t) *bytes + offset_in_cluster;
547 /* compute how many bytes there are between the start of the cluster
548 * containing offset and the end of the l2 slice that contains
549 * the entry pointing to it */
550 bytes_available =
551 ((uint64_t) (s->l2_slice_size - offset_to_l2_slice_index(s, offset)))
552 << s->cluster_bits;
554 if (bytes_needed > bytes_available) {
555 bytes_needed = bytes_available;
558 *cluster_offset = 0;
560 /* seek to the l2 offset in the l1 table */
562 l1_index = offset_to_l1_index(s, offset);
563 if (l1_index >= s->l1_size) {
564 type = QCOW2_CLUSTER_UNALLOCATED;
565 goto out;
568 l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
569 if (!l2_offset) {
570 type = QCOW2_CLUSTER_UNALLOCATED;
571 goto out;
574 if (offset_into_cluster(s, l2_offset)) {
575 qcow2_signal_corruption(bs, true, -1, -1, "L2 table offset %#" PRIx64
576 " unaligned (L1 index: %#" PRIx64 ")",
577 l2_offset, l1_index);
578 return -EIO;
581 /* load the l2 slice in memory */
583 ret = l2_load(bs, offset, l2_offset, &l2_slice);
584 if (ret < 0) {
585 return ret;
588 /* find the cluster offset for the given disk offset */
590 l2_index = offset_to_l2_slice_index(s, offset);
591 *cluster_offset = be64_to_cpu(l2_slice[l2_index]);
593 nb_clusters = size_to_clusters(s, bytes_needed);
594 /* bytes_needed <= *bytes + offset_in_cluster, both of which are unsigned
595 * integers; the minimum cluster size is 512, so this assertion is always
596 * true */
597 assert(nb_clusters <= INT_MAX);
599 type = qcow2_get_cluster_type(bs, *cluster_offset);
600 if (s->qcow_version < 3 && (type == QCOW2_CLUSTER_ZERO_PLAIN ||
601 type == QCOW2_CLUSTER_ZERO_ALLOC)) {
602 qcow2_signal_corruption(bs, true, -1, -1, "Zero cluster entry found"
603 " in pre-v3 image (L2 offset: %#" PRIx64
604 ", L2 index: %#x)", l2_offset, l2_index);
605 ret = -EIO;
606 goto fail;
608 switch (type) {
609 case QCOW2_CLUSTER_COMPRESSED:
610 if (has_data_file(bs)) {
611 qcow2_signal_corruption(bs, true, -1, -1, "Compressed cluster "
612 "entry found in image with external data "
613 "file (L2 offset: %#" PRIx64 ", L2 index: "
614 "%#x)", l2_offset, l2_index);
615 ret = -EIO;
616 goto fail;
618 /* Compressed clusters can only be processed one by one */
619 c = 1;
620 *cluster_offset &= L2E_COMPRESSED_OFFSET_SIZE_MASK;
621 break;
622 case QCOW2_CLUSTER_ZERO_PLAIN:
623 case QCOW2_CLUSTER_UNALLOCATED:
624 /* how many empty clusters ? */
625 c = count_contiguous_clusters_unallocated(bs, nb_clusters,
626 &l2_slice[l2_index], type);
627 *cluster_offset = 0;
628 break;
629 case QCOW2_CLUSTER_ZERO_ALLOC:
630 case QCOW2_CLUSTER_NORMAL:
631 /* how many allocated clusters ? */
632 c = count_contiguous_clusters(bs, nb_clusters, s->cluster_size,
633 &l2_slice[l2_index], QCOW_OFLAG_ZERO);
634 *cluster_offset &= L2E_OFFSET_MASK;
635 if (offset_into_cluster(s, *cluster_offset)) {
636 qcow2_signal_corruption(bs, true, -1, -1,
637 "Cluster allocation offset %#"
638 PRIx64 " unaligned (L2 offset: %#" PRIx64
639 ", L2 index: %#x)", *cluster_offset,
640 l2_offset, l2_index);
641 ret = -EIO;
642 goto fail;
644 if (has_data_file(bs) && *cluster_offset != offset - offset_in_cluster)
646 qcow2_signal_corruption(bs, true, -1, -1,
647 "External data file host cluster offset %#"
648 PRIx64 " does not match guest cluster "
649 "offset: %#" PRIx64
650 ", L2 index: %#x)", *cluster_offset,
651 offset - offset_in_cluster, l2_index);
652 ret = -EIO;
653 goto fail;
655 break;
656 default:
657 abort();
660 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
662 bytes_available = (int64_t)c * s->cluster_size;
664 out:
665 if (bytes_available > bytes_needed) {
666 bytes_available = bytes_needed;
669 /* bytes_available <= bytes_needed <= *bytes + offset_in_cluster;
670 * subtracting offset_in_cluster will therefore definitely yield something
671 * not exceeding UINT_MAX */
672 assert(bytes_available - offset_in_cluster <= UINT_MAX);
673 *bytes = bytes_available - offset_in_cluster;
675 return type;
677 fail:
678 qcow2_cache_put(s->l2_table_cache, (void **)&l2_slice);
679 return ret;
683 * get_cluster_table
685 * for a given disk offset, load (and allocate if needed)
686 * the appropriate slice of its l2 table.
688 * the cluster index in the l2 slice is given to the caller.
690 * Returns 0 on success, -errno in failure case
692 static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
693 uint64_t **new_l2_slice,
694 int *new_l2_index)
696 BDRVQcow2State *s = bs->opaque;
697 unsigned int l2_index;
698 uint64_t l1_index, l2_offset;
699 uint64_t *l2_slice = NULL;
700 int ret;
702 /* seek to the l2 offset in the l1 table */
704 l1_index = offset_to_l1_index(s, offset);
705 if (l1_index >= s->l1_size) {
706 ret = qcow2_grow_l1_table(bs, l1_index + 1, false);
707 if (ret < 0) {
708 return ret;
712 assert(l1_index < s->l1_size);
713 l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
714 if (offset_into_cluster(s, l2_offset)) {
715 qcow2_signal_corruption(bs, true, -1, -1, "L2 table offset %#" PRIx64
716 " unaligned (L1 index: %#" PRIx64 ")",
717 l2_offset, l1_index);
718 return -EIO;
721 if (!(s->l1_table[l1_index] & QCOW_OFLAG_COPIED)) {
722 /* First allocate a new L2 table (and do COW if needed) */
723 ret = l2_allocate(bs, l1_index);
724 if (ret < 0) {
725 return ret;
728 /* Then decrease the refcount of the old table */
729 if (l2_offset) {
730 qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t),
731 QCOW2_DISCARD_OTHER);
734 /* Get the offset of the newly-allocated l2 table */
735 l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
736 assert(offset_into_cluster(s, l2_offset) == 0);
739 /* load the l2 slice in memory */
740 ret = l2_load(bs, offset, l2_offset, &l2_slice);
741 if (ret < 0) {
742 return ret;
745 /* find the cluster offset for the given disk offset */
747 l2_index = offset_to_l2_slice_index(s, offset);
749 *new_l2_slice = l2_slice;
750 *new_l2_index = l2_index;
752 return 0;
756 * alloc_compressed_cluster_offset
758 * For a given offset on the virtual disk, allocate a new compressed cluster
759 * and put the host offset of the cluster into *host_offset. If a cluster is
760 * already allocated at the offset, return an error.
762 * Return 0 on success and -errno in error cases
764 int qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
765 uint64_t offset,
766 int compressed_size,
767 uint64_t *host_offset)
769 BDRVQcow2State *s = bs->opaque;
770 int l2_index, ret;
771 uint64_t *l2_slice;
772 int64_t cluster_offset;
773 int nb_csectors;
775 if (has_data_file(bs)) {
776 return 0;
779 ret = get_cluster_table(bs, offset, &l2_slice, &l2_index);
780 if (ret < 0) {
781 return ret;
784 /* Compression can't overwrite anything. Fail if the cluster was already
785 * allocated. */
786 cluster_offset = be64_to_cpu(l2_slice[l2_index]);
787 if (cluster_offset & L2E_OFFSET_MASK) {
788 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
789 return -EIO;
792 cluster_offset = qcow2_alloc_bytes(bs, compressed_size);
793 if (cluster_offset < 0) {
794 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
795 return cluster_offset;
798 nb_csectors =
799 (cluster_offset + compressed_size - 1) / QCOW2_COMPRESSED_SECTOR_SIZE -
800 (cluster_offset / QCOW2_COMPRESSED_SECTOR_SIZE);
802 cluster_offset |= QCOW_OFLAG_COMPRESSED |
803 ((uint64_t)nb_csectors << s->csize_shift);
805 /* update L2 table */
807 /* compressed clusters never have the copied flag */
809 BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE_COMPRESSED);
810 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
811 l2_slice[l2_index] = cpu_to_be64(cluster_offset);
812 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
814 *host_offset = cluster_offset & s->cluster_offset_mask;
815 return 0;
818 static int perform_cow(BlockDriverState *bs, QCowL2Meta *m)
820 BDRVQcow2State *s = bs->opaque;
821 Qcow2COWRegion *start = &m->cow_start;
822 Qcow2COWRegion *end = &m->cow_end;
823 unsigned buffer_size;
824 unsigned data_bytes = end->offset - (start->offset + start->nb_bytes);
825 bool merge_reads;
826 uint8_t *start_buffer, *end_buffer;
827 QEMUIOVector qiov;
828 int ret;
830 assert(start->nb_bytes <= UINT_MAX - end->nb_bytes);
831 assert(start->nb_bytes + end->nb_bytes <= UINT_MAX - data_bytes);
832 assert(start->offset + start->nb_bytes <= end->offset);
834 if ((start->nb_bytes == 0 && end->nb_bytes == 0) || m->skip_cow) {
835 return 0;
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;
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;
861 /* The part of the buffer where the end region is located */
862 end_buffer = start_buffer + buffer_size - end->nb_bytes;
864 qemu_iovec_init(&qiov, 2 + (m->data_qiov ?
865 qemu_iovec_subvec_niov(m->data_qiov,
866 m->data_qiov_offset,
867 data_bytes)
868 : 0));
870 qemu_co_mutex_unlock(&s->lock);
871 /* First we read the existing data from both COW regions. We
872 * either read the whole region in one go, or the start and end
873 * regions separately. */
874 if (merge_reads) {
875 qemu_iovec_add(&qiov, start_buffer, buffer_size);
876 ret = do_perform_cow_read(bs, m->offset, start->offset, &qiov);
877 } else {
878 qemu_iovec_add(&qiov, start_buffer, start->nb_bytes);
879 ret = do_perform_cow_read(bs, m->offset, start->offset, &qiov);
880 if (ret < 0) {
881 goto fail;
884 qemu_iovec_reset(&qiov);
885 qemu_iovec_add(&qiov, end_buffer, end->nb_bytes);
886 ret = do_perform_cow_read(bs, m->offset, end->offset, &qiov);
888 if (ret < 0) {
889 goto fail;
892 /* Encrypt the data if necessary before writing it */
893 if (bs->encrypted) {
894 if (!do_perform_cow_encrypt(bs, m->offset, m->alloc_offset,
895 start->offset, start_buffer,
896 start->nb_bytes) ||
897 !do_perform_cow_encrypt(bs, m->offset, m->alloc_offset,
898 end->offset, end_buffer, end->nb_bytes)) {
899 ret = -EIO;
900 goto fail;
904 /* And now we can write everything. If we have the guest data we
905 * can write everything in one single operation */
906 if (m->data_qiov) {
907 qemu_iovec_reset(&qiov);
908 if (start->nb_bytes) {
909 qemu_iovec_add(&qiov, start_buffer, start->nb_bytes);
911 qemu_iovec_concat(&qiov, m->data_qiov, m->data_qiov_offset, data_bytes);
912 if (end->nb_bytes) {
913 qemu_iovec_add(&qiov, end_buffer, end->nb_bytes);
915 /* NOTE: we have a write_aio blkdebug event here followed by
916 * a cow_write one in do_perform_cow_write(), but there's only
917 * one single I/O operation */
918 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
919 ret = do_perform_cow_write(bs, m->alloc_offset, start->offset, &qiov);
920 } else {
921 /* If there's no guest data then write both COW regions separately */
922 qemu_iovec_reset(&qiov);
923 qemu_iovec_add(&qiov, start_buffer, start->nb_bytes);
924 ret = do_perform_cow_write(bs, m->alloc_offset, start->offset, &qiov);
925 if (ret < 0) {
926 goto fail;
929 qemu_iovec_reset(&qiov);
930 qemu_iovec_add(&qiov, end_buffer, end->nb_bytes);
931 ret = do_perform_cow_write(bs, m->alloc_offset, end->offset, &qiov);
934 fail:
935 qemu_co_mutex_lock(&s->lock);
938 * Before we update the L2 table to actually point to the new cluster, we
939 * need to be sure that the refcounts have been increased and COW was
940 * handled.
942 if (ret == 0) {
943 qcow2_cache_depends_on_flush(s->l2_table_cache);
946 qemu_vfree(start_buffer);
947 qemu_iovec_destroy(&qiov);
948 return ret;
951 int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)
953 BDRVQcow2State *s = bs->opaque;
954 int i, j = 0, l2_index, ret;
955 uint64_t *old_cluster, *l2_slice;
956 uint64_t cluster_offset = m->alloc_offset;
958 trace_qcow2_cluster_link_l2(qemu_coroutine_self(), m->nb_clusters);
959 assert(m->nb_clusters > 0);
961 old_cluster = g_try_new(uint64_t, m->nb_clusters);
962 if (old_cluster == NULL) {
963 ret = -ENOMEM;
964 goto err;
967 /* copy content of unmodified sectors */
968 ret = perform_cow(bs, m);
969 if (ret < 0) {
970 goto err;
973 /* Update L2 table. */
974 if (s->use_lazy_refcounts) {
975 qcow2_mark_dirty(bs);
977 if (qcow2_need_accurate_refcounts(s)) {
978 qcow2_cache_set_dependency(bs, s->l2_table_cache,
979 s->refcount_block_cache);
982 ret = get_cluster_table(bs, m->offset, &l2_slice, &l2_index);
983 if (ret < 0) {
984 goto err;
986 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
988 assert(l2_index + m->nb_clusters <= s->l2_slice_size);
989 for (i = 0; i < m->nb_clusters; i++) {
990 /* if two concurrent writes happen to the same unallocated cluster
991 * each write allocates separate cluster and writes data concurrently.
992 * The first one to complete updates l2 table with pointer to its
993 * cluster the second one has to do RMW (which is done above by
994 * perform_cow()), update l2 table with its cluster pointer and free
995 * old cluster. This is what this loop does */
996 if (l2_slice[l2_index + i] != 0) {
997 old_cluster[j++] = l2_slice[l2_index + i];
1000 l2_slice[l2_index + i] = cpu_to_be64((cluster_offset +
1001 (i << s->cluster_bits)) | QCOW_OFLAG_COPIED);
1005 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
1008 * If this was a COW, we need to decrease the refcount of the old cluster.
1010 * Don't discard clusters that reach a refcount of 0 (e.g. compressed
1011 * clusters), the next write will reuse them anyway.
1013 if (!m->keep_old_clusters && j != 0) {
1014 for (i = 0; i < j; i++) {
1015 qcow2_free_any_clusters(bs, be64_to_cpu(old_cluster[i]), 1,
1016 QCOW2_DISCARD_NEVER);
1020 ret = 0;
1021 err:
1022 g_free(old_cluster);
1023 return ret;
1027 * Frees the allocated clusters because the request failed and they won't
1028 * actually be linked.
1030 void qcow2_alloc_cluster_abort(BlockDriverState *bs, QCowL2Meta *m)
1032 BDRVQcow2State *s = bs->opaque;
1033 qcow2_free_clusters(bs, m->alloc_offset, m->nb_clusters << s->cluster_bits,
1034 QCOW2_DISCARD_NEVER);
1038 * Returns the number of contiguous clusters that can be used for an allocating
1039 * write, but require COW to be performed (this includes yet unallocated space,
1040 * which must copy from the backing file)
1042 static int count_cow_clusters(BlockDriverState *bs, int nb_clusters,
1043 uint64_t *l2_slice, int l2_index)
1045 int i;
1047 for (i = 0; i < nb_clusters; i++) {
1048 uint64_t l2_entry = be64_to_cpu(l2_slice[l2_index + i]);
1049 QCow2ClusterType cluster_type = qcow2_get_cluster_type(bs, l2_entry);
1051 switch(cluster_type) {
1052 case QCOW2_CLUSTER_NORMAL:
1053 if (l2_entry & QCOW_OFLAG_COPIED) {
1054 goto out;
1056 break;
1057 case QCOW2_CLUSTER_UNALLOCATED:
1058 case QCOW2_CLUSTER_COMPRESSED:
1059 case QCOW2_CLUSTER_ZERO_PLAIN:
1060 case QCOW2_CLUSTER_ZERO_ALLOC:
1061 break;
1062 default:
1063 abort();
1067 out:
1068 assert(i <= nb_clusters);
1069 return i;
1073 * Check if there already is an AIO write request in flight which allocates
1074 * the same cluster. In this case we need to wait until the previous
1075 * request has completed and updated the L2 table accordingly.
1077 * Returns:
1078 * 0 if there was no dependency. *cur_bytes indicates the number of
1079 * bytes from guest_offset that can be read before the next
1080 * dependency must be processed (or the request is complete)
1082 * -EAGAIN if we had to wait for another request, previously gathered
1083 * information on cluster allocation may be invalid now. The caller
1084 * must start over anyway, so consider *cur_bytes undefined.
1086 static int handle_dependencies(BlockDriverState *bs, uint64_t guest_offset,
1087 uint64_t *cur_bytes, QCowL2Meta **m)
1089 BDRVQcow2State *s = bs->opaque;
1090 QCowL2Meta *old_alloc;
1091 uint64_t bytes = *cur_bytes;
1093 QLIST_FOREACH(old_alloc, &s->cluster_allocs, next_in_flight) {
1095 uint64_t start = guest_offset;
1096 uint64_t end = start + bytes;
1097 uint64_t old_start = l2meta_cow_start(old_alloc);
1098 uint64_t old_end = l2meta_cow_end(old_alloc);
1100 if (end <= old_start || start >= old_end) {
1101 /* No intersection */
1102 } else {
1103 if (start < old_start) {
1104 /* Stop at the start of a running allocation */
1105 bytes = old_start - start;
1106 } else {
1107 bytes = 0;
1110 /* Stop if already an l2meta exists. After yielding, it wouldn't
1111 * be valid any more, so we'd have to clean up the old L2Metas
1112 * and deal with requests depending on them before starting to
1113 * gather new ones. Not worth the trouble. */
1114 if (bytes == 0 && *m) {
1115 *cur_bytes = 0;
1116 return 0;
1119 if (bytes == 0) {
1120 /* Wait for the dependency to complete. We need to recheck
1121 * the free/allocated clusters when we continue. */
1122 qemu_co_queue_wait(&old_alloc->dependent_requests, &s->lock);
1123 return -EAGAIN;
1128 /* Make sure that existing clusters and new allocations are only used up to
1129 * the next dependency if we shortened the request above */
1130 *cur_bytes = bytes;
1132 return 0;
1136 * Checks how many already allocated clusters that don't require a copy on
1137 * write there are at the given guest_offset (up to *bytes). If *host_offset is
1138 * not INV_OFFSET, only physically contiguous clusters beginning at this host
1139 * offset are counted.
1141 * Note that guest_offset may not be cluster aligned. In this case, the
1142 * returned *host_offset points to exact byte referenced by guest_offset and
1143 * therefore isn't cluster aligned as well.
1145 * Returns:
1146 * 0: if no allocated clusters are available at the given offset.
1147 * *bytes is normally unchanged. It is set to 0 if the cluster
1148 * is allocated and doesn't need COW, but doesn't have the right
1149 * physical offset.
1151 * 1: if allocated clusters that don't require a COW are available at
1152 * the requested offset. *bytes may have decreased and describes
1153 * the length of the area that can be written to.
1155 * -errno: in error cases
1157 static int handle_copied(BlockDriverState *bs, uint64_t guest_offset,
1158 uint64_t *host_offset, uint64_t *bytes, QCowL2Meta **m)
1160 BDRVQcow2State *s = bs->opaque;
1161 int l2_index;
1162 uint64_t cluster_offset;
1163 uint64_t *l2_slice;
1164 uint64_t nb_clusters;
1165 unsigned int keep_clusters;
1166 int ret;
1168 trace_qcow2_handle_copied(qemu_coroutine_self(), guest_offset, *host_offset,
1169 *bytes);
1171 assert(*host_offset == INV_OFFSET || offset_into_cluster(s, guest_offset)
1172 == offset_into_cluster(s, *host_offset));
1175 * Calculate the number of clusters to look for. We stop at L2 slice
1176 * boundaries to keep things simple.
1178 nb_clusters =
1179 size_to_clusters(s, offset_into_cluster(s, guest_offset) + *bytes);
1181 l2_index = offset_to_l2_slice_index(s, guest_offset);
1182 nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);
1183 assert(nb_clusters <= INT_MAX);
1185 /* Find L2 entry for the first involved cluster */
1186 ret = get_cluster_table(bs, guest_offset, &l2_slice, &l2_index);
1187 if (ret < 0) {
1188 return ret;
1191 cluster_offset = be64_to_cpu(l2_slice[l2_index]);
1193 /* Check how many clusters are already allocated and don't need COW */
1194 if (qcow2_get_cluster_type(bs, cluster_offset) == QCOW2_CLUSTER_NORMAL
1195 && (cluster_offset & QCOW_OFLAG_COPIED))
1197 /* If a specific host_offset is required, check it */
1198 bool offset_matches =
1199 (cluster_offset & L2E_OFFSET_MASK) == *host_offset;
1201 if (offset_into_cluster(s, cluster_offset & L2E_OFFSET_MASK)) {
1202 qcow2_signal_corruption(bs, true, -1, -1, "Data cluster offset "
1203 "%#llx unaligned (guest offset: %#" PRIx64
1204 ")", cluster_offset & L2E_OFFSET_MASK,
1205 guest_offset);
1206 ret = -EIO;
1207 goto out;
1210 if (*host_offset != INV_OFFSET && !offset_matches) {
1211 *bytes = 0;
1212 ret = 0;
1213 goto out;
1216 /* We keep all QCOW_OFLAG_COPIED clusters */
1217 keep_clusters =
1218 count_contiguous_clusters(bs, nb_clusters, s->cluster_size,
1219 &l2_slice[l2_index],
1220 QCOW_OFLAG_COPIED | QCOW_OFLAG_ZERO);
1221 assert(keep_clusters <= nb_clusters);
1223 *bytes = MIN(*bytes,
1224 keep_clusters * s->cluster_size
1225 - offset_into_cluster(s, guest_offset));
1227 ret = 1;
1228 } else {
1229 ret = 0;
1232 /* Cleanup */
1233 out:
1234 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
1236 /* Only return a host offset if we actually made progress. Otherwise we
1237 * would make requirements for handle_alloc() that it can't fulfill */
1238 if (ret > 0) {
1239 *host_offset = (cluster_offset & L2E_OFFSET_MASK)
1240 + offset_into_cluster(s, guest_offset);
1243 return ret;
1247 * Allocates new clusters for the given guest_offset.
1249 * At most *nb_clusters are allocated, and on return *nb_clusters is updated to
1250 * contain the number of clusters that have been allocated and are contiguous
1251 * in the image file.
1253 * If *host_offset is not INV_OFFSET, it specifies the offset in the image file
1254 * at which the new clusters must start. *nb_clusters can be 0 on return in
1255 * this case if the cluster at host_offset is already in use. If *host_offset
1256 * is INV_OFFSET, the clusters can be allocated anywhere in the image file.
1258 * *host_offset is updated to contain the offset into the image file at which
1259 * the first allocated cluster starts.
1261 * Return 0 on success and -errno in error cases. -EAGAIN means that the
1262 * function has been waiting for another request and the allocation must be
1263 * restarted, but the whole request should not be failed.
1265 static int do_alloc_cluster_offset(BlockDriverState *bs, uint64_t guest_offset,
1266 uint64_t *host_offset, uint64_t *nb_clusters)
1268 BDRVQcow2State *s = bs->opaque;
1270 trace_qcow2_do_alloc_clusters_offset(qemu_coroutine_self(), guest_offset,
1271 *host_offset, *nb_clusters);
1273 if (has_data_file(bs)) {
1274 assert(*host_offset == INV_OFFSET ||
1275 *host_offset == start_of_cluster(s, guest_offset));
1276 *host_offset = start_of_cluster(s, guest_offset);
1277 return 0;
1280 /* Allocate new clusters */
1281 trace_qcow2_cluster_alloc_phys(qemu_coroutine_self());
1282 if (*host_offset == INV_OFFSET) {
1283 int64_t cluster_offset =
1284 qcow2_alloc_clusters(bs, *nb_clusters * s->cluster_size);
1285 if (cluster_offset < 0) {
1286 return cluster_offset;
1288 *host_offset = cluster_offset;
1289 return 0;
1290 } else {
1291 int64_t ret = qcow2_alloc_clusters_at(bs, *host_offset, *nb_clusters);
1292 if (ret < 0) {
1293 return ret;
1295 *nb_clusters = ret;
1296 return 0;
1301 * Allocates new clusters for an area that either is yet unallocated or needs a
1302 * copy on write. If *host_offset is not INV_OFFSET, clusters are only
1303 * allocated if the new allocation can match the specified host offset.
1305 * Note that guest_offset may not be cluster aligned. In this case, the
1306 * returned *host_offset points to exact byte referenced by guest_offset and
1307 * therefore isn't cluster aligned as well.
1309 * Returns:
1310 * 0: if no clusters could be allocated. *bytes is set to 0,
1311 * *host_offset is left unchanged.
1313 * 1: if new clusters were allocated. *bytes may be decreased if the
1314 * new allocation doesn't cover all of the requested area.
1315 * *host_offset is updated to contain the host offset of the first
1316 * newly allocated cluster.
1318 * -errno: in error cases
1320 static int handle_alloc(BlockDriverState *bs, uint64_t guest_offset,
1321 uint64_t *host_offset, uint64_t *bytes, QCowL2Meta **m)
1323 BDRVQcow2State *s = bs->opaque;
1324 int l2_index;
1325 uint64_t *l2_slice;
1326 uint64_t entry;
1327 uint64_t nb_clusters;
1328 int ret;
1329 bool keep_old_clusters = false;
1331 uint64_t alloc_cluster_offset = INV_OFFSET;
1333 trace_qcow2_handle_alloc(qemu_coroutine_self(), guest_offset, *host_offset,
1334 *bytes);
1335 assert(*bytes > 0);
1338 * Calculate the number of clusters to look for. We stop at L2 slice
1339 * boundaries to keep things simple.
1341 nb_clusters =
1342 size_to_clusters(s, offset_into_cluster(s, guest_offset) + *bytes);
1344 l2_index = offset_to_l2_slice_index(s, guest_offset);
1345 nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);
1346 assert(nb_clusters <= INT_MAX);
1348 /* Find L2 entry for the first involved cluster */
1349 ret = get_cluster_table(bs, guest_offset, &l2_slice, &l2_index);
1350 if (ret < 0) {
1351 return ret;
1354 entry = be64_to_cpu(l2_slice[l2_index]);
1355 nb_clusters = count_cow_clusters(bs, nb_clusters, l2_slice, l2_index);
1357 /* This function is only called when there were no non-COW clusters, so if
1358 * we can't find any unallocated or COW clusters either, something is
1359 * wrong with our code. */
1360 assert(nb_clusters > 0);
1362 if (qcow2_get_cluster_type(bs, entry) == QCOW2_CLUSTER_ZERO_ALLOC &&
1363 (entry & QCOW_OFLAG_COPIED) &&
1364 (*host_offset == INV_OFFSET ||
1365 start_of_cluster(s, *host_offset) == (entry & L2E_OFFSET_MASK)))
1367 int preallocated_nb_clusters;
1369 if (offset_into_cluster(s, entry & L2E_OFFSET_MASK)) {
1370 qcow2_signal_corruption(bs, true, -1, -1, "Preallocated zero "
1371 "cluster offset %#llx unaligned (guest "
1372 "offset: %#" PRIx64 ")",
1373 entry & L2E_OFFSET_MASK, guest_offset);
1374 ret = -EIO;
1375 goto fail;
1378 /* Try to reuse preallocated zero clusters; contiguous normal clusters
1379 * would be fine, too, but count_cow_clusters() above has limited
1380 * nb_clusters already to a range of COW clusters */
1381 preallocated_nb_clusters =
1382 count_contiguous_clusters(bs, nb_clusters, s->cluster_size,
1383 &l2_slice[l2_index], QCOW_OFLAG_COPIED);
1384 assert(preallocated_nb_clusters > 0);
1386 nb_clusters = preallocated_nb_clusters;
1387 alloc_cluster_offset = entry & L2E_OFFSET_MASK;
1389 /* We want to reuse these clusters, so qcow2_alloc_cluster_link_l2()
1390 * should not free them. */
1391 keep_old_clusters = true;
1394 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
1396 if (alloc_cluster_offset == INV_OFFSET) {
1397 /* Allocate, if necessary at a given offset in the image file */
1398 alloc_cluster_offset = *host_offset == INV_OFFSET ? INV_OFFSET :
1399 start_of_cluster(s, *host_offset);
1400 ret = do_alloc_cluster_offset(bs, guest_offset, &alloc_cluster_offset,
1401 &nb_clusters);
1402 if (ret < 0) {
1403 goto fail;
1406 /* Can't extend contiguous allocation */
1407 if (nb_clusters == 0) {
1408 *bytes = 0;
1409 return 0;
1412 assert(alloc_cluster_offset != INV_OFFSET);
1416 * Save info needed for meta data update.
1418 * requested_bytes: Number of bytes from the start of the first
1419 * newly allocated cluster to the end of the (possibly shortened
1420 * before) write request.
1422 * avail_bytes: Number of bytes from the start of the first
1423 * newly allocated to the end of the last newly allocated cluster.
1425 * nb_bytes: The number of bytes from the start of the first
1426 * newly allocated cluster to the end of the area that the write
1427 * request actually writes to (excluding COW at the end)
1429 uint64_t requested_bytes = *bytes + offset_into_cluster(s, guest_offset);
1430 int avail_bytes = MIN(INT_MAX, nb_clusters << s->cluster_bits);
1431 int nb_bytes = MIN(requested_bytes, avail_bytes);
1432 QCowL2Meta *old_m = *m;
1434 *m = g_malloc0(sizeof(**m));
1436 **m = (QCowL2Meta) {
1437 .next = old_m,
1439 .alloc_offset = alloc_cluster_offset,
1440 .offset = start_of_cluster(s, guest_offset),
1441 .nb_clusters = nb_clusters,
1443 .keep_old_clusters = keep_old_clusters,
1445 .cow_start = {
1446 .offset = 0,
1447 .nb_bytes = offset_into_cluster(s, guest_offset),
1449 .cow_end = {
1450 .offset = nb_bytes,
1451 .nb_bytes = avail_bytes - nb_bytes,
1454 qemu_co_queue_init(&(*m)->dependent_requests);
1455 QLIST_INSERT_HEAD(&s->cluster_allocs, *m, next_in_flight);
1457 *host_offset = alloc_cluster_offset + offset_into_cluster(s, guest_offset);
1458 *bytes = MIN(*bytes, nb_bytes - offset_into_cluster(s, guest_offset));
1459 assert(*bytes != 0);
1461 return 1;
1463 fail:
1464 if (*m && (*m)->nb_clusters > 0) {
1465 QLIST_REMOVE(*m, next_in_flight);
1467 return ret;
1471 * alloc_cluster_offset
1473 * For a given offset on the virtual disk, find the cluster offset in qcow2
1474 * file. If the offset is not found, allocate a new cluster.
1476 * If the cluster was already allocated, m->nb_clusters is set to 0 and
1477 * other fields in m are meaningless.
1479 * If the cluster is newly allocated, m->nb_clusters is set to the number of
1480 * contiguous clusters that have been allocated. In this case, the other
1481 * fields of m are valid and contain information about the first allocated
1482 * cluster.
1484 * If the request conflicts with another write request in flight, the coroutine
1485 * is queued and will be reentered when the dependency has completed.
1487 * Return 0 on success and -errno in error cases
1489 int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
1490 unsigned int *bytes, uint64_t *host_offset,
1491 QCowL2Meta **m)
1493 BDRVQcow2State *s = bs->opaque;
1494 uint64_t start, remaining;
1495 uint64_t cluster_offset;
1496 uint64_t cur_bytes;
1497 int ret;
1499 trace_qcow2_alloc_clusters_offset(qemu_coroutine_self(), offset, *bytes);
1501 again:
1502 start = offset;
1503 remaining = *bytes;
1504 cluster_offset = INV_OFFSET;
1505 *host_offset = INV_OFFSET;
1506 cur_bytes = 0;
1507 *m = NULL;
1509 while (true) {
1511 if (*host_offset == INV_OFFSET && cluster_offset != INV_OFFSET) {
1512 *host_offset = start_of_cluster(s, cluster_offset);
1515 assert(remaining >= cur_bytes);
1517 start += cur_bytes;
1518 remaining -= cur_bytes;
1520 if (cluster_offset != INV_OFFSET) {
1521 cluster_offset += cur_bytes;
1524 if (remaining == 0) {
1525 break;
1528 cur_bytes = remaining;
1531 * Now start gathering as many contiguous clusters as possible:
1533 * 1. Check for overlaps with in-flight allocations
1535 * a) Overlap not in the first cluster -> shorten this request and
1536 * let the caller handle the rest in its next loop iteration.
1538 * b) Real overlaps of two requests. Yield and restart the search
1539 * for contiguous clusters (the situation could have changed
1540 * while we were sleeping)
1542 * c) TODO: Request starts in the same cluster as the in-flight
1543 * allocation ends. Shorten the COW of the in-fight allocation,
1544 * set cluster_offset to write to the same cluster and set up
1545 * the right synchronisation between the in-flight request and
1546 * the new one.
1548 ret = handle_dependencies(bs, start, &cur_bytes, m);
1549 if (ret == -EAGAIN) {
1550 /* Currently handle_dependencies() doesn't yield if we already had
1551 * an allocation. If it did, we would have to clean up the L2Meta
1552 * structs before starting over. */
1553 assert(*m == NULL);
1554 goto again;
1555 } else if (ret < 0) {
1556 return ret;
1557 } else if (cur_bytes == 0) {
1558 break;
1559 } else {
1560 /* handle_dependencies() may have decreased cur_bytes (shortened
1561 * the allocations below) so that the next dependency is processed
1562 * correctly during the next loop iteration. */
1566 * 2. Count contiguous COPIED clusters.
1568 ret = handle_copied(bs, start, &cluster_offset, &cur_bytes, m);
1569 if (ret < 0) {
1570 return ret;
1571 } else if (ret) {
1572 continue;
1573 } else if (cur_bytes == 0) {
1574 break;
1578 * 3. If the request still hasn't completed, allocate new clusters,
1579 * considering any cluster_offset of steps 1c or 2.
1581 ret = handle_alloc(bs, start, &cluster_offset, &cur_bytes, m);
1582 if (ret < 0) {
1583 return ret;
1584 } else if (ret) {
1585 continue;
1586 } else {
1587 assert(cur_bytes == 0);
1588 break;
1592 *bytes -= remaining;
1593 assert(*bytes > 0);
1594 assert(*host_offset != INV_OFFSET);
1596 return 0;
1600 * This discards as many clusters of nb_clusters as possible at once (i.e.
1601 * all clusters in the same L2 slice) and returns the number of discarded
1602 * clusters.
1604 static int discard_in_l2_slice(BlockDriverState *bs, uint64_t offset,
1605 uint64_t nb_clusters,
1606 enum qcow2_discard_type type, bool full_discard)
1608 BDRVQcow2State *s = bs->opaque;
1609 uint64_t *l2_slice;
1610 int l2_index;
1611 int ret;
1612 int i;
1614 ret = get_cluster_table(bs, offset, &l2_slice, &l2_index);
1615 if (ret < 0) {
1616 return ret;
1619 /* Limit nb_clusters to one L2 slice */
1620 nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);
1621 assert(nb_clusters <= INT_MAX);
1623 for (i = 0; i < nb_clusters; i++) {
1624 uint64_t old_l2_entry;
1626 old_l2_entry = be64_to_cpu(l2_slice[l2_index + i]);
1629 * If full_discard is false, make sure that a discarded area reads back
1630 * as zeroes for v3 images (we cannot do it for v2 without actually
1631 * writing a zero-filled buffer). We can skip the operation if the
1632 * cluster is already marked as zero, or if it's unallocated and we
1633 * don't have a backing file.
1635 * TODO We might want to use bdrv_block_status(bs) here, but we're
1636 * holding s->lock, so that doesn't work today.
1638 * If full_discard is true, the sector should not read back as zeroes,
1639 * but rather fall through to the backing file.
1641 switch (qcow2_get_cluster_type(bs, old_l2_entry)) {
1642 case QCOW2_CLUSTER_UNALLOCATED:
1643 if (full_discard || !bs->backing) {
1644 continue;
1646 break;
1648 case QCOW2_CLUSTER_ZERO_PLAIN:
1649 if (!full_discard) {
1650 continue;
1652 break;
1654 case QCOW2_CLUSTER_ZERO_ALLOC:
1655 case QCOW2_CLUSTER_NORMAL:
1656 case QCOW2_CLUSTER_COMPRESSED:
1657 break;
1659 default:
1660 abort();
1663 /* First remove L2 entries */
1664 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
1665 if (!full_discard && s->qcow_version >= 3) {
1666 l2_slice[l2_index + i] = cpu_to_be64(QCOW_OFLAG_ZERO);
1667 } else {
1668 l2_slice[l2_index + i] = cpu_to_be64(0);
1671 /* Then decrease the refcount */
1672 qcow2_free_any_clusters(bs, old_l2_entry, 1, type);
1675 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
1677 return nb_clusters;
1680 int qcow2_cluster_discard(BlockDriverState *bs, uint64_t offset,
1681 uint64_t bytes, enum qcow2_discard_type type,
1682 bool full_discard)
1684 BDRVQcow2State *s = bs->opaque;
1685 uint64_t end_offset = offset + bytes;
1686 uint64_t nb_clusters;
1687 int64_t cleared;
1688 int ret;
1690 /* Caller must pass aligned values, except at image end */
1691 assert(QEMU_IS_ALIGNED(offset, s->cluster_size));
1692 assert(QEMU_IS_ALIGNED(end_offset, s->cluster_size) ||
1693 end_offset == bs->total_sectors << BDRV_SECTOR_BITS);
1695 nb_clusters = size_to_clusters(s, bytes);
1697 s->cache_discards = true;
1699 /* Each L2 slice is handled by its own loop iteration */
1700 while (nb_clusters > 0) {
1701 cleared = discard_in_l2_slice(bs, offset, nb_clusters, type,
1702 full_discard);
1703 if (cleared < 0) {
1704 ret = cleared;
1705 goto fail;
1708 nb_clusters -= cleared;
1709 offset += (cleared * s->cluster_size);
1712 ret = 0;
1713 fail:
1714 s->cache_discards = false;
1715 qcow2_process_discards(bs, ret);
1717 return ret;
1721 * This zeroes as many clusters of nb_clusters as possible at once (i.e.
1722 * all clusters in the same L2 slice) and returns the number of zeroed
1723 * clusters.
1725 static int zero_in_l2_slice(BlockDriverState *bs, uint64_t offset,
1726 uint64_t nb_clusters, int flags)
1728 BDRVQcow2State *s = bs->opaque;
1729 uint64_t *l2_slice;
1730 int l2_index;
1731 int ret;
1732 int i;
1733 bool unmap = !!(flags & BDRV_REQ_MAY_UNMAP);
1735 ret = get_cluster_table(bs, offset, &l2_slice, &l2_index);
1736 if (ret < 0) {
1737 return ret;
1740 /* Limit nb_clusters to one L2 slice */
1741 nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);
1742 assert(nb_clusters <= INT_MAX);
1744 for (i = 0; i < nb_clusters; i++) {
1745 uint64_t old_offset;
1746 QCow2ClusterType cluster_type;
1748 old_offset = be64_to_cpu(l2_slice[l2_index + i]);
1751 * Minimize L2 changes if the cluster already reads back as
1752 * zeroes with correct allocation.
1754 cluster_type = qcow2_get_cluster_type(bs, old_offset);
1755 if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN ||
1756 (cluster_type == QCOW2_CLUSTER_ZERO_ALLOC && !unmap)) {
1757 continue;
1760 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
1761 if (cluster_type == QCOW2_CLUSTER_COMPRESSED || unmap) {
1762 l2_slice[l2_index + i] = cpu_to_be64(QCOW_OFLAG_ZERO);
1763 qcow2_free_any_clusters(bs, old_offset, 1, QCOW2_DISCARD_REQUEST);
1764 } else {
1765 l2_slice[l2_index + i] |= cpu_to_be64(QCOW_OFLAG_ZERO);
1769 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
1771 return nb_clusters;
1774 int qcow2_cluster_zeroize(BlockDriverState *bs, uint64_t offset,
1775 uint64_t bytes, int flags)
1777 BDRVQcow2State *s = bs->opaque;
1778 uint64_t end_offset = offset + bytes;
1779 uint64_t nb_clusters;
1780 int64_t cleared;
1781 int ret;
1783 /* If we have to stay in sync with an external data file, zero out
1784 * s->data_file first. */
1785 if (data_file_is_raw(bs)) {
1786 assert(has_data_file(bs));
1787 ret = bdrv_co_pwrite_zeroes(s->data_file, offset, bytes, flags);
1788 if (ret < 0) {
1789 return ret;
1793 /* Caller must pass aligned values, except at image end */
1794 assert(QEMU_IS_ALIGNED(offset, s->cluster_size));
1795 assert(QEMU_IS_ALIGNED(end_offset, s->cluster_size) ||
1796 end_offset == bs->total_sectors << BDRV_SECTOR_BITS);
1798 /* The zero flag is only supported by version 3 and newer */
1799 if (s->qcow_version < 3) {
1800 return -ENOTSUP;
1803 /* Each L2 slice is handled by its own loop iteration */
1804 nb_clusters = size_to_clusters(s, bytes);
1806 s->cache_discards = true;
1808 while (nb_clusters > 0) {
1809 cleared = zero_in_l2_slice(bs, offset, nb_clusters, flags);
1810 if (cleared < 0) {
1811 ret = cleared;
1812 goto fail;
1815 nb_clusters -= cleared;
1816 offset += (cleared * s->cluster_size);
1819 ret = 0;
1820 fail:
1821 s->cache_discards = false;
1822 qcow2_process_discards(bs, ret);
1824 return ret;
1828 * Expands all zero clusters in a specific L1 table (or deallocates them, for
1829 * non-backed non-pre-allocated zero clusters).
1831 * l1_entries and *visited_l1_entries are used to keep track of progress for
1832 * status_cb(). l1_entries contains the total number of L1 entries and
1833 * *visited_l1_entries counts all visited L1 entries.
1835 static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,
1836 int l1_size, int64_t *visited_l1_entries,
1837 int64_t l1_entries,
1838 BlockDriverAmendStatusCB *status_cb,
1839 void *cb_opaque)
1841 BDRVQcow2State *s = bs->opaque;
1842 bool is_active_l1 = (l1_table == s->l1_table);
1843 uint64_t *l2_slice = NULL;
1844 unsigned slice, slice_size2, n_slices;
1845 int ret;
1846 int i, j;
1848 slice_size2 = s->l2_slice_size * sizeof(uint64_t);
1849 n_slices = s->cluster_size / slice_size2;
1851 if (!is_active_l1) {
1852 /* inactive L2 tables require a buffer to be stored in when loading
1853 * them from disk */
1854 l2_slice = qemu_try_blockalign(bs->file->bs, slice_size2);
1855 if (l2_slice == NULL) {
1856 return -ENOMEM;
1860 for (i = 0; i < l1_size; i++) {
1861 uint64_t l2_offset = l1_table[i] & L1E_OFFSET_MASK;
1862 uint64_t l2_refcount;
1864 if (!l2_offset) {
1865 /* unallocated */
1866 (*visited_l1_entries)++;
1867 if (status_cb) {
1868 status_cb(bs, *visited_l1_entries, l1_entries, cb_opaque);
1870 continue;
1873 if (offset_into_cluster(s, l2_offset)) {
1874 qcow2_signal_corruption(bs, true, -1, -1, "L2 table offset %#"
1875 PRIx64 " unaligned (L1 index: %#x)",
1876 l2_offset, i);
1877 ret = -EIO;
1878 goto fail;
1881 ret = qcow2_get_refcount(bs, l2_offset >> s->cluster_bits,
1882 &l2_refcount);
1883 if (ret < 0) {
1884 goto fail;
1887 for (slice = 0; slice < n_slices; slice++) {
1888 uint64_t slice_offset = l2_offset + slice * slice_size2;
1889 bool l2_dirty = false;
1890 if (is_active_l1) {
1891 /* get active L2 tables from cache */
1892 ret = qcow2_cache_get(bs, s->l2_table_cache, slice_offset,
1893 (void **)&l2_slice);
1894 } else {
1895 /* load inactive L2 tables from disk */
1896 ret = bdrv_pread(bs->file, slice_offset, l2_slice, slice_size2);
1898 if (ret < 0) {
1899 goto fail;
1902 for (j = 0; j < s->l2_slice_size; j++) {
1903 uint64_t l2_entry = be64_to_cpu(l2_slice[j]);
1904 int64_t offset = l2_entry & L2E_OFFSET_MASK;
1905 QCow2ClusterType cluster_type =
1906 qcow2_get_cluster_type(bs, l2_entry);
1908 if (cluster_type != QCOW2_CLUSTER_ZERO_PLAIN &&
1909 cluster_type != QCOW2_CLUSTER_ZERO_ALLOC) {
1910 continue;
1913 if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN) {
1914 if (!bs->backing) {
1915 /* not backed; therefore we can simply deallocate the
1916 * cluster */
1917 l2_slice[j] = 0;
1918 l2_dirty = true;
1919 continue;
1922 offset = qcow2_alloc_clusters(bs, s->cluster_size);
1923 if (offset < 0) {
1924 ret = offset;
1925 goto fail;
1928 if (l2_refcount > 1) {
1929 /* For shared L2 tables, set the refcount accordingly
1930 * (it is already 1 and needs to be l2_refcount) */
1931 ret = qcow2_update_cluster_refcount(
1932 bs, offset >> s->cluster_bits,
1933 refcount_diff(1, l2_refcount), false,
1934 QCOW2_DISCARD_OTHER);
1935 if (ret < 0) {
1936 qcow2_free_clusters(bs, offset, s->cluster_size,
1937 QCOW2_DISCARD_OTHER);
1938 goto fail;
1943 if (offset_into_cluster(s, offset)) {
1944 int l2_index = slice * s->l2_slice_size + j;
1945 qcow2_signal_corruption(
1946 bs, true, -1, -1,
1947 "Cluster allocation offset "
1948 "%#" PRIx64 " unaligned (L2 offset: %#"
1949 PRIx64 ", L2 index: %#x)", offset,
1950 l2_offset, l2_index);
1951 if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN) {
1952 qcow2_free_clusters(bs, offset, s->cluster_size,
1953 QCOW2_DISCARD_ALWAYS);
1955 ret = -EIO;
1956 goto fail;
1959 ret = qcow2_pre_write_overlap_check(bs, 0, offset,
1960 s->cluster_size, true);
1961 if (ret < 0) {
1962 if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN) {
1963 qcow2_free_clusters(bs, offset, s->cluster_size,
1964 QCOW2_DISCARD_ALWAYS);
1966 goto fail;
1969 ret = bdrv_pwrite_zeroes(s->data_file, offset,
1970 s->cluster_size, 0);
1971 if (ret < 0) {
1972 if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN) {
1973 qcow2_free_clusters(bs, offset, s->cluster_size,
1974 QCOW2_DISCARD_ALWAYS);
1976 goto fail;
1979 if (l2_refcount == 1) {
1980 l2_slice[j] = cpu_to_be64(offset | QCOW_OFLAG_COPIED);
1981 } else {
1982 l2_slice[j] = cpu_to_be64(offset);
1984 l2_dirty = true;
1987 if (is_active_l1) {
1988 if (l2_dirty) {
1989 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
1990 qcow2_cache_depends_on_flush(s->l2_table_cache);
1992 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
1993 } else {
1994 if (l2_dirty) {
1995 ret = qcow2_pre_write_overlap_check(
1996 bs, QCOW2_OL_INACTIVE_L2 | QCOW2_OL_ACTIVE_L2,
1997 slice_offset, slice_size2, false);
1998 if (ret < 0) {
1999 goto fail;
2002 ret = bdrv_pwrite(bs->file, slice_offset,
2003 l2_slice, slice_size2);
2004 if (ret < 0) {
2005 goto fail;
2011 (*visited_l1_entries)++;
2012 if (status_cb) {
2013 status_cb(bs, *visited_l1_entries, l1_entries, cb_opaque);
2017 ret = 0;
2019 fail:
2020 if (l2_slice) {
2021 if (!is_active_l1) {
2022 qemu_vfree(l2_slice);
2023 } else {
2024 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
2027 return ret;
2031 * For backed images, expands all zero clusters on the image. For non-backed
2032 * images, deallocates all non-pre-allocated zero clusters (and claims the
2033 * allocation for pre-allocated ones). This is important for downgrading to a
2034 * qcow2 version which doesn't yet support metadata zero clusters.
2036 int qcow2_expand_zero_clusters(BlockDriverState *bs,
2037 BlockDriverAmendStatusCB *status_cb,
2038 void *cb_opaque)
2040 BDRVQcow2State *s = bs->opaque;
2041 uint64_t *l1_table = NULL;
2042 int64_t l1_entries = 0, visited_l1_entries = 0;
2043 int ret;
2044 int i, j;
2046 if (status_cb) {
2047 l1_entries = s->l1_size;
2048 for (i = 0; i < s->nb_snapshots; i++) {
2049 l1_entries += s->snapshots[i].l1_size;
2053 ret = expand_zero_clusters_in_l1(bs, s->l1_table, s->l1_size,
2054 &visited_l1_entries, l1_entries,
2055 status_cb, cb_opaque);
2056 if (ret < 0) {
2057 goto fail;
2060 /* Inactive L1 tables may point to active L2 tables - therefore it is
2061 * necessary to flush the L2 table cache before trying to access the L2
2062 * tables pointed to by inactive L1 entries (else we might try to expand
2063 * zero clusters that have already been expanded); furthermore, it is also
2064 * necessary to empty the L2 table cache, since it may contain tables which
2065 * are now going to be modified directly on disk, bypassing the cache.
2066 * qcow2_cache_empty() does both for us. */
2067 ret = qcow2_cache_empty(bs, s->l2_table_cache);
2068 if (ret < 0) {
2069 goto fail;
2072 for (i = 0; i < s->nb_snapshots; i++) {
2073 int l1_size2;
2074 uint64_t *new_l1_table;
2075 Error *local_err = NULL;
2077 ret = qcow2_validate_table(bs, s->snapshots[i].l1_table_offset,
2078 s->snapshots[i].l1_size, sizeof(uint64_t),
2079 QCOW_MAX_L1_SIZE, "Snapshot L1 table",
2080 &local_err);
2081 if (ret < 0) {
2082 error_report_err(local_err);
2083 goto fail;
2086 l1_size2 = s->snapshots[i].l1_size * sizeof(uint64_t);
2087 new_l1_table = g_try_realloc(l1_table, l1_size2);
2089 if (!new_l1_table) {
2090 ret = -ENOMEM;
2091 goto fail;
2094 l1_table = new_l1_table;
2096 ret = bdrv_pread(bs->file, s->snapshots[i].l1_table_offset,
2097 l1_table, l1_size2);
2098 if (ret < 0) {
2099 goto fail;
2102 for (j = 0; j < s->snapshots[i].l1_size; j++) {
2103 be64_to_cpus(&l1_table[j]);
2106 ret = expand_zero_clusters_in_l1(bs, l1_table, s->snapshots[i].l1_size,
2107 &visited_l1_entries, l1_entries,
2108 status_cb, cb_opaque);
2109 if (ret < 0) {
2110 goto fail;
2114 ret = 0;
2116 fail:
2117 g_free(l1_table);
2118 return ret;