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
25 #include "qemu/osdep.h"
28 #include "qapi/error.h"
29 #include "qemu-common.h"
31 #include "qemu/bswap.h"
34 int qcow2_shrink_l1_table(BlockDriverState
*bs
, uint64_t exact_size
)
36 BDRVQcow2State
*s
= bs
->opaque
;
37 int new_l1_size
, i
, ret
;
39 if (exact_size
>= s
->l1_size
) {
43 new_l1_size
= exact_size
;
46 fprintf(stderr
, "shrink l1_table from %d to %d\n", s
->l1_size
, new_l1_size
);
49 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_SHRINK_WRITE_TABLE
);
50 ret
= bdrv_pwrite_zeroes(bs
->file
, s
->l1_table_offset
+
51 new_l1_size
* sizeof(uint64_t),
52 (s
->l1_size
- new_l1_size
) * sizeof(uint64_t), 0);
57 ret
= bdrv_flush(bs
->file
->bs
);
62 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_SHRINK_FREE_L2_CLUSTERS
);
63 for (i
= s
->l1_size
- 1; i
> new_l1_size
- 1; i
--) {
64 if ((s
->l1_table
[i
] & L1E_OFFSET_MASK
) == 0) {
67 qcow2_free_clusters(bs
, s
->l1_table
[i
] & L1E_OFFSET_MASK
,
68 s
->cluster_size
, QCOW2_DISCARD_ALWAYS
);
75 * If the write in the l1_table failed the image may contain a partially
76 * overwritten l1_table. In this case it would be better to clear the
77 * l1_table in memory to avoid possible image corruption.
79 memset(s
->l1_table
+ new_l1_size
, 0,
80 (s
->l1_size
- new_l1_size
) * sizeof(uint64_t));
84 int qcow2_grow_l1_table(BlockDriverState
*bs
, uint64_t min_size
,
87 BDRVQcow2State
*s
= bs
->opaque
;
88 int new_l1_size2
, ret
, i
;
89 uint64_t *new_l1_table
;
90 int64_t old_l1_table_offset
, old_l1_size
;
91 int64_t new_l1_table_offset
, new_l1_size
;
94 if (min_size
<= s
->l1_size
)
97 /* Do a sanity check on min_size before trying to calculate new_l1_size
98 * (this prevents overflows during the while loop for the calculation of
100 if (min_size
> INT_MAX
/ sizeof(uint64_t)) {
105 new_l1_size
= min_size
;
107 /* Bump size up to reduce the number of times we have to grow */
108 new_l1_size
= s
->l1_size
;
109 if (new_l1_size
== 0) {
112 while (min_size
> new_l1_size
) {
113 new_l1_size
= DIV_ROUND_UP(new_l1_size
* 3, 2);
117 QEMU_BUILD_BUG_ON(QCOW_MAX_L1_SIZE
> INT_MAX
);
118 if (new_l1_size
> QCOW_MAX_L1_SIZE
/ sizeof(uint64_t)) {
123 fprintf(stderr
, "grow l1_table from %d to %" PRId64
"\n",
124 s
->l1_size
, new_l1_size
);
127 new_l1_size2
= sizeof(uint64_t) * new_l1_size
;
128 new_l1_table
= qemu_try_blockalign(bs
->file
->bs
,
129 ROUND_UP(new_l1_size2
, 512));
130 if (new_l1_table
== NULL
) {
133 memset(new_l1_table
, 0, ROUND_UP(new_l1_size2
, 512));
136 memcpy(new_l1_table
, s
->l1_table
, s
->l1_size
* sizeof(uint64_t));
139 /* write new table (align to cluster) */
140 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_GROW_ALLOC_TABLE
);
141 new_l1_table_offset
= qcow2_alloc_clusters(bs
, new_l1_size2
);
142 if (new_l1_table_offset
< 0) {
143 qemu_vfree(new_l1_table
);
144 return new_l1_table_offset
;
147 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
152 /* the L1 position has not yet been updated, so these clusters must
153 * indeed be completely free */
154 ret
= qcow2_pre_write_overlap_check(bs
, 0, new_l1_table_offset
,
155 new_l1_size2
, false);
160 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_GROW_WRITE_TABLE
);
161 for(i
= 0; i
< s
->l1_size
; i
++)
162 new_l1_table
[i
] = cpu_to_be64(new_l1_table
[i
]);
163 ret
= bdrv_pwrite_sync(bs
->file
, new_l1_table_offset
,
164 new_l1_table
, new_l1_size2
);
167 for(i
= 0; i
< s
->l1_size
; i
++)
168 new_l1_table
[i
] = be64_to_cpu(new_l1_table
[i
]);
171 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_GROW_ACTIVATE_TABLE
);
172 stl_be_p(data
, new_l1_size
);
173 stq_be_p(data
+ 4, new_l1_table_offset
);
174 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, l1_size
),
179 qemu_vfree(s
->l1_table
);
180 old_l1_table_offset
= s
->l1_table_offset
;
181 s
->l1_table_offset
= new_l1_table_offset
;
182 s
->l1_table
= new_l1_table
;
183 old_l1_size
= s
->l1_size
;
184 s
->l1_size
= new_l1_size
;
185 qcow2_free_clusters(bs
, old_l1_table_offset
, old_l1_size
* sizeof(uint64_t),
186 QCOW2_DISCARD_OTHER
);
189 qemu_vfree(new_l1_table
);
190 qcow2_free_clusters(bs
, new_l1_table_offset
, new_l1_size2
,
191 QCOW2_DISCARD_OTHER
);
198 * @bs: The BlockDriverState
199 * @offset: A guest offset, used to calculate what slice of the L2
201 * @l2_offset: Offset to the L2 table in the image file.
202 * @l2_slice: Location to store the pointer to the L2 slice.
204 * Loads a L2 slice into memory (L2 slices are the parts of L2 tables
205 * that are loaded by the qcow2 cache). If the slice is in the cache,
206 * the cache is used; otherwise the L2 slice is loaded from the image
209 static int l2_load(BlockDriverState
*bs
, uint64_t offset
,
210 uint64_t l2_offset
, uint64_t **l2_slice
)
212 BDRVQcow2State
*s
= bs
->opaque
;
213 int start_of_slice
= sizeof(uint64_t) *
214 (offset_to_l2_index(s
, offset
) - offset_to_l2_slice_index(s
, offset
));
216 return qcow2_cache_get(bs
, s
->l2_table_cache
, l2_offset
+ start_of_slice
,
221 * Writes one sector of the L1 table to the disk (can't update single entries
222 * and we really don't want bdrv_pread to perform a read-modify-write)
224 #define L1_ENTRIES_PER_SECTOR (512 / 8)
225 int qcow2_write_l1_entry(BlockDriverState
*bs
, int l1_index
)
227 BDRVQcow2State
*s
= bs
->opaque
;
228 uint64_t buf
[L1_ENTRIES_PER_SECTOR
] = { 0 };
232 l1_start_index
= l1_index
& ~(L1_ENTRIES_PER_SECTOR
- 1);
233 for (i
= 0; i
< L1_ENTRIES_PER_SECTOR
&& l1_start_index
+ i
< s
->l1_size
;
236 buf
[i
] = cpu_to_be64(s
->l1_table
[l1_start_index
+ i
]);
239 ret
= qcow2_pre_write_overlap_check(bs
, QCOW2_OL_ACTIVE_L1
,
240 s
->l1_table_offset
+ 8 * l1_start_index
, sizeof(buf
), false);
245 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_UPDATE
);
246 ret
= bdrv_pwrite_sync(bs
->file
,
247 s
->l1_table_offset
+ 8 * l1_start_index
,
259 * Allocate a new l2 entry in the file. If l1_index points to an already
260 * used entry in the L2 table (i.e. we are doing a copy on write for the L2
261 * table) copy the contents of the old L2 table into the newly allocated one.
262 * Otherwise the new table is initialized with zeros.
266 static int l2_allocate(BlockDriverState
*bs
, int l1_index
)
268 BDRVQcow2State
*s
= bs
->opaque
;
269 uint64_t old_l2_offset
;
270 uint64_t *l2_slice
= NULL
;
271 unsigned slice
, slice_size2
, n_slices
;
275 old_l2_offset
= s
->l1_table
[l1_index
];
277 trace_qcow2_l2_allocate(bs
, l1_index
);
279 /* allocate a new l2 entry */
281 l2_offset
= qcow2_alloc_clusters(bs
, s
->l2_size
* sizeof(uint64_t));
287 /* The offset must fit in the offset field of the L1 table entry */
288 assert((l2_offset
& L1E_OFFSET_MASK
) == l2_offset
);
290 /* If we're allocating the table at offset 0 then something is wrong */
291 if (l2_offset
== 0) {
292 qcow2_signal_corruption(bs
, true, -1, -1, "Preventing invalid "
293 "allocation of L2 table at offset 0");
298 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
303 /* allocate a new entry in the l2 cache */
305 slice_size2
= s
->l2_slice_size
* sizeof(uint64_t);
306 n_slices
= s
->cluster_size
/ slice_size2
;
308 trace_qcow2_l2_allocate_get_empty(bs
, l1_index
);
309 for (slice
= 0; slice
< n_slices
; slice
++) {
310 ret
= qcow2_cache_get_empty(bs
, s
->l2_table_cache
,
311 l2_offset
+ slice
* slice_size2
,
312 (void **) &l2_slice
);
317 if ((old_l2_offset
& L1E_OFFSET_MASK
) == 0) {
318 /* if there was no old l2 table, clear the new slice */
319 memset(l2_slice
, 0, slice_size2
);
322 uint64_t old_l2_slice_offset
=
323 (old_l2_offset
& L1E_OFFSET_MASK
) + slice
* slice_size2
;
325 /* if there was an old l2 table, read a slice from the disk */
326 BLKDBG_EVENT(bs
->file
, BLKDBG_L2_ALLOC_COW_READ
);
327 ret
= qcow2_cache_get(bs
, s
->l2_table_cache
, old_l2_slice_offset
,
328 (void **) &old_slice
);
333 memcpy(l2_slice
, old_slice
, slice_size2
);
335 qcow2_cache_put(s
->l2_table_cache
, (void **) &old_slice
);
338 /* write the l2 slice to the file */
339 BLKDBG_EVENT(bs
->file
, BLKDBG_L2_ALLOC_WRITE
);
341 trace_qcow2_l2_allocate_write_l2(bs
, l1_index
);
342 qcow2_cache_entry_mark_dirty(s
->l2_table_cache
, l2_slice
);
343 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
346 ret
= qcow2_cache_flush(bs
, s
->l2_table_cache
);
351 /* update the L1 entry */
352 trace_qcow2_l2_allocate_write_l1(bs
, l1_index
);
353 s
->l1_table
[l1_index
] = l2_offset
| QCOW_OFLAG_COPIED
;
354 ret
= qcow2_write_l1_entry(bs
, l1_index
);
359 trace_qcow2_l2_allocate_done(bs
, l1_index
, 0);
363 trace_qcow2_l2_allocate_done(bs
, l1_index
, ret
);
364 if (l2_slice
!= NULL
) {
365 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
367 s
->l1_table
[l1_index
] = old_l2_offset
;
369 qcow2_free_clusters(bs
, l2_offset
, s
->l2_size
* sizeof(uint64_t),
370 QCOW2_DISCARD_ALWAYS
);
376 * Checks how many clusters in a given L2 slice are contiguous in the image
377 * file. As soon as one of the flags in the bitmask stop_flags changes compared
378 * to the first cluster, the search is stopped and the cluster is not counted
379 * as contiguous. (This allows it, for example, to stop at the first compressed
380 * cluster which may require a different handling)
382 static int count_contiguous_clusters(BlockDriverState
*bs
, int nb_clusters
,
383 int cluster_size
, uint64_t *l2_slice
, uint64_t stop_flags
)
386 QCow2ClusterType first_cluster_type
;
387 uint64_t mask
= stop_flags
| L2E_OFFSET_MASK
| QCOW_OFLAG_COMPRESSED
;
388 uint64_t first_entry
= be64_to_cpu(l2_slice
[0]);
389 uint64_t offset
= first_entry
& mask
;
391 first_cluster_type
= qcow2_get_cluster_type(bs
, first_entry
);
392 if (first_cluster_type
== QCOW2_CLUSTER_UNALLOCATED
) {
396 /* must be allocated */
397 assert(first_cluster_type
== QCOW2_CLUSTER_NORMAL
||
398 first_cluster_type
== QCOW2_CLUSTER_ZERO_ALLOC
);
400 for (i
= 0; i
< nb_clusters
; i
++) {
401 uint64_t l2_entry
= be64_to_cpu(l2_slice
[i
]) & mask
;
402 if (offset
+ (uint64_t) i
* cluster_size
!= l2_entry
) {
411 * Checks how many consecutive unallocated clusters in a given L2
412 * slice have the same cluster type.
414 static int count_contiguous_clusters_unallocated(BlockDriverState
*bs
,
417 QCow2ClusterType wanted_type
)
421 assert(wanted_type
== QCOW2_CLUSTER_ZERO_PLAIN
||
422 wanted_type
== QCOW2_CLUSTER_UNALLOCATED
);
423 for (i
= 0; i
< nb_clusters
; i
++) {
424 uint64_t entry
= be64_to_cpu(l2_slice
[i
]);
425 QCow2ClusterType type
= qcow2_get_cluster_type(bs
, entry
);
427 if (type
!= wanted_type
) {
435 static int coroutine_fn
do_perform_cow_read(BlockDriverState
*bs
,
436 uint64_t src_cluster_offset
,
437 unsigned offset_in_cluster
,
442 if (qiov
->size
== 0) {
446 BLKDBG_EVENT(bs
->file
, BLKDBG_COW_READ
);
452 /* Call .bdrv_co_readv() directly instead of using the public block-layer
453 * interface. This avoids double I/O throttling and request tracking,
454 * which can lead to deadlock when block layer copy-on-read is enabled.
456 ret
= bs
->drv
->bdrv_co_preadv(bs
, src_cluster_offset
+ offset_in_cluster
,
457 qiov
->size
, qiov
, 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
,
472 if (bytes
&& bs
->encrypted
) {
473 BDRVQcow2State
*s
= bs
->opaque
;
474 assert((offset_in_cluster
& ~BDRV_SECTOR_MASK
) == 0);
475 assert((bytes
& ~BDRV_SECTOR_MASK
) == 0);
477 if (qcow2_co_encrypt(bs
, cluster_offset
,
478 src_cluster_offset
+ offset_in_cluster
,
479 buffer
, bytes
) < 0) {
486 static int coroutine_fn
do_perform_cow_write(BlockDriverState
*bs
,
487 uint64_t cluster_offset
,
488 unsigned offset_in_cluster
,
491 BDRVQcow2State
*s
= bs
->opaque
;
494 if (qiov
->size
== 0) {
498 ret
= qcow2_pre_write_overlap_check(bs
, 0,
499 cluster_offset
+ offset_in_cluster
, qiov
->size
, true);
504 BLKDBG_EVENT(bs
->file
, BLKDBG_COW_WRITE
);
505 ret
= bdrv_co_pwritev(s
->data_file
, cluster_offset
+ offset_in_cluster
,
506 qiov
->size
, qiov
, 0);
518 * For a given offset of the virtual disk, find the cluster type and offset in
519 * the qcow2 file. The offset is stored in *cluster_offset.
521 * On entry, *bytes is the maximum number of contiguous bytes starting at
522 * offset that we are interested in.
524 * On exit, *bytes is the number of bytes starting at offset that have the same
525 * cluster type and (if applicable) are stored contiguously in the image file.
526 * Compressed clusters are always returned one by one.
528 * Returns the cluster type (QCOW2_CLUSTER_*) on success, -errno in error
531 int qcow2_get_cluster_offset(BlockDriverState
*bs
, uint64_t offset
,
532 unsigned int *bytes
, uint64_t *cluster_offset
)
534 BDRVQcow2State
*s
= bs
->opaque
;
535 unsigned int l2_index
;
536 uint64_t l1_index
, l2_offset
, *l2_slice
;
538 unsigned int offset_in_cluster
;
539 uint64_t bytes_available
, bytes_needed
, nb_clusters
;
540 QCow2ClusterType type
;
543 offset_in_cluster
= offset_into_cluster(s
, offset
);
544 bytes_needed
= (uint64_t) *bytes
+ offset_in_cluster
;
546 /* compute how many bytes there are between the start of the cluster
547 * containing offset and the end of the l2 slice that contains
548 * the entry pointing to it */
550 ((uint64_t) (s
->l2_slice_size
- offset_to_l2_slice_index(s
, offset
)))
553 if (bytes_needed
> bytes_available
) {
554 bytes_needed
= bytes_available
;
559 /* seek to the l2 offset in the l1 table */
561 l1_index
= offset_to_l1_index(s
, offset
);
562 if (l1_index
>= s
->l1_size
) {
563 type
= QCOW2_CLUSTER_UNALLOCATED
;
567 l2_offset
= s
->l1_table
[l1_index
] & L1E_OFFSET_MASK
;
569 type
= QCOW2_CLUSTER_UNALLOCATED
;
573 if (offset_into_cluster(s
, l2_offset
)) {
574 qcow2_signal_corruption(bs
, true, -1, -1, "L2 table offset %#" PRIx64
575 " unaligned (L1 index: %#" PRIx64
")",
576 l2_offset
, l1_index
);
580 /* load the l2 slice in memory */
582 ret
= l2_load(bs
, offset
, l2_offset
, &l2_slice
);
587 /* find the cluster offset for the given disk offset */
589 l2_index
= offset_to_l2_slice_index(s
, offset
);
590 *cluster_offset
= be64_to_cpu(l2_slice
[l2_index
]);
592 nb_clusters
= size_to_clusters(s
, bytes_needed
);
593 /* bytes_needed <= *bytes + offset_in_cluster, both of which are unsigned
594 * integers; the minimum cluster size is 512, so this assertion is always
596 assert(nb_clusters
<= INT_MAX
);
598 type
= qcow2_get_cluster_type(bs
, *cluster_offset
);
599 if (s
->qcow_version
< 3 && (type
== QCOW2_CLUSTER_ZERO_PLAIN
||
600 type
== QCOW2_CLUSTER_ZERO_ALLOC
)) {
601 qcow2_signal_corruption(bs
, true, -1, -1, "Zero cluster entry found"
602 " in pre-v3 image (L2 offset: %#" PRIx64
603 ", L2 index: %#x)", l2_offset
, l2_index
);
608 case QCOW2_CLUSTER_COMPRESSED
:
609 if (has_data_file(bs
)) {
610 qcow2_signal_corruption(bs
, true, -1, -1, "Compressed cluster "
611 "entry found in image with external data "
612 "file (L2 offset: %#" PRIx64
", L2 index: "
613 "%#x)", l2_offset
, l2_index
);
617 /* Compressed clusters can only be processed one by one */
619 *cluster_offset
&= L2E_COMPRESSED_OFFSET_SIZE_MASK
;
621 case QCOW2_CLUSTER_ZERO_PLAIN
:
622 case QCOW2_CLUSTER_UNALLOCATED
:
623 /* how many empty clusters ? */
624 c
= count_contiguous_clusters_unallocated(bs
, nb_clusters
,
625 &l2_slice
[l2_index
], type
);
628 case QCOW2_CLUSTER_ZERO_ALLOC
:
629 case QCOW2_CLUSTER_NORMAL
:
630 /* how many allocated clusters ? */
631 c
= count_contiguous_clusters(bs
, nb_clusters
, s
->cluster_size
,
632 &l2_slice
[l2_index
], QCOW_OFLAG_ZERO
);
633 *cluster_offset
&= L2E_OFFSET_MASK
;
634 if (offset_into_cluster(s
, *cluster_offset
)) {
635 qcow2_signal_corruption(bs
, true, -1, -1,
636 "Cluster allocation offset %#"
637 PRIx64
" unaligned (L2 offset: %#" PRIx64
638 ", L2 index: %#x)", *cluster_offset
,
639 l2_offset
, l2_index
);
643 if (has_data_file(bs
) && *cluster_offset
!= offset
- offset_in_cluster
)
645 qcow2_signal_corruption(bs
, true, -1, -1,
646 "External data file host cluster offset %#"
647 PRIx64
" does not match guest cluster "
649 ", L2 index: %#x)", *cluster_offset
,
650 offset
- offset_in_cluster
, l2_index
);
659 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
661 bytes_available
= (int64_t)c
* s
->cluster_size
;
664 if (bytes_available
> bytes_needed
) {
665 bytes_available
= bytes_needed
;
668 /* bytes_available <= bytes_needed <= *bytes + offset_in_cluster;
669 * subtracting offset_in_cluster will therefore definitely yield something
670 * not exceeding UINT_MAX */
671 assert(bytes_available
- offset_in_cluster
<= UINT_MAX
);
672 *bytes
= bytes_available
- offset_in_cluster
;
677 qcow2_cache_put(s
->l2_table_cache
, (void **)&l2_slice
);
684 * for a given disk offset, load (and allocate if needed)
685 * the appropriate slice of its l2 table.
687 * the cluster index in the l2 slice is given to the caller.
689 * Returns 0 on success, -errno in failure case
691 static int get_cluster_table(BlockDriverState
*bs
, uint64_t offset
,
692 uint64_t **new_l2_slice
,
695 BDRVQcow2State
*s
= bs
->opaque
;
696 unsigned int l2_index
;
697 uint64_t l1_index
, l2_offset
;
698 uint64_t *l2_slice
= NULL
;
701 /* seek to the l2 offset in the l1 table */
703 l1_index
= offset_to_l1_index(s
, offset
);
704 if (l1_index
>= s
->l1_size
) {
705 ret
= qcow2_grow_l1_table(bs
, l1_index
+ 1, false);
711 assert(l1_index
< s
->l1_size
);
712 l2_offset
= s
->l1_table
[l1_index
] & L1E_OFFSET_MASK
;
713 if (offset_into_cluster(s
, l2_offset
)) {
714 qcow2_signal_corruption(bs
, true, -1, -1, "L2 table offset %#" PRIx64
715 " unaligned (L1 index: %#" PRIx64
")",
716 l2_offset
, l1_index
);
720 if (!(s
->l1_table
[l1_index
] & QCOW_OFLAG_COPIED
)) {
721 /* First allocate a new L2 table (and do COW if needed) */
722 ret
= l2_allocate(bs
, l1_index
);
727 /* Then decrease the refcount of the old table */
729 qcow2_free_clusters(bs
, l2_offset
, s
->l2_size
* sizeof(uint64_t),
730 QCOW2_DISCARD_OTHER
);
733 /* Get the offset of the newly-allocated l2 table */
734 l2_offset
= s
->l1_table
[l1_index
] & L1E_OFFSET_MASK
;
735 assert(offset_into_cluster(s
, l2_offset
) == 0);
738 /* load the l2 slice in memory */
739 ret
= l2_load(bs
, offset
, l2_offset
, &l2_slice
);
744 /* find the cluster offset for the given disk offset */
746 l2_index
= offset_to_l2_slice_index(s
, offset
);
748 *new_l2_slice
= l2_slice
;
749 *new_l2_index
= l2_index
;
755 * alloc_compressed_cluster_offset
757 * For a given offset on the virtual disk, allocate a new compressed cluster
758 * and put the host offset of the cluster into *host_offset. If a cluster is
759 * already allocated at the offset, return an error.
761 * Return 0 on success and -errno in error cases
763 int qcow2_alloc_compressed_cluster_offset(BlockDriverState
*bs
,
766 uint64_t *host_offset
)
768 BDRVQcow2State
*s
= bs
->opaque
;
771 int64_t cluster_offset
;
774 if (has_data_file(bs
)) {
778 ret
= get_cluster_table(bs
, offset
, &l2_slice
, &l2_index
);
783 /* Compression can't overwrite anything. Fail if the cluster was already
785 cluster_offset
= be64_to_cpu(l2_slice
[l2_index
]);
786 if (cluster_offset
& L2E_OFFSET_MASK
) {
787 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
791 cluster_offset
= qcow2_alloc_bytes(bs
, compressed_size
);
792 if (cluster_offset
< 0) {
793 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
794 return cluster_offset
;
798 (cluster_offset
+ compressed_size
- 1) / QCOW2_COMPRESSED_SECTOR_SIZE
-
799 (cluster_offset
/ QCOW2_COMPRESSED_SECTOR_SIZE
);
801 cluster_offset
|= QCOW_OFLAG_COMPRESSED
|
802 ((uint64_t)nb_csectors
<< s
->csize_shift
);
804 /* update L2 table */
806 /* compressed clusters never have the copied flag */
808 BLKDBG_EVENT(bs
->file
, BLKDBG_L2_UPDATE_COMPRESSED
);
809 qcow2_cache_entry_mark_dirty(s
->l2_table_cache
, l2_slice
);
810 l2_slice
[l2_index
] = cpu_to_be64(cluster_offset
);
811 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
813 *host_offset
= cluster_offset
& s
->cluster_offset_mask
;
817 static int perform_cow(BlockDriverState
*bs
, QCowL2Meta
*m
)
819 BDRVQcow2State
*s
= bs
->opaque
;
820 Qcow2COWRegion
*start
= &m
->cow_start
;
821 Qcow2COWRegion
*end
= &m
->cow_end
;
822 unsigned buffer_size
;
823 unsigned data_bytes
= end
->offset
- (start
->offset
+ start
->nb_bytes
);
825 uint8_t *start_buffer
, *end_buffer
;
829 assert(start
->nb_bytes
<= UINT_MAX
- end
->nb_bytes
);
830 assert(start
->nb_bytes
+ end
->nb_bytes
<= UINT_MAX
- data_bytes
);
831 assert(start
->offset
+ start
->nb_bytes
<= end
->offset
);
832 assert(!m
->data_qiov
|| m
->data_qiov
->size
== data_bytes
);
834 if ((start
->nb_bytes
== 0 && end
->nb_bytes
== 0) || m
->skip_cow
) {
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
841 merge_reads
= start
->nb_bytes
&& end
->nb_bytes
&& data_bytes
<= 16384;
843 buffer_size
= start
->nb_bytes
+ data_bytes
+ end
->nb_bytes
;
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
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
857 start_buffer
= qemu_try_blockalign(bs
, buffer_size
);
858 if (start_buffer
== NULL
) {
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
? m
->data_qiov
->niov
: 0));
866 qemu_co_mutex_unlock(&s
->lock
);
867 /* First we read the existing data from both COW regions. We
868 * either read the whole region in one go, or the start and end
869 * regions separately. */
871 qemu_iovec_add(&qiov
, start_buffer
, buffer_size
);
872 ret
= do_perform_cow_read(bs
, m
->offset
, start
->offset
, &qiov
);
874 qemu_iovec_add(&qiov
, start_buffer
, start
->nb_bytes
);
875 ret
= do_perform_cow_read(bs
, m
->offset
, start
->offset
, &qiov
);
880 qemu_iovec_reset(&qiov
);
881 qemu_iovec_add(&qiov
, end_buffer
, end
->nb_bytes
);
882 ret
= do_perform_cow_read(bs
, m
->offset
, end
->offset
, &qiov
);
888 /* Encrypt the data if necessary before writing it */
890 if (!do_perform_cow_encrypt(bs
, m
->offset
, m
->alloc_offset
,
891 start
->offset
, start_buffer
,
893 !do_perform_cow_encrypt(bs
, m
->offset
, m
->alloc_offset
,
894 end
->offset
, end_buffer
, end
->nb_bytes
)) {
900 /* And now we can write everything. If we have the guest data we
901 * can write everything in one single operation */
903 qemu_iovec_reset(&qiov
);
904 if (start
->nb_bytes
) {
905 qemu_iovec_add(&qiov
, start_buffer
, start
->nb_bytes
);
907 qemu_iovec_concat(&qiov
, m
->data_qiov
, 0, data_bytes
);
909 qemu_iovec_add(&qiov
, end_buffer
, end
->nb_bytes
);
911 /* NOTE: we have a write_aio blkdebug event here followed by
912 * a cow_write one in do_perform_cow_write(), but there's only
913 * one single I/O operation */
914 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_AIO
);
915 ret
= do_perform_cow_write(bs
, m
->alloc_offset
, start
->offset
, &qiov
);
917 /* If there's no guest data then write both COW regions separately */
918 qemu_iovec_reset(&qiov
);
919 qemu_iovec_add(&qiov
, start_buffer
, start
->nb_bytes
);
920 ret
= do_perform_cow_write(bs
, m
->alloc_offset
, start
->offset
, &qiov
);
925 qemu_iovec_reset(&qiov
);
926 qemu_iovec_add(&qiov
, end_buffer
, end
->nb_bytes
);
927 ret
= do_perform_cow_write(bs
, m
->alloc_offset
, end
->offset
, &qiov
);
931 qemu_co_mutex_lock(&s
->lock
);
934 * Before we update the L2 table to actually point to the new cluster, we
935 * need to be sure that the refcounts have been increased and COW was
939 qcow2_cache_depends_on_flush(s
->l2_table_cache
);
942 qemu_vfree(start_buffer
);
943 qemu_iovec_destroy(&qiov
);
947 int qcow2_alloc_cluster_link_l2(BlockDriverState
*bs
, QCowL2Meta
*m
)
949 BDRVQcow2State
*s
= bs
->opaque
;
950 int i
, j
= 0, l2_index
, ret
;
951 uint64_t *old_cluster
, *l2_slice
;
952 uint64_t cluster_offset
= m
->alloc_offset
;
954 trace_qcow2_cluster_link_l2(qemu_coroutine_self(), m
->nb_clusters
);
955 assert(m
->nb_clusters
> 0);
957 old_cluster
= g_try_new(uint64_t, m
->nb_clusters
);
958 if (old_cluster
== NULL
) {
963 /* copy content of unmodified sectors */
964 ret
= perform_cow(bs
, m
);
969 /* Update L2 table. */
970 if (s
->use_lazy_refcounts
) {
971 qcow2_mark_dirty(bs
);
973 if (qcow2_need_accurate_refcounts(s
)) {
974 qcow2_cache_set_dependency(bs
, s
->l2_table_cache
,
975 s
->refcount_block_cache
);
978 ret
= get_cluster_table(bs
, m
->offset
, &l2_slice
, &l2_index
);
982 qcow2_cache_entry_mark_dirty(s
->l2_table_cache
, l2_slice
);
984 assert(l2_index
+ m
->nb_clusters
<= s
->l2_slice_size
);
985 for (i
= 0; i
< m
->nb_clusters
; i
++) {
986 /* if two concurrent writes happen to the same unallocated cluster
987 * each write allocates separate cluster and writes data concurrently.
988 * The first one to complete updates l2 table with pointer to its
989 * cluster the second one has to do RMW (which is done above by
990 * perform_cow()), update l2 table with its cluster pointer and free
991 * old cluster. This is what this loop does */
992 if (l2_slice
[l2_index
+ i
] != 0) {
993 old_cluster
[j
++] = l2_slice
[l2_index
+ i
];
996 l2_slice
[l2_index
+ i
] = cpu_to_be64((cluster_offset
+
997 (i
<< s
->cluster_bits
)) | QCOW_OFLAG_COPIED
);
1001 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
1004 * If this was a COW, we need to decrease the refcount of the old cluster.
1006 * Don't discard clusters that reach a refcount of 0 (e.g. compressed
1007 * clusters), the next write will reuse them anyway.
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
);
1018 g_free(old_cluster
);
1023 * Frees the allocated clusters because the request failed and they won't
1024 * actually be linked.
1026 void qcow2_alloc_cluster_abort(BlockDriverState
*bs
, QCowL2Meta
*m
)
1028 BDRVQcow2State
*s
= bs
->opaque
;
1029 qcow2_free_clusters(bs
, m
->alloc_offset
, m
->nb_clusters
<< s
->cluster_bits
,
1030 QCOW2_DISCARD_NEVER
);
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)
1038 static int count_cow_clusters(BlockDriverState
*bs
, int nb_clusters
,
1039 uint64_t *l2_slice
, int l2_index
)
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
);
1047 switch(cluster_type
) {
1048 case QCOW2_CLUSTER_NORMAL
:
1049 if (l2_entry
& QCOW_OFLAG_COPIED
) {
1053 case QCOW2_CLUSTER_UNALLOCATED
:
1054 case QCOW2_CLUSTER_COMPRESSED
:
1055 case QCOW2_CLUSTER_ZERO_PLAIN
:
1056 case QCOW2_CLUSTER_ZERO_ALLOC
:
1064 assert(i
<= nb_clusters
);
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.
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)
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.
1082 static int handle_dependencies(BlockDriverState
*bs
, uint64_t guest_offset
,
1083 uint64_t *cur_bytes
, QCowL2Meta
**m
)
1085 BDRVQcow2State
*s
= bs
->opaque
;
1086 QCowL2Meta
*old_alloc
;
1087 uint64_t bytes
= *cur_bytes
;
1089 QLIST_FOREACH(old_alloc
, &s
->cluster_allocs
, next_in_flight
) {
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
);
1096 if (end
<= old_start
|| start
>= old_end
) {
1097 /* No intersection */
1099 if (start
< old_start
) {
1100 /* Stop at the start of a running allocation */
1101 bytes
= old_start
- start
;
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
) {
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
);
1124 /* Make sure that existing clusters and new allocations are only used up to
1125 * the next dependency if we shortened the request above */
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.
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.
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
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.
1151 * -errno: in error cases
1153 static int handle_copied(BlockDriverState
*bs
, uint64_t guest_offset
,
1154 uint64_t *host_offset
, uint64_t *bytes
, QCowL2Meta
**m
)
1156 BDRVQcow2State
*s
= bs
->opaque
;
1158 uint64_t cluster_offset
;
1160 uint64_t nb_clusters
;
1161 unsigned int keep_clusters
;
1164 trace_qcow2_handle_copied(qemu_coroutine_self(), guest_offset
, *host_offset
,
1167 assert(*host_offset
== INV_OFFSET
|| offset_into_cluster(s
, guest_offset
)
1168 == offset_into_cluster(s
, *host_offset
));
1171 * Calculate the number of clusters to look for. We stop at L2 slice
1172 * boundaries to keep things simple.
1175 size_to_clusters(s
, offset_into_cluster(s
, guest_offset
) + *bytes
);
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
);
1181 /* Find L2 entry for the first involved cluster */
1182 ret
= get_cluster_table(bs
, guest_offset
, &l2_slice
, &l2_index
);
1187 cluster_offset
= be64_to_cpu(l2_slice
[l2_index
]);
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
))
1193 /* If a specific host_offset is required, check it */
1194 bool offset_matches
=
1195 (cluster_offset
& L2E_OFFSET_MASK
) == *host_offset
;
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
,
1206 if (*host_offset
!= INV_OFFSET
&& !offset_matches
) {
1212 /* We keep all QCOW_OFLAG_COPIED 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
);
1219 *bytes
= MIN(*bytes
,
1220 keep_clusters
* s
->cluster_size
1221 - offset_into_cluster(s
, guest_offset
));
1230 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
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 */
1235 *host_offset
= (cluster_offset
& L2E_OFFSET_MASK
)
1236 + offset_into_cluster(s
, guest_offset
);
1243 * Allocates new clusters for the given guest_offset.
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.
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.
1254 * *host_offset is updated to contain the offset into the image file at which
1255 * the first allocated cluster starts.
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.
1261 static int do_alloc_cluster_offset(BlockDriverState
*bs
, uint64_t guest_offset
,
1262 uint64_t *host_offset
, uint64_t *nb_clusters
)
1264 BDRVQcow2State
*s
= bs
->opaque
;
1266 trace_qcow2_do_alloc_clusters_offset(qemu_coroutine_self(), guest_offset
,
1267 *host_offset
, *nb_clusters
);
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
);
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
;
1284 *host_offset
= cluster_offset
;
1287 int64_t ret
= qcow2_alloc_clusters_at(bs
, *host_offset
, *nb_clusters
);
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.
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.
1306 * 0: if no clusters could be allocated. *bytes is set to 0,
1307 * *host_offset is left unchanged.
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.
1314 * -errno: in error cases
1316 static int handle_alloc(BlockDriverState
*bs
, uint64_t guest_offset
,
1317 uint64_t *host_offset
, uint64_t *bytes
, QCowL2Meta
**m
)
1319 BDRVQcow2State
*s
= bs
->opaque
;
1323 uint64_t nb_clusters
;
1325 bool keep_old_clusters
= false;
1327 uint64_t alloc_cluster_offset
= INV_OFFSET
;
1329 trace_qcow2_handle_alloc(qemu_coroutine_self(), guest_offset
, *host_offset
,
1334 * Calculate the number of clusters to look for. We stop at L2 slice
1335 * boundaries to keep things simple.
1338 size_to_clusters(s
, offset_into_cluster(s
, guest_offset
) + *bytes
);
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
);
1344 /* Find L2 entry for the first involved cluster */
1345 ret
= get_cluster_table(bs
, guest_offset
, &l2_slice
, &l2_index
);
1350 entry
= be64_to_cpu(l2_slice
[l2_index
]);
1352 /* For the moment, overwrite compressed clusters one by one */
1353 if (entry
& QCOW_OFLAG_COMPRESSED
) {
1356 nb_clusters
= count_cow_clusters(bs
, nb_clusters
, l2_slice
, l2_index
);
1359 /* This function is only called when there were no non-COW clusters, so if
1360 * we can't find any unallocated or COW clusters either, something is
1361 * wrong with our code. */
1362 assert(nb_clusters
> 0);
1364 if (qcow2_get_cluster_type(bs
, entry
) == QCOW2_CLUSTER_ZERO_ALLOC
&&
1365 (entry
& QCOW_OFLAG_COPIED
) &&
1366 (*host_offset
== INV_OFFSET
||
1367 start_of_cluster(s
, *host_offset
) == (entry
& L2E_OFFSET_MASK
)))
1369 int preallocated_nb_clusters
;
1371 if (offset_into_cluster(s
, entry
& L2E_OFFSET_MASK
)) {
1372 qcow2_signal_corruption(bs
, true, -1, -1, "Preallocated zero "
1373 "cluster offset %#llx unaligned (guest "
1374 "offset: %#" PRIx64
")",
1375 entry
& L2E_OFFSET_MASK
, guest_offset
);
1380 /* Try to reuse preallocated zero clusters; contiguous normal clusters
1381 * would be fine, too, but count_cow_clusters() above has limited
1382 * nb_clusters already to a range of COW clusters */
1383 preallocated_nb_clusters
=
1384 count_contiguous_clusters(bs
, nb_clusters
, s
->cluster_size
,
1385 &l2_slice
[l2_index
], QCOW_OFLAG_COPIED
);
1386 assert(preallocated_nb_clusters
> 0);
1388 nb_clusters
= preallocated_nb_clusters
;
1389 alloc_cluster_offset
= entry
& L2E_OFFSET_MASK
;
1391 /* We want to reuse these clusters, so qcow2_alloc_cluster_link_l2()
1392 * should not free them. */
1393 keep_old_clusters
= true;
1396 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
1398 if (alloc_cluster_offset
== INV_OFFSET
) {
1399 /* Allocate, if necessary at a given offset in the image file */
1400 alloc_cluster_offset
= *host_offset
== INV_OFFSET
? INV_OFFSET
:
1401 start_of_cluster(s
, *host_offset
);
1402 ret
= do_alloc_cluster_offset(bs
, guest_offset
, &alloc_cluster_offset
,
1408 /* Can't extend contiguous allocation */
1409 if (nb_clusters
== 0) {
1414 assert(alloc_cluster_offset
!= INV_OFFSET
);
1418 * Save info needed for meta data update.
1420 * requested_bytes: Number of bytes from the start of the first
1421 * newly allocated cluster to the end of the (possibly shortened
1422 * before) write request.
1424 * avail_bytes: Number of bytes from the start of the first
1425 * newly allocated to the end of the last newly allocated cluster.
1427 * nb_bytes: The number of bytes from the start of the first
1428 * newly allocated cluster to the end of the area that the write
1429 * request actually writes to (excluding COW at the end)
1431 uint64_t requested_bytes
= *bytes
+ offset_into_cluster(s
, guest_offset
);
1432 int avail_bytes
= MIN(INT_MAX
, nb_clusters
<< s
->cluster_bits
);
1433 int nb_bytes
= MIN(requested_bytes
, avail_bytes
);
1434 QCowL2Meta
*old_m
= *m
;
1436 *m
= g_malloc0(sizeof(**m
));
1438 **m
= (QCowL2Meta
) {
1441 .alloc_offset
= alloc_cluster_offset
,
1442 .offset
= start_of_cluster(s
, guest_offset
),
1443 .nb_clusters
= nb_clusters
,
1445 .keep_old_clusters
= keep_old_clusters
,
1449 .nb_bytes
= offset_into_cluster(s
, guest_offset
),
1453 .nb_bytes
= avail_bytes
- nb_bytes
,
1456 qemu_co_queue_init(&(*m
)->dependent_requests
);
1457 QLIST_INSERT_HEAD(&s
->cluster_allocs
, *m
, next_in_flight
);
1459 *host_offset
= alloc_cluster_offset
+ offset_into_cluster(s
, guest_offset
);
1460 *bytes
= MIN(*bytes
, nb_bytes
- offset_into_cluster(s
, guest_offset
));
1461 assert(*bytes
!= 0);
1466 if (*m
&& (*m
)->nb_clusters
> 0) {
1467 QLIST_REMOVE(*m
, next_in_flight
);
1473 * alloc_cluster_offset
1475 * For a given offset on the virtual disk, find the cluster offset in qcow2
1476 * file. If the offset is not found, allocate a new cluster.
1478 * If the cluster was already allocated, m->nb_clusters is set to 0 and
1479 * other fields in m are meaningless.
1481 * If the cluster is newly allocated, m->nb_clusters is set to the number of
1482 * contiguous clusters that have been allocated. In this case, the other
1483 * fields of m are valid and contain information about the first allocated
1486 * If the request conflicts with another write request in flight, the coroutine
1487 * is queued and will be reentered when the dependency has completed.
1489 * Return 0 on success and -errno in error cases
1491 int qcow2_alloc_cluster_offset(BlockDriverState
*bs
, uint64_t offset
,
1492 unsigned int *bytes
, uint64_t *host_offset
,
1495 BDRVQcow2State
*s
= bs
->opaque
;
1496 uint64_t start
, remaining
;
1497 uint64_t cluster_offset
;
1501 trace_qcow2_alloc_clusters_offset(qemu_coroutine_self(), offset
, *bytes
);
1506 cluster_offset
= INV_OFFSET
;
1507 *host_offset
= INV_OFFSET
;
1513 if (*host_offset
== INV_OFFSET
&& cluster_offset
!= INV_OFFSET
) {
1514 *host_offset
= start_of_cluster(s
, cluster_offset
);
1517 assert(remaining
>= cur_bytes
);
1520 remaining
-= cur_bytes
;
1522 if (cluster_offset
!= INV_OFFSET
) {
1523 cluster_offset
+= cur_bytes
;
1526 if (remaining
== 0) {
1530 cur_bytes
= remaining
;
1533 * Now start gathering as many contiguous clusters as possible:
1535 * 1. Check for overlaps with in-flight allocations
1537 * a) Overlap not in the first cluster -> shorten this request and
1538 * let the caller handle the rest in its next loop iteration.
1540 * b) Real overlaps of two requests. Yield and restart the search
1541 * for contiguous clusters (the situation could have changed
1542 * while we were sleeping)
1544 * c) TODO: Request starts in the same cluster as the in-flight
1545 * allocation ends. Shorten the COW of the in-fight allocation,
1546 * set cluster_offset to write to the same cluster and set up
1547 * the right synchronisation between the in-flight request and
1550 ret
= handle_dependencies(bs
, start
, &cur_bytes
, m
);
1551 if (ret
== -EAGAIN
) {
1552 /* Currently handle_dependencies() doesn't yield if we already had
1553 * an allocation. If it did, we would have to clean up the L2Meta
1554 * structs before starting over. */
1557 } else if (ret
< 0) {
1559 } else if (cur_bytes
== 0) {
1562 /* handle_dependencies() may have decreased cur_bytes (shortened
1563 * the allocations below) so that the next dependency is processed
1564 * correctly during the next loop iteration. */
1568 * 2. Count contiguous COPIED clusters.
1570 ret
= handle_copied(bs
, start
, &cluster_offset
, &cur_bytes
, m
);
1575 } else if (cur_bytes
== 0) {
1580 * 3. If the request still hasn't completed, allocate new clusters,
1581 * considering any cluster_offset of steps 1c or 2.
1583 ret
= handle_alloc(bs
, start
, &cluster_offset
, &cur_bytes
, m
);
1589 assert(cur_bytes
== 0);
1594 *bytes
-= remaining
;
1596 assert(*host_offset
!= INV_OFFSET
);
1602 * This discards as many clusters of nb_clusters as possible at once (i.e.
1603 * all clusters in the same L2 slice) and returns the number of discarded
1606 static int discard_in_l2_slice(BlockDriverState
*bs
, uint64_t offset
,
1607 uint64_t nb_clusters
,
1608 enum qcow2_discard_type type
, bool full_discard
)
1610 BDRVQcow2State
*s
= bs
->opaque
;
1616 ret
= get_cluster_table(bs
, offset
, &l2_slice
, &l2_index
);
1621 /* Limit nb_clusters to one L2 slice */
1622 nb_clusters
= MIN(nb_clusters
, s
->l2_slice_size
- l2_index
);
1623 assert(nb_clusters
<= INT_MAX
);
1625 for (i
= 0; i
< nb_clusters
; i
++) {
1626 uint64_t old_l2_entry
;
1628 old_l2_entry
= be64_to_cpu(l2_slice
[l2_index
+ i
]);
1631 * If full_discard is false, make sure that a discarded area reads back
1632 * as zeroes for v3 images (we cannot do it for v2 without actually
1633 * writing a zero-filled buffer). We can skip the operation if the
1634 * cluster is already marked as zero, or if it's unallocated and we
1635 * don't have a backing file.
1637 * TODO We might want to use bdrv_block_status(bs) here, but we're
1638 * holding s->lock, so that doesn't work today.
1640 * If full_discard is true, the sector should not read back as zeroes,
1641 * but rather fall through to the backing file.
1643 switch (qcow2_get_cluster_type(bs
, old_l2_entry
)) {
1644 case QCOW2_CLUSTER_UNALLOCATED
:
1645 if (full_discard
|| !bs
->backing
) {
1650 case QCOW2_CLUSTER_ZERO_PLAIN
:
1651 if (!full_discard
) {
1656 case QCOW2_CLUSTER_ZERO_ALLOC
:
1657 case QCOW2_CLUSTER_NORMAL
:
1658 case QCOW2_CLUSTER_COMPRESSED
:
1665 /* First remove L2 entries */
1666 qcow2_cache_entry_mark_dirty(s
->l2_table_cache
, l2_slice
);
1667 if (!full_discard
&& s
->qcow_version
>= 3) {
1668 l2_slice
[l2_index
+ i
] = cpu_to_be64(QCOW_OFLAG_ZERO
);
1670 l2_slice
[l2_index
+ i
] = cpu_to_be64(0);
1673 /* Then decrease the refcount */
1674 qcow2_free_any_clusters(bs
, old_l2_entry
, 1, type
);
1677 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
1682 int qcow2_cluster_discard(BlockDriverState
*bs
, uint64_t offset
,
1683 uint64_t bytes
, enum qcow2_discard_type type
,
1686 BDRVQcow2State
*s
= bs
->opaque
;
1687 uint64_t end_offset
= offset
+ bytes
;
1688 uint64_t nb_clusters
;
1692 /* Caller must pass aligned values, except at image end */
1693 assert(QEMU_IS_ALIGNED(offset
, s
->cluster_size
));
1694 assert(QEMU_IS_ALIGNED(end_offset
, s
->cluster_size
) ||
1695 end_offset
== bs
->total_sectors
<< BDRV_SECTOR_BITS
);
1697 nb_clusters
= size_to_clusters(s
, bytes
);
1699 s
->cache_discards
= true;
1701 /* Each L2 slice is handled by its own loop iteration */
1702 while (nb_clusters
> 0) {
1703 cleared
= discard_in_l2_slice(bs
, offset
, nb_clusters
, type
,
1710 nb_clusters
-= cleared
;
1711 offset
+= (cleared
* s
->cluster_size
);
1716 s
->cache_discards
= false;
1717 qcow2_process_discards(bs
, ret
);
1723 * This zeroes as many clusters of nb_clusters as possible at once (i.e.
1724 * all clusters in the same L2 slice) and returns the number of zeroed
1727 static int zero_in_l2_slice(BlockDriverState
*bs
, uint64_t offset
,
1728 uint64_t nb_clusters
, int flags
)
1730 BDRVQcow2State
*s
= bs
->opaque
;
1735 bool unmap
= !!(flags
& BDRV_REQ_MAY_UNMAP
);
1737 ret
= get_cluster_table(bs
, offset
, &l2_slice
, &l2_index
);
1742 /* Limit nb_clusters to one L2 slice */
1743 nb_clusters
= MIN(nb_clusters
, s
->l2_slice_size
- l2_index
);
1744 assert(nb_clusters
<= INT_MAX
);
1746 for (i
= 0; i
< nb_clusters
; i
++) {
1747 uint64_t old_offset
;
1748 QCow2ClusterType cluster_type
;
1750 old_offset
= be64_to_cpu(l2_slice
[l2_index
+ i
]);
1753 * Minimize L2 changes if the cluster already reads back as
1754 * zeroes with correct allocation.
1756 cluster_type
= qcow2_get_cluster_type(bs
, old_offset
);
1757 if (cluster_type
== QCOW2_CLUSTER_ZERO_PLAIN
||
1758 (cluster_type
== QCOW2_CLUSTER_ZERO_ALLOC
&& !unmap
)) {
1762 qcow2_cache_entry_mark_dirty(s
->l2_table_cache
, l2_slice
);
1763 if (cluster_type
== QCOW2_CLUSTER_COMPRESSED
|| unmap
) {
1764 l2_slice
[l2_index
+ i
] = cpu_to_be64(QCOW_OFLAG_ZERO
);
1765 qcow2_free_any_clusters(bs
, old_offset
, 1, QCOW2_DISCARD_REQUEST
);
1767 l2_slice
[l2_index
+ i
] |= cpu_to_be64(QCOW_OFLAG_ZERO
);
1771 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
1776 int qcow2_cluster_zeroize(BlockDriverState
*bs
, uint64_t offset
,
1777 uint64_t bytes
, int flags
)
1779 BDRVQcow2State
*s
= bs
->opaque
;
1780 uint64_t end_offset
= offset
+ bytes
;
1781 uint64_t nb_clusters
;
1785 /* If we have to stay in sync with an external data file, zero out
1786 * s->data_file first. */
1787 if (data_file_is_raw(bs
)) {
1788 assert(has_data_file(bs
));
1789 ret
= bdrv_co_pwrite_zeroes(s
->data_file
, offset
, bytes
, flags
);
1795 /* Caller must pass aligned values, except at image end */
1796 assert(QEMU_IS_ALIGNED(offset
, s
->cluster_size
));
1797 assert(QEMU_IS_ALIGNED(end_offset
, s
->cluster_size
) ||
1798 end_offset
== bs
->total_sectors
<< BDRV_SECTOR_BITS
);
1800 /* The zero flag is only supported by version 3 and newer */
1801 if (s
->qcow_version
< 3) {
1805 /* Each L2 slice is handled by its own loop iteration */
1806 nb_clusters
= size_to_clusters(s
, bytes
);
1808 s
->cache_discards
= true;
1810 while (nb_clusters
> 0) {
1811 cleared
= zero_in_l2_slice(bs
, offset
, nb_clusters
, flags
);
1817 nb_clusters
-= cleared
;
1818 offset
+= (cleared
* s
->cluster_size
);
1823 s
->cache_discards
= false;
1824 qcow2_process_discards(bs
, ret
);
1830 * Expands all zero clusters in a specific L1 table (or deallocates them, for
1831 * non-backed non-pre-allocated zero clusters).
1833 * l1_entries and *visited_l1_entries are used to keep track of progress for
1834 * status_cb(). l1_entries contains the total number of L1 entries and
1835 * *visited_l1_entries counts all visited L1 entries.
1837 static int expand_zero_clusters_in_l1(BlockDriverState
*bs
, uint64_t *l1_table
,
1838 int l1_size
, int64_t *visited_l1_entries
,
1840 BlockDriverAmendStatusCB
*status_cb
,
1843 BDRVQcow2State
*s
= bs
->opaque
;
1844 bool is_active_l1
= (l1_table
== s
->l1_table
);
1845 uint64_t *l2_slice
= NULL
;
1846 unsigned slice
, slice_size2
, n_slices
;
1850 slice_size2
= s
->l2_slice_size
* sizeof(uint64_t);
1851 n_slices
= s
->cluster_size
/ slice_size2
;
1853 if (!is_active_l1
) {
1854 /* inactive L2 tables require a buffer to be stored in when loading
1856 l2_slice
= qemu_try_blockalign(bs
->file
->bs
, slice_size2
);
1857 if (l2_slice
== NULL
) {
1862 for (i
= 0; i
< l1_size
; i
++) {
1863 uint64_t l2_offset
= l1_table
[i
] & L1E_OFFSET_MASK
;
1864 uint64_t l2_refcount
;
1868 (*visited_l1_entries
)++;
1870 status_cb(bs
, *visited_l1_entries
, l1_entries
, cb_opaque
);
1875 if (offset_into_cluster(s
, l2_offset
)) {
1876 qcow2_signal_corruption(bs
, true, -1, -1, "L2 table offset %#"
1877 PRIx64
" unaligned (L1 index: %#x)",
1883 ret
= qcow2_get_refcount(bs
, l2_offset
>> s
->cluster_bits
,
1889 for (slice
= 0; slice
< n_slices
; slice
++) {
1890 uint64_t slice_offset
= l2_offset
+ slice
* slice_size2
;
1891 bool l2_dirty
= false;
1893 /* get active L2 tables from cache */
1894 ret
= qcow2_cache_get(bs
, s
->l2_table_cache
, slice_offset
,
1895 (void **)&l2_slice
);
1897 /* load inactive L2 tables from disk */
1898 ret
= bdrv_pread(bs
->file
, slice_offset
, l2_slice
, slice_size2
);
1904 for (j
= 0; j
< s
->l2_slice_size
; j
++) {
1905 uint64_t l2_entry
= be64_to_cpu(l2_slice
[j
]);
1906 int64_t offset
= l2_entry
& L2E_OFFSET_MASK
;
1907 QCow2ClusterType cluster_type
=
1908 qcow2_get_cluster_type(bs
, l2_entry
);
1910 if (cluster_type
!= QCOW2_CLUSTER_ZERO_PLAIN
&&
1911 cluster_type
!= QCOW2_CLUSTER_ZERO_ALLOC
) {
1915 if (cluster_type
== QCOW2_CLUSTER_ZERO_PLAIN
) {
1917 /* not backed; therefore we can simply deallocate the
1924 offset
= qcow2_alloc_clusters(bs
, s
->cluster_size
);
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
);
1938 qcow2_free_clusters(bs
, offset
, s
->cluster_size
,
1939 QCOW2_DISCARD_OTHER
);
1945 if (offset_into_cluster(s
, offset
)) {
1946 int l2_index
= slice
* s
->l2_slice_size
+ j
;
1947 qcow2_signal_corruption(
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
);
1961 ret
= qcow2_pre_write_overlap_check(bs
, 0, offset
,
1962 s
->cluster_size
, true);
1964 if (cluster_type
== QCOW2_CLUSTER_ZERO_PLAIN
) {
1965 qcow2_free_clusters(bs
, offset
, s
->cluster_size
,
1966 QCOW2_DISCARD_ALWAYS
);
1971 ret
= bdrv_pwrite_zeroes(s
->data_file
, offset
,
1972 s
->cluster_size
, 0);
1974 if (cluster_type
== QCOW2_CLUSTER_ZERO_PLAIN
) {
1975 qcow2_free_clusters(bs
, offset
, s
->cluster_size
,
1976 QCOW2_DISCARD_ALWAYS
);
1981 if (l2_refcount
== 1) {
1982 l2_slice
[j
] = cpu_to_be64(offset
| QCOW_OFLAG_COPIED
);
1984 l2_slice
[j
] = cpu_to_be64(offset
);
1991 qcow2_cache_entry_mark_dirty(s
->l2_table_cache
, l2_slice
);
1992 qcow2_cache_depends_on_flush(s
->l2_table_cache
);
1994 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
1997 ret
= qcow2_pre_write_overlap_check(
1998 bs
, QCOW2_OL_INACTIVE_L2
| QCOW2_OL_ACTIVE_L2
,
1999 slice_offset
, slice_size2
, false);
2004 ret
= bdrv_pwrite(bs
->file
, slice_offset
,
2005 l2_slice
, slice_size2
);
2013 (*visited_l1_entries
)++;
2015 status_cb(bs
, *visited_l1_entries
, l1_entries
, cb_opaque
);
2023 if (!is_active_l1
) {
2024 qemu_vfree(l2_slice
);
2026 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
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.
2038 int qcow2_expand_zero_clusters(BlockDriverState
*bs
,
2039 BlockDriverAmendStatusCB
*status_cb
,
2042 BDRVQcow2State
*s
= bs
->opaque
;
2043 uint64_t *l1_table
= NULL
;
2044 int64_t l1_entries
= 0, visited_l1_entries
= 0;
2049 l1_entries
= s
->l1_size
;
2050 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
2051 l1_entries
+= s
->snapshots
[i
].l1_size
;
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
);
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
);
2074 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
2076 uint64_t *new_l1_table
;
2077 Error
*local_err
= NULL
;
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",
2084 error_report_err(local_err
);
2088 l1_size2
= s
->snapshots
[i
].l1_size
* sizeof(uint64_t);
2089 new_l1_table
= g_try_realloc(l1_table
, l1_size2
);
2091 if (!new_l1_table
) {
2096 l1_table
= new_l1_table
;
2098 ret
= bdrv_pread(bs
->file
, s
->snapshots
[i
].l1_table_offset
,
2099 l1_table
, l1_size2
);
2104 for (j
= 0; j
< s
->snapshots
[i
].l1_size
; j
++) {
2105 be64_to_cpus(&l1_table
[j
]);
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
);