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"
30 #include "block/block_int.h"
32 #include "qemu/bswap.h"
35 int qcow2_shrink_l1_table(BlockDriverState
*bs
, uint64_t exact_size
)
37 BDRVQcow2State
*s
= bs
->opaque
;
38 int new_l1_size
, i
, ret
;
40 if (exact_size
>= s
->l1_size
) {
44 new_l1_size
= exact_size
;
47 fprintf(stderr
, "shrink l1_table from %d to %d\n", s
->l1_size
, new_l1_size
);
50 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_SHRINK_WRITE_TABLE
);
51 ret
= bdrv_pwrite_zeroes(bs
->file
, s
->l1_table_offset
+
52 new_l1_size
* sizeof(uint64_t),
53 (s
->l1_size
- new_l1_size
) * sizeof(uint64_t), 0);
58 ret
= bdrv_flush(bs
->file
->bs
);
63 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_SHRINK_FREE_L2_CLUSTERS
);
64 for (i
= s
->l1_size
- 1; i
> new_l1_size
- 1; i
--) {
65 if ((s
->l1_table
[i
] & L1E_OFFSET_MASK
) == 0) {
68 qcow2_free_clusters(bs
, s
->l1_table
[i
] & L1E_OFFSET_MASK
,
69 s
->cluster_size
, QCOW2_DISCARD_ALWAYS
);
76 * If the write in the l1_table failed the image may contain a partially
77 * overwritten l1_table. In this case it would be better to clear the
78 * l1_table in memory to avoid possible image corruption.
80 memset(s
->l1_table
+ new_l1_size
, 0,
81 (s
->l1_size
- new_l1_size
) * sizeof(uint64_t));
85 int qcow2_grow_l1_table(BlockDriverState
*bs
, uint64_t min_size
,
88 BDRVQcow2State
*s
= bs
->opaque
;
89 int new_l1_size2
, ret
, i
;
90 uint64_t *new_l1_table
;
91 int64_t old_l1_table_offset
, old_l1_size
;
92 int64_t new_l1_table_offset
, new_l1_size
;
95 if (min_size
<= s
->l1_size
)
98 /* Do a sanity check on min_size before trying to calculate new_l1_size
99 * (this prevents overflows during the while loop for the calculation of
101 if (min_size
> INT_MAX
/ sizeof(uint64_t)) {
106 new_l1_size
= min_size
;
108 /* Bump size up to reduce the number of times we have to grow */
109 new_l1_size
= s
->l1_size
;
110 if (new_l1_size
== 0) {
113 while (min_size
> new_l1_size
) {
114 new_l1_size
= DIV_ROUND_UP(new_l1_size
* 3, 2);
118 QEMU_BUILD_BUG_ON(QCOW_MAX_L1_SIZE
> INT_MAX
);
119 if (new_l1_size
> QCOW_MAX_L1_SIZE
/ sizeof(uint64_t)) {
124 fprintf(stderr
, "grow l1_table from %d to %" PRId64
"\n",
125 s
->l1_size
, new_l1_size
);
128 new_l1_size2
= sizeof(uint64_t) * new_l1_size
;
129 new_l1_table
= qemu_try_blockalign(bs
->file
->bs
,
130 ROUND_UP(new_l1_size2
, 512));
131 if (new_l1_table
== NULL
) {
134 memset(new_l1_table
, 0, ROUND_UP(new_l1_size2
, 512));
137 memcpy(new_l1_table
, s
->l1_table
, s
->l1_size
* sizeof(uint64_t));
140 /* write new table (align to cluster) */
141 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_GROW_ALLOC_TABLE
);
142 new_l1_table_offset
= qcow2_alloc_clusters(bs
, new_l1_size2
);
143 if (new_l1_table_offset
< 0) {
144 qemu_vfree(new_l1_table
);
145 return new_l1_table_offset
;
148 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
153 /* the L1 position has not yet been updated, so these clusters must
154 * indeed be completely free */
155 ret
= qcow2_pre_write_overlap_check(bs
, 0, new_l1_table_offset
,
161 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_GROW_WRITE_TABLE
);
162 for(i
= 0; i
< s
->l1_size
; i
++)
163 new_l1_table
[i
] = cpu_to_be64(new_l1_table
[i
]);
164 ret
= bdrv_pwrite_sync(bs
->file
, new_l1_table_offset
,
165 new_l1_table
, new_l1_size2
);
168 for(i
= 0; i
< s
->l1_size
; i
++)
169 new_l1_table
[i
] = be64_to_cpu(new_l1_table
[i
]);
172 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_GROW_ACTIVATE_TABLE
);
173 stl_be_p(data
, new_l1_size
);
174 stq_be_p(data
+ 4, new_l1_table_offset
);
175 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, l1_size
),
180 qemu_vfree(s
->l1_table
);
181 old_l1_table_offset
= s
->l1_table_offset
;
182 s
->l1_table_offset
= new_l1_table_offset
;
183 s
->l1_table
= new_l1_table
;
184 old_l1_size
= s
->l1_size
;
185 s
->l1_size
= new_l1_size
;
186 qcow2_free_clusters(bs
, old_l1_table_offset
, old_l1_size
* sizeof(uint64_t),
187 QCOW2_DISCARD_OTHER
);
190 qemu_vfree(new_l1_table
);
191 qcow2_free_clusters(bs
, new_l1_table_offset
, new_l1_size2
,
192 QCOW2_DISCARD_OTHER
);
199 * @bs: The BlockDriverState
200 * @offset: A guest offset, used to calculate what slice of the L2
202 * @l2_offset: Offset to the L2 table in the image file.
203 * @l2_slice: Location to store the pointer to the L2 slice.
205 * Loads a L2 slice into memory (L2 slices are the parts of L2 tables
206 * that are loaded by the qcow2 cache). If the slice is in the cache,
207 * the cache is used; otherwise the L2 slice is loaded from the image
210 static int l2_load(BlockDriverState
*bs
, uint64_t offset
,
211 uint64_t l2_offset
, uint64_t **l2_slice
)
213 BDRVQcow2State
*s
= bs
->opaque
;
214 int start_of_slice
= sizeof(uint64_t) *
215 (offset_to_l2_index(s
, offset
) - offset_to_l2_slice_index(s
, offset
));
217 return qcow2_cache_get(bs
, s
->l2_table_cache
, l2_offset
+ start_of_slice
,
222 * Writes one sector of the L1 table to the disk (can't update single entries
223 * and we really don't want bdrv_pread to perform a read-modify-write)
225 #define L1_ENTRIES_PER_SECTOR (512 / 8)
226 int qcow2_write_l1_entry(BlockDriverState
*bs
, int l1_index
)
228 BDRVQcow2State
*s
= bs
->opaque
;
229 uint64_t buf
[L1_ENTRIES_PER_SECTOR
] = { 0 };
233 l1_start_index
= l1_index
& ~(L1_ENTRIES_PER_SECTOR
- 1);
234 for (i
= 0; i
< L1_ENTRIES_PER_SECTOR
&& l1_start_index
+ i
< s
->l1_size
;
237 buf
[i
] = cpu_to_be64(s
->l1_table
[l1_start_index
+ i
]);
240 ret
= qcow2_pre_write_overlap_check(bs
, QCOW2_OL_ACTIVE_L1
,
241 s
->l1_table_offset
+ 8 * l1_start_index
, sizeof(buf
));
246 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_UPDATE
);
247 ret
= bdrv_pwrite_sync(bs
->file
,
248 s
->l1_table_offset
+ 8 * l1_start_index
,
260 * Allocate a new l2 entry in the file. If l1_index points to an already
261 * used entry in the L2 table (i.e. we are doing a copy on write for the L2
262 * table) copy the contents of the old L2 table into the newly allocated one.
263 * Otherwise the new table is initialized with zeros.
267 static int l2_allocate(BlockDriverState
*bs
, int l1_index
)
269 BDRVQcow2State
*s
= bs
->opaque
;
270 uint64_t old_l2_offset
;
271 uint64_t *l2_slice
= NULL
;
272 unsigned slice
, slice_size2
, n_slices
;
276 old_l2_offset
= s
->l1_table
[l1_index
];
278 trace_qcow2_l2_allocate(bs
, l1_index
);
280 /* allocate a new l2 entry */
282 l2_offset
= qcow2_alloc_clusters(bs
, s
->l2_size
* sizeof(uint64_t));
288 /* If we're allocating the table at offset 0 then something is wrong */
289 if (l2_offset
== 0) {
290 qcow2_signal_corruption(bs
, true, -1, -1, "Preventing invalid "
291 "allocation of L2 table at offset 0");
296 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
301 /* allocate a new entry in the l2 cache */
303 slice_size2
= s
->l2_slice_size
* sizeof(uint64_t);
304 n_slices
= s
->cluster_size
/ slice_size2
;
306 trace_qcow2_l2_allocate_get_empty(bs
, l1_index
);
307 for (slice
= 0; slice
< n_slices
; slice
++) {
308 ret
= qcow2_cache_get_empty(bs
, s
->l2_table_cache
,
309 l2_offset
+ slice
* slice_size2
,
310 (void **) &l2_slice
);
315 if ((old_l2_offset
& L1E_OFFSET_MASK
) == 0) {
316 /* if there was no old l2 table, clear the new slice */
317 memset(l2_slice
, 0, slice_size2
);
320 uint64_t old_l2_slice_offset
=
321 (old_l2_offset
& L1E_OFFSET_MASK
) + slice
* slice_size2
;
323 /* if there was an old l2 table, read a slice from the disk */
324 BLKDBG_EVENT(bs
->file
, BLKDBG_L2_ALLOC_COW_READ
);
325 ret
= qcow2_cache_get(bs
, s
->l2_table_cache
, old_l2_slice_offset
,
326 (void **) &old_slice
);
331 memcpy(l2_slice
, old_slice
, slice_size2
);
333 qcow2_cache_put(s
->l2_table_cache
, (void **) &old_slice
);
336 /* write the l2 slice to the file */
337 BLKDBG_EVENT(bs
->file
, BLKDBG_L2_ALLOC_WRITE
);
339 trace_qcow2_l2_allocate_write_l2(bs
, l1_index
);
340 qcow2_cache_entry_mark_dirty(s
->l2_table_cache
, l2_slice
);
341 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
344 ret
= qcow2_cache_flush(bs
, s
->l2_table_cache
);
349 /* update the L1 entry */
350 trace_qcow2_l2_allocate_write_l1(bs
, l1_index
);
351 s
->l1_table
[l1_index
] = l2_offset
| QCOW_OFLAG_COPIED
;
352 ret
= qcow2_write_l1_entry(bs
, l1_index
);
357 trace_qcow2_l2_allocate_done(bs
, l1_index
, 0);
361 trace_qcow2_l2_allocate_done(bs
, l1_index
, ret
);
362 if (l2_slice
!= NULL
) {
363 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
365 s
->l1_table
[l1_index
] = old_l2_offset
;
367 qcow2_free_clusters(bs
, l2_offset
, s
->l2_size
* sizeof(uint64_t),
368 QCOW2_DISCARD_ALWAYS
);
374 * Checks how many clusters in a given L2 slice are contiguous in the image
375 * file. As soon as one of the flags in the bitmask stop_flags changes compared
376 * to the first cluster, the search is stopped and the cluster is not counted
377 * as contiguous. (This allows it, for example, to stop at the first compressed
378 * cluster which may require a different handling)
380 static int count_contiguous_clusters(int nb_clusters
, int cluster_size
,
381 uint64_t *l2_slice
, uint64_t stop_flags
)
384 QCow2ClusterType first_cluster_type
;
385 uint64_t mask
= stop_flags
| L2E_OFFSET_MASK
| QCOW_OFLAG_COMPRESSED
;
386 uint64_t first_entry
= be64_to_cpu(l2_slice
[0]);
387 uint64_t offset
= first_entry
& mask
;
393 /* must be allocated */
394 first_cluster_type
= qcow2_get_cluster_type(first_entry
);
395 assert(first_cluster_type
== QCOW2_CLUSTER_NORMAL
||
396 first_cluster_type
== QCOW2_CLUSTER_ZERO_ALLOC
);
398 for (i
= 0; i
< nb_clusters
; i
++) {
399 uint64_t l2_entry
= be64_to_cpu(l2_slice
[i
]) & mask
;
400 if (offset
+ (uint64_t) i
* cluster_size
!= l2_entry
) {
409 * Checks how many consecutive unallocated clusters in a given L2
410 * slice have the same cluster type.
412 static int count_contiguous_clusters_unallocated(int nb_clusters
,
414 QCow2ClusterType wanted_type
)
418 assert(wanted_type
== QCOW2_CLUSTER_ZERO_PLAIN
||
419 wanted_type
== QCOW2_CLUSTER_UNALLOCATED
);
420 for (i
= 0; i
< nb_clusters
; i
++) {
421 uint64_t entry
= be64_to_cpu(l2_slice
[i
]);
422 QCow2ClusterType type
= qcow2_get_cluster_type(entry
);
424 if (type
!= wanted_type
) {
432 static int coroutine_fn
do_perform_cow_read(BlockDriverState
*bs
,
433 uint64_t src_cluster_offset
,
434 unsigned offset_in_cluster
,
439 if (qiov
->size
== 0) {
443 BLKDBG_EVENT(bs
->file
, BLKDBG_COW_READ
);
449 /* Call .bdrv_co_readv() directly instead of using the public block-layer
450 * interface. This avoids double I/O throttling and request tracking,
451 * which can lead to deadlock when block layer copy-on-read is enabled.
453 ret
= bs
->drv
->bdrv_co_preadv(bs
, src_cluster_offset
+ offset_in_cluster
,
454 qiov
->size
, qiov
, 0);
462 static bool coroutine_fn
do_perform_cow_encrypt(BlockDriverState
*bs
,
463 uint64_t src_cluster_offset
,
464 uint64_t cluster_offset
,
465 unsigned offset_in_cluster
,
469 if (bytes
&& bs
->encrypted
) {
470 BDRVQcow2State
*s
= bs
->opaque
;
471 int64_t offset
= (s
->crypt_physical_offset
?
472 (cluster_offset
+ offset_in_cluster
) :
473 (src_cluster_offset
+ offset_in_cluster
));
474 assert((offset_in_cluster
& ~BDRV_SECTOR_MASK
) == 0);
475 assert((bytes
& ~BDRV_SECTOR_MASK
) == 0);
477 if (qcrypto_block_encrypt(s
->crypto
, offset
, buffer
, bytes
, NULL
) < 0) {
484 static int coroutine_fn
do_perform_cow_write(BlockDriverState
*bs
,
485 uint64_t cluster_offset
,
486 unsigned offset_in_cluster
,
491 if (qiov
->size
== 0) {
495 ret
= qcow2_pre_write_overlap_check(bs
, 0,
496 cluster_offset
+ offset_in_cluster
, qiov
->size
);
501 BLKDBG_EVENT(bs
->file
, BLKDBG_COW_WRITE
);
502 ret
= bdrv_co_pwritev(bs
->file
, cluster_offset
+ offset_in_cluster
,
503 qiov
->size
, qiov
, 0);
515 * For a given offset of the virtual disk, find the cluster type and offset in
516 * the qcow2 file. The offset is stored in *cluster_offset.
518 * On entry, *bytes is the maximum number of contiguous bytes starting at
519 * offset that we are interested in.
521 * On exit, *bytes is the number of bytes starting at offset that have the same
522 * cluster type and (if applicable) are stored contiguously in the image file.
523 * Compressed clusters are always returned one by one.
525 * Returns the cluster type (QCOW2_CLUSTER_*) on success, -errno in error
528 int qcow2_get_cluster_offset(BlockDriverState
*bs
, uint64_t offset
,
529 unsigned int *bytes
, uint64_t *cluster_offset
)
531 BDRVQcow2State
*s
= bs
->opaque
;
532 unsigned int l2_index
;
533 uint64_t l1_index
, l2_offset
, *l2_slice
;
535 unsigned int offset_in_cluster
;
536 uint64_t bytes_available
, bytes_needed
, nb_clusters
;
537 QCow2ClusterType type
;
540 offset_in_cluster
= offset_into_cluster(s
, offset
);
541 bytes_needed
= (uint64_t) *bytes
+ offset_in_cluster
;
543 /* compute how many bytes there are between the start of the cluster
544 * containing offset and the end of the l2 slice that contains
545 * the entry pointing to it */
547 ((uint64_t) (s
->l2_slice_size
- offset_to_l2_slice_index(s
, offset
)))
550 if (bytes_needed
> bytes_available
) {
551 bytes_needed
= bytes_available
;
556 /* seek to the l2 offset in the l1 table */
558 l1_index
= offset_to_l1_index(s
, offset
);
559 if (l1_index
>= s
->l1_size
) {
560 type
= QCOW2_CLUSTER_UNALLOCATED
;
564 l2_offset
= s
->l1_table
[l1_index
] & L1E_OFFSET_MASK
;
566 type
= QCOW2_CLUSTER_UNALLOCATED
;
570 if (offset_into_cluster(s
, l2_offset
)) {
571 qcow2_signal_corruption(bs
, true, -1, -1, "L2 table offset %#" PRIx64
572 " unaligned (L1 index: %#" PRIx64
")",
573 l2_offset
, l1_index
);
577 /* load the l2 slice in memory */
579 ret
= l2_load(bs
, offset
, l2_offset
, &l2_slice
);
584 /* find the cluster offset for the given disk offset */
586 l2_index
= offset_to_l2_slice_index(s
, offset
);
587 *cluster_offset
= be64_to_cpu(l2_slice
[l2_index
]);
589 nb_clusters
= size_to_clusters(s
, bytes_needed
);
590 /* bytes_needed <= *bytes + offset_in_cluster, both of which are unsigned
591 * integers; the minimum cluster size is 512, so this assertion is always
593 assert(nb_clusters
<= INT_MAX
);
595 type
= qcow2_get_cluster_type(*cluster_offset
);
596 if (s
->qcow_version
< 3 && (type
== QCOW2_CLUSTER_ZERO_PLAIN
||
597 type
== QCOW2_CLUSTER_ZERO_ALLOC
)) {
598 qcow2_signal_corruption(bs
, true, -1, -1, "Zero cluster entry found"
599 " in pre-v3 image (L2 offset: %#" PRIx64
600 ", L2 index: %#x)", l2_offset
, l2_index
);
605 case QCOW2_CLUSTER_COMPRESSED
:
606 /* Compressed clusters can only be processed one by one */
608 *cluster_offset
&= L2E_COMPRESSED_OFFSET_SIZE_MASK
;
610 case QCOW2_CLUSTER_ZERO_PLAIN
:
611 case QCOW2_CLUSTER_UNALLOCATED
:
612 /* how many empty clusters ? */
613 c
= count_contiguous_clusters_unallocated(nb_clusters
,
614 &l2_slice
[l2_index
], type
);
617 case QCOW2_CLUSTER_ZERO_ALLOC
:
618 case QCOW2_CLUSTER_NORMAL
:
619 /* how many allocated clusters ? */
620 c
= count_contiguous_clusters(nb_clusters
, s
->cluster_size
,
621 &l2_slice
[l2_index
], QCOW_OFLAG_ZERO
);
622 *cluster_offset
&= L2E_OFFSET_MASK
;
623 if (offset_into_cluster(s
, *cluster_offset
)) {
624 qcow2_signal_corruption(bs
, true, -1, -1,
625 "Cluster allocation offset %#"
626 PRIx64
" unaligned (L2 offset: %#" PRIx64
627 ", L2 index: %#x)", *cluster_offset
,
628 l2_offset
, l2_index
);
637 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
639 bytes_available
= (int64_t)c
* s
->cluster_size
;
642 if (bytes_available
> bytes_needed
) {
643 bytes_available
= bytes_needed
;
646 /* bytes_available <= bytes_needed <= *bytes + offset_in_cluster;
647 * subtracting offset_in_cluster will therefore definitely yield something
648 * not exceeding UINT_MAX */
649 assert(bytes_available
- offset_in_cluster
<= UINT_MAX
);
650 *bytes
= bytes_available
- offset_in_cluster
;
655 qcow2_cache_put(s
->l2_table_cache
, (void **)&l2_slice
);
662 * for a given disk offset, load (and allocate if needed)
663 * the appropriate slice of its l2 table.
665 * the cluster index in the l2 slice is given to the caller.
667 * Returns 0 on success, -errno in failure case
669 static int get_cluster_table(BlockDriverState
*bs
, uint64_t offset
,
670 uint64_t **new_l2_slice
,
673 BDRVQcow2State
*s
= bs
->opaque
;
674 unsigned int l2_index
;
675 uint64_t l1_index
, l2_offset
;
676 uint64_t *l2_slice
= NULL
;
679 /* seek to the l2 offset in the l1 table */
681 l1_index
= offset_to_l1_index(s
, offset
);
682 if (l1_index
>= s
->l1_size
) {
683 ret
= qcow2_grow_l1_table(bs
, l1_index
+ 1, false);
689 assert(l1_index
< s
->l1_size
);
690 l2_offset
= s
->l1_table
[l1_index
] & L1E_OFFSET_MASK
;
691 if (offset_into_cluster(s
, l2_offset
)) {
692 qcow2_signal_corruption(bs
, true, -1, -1, "L2 table offset %#" PRIx64
693 " unaligned (L1 index: %#" PRIx64
")",
694 l2_offset
, l1_index
);
698 if (!(s
->l1_table
[l1_index
] & QCOW_OFLAG_COPIED
)) {
699 /* First allocate a new L2 table (and do COW if needed) */
700 ret
= l2_allocate(bs
, l1_index
);
705 /* Then decrease the refcount of the old table */
707 qcow2_free_clusters(bs
, l2_offset
, s
->l2_size
* sizeof(uint64_t),
708 QCOW2_DISCARD_OTHER
);
711 /* Get the offset of the newly-allocated l2 table */
712 l2_offset
= s
->l1_table
[l1_index
] & L1E_OFFSET_MASK
;
713 assert(offset_into_cluster(s
, l2_offset
) == 0);
716 /* load the l2 slice in memory */
717 ret
= l2_load(bs
, offset
, l2_offset
, &l2_slice
);
722 /* find the cluster offset for the given disk offset */
724 l2_index
= offset_to_l2_slice_index(s
, offset
);
726 *new_l2_slice
= l2_slice
;
727 *new_l2_index
= l2_index
;
733 * alloc_compressed_cluster_offset
735 * For a given offset of the disk image, return cluster offset in
738 * If the offset is not found, allocate a new compressed cluster.
740 * Return the cluster offset if successful,
741 * Return 0, otherwise.
745 uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState
*bs
,
749 BDRVQcow2State
*s
= bs
->opaque
;
752 int64_t cluster_offset
;
755 ret
= get_cluster_table(bs
, offset
, &l2_slice
, &l2_index
);
760 /* Compression can't overwrite anything. Fail if the cluster was already
762 cluster_offset
= be64_to_cpu(l2_slice
[l2_index
]);
763 if (cluster_offset
& L2E_OFFSET_MASK
) {
764 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
768 cluster_offset
= qcow2_alloc_bytes(bs
, compressed_size
);
769 if (cluster_offset
< 0) {
770 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
774 nb_csectors
= ((cluster_offset
+ compressed_size
- 1) >> 9) -
775 (cluster_offset
>> 9);
777 cluster_offset
|= QCOW_OFLAG_COMPRESSED
|
778 ((uint64_t)nb_csectors
<< s
->csize_shift
);
780 /* update L2 table */
782 /* compressed clusters never have the copied flag */
784 BLKDBG_EVENT(bs
->file
, BLKDBG_L2_UPDATE_COMPRESSED
);
785 qcow2_cache_entry_mark_dirty(s
->l2_table_cache
, l2_slice
);
786 l2_slice
[l2_index
] = cpu_to_be64(cluster_offset
);
787 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
789 return cluster_offset
;
792 static int perform_cow(BlockDriverState
*bs
, QCowL2Meta
*m
)
794 BDRVQcow2State
*s
= bs
->opaque
;
795 Qcow2COWRegion
*start
= &m
->cow_start
;
796 Qcow2COWRegion
*end
= &m
->cow_end
;
797 unsigned buffer_size
;
798 unsigned data_bytes
= end
->offset
- (start
->offset
+ start
->nb_bytes
);
800 uint8_t *start_buffer
, *end_buffer
;
804 assert(start
->nb_bytes
<= UINT_MAX
- end
->nb_bytes
);
805 assert(start
->nb_bytes
+ end
->nb_bytes
<= UINT_MAX
- data_bytes
);
806 assert(start
->offset
+ start
->nb_bytes
<= end
->offset
);
807 assert(!m
->data_qiov
|| m
->data_qiov
->size
== data_bytes
);
809 if (start
->nb_bytes
== 0 && end
->nb_bytes
== 0) {
813 /* If we have to read both the start and end COW regions and the
814 * middle region is not too large then perform just one read
816 merge_reads
= start
->nb_bytes
&& end
->nb_bytes
&& data_bytes
<= 16384;
818 buffer_size
= start
->nb_bytes
+ data_bytes
+ end
->nb_bytes
;
820 /* If we have to do two reads, add some padding in the middle
821 * if necessary to make sure that the end region is optimally
823 size_t align
= bdrv_opt_mem_align(bs
);
824 assert(align
> 0 && align
<= UINT_MAX
);
825 assert(QEMU_ALIGN_UP(start
->nb_bytes
, align
) <=
826 UINT_MAX
- end
->nb_bytes
);
827 buffer_size
= QEMU_ALIGN_UP(start
->nb_bytes
, align
) + end
->nb_bytes
;
830 /* Reserve a buffer large enough to store all the data that we're
832 start_buffer
= qemu_try_blockalign(bs
, buffer_size
);
833 if (start_buffer
== NULL
) {
836 /* The part of the buffer where the end region is located */
837 end_buffer
= start_buffer
+ buffer_size
- end
->nb_bytes
;
839 qemu_iovec_init(&qiov
, 2 + (m
->data_qiov
? m
->data_qiov
->niov
: 0));
841 qemu_co_mutex_unlock(&s
->lock
);
842 /* First we read the existing data from both COW regions. We
843 * either read the whole region in one go, or the start and end
844 * regions separately. */
846 qemu_iovec_add(&qiov
, start_buffer
, buffer_size
);
847 ret
= do_perform_cow_read(bs
, m
->offset
, start
->offset
, &qiov
);
849 qemu_iovec_add(&qiov
, start_buffer
, start
->nb_bytes
);
850 ret
= do_perform_cow_read(bs
, m
->offset
, start
->offset
, &qiov
);
855 qemu_iovec_reset(&qiov
);
856 qemu_iovec_add(&qiov
, end_buffer
, end
->nb_bytes
);
857 ret
= do_perform_cow_read(bs
, m
->offset
, end
->offset
, &qiov
);
863 /* Encrypt the data if necessary before writing it */
865 if (!do_perform_cow_encrypt(bs
, m
->offset
, m
->alloc_offset
,
866 start
->offset
, start_buffer
,
868 !do_perform_cow_encrypt(bs
, m
->offset
, m
->alloc_offset
,
869 end
->offset
, end_buffer
, end
->nb_bytes
)) {
875 /* And now we can write everything. If we have the guest data we
876 * can write everything in one single operation */
878 qemu_iovec_reset(&qiov
);
879 if (start
->nb_bytes
) {
880 qemu_iovec_add(&qiov
, start_buffer
, start
->nb_bytes
);
882 qemu_iovec_concat(&qiov
, m
->data_qiov
, 0, data_bytes
);
884 qemu_iovec_add(&qiov
, end_buffer
, end
->nb_bytes
);
886 /* NOTE: we have a write_aio blkdebug event here followed by
887 * a cow_write one in do_perform_cow_write(), but there's only
888 * one single I/O operation */
889 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_AIO
);
890 ret
= do_perform_cow_write(bs
, m
->alloc_offset
, start
->offset
, &qiov
);
892 /* If there's no guest data then write both COW regions separately */
893 qemu_iovec_reset(&qiov
);
894 qemu_iovec_add(&qiov
, start_buffer
, start
->nb_bytes
);
895 ret
= do_perform_cow_write(bs
, m
->alloc_offset
, start
->offset
, &qiov
);
900 qemu_iovec_reset(&qiov
);
901 qemu_iovec_add(&qiov
, end_buffer
, end
->nb_bytes
);
902 ret
= do_perform_cow_write(bs
, m
->alloc_offset
, end
->offset
, &qiov
);
906 qemu_co_mutex_lock(&s
->lock
);
909 * Before we update the L2 table to actually point to the new cluster, we
910 * need to be sure that the refcounts have been increased and COW was
914 qcow2_cache_depends_on_flush(s
->l2_table_cache
);
917 qemu_vfree(start_buffer
);
918 qemu_iovec_destroy(&qiov
);
922 int qcow2_alloc_cluster_link_l2(BlockDriverState
*bs
, QCowL2Meta
*m
)
924 BDRVQcow2State
*s
= bs
->opaque
;
925 int i
, j
= 0, l2_index
, ret
;
926 uint64_t *old_cluster
, *l2_slice
;
927 uint64_t cluster_offset
= m
->alloc_offset
;
929 trace_qcow2_cluster_link_l2(qemu_coroutine_self(), m
->nb_clusters
);
930 assert(m
->nb_clusters
> 0);
932 old_cluster
= g_try_new(uint64_t, m
->nb_clusters
);
933 if (old_cluster
== NULL
) {
938 /* copy content of unmodified sectors */
939 ret
= perform_cow(bs
, m
);
944 /* Update L2 table. */
945 if (s
->use_lazy_refcounts
) {
946 qcow2_mark_dirty(bs
);
948 if (qcow2_need_accurate_refcounts(s
)) {
949 qcow2_cache_set_dependency(bs
, s
->l2_table_cache
,
950 s
->refcount_block_cache
);
953 ret
= get_cluster_table(bs
, m
->offset
, &l2_slice
, &l2_index
);
957 qcow2_cache_entry_mark_dirty(s
->l2_table_cache
, l2_slice
);
959 assert(l2_index
+ m
->nb_clusters
<= s
->l2_slice_size
);
960 for (i
= 0; i
< m
->nb_clusters
; i
++) {
961 /* if two concurrent writes happen to the same unallocated cluster
962 * each write allocates separate cluster and writes data concurrently.
963 * The first one to complete updates l2 table with pointer to its
964 * cluster the second one has to do RMW (which is done above by
965 * perform_cow()), update l2 table with its cluster pointer and free
966 * old cluster. This is what this loop does */
967 if (l2_slice
[l2_index
+ i
] != 0) {
968 old_cluster
[j
++] = l2_slice
[l2_index
+ i
];
971 l2_slice
[l2_index
+ i
] = cpu_to_be64((cluster_offset
+
972 (i
<< s
->cluster_bits
)) | QCOW_OFLAG_COPIED
);
976 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
979 * If this was a COW, we need to decrease the refcount of the old cluster.
981 * Don't discard clusters that reach a refcount of 0 (e.g. compressed
982 * clusters), the next write will reuse them anyway.
984 if (!m
->keep_old_clusters
&& j
!= 0) {
985 for (i
= 0; i
< j
; i
++) {
986 qcow2_free_any_clusters(bs
, be64_to_cpu(old_cluster
[i
]), 1,
987 QCOW2_DISCARD_NEVER
);
998 * Frees the allocated clusters because the request failed and they won't
999 * actually be linked.
1001 void qcow2_alloc_cluster_abort(BlockDriverState
*bs
, QCowL2Meta
*m
)
1003 BDRVQcow2State
*s
= bs
->opaque
;
1004 qcow2_free_clusters(bs
, m
->alloc_offset
, m
->nb_clusters
<< s
->cluster_bits
,
1005 QCOW2_DISCARD_NEVER
);
1009 * Returns the number of contiguous clusters that can be used for an allocating
1010 * write, but require COW to be performed (this includes yet unallocated space,
1011 * which must copy from the backing file)
1013 static int count_cow_clusters(BDRVQcow2State
*s
, int nb_clusters
,
1014 uint64_t *l2_slice
, int l2_index
)
1018 for (i
= 0; i
< nb_clusters
; i
++) {
1019 uint64_t l2_entry
= be64_to_cpu(l2_slice
[l2_index
+ i
]);
1020 QCow2ClusterType cluster_type
= qcow2_get_cluster_type(l2_entry
);
1022 switch(cluster_type
) {
1023 case QCOW2_CLUSTER_NORMAL
:
1024 if (l2_entry
& QCOW_OFLAG_COPIED
) {
1028 case QCOW2_CLUSTER_UNALLOCATED
:
1029 case QCOW2_CLUSTER_COMPRESSED
:
1030 case QCOW2_CLUSTER_ZERO_PLAIN
:
1031 case QCOW2_CLUSTER_ZERO_ALLOC
:
1039 assert(i
<= nb_clusters
);
1044 * Check if there already is an AIO write request in flight which allocates
1045 * the same cluster. In this case we need to wait until the previous
1046 * request has completed and updated the L2 table accordingly.
1049 * 0 if there was no dependency. *cur_bytes indicates the number of
1050 * bytes from guest_offset that can be read before the next
1051 * dependency must be processed (or the request is complete)
1053 * -EAGAIN if we had to wait for another request, previously gathered
1054 * information on cluster allocation may be invalid now. The caller
1055 * must start over anyway, so consider *cur_bytes undefined.
1057 static int handle_dependencies(BlockDriverState
*bs
, uint64_t guest_offset
,
1058 uint64_t *cur_bytes
, QCowL2Meta
**m
)
1060 BDRVQcow2State
*s
= bs
->opaque
;
1061 QCowL2Meta
*old_alloc
;
1062 uint64_t bytes
= *cur_bytes
;
1064 QLIST_FOREACH(old_alloc
, &s
->cluster_allocs
, next_in_flight
) {
1066 uint64_t start
= guest_offset
;
1067 uint64_t end
= start
+ bytes
;
1068 uint64_t old_start
= l2meta_cow_start(old_alloc
);
1069 uint64_t old_end
= l2meta_cow_end(old_alloc
);
1071 if (end
<= old_start
|| start
>= old_end
) {
1072 /* No intersection */
1074 if (start
< old_start
) {
1075 /* Stop at the start of a running allocation */
1076 bytes
= old_start
- start
;
1081 /* Stop if already an l2meta exists. After yielding, it wouldn't
1082 * be valid any more, so we'd have to clean up the old L2Metas
1083 * and deal with requests depending on them before starting to
1084 * gather new ones. Not worth the trouble. */
1085 if (bytes
== 0 && *m
) {
1091 /* Wait for the dependency to complete. We need to recheck
1092 * the free/allocated clusters when we continue. */
1093 qemu_co_queue_wait(&old_alloc
->dependent_requests
, &s
->lock
);
1099 /* Make sure that existing clusters and new allocations are only used up to
1100 * the next dependency if we shortened the request above */
1107 * Checks how many already allocated clusters that don't require a copy on
1108 * write there are at the given guest_offset (up to *bytes). If
1109 * *host_offset is not zero, only physically contiguous clusters beginning at
1110 * this host offset are counted.
1112 * Note that guest_offset may not be cluster aligned. In this case, the
1113 * returned *host_offset points to exact byte referenced by guest_offset and
1114 * therefore isn't cluster aligned as well.
1117 * 0: if no allocated clusters are available at the given offset.
1118 * *bytes is normally unchanged. It is set to 0 if the cluster
1119 * is allocated and doesn't need COW, but doesn't have the right
1122 * 1: if allocated clusters that don't require a COW are available at
1123 * the requested offset. *bytes may have decreased and describes
1124 * the length of the area that can be written to.
1126 * -errno: in error cases
1128 static int handle_copied(BlockDriverState
*bs
, uint64_t guest_offset
,
1129 uint64_t *host_offset
, uint64_t *bytes
, QCowL2Meta
**m
)
1131 BDRVQcow2State
*s
= bs
->opaque
;
1133 uint64_t cluster_offset
;
1135 uint64_t nb_clusters
;
1136 unsigned int keep_clusters
;
1139 trace_qcow2_handle_copied(qemu_coroutine_self(), guest_offset
, *host_offset
,
1142 assert(*host_offset
== 0 || offset_into_cluster(s
, guest_offset
)
1143 == offset_into_cluster(s
, *host_offset
));
1146 * Calculate the number of clusters to look for. We stop at L2 slice
1147 * boundaries to keep things simple.
1150 size_to_clusters(s
, offset_into_cluster(s
, guest_offset
) + *bytes
);
1152 l2_index
= offset_to_l2_slice_index(s
, guest_offset
);
1153 nb_clusters
= MIN(nb_clusters
, s
->l2_slice_size
- l2_index
);
1154 assert(nb_clusters
<= INT_MAX
);
1156 /* Find L2 entry for the first involved cluster */
1157 ret
= get_cluster_table(bs
, guest_offset
, &l2_slice
, &l2_index
);
1162 cluster_offset
= be64_to_cpu(l2_slice
[l2_index
]);
1164 /* Check how many clusters are already allocated and don't need COW */
1165 if (qcow2_get_cluster_type(cluster_offset
) == QCOW2_CLUSTER_NORMAL
1166 && (cluster_offset
& QCOW_OFLAG_COPIED
))
1168 /* If a specific host_offset is required, check it */
1169 bool offset_matches
=
1170 (cluster_offset
& L2E_OFFSET_MASK
) == *host_offset
;
1172 if (offset_into_cluster(s
, cluster_offset
& L2E_OFFSET_MASK
)) {
1173 qcow2_signal_corruption(bs
, true, -1, -1, "Data cluster offset "
1174 "%#llx unaligned (guest offset: %#" PRIx64
1175 ")", cluster_offset
& L2E_OFFSET_MASK
,
1181 if (*host_offset
!= 0 && !offset_matches
) {
1187 /* We keep all QCOW_OFLAG_COPIED clusters */
1189 count_contiguous_clusters(nb_clusters
, s
->cluster_size
,
1190 &l2_slice
[l2_index
],
1191 QCOW_OFLAG_COPIED
| QCOW_OFLAG_ZERO
);
1192 assert(keep_clusters
<= nb_clusters
);
1194 *bytes
= MIN(*bytes
,
1195 keep_clusters
* s
->cluster_size
1196 - offset_into_cluster(s
, guest_offset
));
1205 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
1207 /* Only return a host offset if we actually made progress. Otherwise we
1208 * would make requirements for handle_alloc() that it can't fulfill */
1210 *host_offset
= (cluster_offset
& L2E_OFFSET_MASK
)
1211 + offset_into_cluster(s
, guest_offset
);
1218 * Allocates new clusters for the given guest_offset.
1220 * At most *nb_clusters are allocated, and on return *nb_clusters is updated to
1221 * contain the number of clusters that have been allocated and are contiguous
1222 * in the image file.
1224 * If *host_offset is non-zero, it specifies the offset in the image file at
1225 * which the new clusters must start. *nb_clusters can be 0 on return in this
1226 * case if the cluster at host_offset is already in use. If *host_offset is
1227 * zero, the clusters can be allocated anywhere in the image file.
1229 * *host_offset is updated to contain the offset into the image file at which
1230 * the first allocated cluster starts.
1232 * Return 0 on success and -errno in error cases. -EAGAIN means that the
1233 * function has been waiting for another request and the allocation must be
1234 * restarted, but the whole request should not be failed.
1236 static int do_alloc_cluster_offset(BlockDriverState
*bs
, uint64_t guest_offset
,
1237 uint64_t *host_offset
, uint64_t *nb_clusters
)
1239 BDRVQcow2State
*s
= bs
->opaque
;
1241 trace_qcow2_do_alloc_clusters_offset(qemu_coroutine_self(), guest_offset
,
1242 *host_offset
, *nb_clusters
);
1244 /* Allocate new clusters */
1245 trace_qcow2_cluster_alloc_phys(qemu_coroutine_self());
1246 if (*host_offset
== 0) {
1247 int64_t cluster_offset
=
1248 qcow2_alloc_clusters(bs
, *nb_clusters
* s
->cluster_size
);
1249 if (cluster_offset
< 0) {
1250 return cluster_offset
;
1252 *host_offset
= cluster_offset
;
1255 int64_t ret
= qcow2_alloc_clusters_at(bs
, *host_offset
, *nb_clusters
);
1265 * Allocates new clusters for an area that either is yet unallocated or needs a
1266 * copy on write. If *host_offset is non-zero, clusters are only allocated if
1267 * the new allocation can match the specified host offset.
1269 * Note that guest_offset may not be cluster aligned. In this case, the
1270 * returned *host_offset points to exact byte referenced by guest_offset and
1271 * therefore isn't cluster aligned as well.
1274 * 0: if no clusters could be allocated. *bytes is set to 0,
1275 * *host_offset is left unchanged.
1277 * 1: if new clusters were allocated. *bytes may be decreased if the
1278 * new allocation doesn't cover all of the requested area.
1279 * *host_offset is updated to contain the host offset of the first
1280 * newly allocated cluster.
1282 * -errno: in error cases
1284 static int handle_alloc(BlockDriverState
*bs
, uint64_t guest_offset
,
1285 uint64_t *host_offset
, uint64_t *bytes
, QCowL2Meta
**m
)
1287 BDRVQcow2State
*s
= bs
->opaque
;
1291 uint64_t nb_clusters
;
1293 bool keep_old_clusters
= false;
1295 uint64_t alloc_cluster_offset
= 0;
1297 trace_qcow2_handle_alloc(qemu_coroutine_self(), guest_offset
, *host_offset
,
1302 * Calculate the number of clusters to look for. We stop at L2 slice
1303 * boundaries to keep things simple.
1306 size_to_clusters(s
, offset_into_cluster(s
, guest_offset
) + *bytes
);
1308 l2_index
= offset_to_l2_slice_index(s
, guest_offset
);
1309 nb_clusters
= MIN(nb_clusters
, s
->l2_slice_size
- l2_index
);
1310 assert(nb_clusters
<= INT_MAX
);
1312 /* Find L2 entry for the first involved cluster */
1313 ret
= get_cluster_table(bs
, guest_offset
, &l2_slice
, &l2_index
);
1318 entry
= be64_to_cpu(l2_slice
[l2_index
]);
1320 /* For the moment, overwrite compressed clusters one by one */
1321 if (entry
& QCOW_OFLAG_COMPRESSED
) {
1324 nb_clusters
= count_cow_clusters(s
, nb_clusters
, l2_slice
, l2_index
);
1327 /* This function is only called when there were no non-COW clusters, so if
1328 * we can't find any unallocated or COW clusters either, something is
1329 * wrong with our code. */
1330 assert(nb_clusters
> 0);
1332 if (qcow2_get_cluster_type(entry
) == QCOW2_CLUSTER_ZERO_ALLOC
&&
1333 (entry
& QCOW_OFLAG_COPIED
) &&
1335 start_of_cluster(s
, *host_offset
) == (entry
& L2E_OFFSET_MASK
)))
1337 int preallocated_nb_clusters
;
1339 if (offset_into_cluster(s
, entry
& L2E_OFFSET_MASK
)) {
1340 qcow2_signal_corruption(bs
, true, -1, -1, "Preallocated zero "
1341 "cluster offset %#llx unaligned (guest "
1342 "offset: %#" PRIx64
")",
1343 entry
& L2E_OFFSET_MASK
, guest_offset
);
1348 /* Try to reuse preallocated zero clusters; contiguous normal clusters
1349 * would be fine, too, but count_cow_clusters() above has limited
1350 * nb_clusters already to a range of COW clusters */
1351 preallocated_nb_clusters
=
1352 count_contiguous_clusters(nb_clusters
, s
->cluster_size
,
1353 &l2_slice
[l2_index
], QCOW_OFLAG_COPIED
);
1354 assert(preallocated_nb_clusters
> 0);
1356 nb_clusters
= preallocated_nb_clusters
;
1357 alloc_cluster_offset
= entry
& L2E_OFFSET_MASK
;
1359 /* We want to reuse these clusters, so qcow2_alloc_cluster_link_l2()
1360 * should not free them. */
1361 keep_old_clusters
= true;
1364 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
1366 if (!alloc_cluster_offset
) {
1367 /* Allocate, if necessary at a given offset in the image file */
1368 alloc_cluster_offset
= start_of_cluster(s
, *host_offset
);
1369 ret
= do_alloc_cluster_offset(bs
, guest_offset
, &alloc_cluster_offset
,
1375 /* Can't extend contiguous allocation */
1376 if (nb_clusters
== 0) {
1381 /* !*host_offset would overwrite the image header and is reserved for
1382 * "no host offset preferred". If 0 was a valid host offset, it'd
1383 * trigger the following overlap check; do that now to avoid having an
1384 * invalid value in *host_offset. */
1385 if (!alloc_cluster_offset
) {
1386 ret
= qcow2_pre_write_overlap_check(bs
, 0, alloc_cluster_offset
,
1387 nb_clusters
* s
->cluster_size
);
1394 * Save info needed for meta data update.
1396 * requested_bytes: Number of bytes from the start of the first
1397 * newly allocated cluster to the end of the (possibly shortened
1398 * before) write request.
1400 * avail_bytes: Number of bytes from the start of the first
1401 * newly allocated to the end of the last newly allocated cluster.
1403 * nb_bytes: The number of bytes from the start of the first
1404 * newly allocated cluster to the end of the area that the write
1405 * request actually writes to (excluding COW at the end)
1407 uint64_t requested_bytes
= *bytes
+ offset_into_cluster(s
, guest_offset
);
1408 int avail_bytes
= MIN(INT_MAX
, nb_clusters
<< s
->cluster_bits
);
1409 int nb_bytes
= MIN(requested_bytes
, avail_bytes
);
1410 QCowL2Meta
*old_m
= *m
;
1412 *m
= g_malloc0(sizeof(**m
));
1414 **m
= (QCowL2Meta
) {
1417 .alloc_offset
= alloc_cluster_offset
,
1418 .offset
= start_of_cluster(s
, guest_offset
),
1419 .nb_clusters
= nb_clusters
,
1421 .keep_old_clusters
= keep_old_clusters
,
1425 .nb_bytes
= offset_into_cluster(s
, guest_offset
),
1429 .nb_bytes
= avail_bytes
- nb_bytes
,
1432 qemu_co_queue_init(&(*m
)->dependent_requests
);
1433 QLIST_INSERT_HEAD(&s
->cluster_allocs
, *m
, next_in_flight
);
1435 *host_offset
= alloc_cluster_offset
+ offset_into_cluster(s
, guest_offset
);
1436 *bytes
= MIN(*bytes
, nb_bytes
- offset_into_cluster(s
, guest_offset
));
1437 assert(*bytes
!= 0);
1442 if (*m
&& (*m
)->nb_clusters
> 0) {
1443 QLIST_REMOVE(*m
, next_in_flight
);
1449 * alloc_cluster_offset
1451 * For a given offset on the virtual disk, find the cluster offset in qcow2
1452 * file. If the offset is not found, allocate a new cluster.
1454 * If the cluster was already allocated, m->nb_clusters is set to 0 and
1455 * other fields in m are meaningless.
1457 * If the cluster is newly allocated, m->nb_clusters is set to the number of
1458 * contiguous clusters that have been allocated. In this case, the other
1459 * fields of m are valid and contain information about the first allocated
1462 * If the request conflicts with another write request in flight, the coroutine
1463 * is queued and will be reentered when the dependency has completed.
1465 * Return 0 on success and -errno in error cases
1467 int qcow2_alloc_cluster_offset(BlockDriverState
*bs
, uint64_t offset
,
1468 unsigned int *bytes
, uint64_t *host_offset
,
1471 BDRVQcow2State
*s
= bs
->opaque
;
1472 uint64_t start
, remaining
;
1473 uint64_t cluster_offset
;
1477 trace_qcow2_alloc_clusters_offset(qemu_coroutine_self(), offset
, *bytes
);
1489 if (!*host_offset
) {
1490 *host_offset
= start_of_cluster(s
, cluster_offset
);
1493 assert(remaining
>= cur_bytes
);
1496 remaining
-= cur_bytes
;
1497 cluster_offset
+= cur_bytes
;
1499 if (remaining
== 0) {
1503 cur_bytes
= remaining
;
1506 * Now start gathering as many contiguous clusters as possible:
1508 * 1. Check for overlaps with in-flight allocations
1510 * a) Overlap not in the first cluster -> shorten this request and
1511 * let the caller handle the rest in its next loop iteration.
1513 * b) Real overlaps of two requests. Yield and restart the search
1514 * for contiguous clusters (the situation could have changed
1515 * while we were sleeping)
1517 * c) TODO: Request starts in the same cluster as the in-flight
1518 * allocation ends. Shorten the COW of the in-fight allocation,
1519 * set cluster_offset to write to the same cluster and set up
1520 * the right synchronisation between the in-flight request and
1523 ret
= handle_dependencies(bs
, start
, &cur_bytes
, m
);
1524 if (ret
== -EAGAIN
) {
1525 /* Currently handle_dependencies() doesn't yield if we already had
1526 * an allocation. If it did, we would have to clean up the L2Meta
1527 * structs before starting over. */
1530 } else if (ret
< 0) {
1532 } else if (cur_bytes
== 0) {
1535 /* handle_dependencies() may have decreased cur_bytes (shortened
1536 * the allocations below) so that the next dependency is processed
1537 * correctly during the next loop iteration. */
1541 * 2. Count contiguous COPIED clusters.
1543 ret
= handle_copied(bs
, start
, &cluster_offset
, &cur_bytes
, m
);
1548 } else if (cur_bytes
== 0) {
1553 * 3. If the request still hasn't completed, allocate new clusters,
1554 * considering any cluster_offset of steps 1c or 2.
1556 ret
= handle_alloc(bs
, start
, &cluster_offset
, &cur_bytes
, m
);
1562 assert(cur_bytes
== 0);
1567 *bytes
-= remaining
;
1569 assert(*host_offset
!= 0);
1574 static int decompress_buffer(uint8_t *out_buf
, int out_buf_size
,
1575 const uint8_t *buf
, int buf_size
)
1577 z_stream strm1
, *strm
= &strm1
;
1580 memset(strm
, 0, sizeof(*strm
));
1582 strm
->next_in
= (uint8_t *)buf
;
1583 strm
->avail_in
= buf_size
;
1584 strm
->next_out
= out_buf
;
1585 strm
->avail_out
= out_buf_size
;
1587 ret
= inflateInit2(strm
, -12);
1590 ret
= inflate(strm
, Z_FINISH
);
1591 out_len
= strm
->next_out
- out_buf
;
1592 if ((ret
!= Z_STREAM_END
&& ret
!= Z_BUF_ERROR
) ||
1593 out_len
!= out_buf_size
) {
1601 int qcow2_decompress_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
)
1603 BDRVQcow2State
*s
= bs
->opaque
;
1604 int ret
, csize
, nb_csectors
, sector_offset
;
1607 coffset
= cluster_offset
& s
->cluster_offset_mask
;
1608 if (s
->cluster_cache_offset
!= coffset
) {
1609 nb_csectors
= ((cluster_offset
>> s
->csize_shift
) & s
->csize_mask
) + 1;
1610 sector_offset
= coffset
& 511;
1611 csize
= nb_csectors
* 512 - sector_offset
;
1613 /* Allocate buffers on first decompress operation, most images are
1614 * uncompressed and the memory overhead can be avoided. The buffers
1615 * are freed in .bdrv_close().
1617 if (!s
->cluster_data
) {
1618 /* one more sector for decompressed data alignment */
1619 s
->cluster_data
= qemu_try_blockalign(bs
->file
->bs
,
1620 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
+ 512);
1621 if (!s
->cluster_data
) {
1625 if (!s
->cluster_cache
) {
1626 s
->cluster_cache
= g_malloc(s
->cluster_size
);
1629 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_COMPRESSED
);
1630 ret
= bdrv_read(bs
->file
, coffset
>> 9, s
->cluster_data
,
1635 if (decompress_buffer(s
->cluster_cache
, s
->cluster_size
,
1636 s
->cluster_data
+ sector_offset
, csize
) < 0) {
1639 s
->cluster_cache_offset
= coffset
;
1645 * This discards as many clusters of nb_clusters as possible at once (i.e.
1646 * all clusters in the same L2 slice) and returns the number of discarded
1649 static int discard_in_l2_slice(BlockDriverState
*bs
, uint64_t offset
,
1650 uint64_t nb_clusters
,
1651 enum qcow2_discard_type type
, bool full_discard
)
1653 BDRVQcow2State
*s
= bs
->opaque
;
1659 ret
= get_cluster_table(bs
, offset
, &l2_slice
, &l2_index
);
1664 /* Limit nb_clusters to one L2 slice */
1665 nb_clusters
= MIN(nb_clusters
, s
->l2_slice_size
- l2_index
);
1666 assert(nb_clusters
<= INT_MAX
);
1668 for (i
= 0; i
< nb_clusters
; i
++) {
1669 uint64_t old_l2_entry
;
1671 old_l2_entry
= be64_to_cpu(l2_slice
[l2_index
+ i
]);
1674 * If full_discard is false, make sure that a discarded area reads back
1675 * as zeroes for v3 images (we cannot do it for v2 without actually
1676 * writing a zero-filled buffer). We can skip the operation if the
1677 * cluster is already marked as zero, or if it's unallocated and we
1678 * don't have a backing file.
1680 * TODO We might want to use bdrv_block_status(bs) here, but we're
1681 * holding s->lock, so that doesn't work today.
1683 * If full_discard is true, the sector should not read back as zeroes,
1684 * but rather fall through to the backing file.
1686 switch (qcow2_get_cluster_type(old_l2_entry
)) {
1687 case QCOW2_CLUSTER_UNALLOCATED
:
1688 if (full_discard
|| !bs
->backing
) {
1693 case QCOW2_CLUSTER_ZERO_PLAIN
:
1694 if (!full_discard
) {
1699 case QCOW2_CLUSTER_ZERO_ALLOC
:
1700 case QCOW2_CLUSTER_NORMAL
:
1701 case QCOW2_CLUSTER_COMPRESSED
:
1708 /* First remove L2 entries */
1709 qcow2_cache_entry_mark_dirty(s
->l2_table_cache
, l2_slice
);
1710 if (!full_discard
&& s
->qcow_version
>= 3) {
1711 l2_slice
[l2_index
+ i
] = cpu_to_be64(QCOW_OFLAG_ZERO
);
1713 l2_slice
[l2_index
+ i
] = cpu_to_be64(0);
1716 /* Then decrease the refcount */
1717 qcow2_free_any_clusters(bs
, old_l2_entry
, 1, type
);
1720 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
1725 int qcow2_cluster_discard(BlockDriverState
*bs
, uint64_t offset
,
1726 uint64_t bytes
, enum qcow2_discard_type type
,
1729 BDRVQcow2State
*s
= bs
->opaque
;
1730 uint64_t end_offset
= offset
+ bytes
;
1731 uint64_t nb_clusters
;
1735 /* Caller must pass aligned values, except at image end */
1736 assert(QEMU_IS_ALIGNED(offset
, s
->cluster_size
));
1737 assert(QEMU_IS_ALIGNED(end_offset
, s
->cluster_size
) ||
1738 end_offset
== bs
->total_sectors
<< BDRV_SECTOR_BITS
);
1740 nb_clusters
= size_to_clusters(s
, bytes
);
1742 s
->cache_discards
= true;
1744 /* Each L2 slice is handled by its own loop iteration */
1745 while (nb_clusters
> 0) {
1746 cleared
= discard_in_l2_slice(bs
, offset
, nb_clusters
, type
,
1753 nb_clusters
-= cleared
;
1754 offset
+= (cleared
* s
->cluster_size
);
1759 s
->cache_discards
= false;
1760 qcow2_process_discards(bs
, ret
);
1766 * This zeroes as many clusters of nb_clusters as possible at once (i.e.
1767 * all clusters in the same L2 slice) and returns the number of zeroed
1770 static int zero_in_l2_slice(BlockDriverState
*bs
, uint64_t offset
,
1771 uint64_t nb_clusters
, int flags
)
1773 BDRVQcow2State
*s
= bs
->opaque
;
1778 bool unmap
= !!(flags
& BDRV_REQ_MAY_UNMAP
);
1780 ret
= get_cluster_table(bs
, offset
, &l2_slice
, &l2_index
);
1785 /* Limit nb_clusters to one L2 slice */
1786 nb_clusters
= MIN(nb_clusters
, s
->l2_slice_size
- l2_index
);
1787 assert(nb_clusters
<= INT_MAX
);
1789 for (i
= 0; i
< nb_clusters
; i
++) {
1790 uint64_t old_offset
;
1791 QCow2ClusterType cluster_type
;
1793 old_offset
= be64_to_cpu(l2_slice
[l2_index
+ i
]);
1796 * Minimize L2 changes if the cluster already reads back as
1797 * zeroes with correct allocation.
1799 cluster_type
= qcow2_get_cluster_type(old_offset
);
1800 if (cluster_type
== QCOW2_CLUSTER_ZERO_PLAIN
||
1801 (cluster_type
== QCOW2_CLUSTER_ZERO_ALLOC
&& !unmap
)) {
1805 qcow2_cache_entry_mark_dirty(s
->l2_table_cache
, l2_slice
);
1806 if (cluster_type
== QCOW2_CLUSTER_COMPRESSED
|| unmap
) {
1807 l2_slice
[l2_index
+ i
] = cpu_to_be64(QCOW_OFLAG_ZERO
);
1808 qcow2_free_any_clusters(bs
, old_offset
, 1, QCOW2_DISCARD_REQUEST
);
1810 l2_slice
[l2_index
+ i
] |= cpu_to_be64(QCOW_OFLAG_ZERO
);
1814 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
1819 int qcow2_cluster_zeroize(BlockDriverState
*bs
, uint64_t offset
,
1820 uint64_t bytes
, int flags
)
1822 BDRVQcow2State
*s
= bs
->opaque
;
1823 uint64_t end_offset
= offset
+ bytes
;
1824 uint64_t nb_clusters
;
1828 /* Caller must pass aligned values, except at image end */
1829 assert(QEMU_IS_ALIGNED(offset
, s
->cluster_size
));
1830 assert(QEMU_IS_ALIGNED(end_offset
, s
->cluster_size
) ||
1831 end_offset
== bs
->total_sectors
<< BDRV_SECTOR_BITS
);
1833 /* The zero flag is only supported by version 3 and newer */
1834 if (s
->qcow_version
< 3) {
1838 /* Each L2 slice is handled by its own loop iteration */
1839 nb_clusters
= size_to_clusters(s
, bytes
);
1841 s
->cache_discards
= true;
1843 while (nb_clusters
> 0) {
1844 cleared
= zero_in_l2_slice(bs
, offset
, nb_clusters
, flags
);
1850 nb_clusters
-= cleared
;
1851 offset
+= (cleared
* s
->cluster_size
);
1856 s
->cache_discards
= false;
1857 qcow2_process_discards(bs
, ret
);
1863 * Expands all zero clusters in a specific L1 table (or deallocates them, for
1864 * non-backed non-pre-allocated zero clusters).
1866 * l1_entries and *visited_l1_entries are used to keep track of progress for
1867 * status_cb(). l1_entries contains the total number of L1 entries and
1868 * *visited_l1_entries counts all visited L1 entries.
1870 static int expand_zero_clusters_in_l1(BlockDriverState
*bs
, uint64_t *l1_table
,
1871 int l1_size
, int64_t *visited_l1_entries
,
1873 BlockDriverAmendStatusCB
*status_cb
,
1876 BDRVQcow2State
*s
= bs
->opaque
;
1877 bool is_active_l1
= (l1_table
== s
->l1_table
);
1878 uint64_t *l2_slice
= NULL
;
1879 unsigned slice
, slice_size2
, n_slices
;
1883 slice_size2
= s
->l2_slice_size
* sizeof(uint64_t);
1884 n_slices
= s
->cluster_size
/ slice_size2
;
1886 if (!is_active_l1
) {
1887 /* inactive L2 tables require a buffer to be stored in when loading
1889 l2_slice
= qemu_try_blockalign(bs
->file
->bs
, slice_size2
);
1890 if (l2_slice
== NULL
) {
1895 for (i
= 0; i
< l1_size
; i
++) {
1896 uint64_t l2_offset
= l1_table
[i
] & L1E_OFFSET_MASK
;
1897 uint64_t l2_refcount
;
1901 (*visited_l1_entries
)++;
1903 status_cb(bs
, *visited_l1_entries
, l1_entries
, cb_opaque
);
1908 if (offset_into_cluster(s
, l2_offset
)) {
1909 qcow2_signal_corruption(bs
, true, -1, -1, "L2 table offset %#"
1910 PRIx64
" unaligned (L1 index: %#x)",
1916 ret
= qcow2_get_refcount(bs
, l2_offset
>> s
->cluster_bits
,
1922 for (slice
= 0; slice
< n_slices
; slice
++) {
1923 uint64_t slice_offset
= l2_offset
+ slice
* slice_size2
;
1924 bool l2_dirty
= false;
1926 /* get active L2 tables from cache */
1927 ret
= qcow2_cache_get(bs
, s
->l2_table_cache
, slice_offset
,
1928 (void **)&l2_slice
);
1930 /* load inactive L2 tables from disk */
1931 ret
= bdrv_pread(bs
->file
, slice_offset
, l2_slice
, slice_size2
);
1937 for (j
= 0; j
< s
->l2_slice_size
; j
++) {
1938 uint64_t l2_entry
= be64_to_cpu(l2_slice
[j
]);
1939 int64_t offset
= l2_entry
& L2E_OFFSET_MASK
;
1940 QCow2ClusterType cluster_type
=
1941 qcow2_get_cluster_type(l2_entry
);
1943 if (cluster_type
!= QCOW2_CLUSTER_ZERO_PLAIN
&&
1944 cluster_type
!= QCOW2_CLUSTER_ZERO_ALLOC
) {
1948 if (cluster_type
== QCOW2_CLUSTER_ZERO_PLAIN
) {
1950 /* not backed; therefore we can simply deallocate the
1957 offset
= qcow2_alloc_clusters(bs
, s
->cluster_size
);
1963 if (l2_refcount
> 1) {
1964 /* For shared L2 tables, set the refcount accordingly
1965 * (it is already 1 and needs to be l2_refcount) */
1966 ret
= qcow2_update_cluster_refcount(
1967 bs
, offset
>> s
->cluster_bits
,
1968 refcount_diff(1, l2_refcount
), false,
1969 QCOW2_DISCARD_OTHER
);
1971 qcow2_free_clusters(bs
, offset
, s
->cluster_size
,
1972 QCOW2_DISCARD_OTHER
);
1978 if (offset_into_cluster(s
, offset
)) {
1979 int l2_index
= slice
* s
->l2_slice_size
+ j
;
1980 qcow2_signal_corruption(
1982 "Cluster allocation offset "
1983 "%#" PRIx64
" unaligned (L2 offset: %#"
1984 PRIx64
", L2 index: %#x)", offset
,
1985 l2_offset
, l2_index
);
1986 if (cluster_type
== QCOW2_CLUSTER_ZERO_PLAIN
) {
1987 qcow2_free_clusters(bs
, offset
, s
->cluster_size
,
1988 QCOW2_DISCARD_ALWAYS
);
1994 ret
= qcow2_pre_write_overlap_check(bs
, 0, offset
,
1997 if (cluster_type
== QCOW2_CLUSTER_ZERO_PLAIN
) {
1998 qcow2_free_clusters(bs
, offset
, s
->cluster_size
,
1999 QCOW2_DISCARD_ALWAYS
);
2004 ret
= bdrv_pwrite_zeroes(bs
->file
, offset
, s
->cluster_size
, 0);
2006 if (cluster_type
== QCOW2_CLUSTER_ZERO_PLAIN
) {
2007 qcow2_free_clusters(bs
, offset
, s
->cluster_size
,
2008 QCOW2_DISCARD_ALWAYS
);
2013 if (l2_refcount
== 1) {
2014 l2_slice
[j
] = cpu_to_be64(offset
| QCOW_OFLAG_COPIED
);
2016 l2_slice
[j
] = cpu_to_be64(offset
);
2023 qcow2_cache_entry_mark_dirty(s
->l2_table_cache
, l2_slice
);
2024 qcow2_cache_depends_on_flush(s
->l2_table_cache
);
2026 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
2029 ret
= qcow2_pre_write_overlap_check(
2030 bs
, QCOW2_OL_INACTIVE_L2
| QCOW2_OL_ACTIVE_L2
,
2031 slice_offset
, slice_size2
);
2036 ret
= bdrv_pwrite(bs
->file
, slice_offset
,
2037 l2_slice
, slice_size2
);
2045 (*visited_l1_entries
)++;
2047 status_cb(bs
, *visited_l1_entries
, l1_entries
, cb_opaque
);
2055 if (!is_active_l1
) {
2056 qemu_vfree(l2_slice
);
2058 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
2065 * For backed images, expands all zero clusters on the image. For non-backed
2066 * images, deallocates all non-pre-allocated zero clusters (and claims the
2067 * allocation for pre-allocated ones). This is important for downgrading to a
2068 * qcow2 version which doesn't yet support metadata zero clusters.
2070 int qcow2_expand_zero_clusters(BlockDriverState
*bs
,
2071 BlockDriverAmendStatusCB
*status_cb
,
2074 BDRVQcow2State
*s
= bs
->opaque
;
2075 uint64_t *l1_table
= NULL
;
2076 int64_t l1_entries
= 0, visited_l1_entries
= 0;
2081 l1_entries
= s
->l1_size
;
2082 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
2083 l1_entries
+= s
->snapshots
[i
].l1_size
;
2087 ret
= expand_zero_clusters_in_l1(bs
, s
->l1_table
, s
->l1_size
,
2088 &visited_l1_entries
, l1_entries
,
2089 status_cb
, cb_opaque
);
2094 /* Inactive L1 tables may point to active L2 tables - therefore it is
2095 * necessary to flush the L2 table cache before trying to access the L2
2096 * tables pointed to by inactive L1 entries (else we might try to expand
2097 * zero clusters that have already been expanded); furthermore, it is also
2098 * necessary to empty the L2 table cache, since it may contain tables which
2099 * are now going to be modified directly on disk, bypassing the cache.
2100 * qcow2_cache_empty() does both for us. */
2101 ret
= qcow2_cache_empty(bs
, s
->l2_table_cache
);
2106 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
2108 uint64_t *new_l1_table
;
2109 Error
*local_err
= NULL
;
2111 ret
= qcow2_validate_table(bs
, s
->snapshots
[i
].l1_table_offset
,
2112 s
->snapshots
[i
].l1_size
, sizeof(uint64_t),
2113 QCOW_MAX_L1_SIZE
, "Snapshot L1 table",
2116 error_report_err(local_err
);
2120 l1_size2
= s
->snapshots
[i
].l1_size
* sizeof(uint64_t);
2121 new_l1_table
= g_try_realloc(l1_table
, l1_size2
);
2123 if (!new_l1_table
) {
2128 l1_table
= new_l1_table
;
2130 ret
= bdrv_pread(bs
->file
, s
->snapshots
[i
].l1_table_offset
,
2131 l1_table
, l1_size2
);
2136 for (j
= 0; j
< s
->snapshots
[i
].l1_size
; j
++) {
2137 be64_to_cpus(&l1_table
[j
]);
2140 ret
= expand_zero_clusters_in_l1(bs
, l1_table
, s
->snapshots
[i
].l1_size
,
2141 &visited_l1_entries
, l1_entries
,
2142 status_cb
, cb_opaque
);