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
27 #include "qemu-common.h"
28 #include "block/block_int.h"
29 #include "block/qcow2.h"
32 int qcow2_grow_l1_table(BlockDriverState
*bs
, uint64_t min_size
,
35 BDRVQcowState
*s
= bs
->opaque
;
36 int new_l1_size2
, ret
, i
;
37 uint64_t *new_l1_table
;
38 int64_t old_l1_table_offset
, old_l1_size
;
39 int64_t new_l1_table_offset
, new_l1_size
;
42 if (min_size
<= s
->l1_size
)
46 new_l1_size
= min_size
;
48 /* Bump size up to reduce the number of times we have to grow */
49 new_l1_size
= s
->l1_size
;
50 if (new_l1_size
== 0) {
53 while (min_size
> new_l1_size
) {
54 new_l1_size
= (new_l1_size
* 3 + 1) / 2;
58 if (new_l1_size
> INT_MAX
/ sizeof(uint64_t)) {
63 fprintf(stderr
, "grow l1_table from %d to %" PRId64
"\n",
64 s
->l1_size
, new_l1_size
);
67 new_l1_size2
= sizeof(uint64_t) * new_l1_size
;
68 new_l1_table
= g_malloc0(align_offset(new_l1_size2
, 512));
69 memcpy(new_l1_table
, s
->l1_table
, s
->l1_size
* sizeof(uint64_t));
71 /* write new table (align to cluster) */
72 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_GROW_ALLOC_TABLE
);
73 new_l1_table_offset
= qcow2_alloc_clusters(bs
, new_l1_size2
);
74 if (new_l1_table_offset
< 0) {
76 return new_l1_table_offset
;
79 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
84 /* the L1 position has not yet been updated, so these clusters must
85 * indeed be completely free */
86 ret
= qcow2_pre_write_overlap_check(bs
, 0, new_l1_table_offset
,
92 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_GROW_WRITE_TABLE
);
93 for(i
= 0; i
< s
->l1_size
; i
++)
94 new_l1_table
[i
] = cpu_to_be64(new_l1_table
[i
]);
95 ret
= bdrv_pwrite_sync(bs
->file
, new_l1_table_offset
, new_l1_table
, new_l1_size2
);
98 for(i
= 0; i
< s
->l1_size
; i
++)
99 new_l1_table
[i
] = be64_to_cpu(new_l1_table
[i
]);
102 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_GROW_ACTIVATE_TABLE
);
103 cpu_to_be32w((uint32_t*)data
, new_l1_size
);
104 stq_be_p(data
+ 4, new_l1_table_offset
);
105 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, l1_size
), data
,sizeof(data
));
110 old_l1_table_offset
= s
->l1_table_offset
;
111 s
->l1_table_offset
= new_l1_table_offset
;
112 s
->l1_table
= new_l1_table
;
113 old_l1_size
= s
->l1_size
;
114 s
->l1_size
= new_l1_size
;
115 qcow2_free_clusters(bs
, old_l1_table_offset
, old_l1_size
* sizeof(uint64_t),
116 QCOW2_DISCARD_OTHER
);
119 g_free(new_l1_table
);
120 qcow2_free_clusters(bs
, new_l1_table_offset
, new_l1_size2
,
121 QCOW2_DISCARD_OTHER
);
128 * Loads a L2 table into memory. If the table is in the cache, the cache
129 * is used; otherwise the L2 table is loaded from the image file.
131 * Returns a pointer to the L2 table on success, or NULL if the read from
132 * the image file failed.
135 static int l2_load(BlockDriverState
*bs
, uint64_t l2_offset
,
138 BDRVQcowState
*s
= bs
->opaque
;
141 ret
= qcow2_cache_get(bs
, s
->l2_table_cache
, l2_offset
, (void**) l2_table
);
147 * Writes one sector of the L1 table to the disk (can't update single entries
148 * and we really don't want bdrv_pread to perform a read-modify-write)
150 #define L1_ENTRIES_PER_SECTOR (512 / 8)
151 int qcow2_write_l1_entry(BlockDriverState
*bs
, int l1_index
)
153 BDRVQcowState
*s
= bs
->opaque
;
154 uint64_t buf
[L1_ENTRIES_PER_SECTOR
];
158 l1_start_index
= l1_index
& ~(L1_ENTRIES_PER_SECTOR
- 1);
159 for (i
= 0; i
< L1_ENTRIES_PER_SECTOR
; i
++) {
160 buf
[i
] = cpu_to_be64(s
->l1_table
[l1_start_index
+ i
]);
163 ret
= qcow2_pre_write_overlap_check(bs
, QCOW2_OL_ACTIVE_L1
,
164 s
->l1_table_offset
+ 8 * l1_start_index
, sizeof(buf
));
169 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_UPDATE
);
170 ret
= bdrv_pwrite_sync(bs
->file
, s
->l1_table_offset
+ 8 * l1_start_index
,
182 * Allocate a new l2 entry in the file. If l1_index points to an already
183 * used entry in the L2 table (i.e. we are doing a copy on write for the L2
184 * table) copy the contents of the old L2 table into the newly allocated one.
185 * Otherwise the new table is initialized with zeros.
189 static int l2_allocate(BlockDriverState
*bs
, int l1_index
, uint64_t **table
)
191 BDRVQcowState
*s
= bs
->opaque
;
192 uint64_t old_l2_offset
;
193 uint64_t *l2_table
= NULL
;
197 old_l2_offset
= s
->l1_table
[l1_index
];
199 trace_qcow2_l2_allocate(bs
, l1_index
);
201 /* allocate a new l2 entry */
203 l2_offset
= qcow2_alloc_clusters(bs
, s
->l2_size
* sizeof(uint64_t));
209 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
214 /* allocate a new entry in the l2 cache */
216 trace_qcow2_l2_allocate_get_empty(bs
, l1_index
);
217 ret
= qcow2_cache_get_empty(bs
, s
->l2_table_cache
, l2_offset
, (void**) table
);
224 if ((old_l2_offset
& L1E_OFFSET_MASK
) == 0) {
225 /* if there was no old l2 table, clear the new table */
226 memset(l2_table
, 0, s
->l2_size
* sizeof(uint64_t));
230 /* if there was an old l2 table, read it from the disk */
231 BLKDBG_EVENT(bs
->file
, BLKDBG_L2_ALLOC_COW_READ
);
232 ret
= qcow2_cache_get(bs
, s
->l2_table_cache
,
233 old_l2_offset
& L1E_OFFSET_MASK
,
234 (void**) &old_table
);
239 memcpy(l2_table
, old_table
, s
->cluster_size
);
241 ret
= qcow2_cache_put(bs
, s
->l2_table_cache
, (void**) &old_table
);
247 /* write the l2 table to the file */
248 BLKDBG_EVENT(bs
->file
, BLKDBG_L2_ALLOC_WRITE
);
250 trace_qcow2_l2_allocate_write_l2(bs
, l1_index
);
251 qcow2_cache_entry_mark_dirty(s
->l2_table_cache
, l2_table
);
252 ret
= qcow2_cache_flush(bs
, s
->l2_table_cache
);
257 /* update the L1 entry */
258 trace_qcow2_l2_allocate_write_l1(bs
, l1_index
);
259 s
->l1_table
[l1_index
] = l2_offset
| QCOW_OFLAG_COPIED
;
260 ret
= qcow2_write_l1_entry(bs
, l1_index
);
266 trace_qcow2_l2_allocate_done(bs
, l1_index
, 0);
270 trace_qcow2_l2_allocate_done(bs
, l1_index
, ret
);
271 if (l2_table
!= NULL
) {
272 qcow2_cache_put(bs
, s
->l2_table_cache
, (void**) table
);
274 s
->l1_table
[l1_index
] = old_l2_offset
;
276 qcow2_free_clusters(bs
, l2_offset
, s
->l2_size
* sizeof(uint64_t),
277 QCOW2_DISCARD_ALWAYS
);
283 * Checks how many clusters in a given L2 table are contiguous in the image
284 * file. As soon as one of the flags in the bitmask stop_flags changes compared
285 * to the first cluster, the search is stopped and the cluster is not counted
286 * as contiguous. (This allows it, for example, to stop at the first compressed
287 * cluster which may require a different handling)
289 static int count_contiguous_clusters(uint64_t nb_clusters
, int cluster_size
,
290 uint64_t *l2_table
, uint64_t stop_flags
)
293 uint64_t mask
= stop_flags
| L2E_OFFSET_MASK
| QCOW_OFLAG_COMPRESSED
;
294 uint64_t first_entry
= be64_to_cpu(l2_table
[0]);
295 uint64_t offset
= first_entry
& mask
;
300 assert(qcow2_get_cluster_type(first_entry
) != QCOW2_CLUSTER_COMPRESSED
);
302 for (i
= 0; i
< nb_clusters
; i
++) {
303 uint64_t l2_entry
= be64_to_cpu(l2_table
[i
]) & mask
;
304 if (offset
+ (uint64_t) i
* cluster_size
!= l2_entry
) {
312 static int count_contiguous_free_clusters(uint64_t nb_clusters
, uint64_t *l2_table
)
316 for (i
= 0; i
< nb_clusters
; i
++) {
317 int type
= qcow2_get_cluster_type(be64_to_cpu(l2_table
[i
]));
319 if (type
!= QCOW2_CLUSTER_UNALLOCATED
) {
327 /* The crypt function is compatible with the linux cryptoloop
328 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
330 void qcow2_encrypt_sectors(BDRVQcowState
*s
, int64_t sector_num
,
331 uint8_t *out_buf
, const uint8_t *in_buf
,
332 int nb_sectors
, int enc
,
341 for(i
= 0; i
< nb_sectors
; i
++) {
342 ivec
.ll
[0] = cpu_to_le64(sector_num
);
344 AES_cbc_encrypt(in_buf
, out_buf
, 512, key
,
352 static int coroutine_fn
copy_sectors(BlockDriverState
*bs
,
354 uint64_t cluster_offset
,
355 int n_start
, int n_end
)
357 BDRVQcowState
*s
= bs
->opaque
;
367 iov
.iov_len
= n
* BDRV_SECTOR_SIZE
;
368 iov
.iov_base
= qemu_blockalign(bs
, iov
.iov_len
);
370 qemu_iovec_init_external(&qiov
, &iov
, 1);
372 BLKDBG_EVENT(bs
->file
, BLKDBG_COW_READ
);
378 /* Call .bdrv_co_readv() directly instead of using the public block-layer
379 * interface. This avoids double I/O throttling and request tracking,
380 * which can lead to deadlock when block layer copy-on-read is enabled.
382 ret
= bs
->drv
->bdrv_co_readv(bs
, start_sect
+ n_start
, n
, &qiov
);
387 if (s
->crypt_method
) {
388 qcow2_encrypt_sectors(s
, start_sect
+ n_start
,
389 iov
.iov_base
, iov
.iov_base
, n
, 1,
390 &s
->aes_encrypt_key
);
393 ret
= qcow2_pre_write_overlap_check(bs
, 0,
394 cluster_offset
+ n_start
* BDRV_SECTOR_SIZE
, n
* BDRV_SECTOR_SIZE
);
399 BLKDBG_EVENT(bs
->file
, BLKDBG_COW_WRITE
);
400 ret
= bdrv_co_writev(bs
->file
, (cluster_offset
>> 9) + n_start
, n
, &qiov
);
407 qemu_vfree(iov
.iov_base
);
415 * For a given offset of the disk image, find the cluster offset in
416 * qcow2 file. The offset is stored in *cluster_offset.
418 * on entry, *num is the number of contiguous sectors we'd like to
419 * access following offset.
421 * on exit, *num is the number of contiguous sectors we can read.
423 * Returns the cluster type (QCOW2_CLUSTER_*) on success, -errno in error
426 int qcow2_get_cluster_offset(BlockDriverState
*bs
, uint64_t offset
,
427 int *num
, uint64_t *cluster_offset
)
429 BDRVQcowState
*s
= bs
->opaque
;
430 unsigned int l2_index
;
431 uint64_t l1_index
, l2_offset
, *l2_table
;
433 unsigned int index_in_cluster
, nb_clusters
;
434 uint64_t nb_available
, nb_needed
;
437 index_in_cluster
= (offset
>> 9) & (s
->cluster_sectors
- 1);
438 nb_needed
= *num
+ index_in_cluster
;
440 l1_bits
= s
->l2_bits
+ s
->cluster_bits
;
442 /* compute how many bytes there are between the offset and
443 * the end of the l1 entry
446 nb_available
= (1ULL << l1_bits
) - (offset
& ((1ULL << l1_bits
) - 1));
448 /* compute the number of available sectors */
450 nb_available
= (nb_available
>> 9) + index_in_cluster
;
452 if (nb_needed
> nb_available
) {
453 nb_needed
= nb_available
;
458 /* seek the the l2 offset in the l1 table */
460 l1_index
= offset
>> l1_bits
;
461 if (l1_index
>= s
->l1_size
) {
462 ret
= QCOW2_CLUSTER_UNALLOCATED
;
466 l2_offset
= s
->l1_table
[l1_index
] & L1E_OFFSET_MASK
;
468 ret
= QCOW2_CLUSTER_UNALLOCATED
;
472 /* load the l2 table in memory */
474 ret
= l2_load(bs
, l2_offset
, &l2_table
);
479 /* find the cluster offset for the given disk offset */
481 l2_index
= (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
482 *cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
483 nb_clusters
= size_to_clusters(s
, nb_needed
<< 9);
485 ret
= qcow2_get_cluster_type(*cluster_offset
);
487 case QCOW2_CLUSTER_COMPRESSED
:
488 /* Compressed clusters can only be processed one by one */
490 *cluster_offset
&= L2E_COMPRESSED_OFFSET_SIZE_MASK
;
492 case QCOW2_CLUSTER_ZERO
:
493 if (s
->qcow_version
< 3) {
494 qcow2_cache_put(bs
, s
->l2_table_cache
, (void**) &l2_table
);
497 c
= count_contiguous_clusters(nb_clusters
, s
->cluster_size
,
498 &l2_table
[l2_index
], QCOW_OFLAG_ZERO
);
501 case QCOW2_CLUSTER_UNALLOCATED
:
502 /* how many empty clusters ? */
503 c
= count_contiguous_free_clusters(nb_clusters
, &l2_table
[l2_index
]);
506 case QCOW2_CLUSTER_NORMAL
:
507 /* how many allocated clusters ? */
508 c
= count_contiguous_clusters(nb_clusters
, s
->cluster_size
,
509 &l2_table
[l2_index
], QCOW_OFLAG_ZERO
);
510 *cluster_offset
&= L2E_OFFSET_MASK
;
516 qcow2_cache_put(bs
, s
->l2_table_cache
, (void**) &l2_table
);
518 nb_available
= (c
* s
->cluster_sectors
);
521 if (nb_available
> nb_needed
)
522 nb_available
= nb_needed
;
524 *num
= nb_available
- index_in_cluster
;
532 * for a given disk offset, load (and allocate if needed)
535 * the l2 table offset in the qcow2 file and the cluster index
536 * in the l2 table are given to the caller.
538 * Returns 0 on success, -errno in failure case
540 static int get_cluster_table(BlockDriverState
*bs
, uint64_t offset
,
541 uint64_t **new_l2_table
,
544 BDRVQcowState
*s
= bs
->opaque
;
545 unsigned int l2_index
;
546 uint64_t l1_index
, l2_offset
;
547 uint64_t *l2_table
= NULL
;
550 /* seek the the l2 offset in the l1 table */
552 l1_index
= offset
>> (s
->l2_bits
+ s
->cluster_bits
);
553 if (l1_index
>= s
->l1_size
) {
554 ret
= qcow2_grow_l1_table(bs
, l1_index
+ 1, false);
560 assert(l1_index
< s
->l1_size
);
561 l2_offset
= s
->l1_table
[l1_index
] & L1E_OFFSET_MASK
;
563 /* seek the l2 table of the given l2 offset */
565 if (s
->l1_table
[l1_index
] & QCOW_OFLAG_COPIED
) {
566 /* load the l2 table in memory */
567 ret
= l2_load(bs
, l2_offset
, &l2_table
);
572 /* First allocate a new L2 table (and do COW if needed) */
573 ret
= l2_allocate(bs
, l1_index
, &l2_table
);
578 /* Then decrease the refcount of the old table */
580 qcow2_free_clusters(bs
, l2_offset
, s
->l2_size
* sizeof(uint64_t),
581 QCOW2_DISCARD_OTHER
);
585 /* find the cluster offset for the given disk offset */
587 l2_index
= (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
589 *new_l2_table
= l2_table
;
590 *new_l2_index
= l2_index
;
596 * alloc_compressed_cluster_offset
598 * For a given offset of the disk image, return cluster offset in
601 * If the offset is not found, allocate a new compressed cluster.
603 * Return the cluster offset if successful,
604 * Return 0, otherwise.
608 uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState
*bs
,
612 BDRVQcowState
*s
= bs
->opaque
;
615 int64_t cluster_offset
;
618 ret
= get_cluster_table(bs
, offset
, &l2_table
, &l2_index
);
623 /* Compression can't overwrite anything. Fail if the cluster was already
625 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
626 if (cluster_offset
& L2E_OFFSET_MASK
) {
627 qcow2_cache_put(bs
, s
->l2_table_cache
, (void**) &l2_table
);
631 cluster_offset
= qcow2_alloc_bytes(bs
, compressed_size
);
632 if (cluster_offset
< 0) {
633 qcow2_cache_put(bs
, s
->l2_table_cache
, (void**) &l2_table
);
637 nb_csectors
= ((cluster_offset
+ compressed_size
- 1) >> 9) -
638 (cluster_offset
>> 9);
640 cluster_offset
|= QCOW_OFLAG_COMPRESSED
|
641 ((uint64_t)nb_csectors
<< s
->csize_shift
);
643 /* update L2 table */
645 /* compressed clusters never have the copied flag */
647 BLKDBG_EVENT(bs
->file
, BLKDBG_L2_UPDATE_COMPRESSED
);
648 qcow2_cache_entry_mark_dirty(s
->l2_table_cache
, l2_table
);
649 l2_table
[l2_index
] = cpu_to_be64(cluster_offset
);
650 ret
= qcow2_cache_put(bs
, s
->l2_table_cache
, (void**) &l2_table
);
655 return cluster_offset
;
658 static int perform_cow(BlockDriverState
*bs
, QCowL2Meta
*m
, Qcow2COWRegion
*r
)
660 BDRVQcowState
*s
= bs
->opaque
;
663 if (r
->nb_sectors
== 0) {
667 qemu_co_mutex_unlock(&s
->lock
);
668 ret
= copy_sectors(bs
, m
->offset
/ BDRV_SECTOR_SIZE
, m
->alloc_offset
,
669 r
->offset
/ BDRV_SECTOR_SIZE
,
670 r
->offset
/ BDRV_SECTOR_SIZE
+ r
->nb_sectors
);
671 qemu_co_mutex_lock(&s
->lock
);
678 * Before we update the L2 table to actually point to the new cluster, we
679 * need to be sure that the refcounts have been increased and COW was
682 qcow2_cache_depends_on_flush(s
->l2_table_cache
);
687 int qcow2_alloc_cluster_link_l2(BlockDriverState
*bs
, QCowL2Meta
*m
)
689 BDRVQcowState
*s
= bs
->opaque
;
690 int i
, j
= 0, l2_index
, ret
;
691 uint64_t *old_cluster
, *l2_table
;
692 uint64_t cluster_offset
= m
->alloc_offset
;
694 trace_qcow2_cluster_link_l2(qemu_coroutine_self(), m
->nb_clusters
);
695 assert(m
->nb_clusters
> 0);
697 old_cluster
= g_malloc(m
->nb_clusters
* sizeof(uint64_t));
699 /* copy content of unmodified sectors */
700 ret
= perform_cow(bs
, m
, &m
->cow_start
);
705 ret
= perform_cow(bs
, m
, &m
->cow_end
);
710 /* Update L2 table. */
711 if (s
->use_lazy_refcounts
) {
712 qcow2_mark_dirty(bs
);
714 if (qcow2_need_accurate_refcounts(s
)) {
715 qcow2_cache_set_dependency(bs
, s
->l2_table_cache
,
716 s
->refcount_block_cache
);
719 ret
= get_cluster_table(bs
, m
->offset
, &l2_table
, &l2_index
);
723 qcow2_cache_entry_mark_dirty(s
->l2_table_cache
, l2_table
);
725 assert(l2_index
+ m
->nb_clusters
<= s
->l2_size
);
726 for (i
= 0; i
< m
->nb_clusters
; i
++) {
727 /* if two concurrent writes happen to the same unallocated cluster
728 * each write allocates separate cluster and writes data concurrently.
729 * The first one to complete updates l2 table with pointer to its
730 * cluster the second one has to do RMW (which is done above by
731 * copy_sectors()), update l2 table with its cluster pointer and free
732 * old cluster. This is what this loop does */
733 if(l2_table
[l2_index
+ i
] != 0)
734 old_cluster
[j
++] = l2_table
[l2_index
+ i
];
736 l2_table
[l2_index
+ i
] = cpu_to_be64((cluster_offset
+
737 (i
<< s
->cluster_bits
)) | QCOW_OFLAG_COPIED
);
741 ret
= qcow2_cache_put(bs
, s
->l2_table_cache
, (void**) &l2_table
);
747 * If this was a COW, we need to decrease the refcount of the old cluster.
748 * Also flush bs->file to get the right order for L2 and refcount update.
750 * Don't discard clusters that reach a refcount of 0 (e.g. compressed
751 * clusters), the next write will reuse them anyway.
754 for (i
= 0; i
< j
; i
++) {
755 qcow2_free_any_clusters(bs
, be64_to_cpu(old_cluster
[i
]), 1,
756 QCOW2_DISCARD_NEVER
);
767 * Returns the number of contiguous clusters that can be used for an allocating
768 * write, but require COW to be performed (this includes yet unallocated space,
769 * which must copy from the backing file)
771 static int count_cow_clusters(BDRVQcowState
*s
, int nb_clusters
,
772 uint64_t *l2_table
, int l2_index
)
776 for (i
= 0; i
< nb_clusters
; i
++) {
777 uint64_t l2_entry
= be64_to_cpu(l2_table
[l2_index
+ i
]);
778 int cluster_type
= qcow2_get_cluster_type(l2_entry
);
780 switch(cluster_type
) {
781 case QCOW2_CLUSTER_NORMAL
:
782 if (l2_entry
& QCOW_OFLAG_COPIED
) {
786 case QCOW2_CLUSTER_UNALLOCATED
:
787 case QCOW2_CLUSTER_COMPRESSED
:
788 case QCOW2_CLUSTER_ZERO
:
796 assert(i
<= nb_clusters
);
801 * Check if there already is an AIO write request in flight which allocates
802 * the same cluster. In this case we need to wait until the previous
803 * request has completed and updated the L2 table accordingly.
806 * 0 if there was no dependency. *cur_bytes indicates the number of
807 * bytes from guest_offset that can be read before the next
808 * dependency must be processed (or the request is complete)
810 * -EAGAIN if we had to wait for another request, previously gathered
811 * information on cluster allocation may be invalid now. The caller
812 * must start over anyway, so consider *cur_bytes undefined.
814 static int handle_dependencies(BlockDriverState
*bs
, uint64_t guest_offset
,
815 uint64_t *cur_bytes
, QCowL2Meta
**m
)
817 BDRVQcowState
*s
= bs
->opaque
;
818 QCowL2Meta
*old_alloc
;
819 uint64_t bytes
= *cur_bytes
;
821 QLIST_FOREACH(old_alloc
, &s
->cluster_allocs
, next_in_flight
) {
823 uint64_t start
= guest_offset
;
824 uint64_t end
= start
+ bytes
;
825 uint64_t old_start
= l2meta_cow_start(old_alloc
);
826 uint64_t old_end
= l2meta_cow_end(old_alloc
);
828 if (end
<= old_start
|| start
>= old_end
) {
829 /* No intersection */
831 if (start
< old_start
) {
832 /* Stop at the start of a running allocation */
833 bytes
= old_start
- start
;
838 /* Stop if already an l2meta exists. After yielding, it wouldn't
839 * be valid any more, so we'd have to clean up the old L2Metas
840 * and deal with requests depending on them before starting to
841 * gather new ones. Not worth the trouble. */
842 if (bytes
== 0 && *m
) {
848 /* Wait for the dependency to complete. We need to recheck
849 * the free/allocated clusters when we continue. */
850 qemu_co_mutex_unlock(&s
->lock
);
851 qemu_co_queue_wait(&old_alloc
->dependent_requests
);
852 qemu_co_mutex_lock(&s
->lock
);
858 /* Make sure that existing clusters and new allocations are only used up to
859 * the next dependency if we shortened the request above */
866 * Checks how many already allocated clusters that don't require a copy on
867 * write there are at the given guest_offset (up to *bytes). If
868 * *host_offset is not zero, only physically contiguous clusters beginning at
869 * this host offset are counted.
871 * Note that guest_offset may not be cluster aligned. In this case, the
872 * returned *host_offset points to exact byte referenced by guest_offset and
873 * therefore isn't cluster aligned as well.
876 * 0: if no allocated clusters are available at the given offset.
877 * *bytes is normally unchanged. It is set to 0 if the cluster
878 * is allocated and doesn't need COW, but doesn't have the right
881 * 1: if allocated clusters that don't require a COW are available at
882 * the requested offset. *bytes may have decreased and describes
883 * the length of the area that can be written to.
885 * -errno: in error cases
887 static int handle_copied(BlockDriverState
*bs
, uint64_t guest_offset
,
888 uint64_t *host_offset
, uint64_t *bytes
, QCowL2Meta
**m
)
890 BDRVQcowState
*s
= bs
->opaque
;
892 uint64_t cluster_offset
;
894 unsigned int nb_clusters
;
895 unsigned int keep_clusters
;
898 trace_qcow2_handle_copied(qemu_coroutine_self(), guest_offset
, *host_offset
,
901 assert(*host_offset
== 0 || offset_into_cluster(s
, guest_offset
)
902 == offset_into_cluster(s
, *host_offset
));
905 * Calculate the number of clusters to look for. We stop at L2 table
906 * boundaries to keep things simple.
909 size_to_clusters(s
, offset_into_cluster(s
, guest_offset
) + *bytes
);
911 l2_index
= offset_to_l2_index(s
, guest_offset
);
912 nb_clusters
= MIN(nb_clusters
, s
->l2_size
- l2_index
);
914 /* Find L2 entry for the first involved cluster */
915 ret
= get_cluster_table(bs
, guest_offset
, &l2_table
, &l2_index
);
920 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
922 /* Check how many clusters are already allocated and don't need COW */
923 if (qcow2_get_cluster_type(cluster_offset
) == QCOW2_CLUSTER_NORMAL
924 && (cluster_offset
& QCOW_OFLAG_COPIED
))
926 /* If a specific host_offset is required, check it */
927 bool offset_matches
=
928 (cluster_offset
& L2E_OFFSET_MASK
) == *host_offset
;
930 if (*host_offset
!= 0 && !offset_matches
) {
936 /* We keep all QCOW_OFLAG_COPIED clusters */
938 count_contiguous_clusters(nb_clusters
, s
->cluster_size
,
940 QCOW_OFLAG_COPIED
| QCOW_OFLAG_ZERO
);
941 assert(keep_clusters
<= nb_clusters
);
944 keep_clusters
* s
->cluster_size
945 - offset_into_cluster(s
, guest_offset
));
954 pret
= qcow2_cache_put(bs
, s
->l2_table_cache
, (void**) &l2_table
);
959 /* Only return a host offset if we actually made progress. Otherwise we
960 * would make requirements for handle_alloc() that it can't fulfill */
962 *host_offset
= (cluster_offset
& L2E_OFFSET_MASK
)
963 + offset_into_cluster(s
, guest_offset
);
970 * Allocates new clusters for the given guest_offset.
972 * At most *nb_clusters are allocated, and on return *nb_clusters is updated to
973 * contain the number of clusters that have been allocated and are contiguous
976 * If *host_offset is non-zero, it specifies the offset in the image file at
977 * which the new clusters must start. *nb_clusters can be 0 on return in this
978 * case if the cluster at host_offset is already in use. If *host_offset is
979 * zero, the clusters can be allocated anywhere in the image file.
981 * *host_offset is updated to contain the offset into the image file at which
982 * the first allocated cluster starts.
984 * Return 0 on success and -errno in error cases. -EAGAIN means that the
985 * function has been waiting for another request and the allocation must be
986 * restarted, but the whole request should not be failed.
988 static int do_alloc_cluster_offset(BlockDriverState
*bs
, uint64_t guest_offset
,
989 uint64_t *host_offset
, unsigned int *nb_clusters
)
991 BDRVQcowState
*s
= bs
->opaque
;
993 trace_qcow2_do_alloc_clusters_offset(qemu_coroutine_self(), guest_offset
,
994 *host_offset
, *nb_clusters
);
996 /* Allocate new clusters */
997 trace_qcow2_cluster_alloc_phys(qemu_coroutine_self());
998 if (*host_offset
== 0) {
999 int64_t cluster_offset
=
1000 qcow2_alloc_clusters(bs
, *nb_clusters
* s
->cluster_size
);
1001 if (cluster_offset
< 0) {
1002 return cluster_offset
;
1004 *host_offset
= cluster_offset
;
1007 int ret
= qcow2_alloc_clusters_at(bs
, *host_offset
, *nb_clusters
);
1017 * Allocates new clusters for an area that either is yet unallocated or needs a
1018 * copy on write. If *host_offset is non-zero, clusters are only allocated if
1019 * the new allocation can match the specified host offset.
1021 * Note that guest_offset may not be cluster aligned. In this case, the
1022 * returned *host_offset points to exact byte referenced by guest_offset and
1023 * therefore isn't cluster aligned as well.
1026 * 0: if no clusters could be allocated. *bytes is set to 0,
1027 * *host_offset is left unchanged.
1029 * 1: if new clusters were allocated. *bytes may be decreased if the
1030 * new allocation doesn't cover all of the requested area.
1031 * *host_offset is updated to contain the host offset of the first
1032 * newly allocated cluster.
1034 * -errno: in error cases
1036 static int handle_alloc(BlockDriverState
*bs
, uint64_t guest_offset
,
1037 uint64_t *host_offset
, uint64_t *bytes
, QCowL2Meta
**m
)
1039 BDRVQcowState
*s
= bs
->opaque
;
1043 unsigned int nb_clusters
;
1046 uint64_t alloc_cluster_offset
;
1048 trace_qcow2_handle_alloc(qemu_coroutine_self(), guest_offset
, *host_offset
,
1053 * Calculate the number of clusters to look for. We stop at L2 table
1054 * boundaries to keep things simple.
1057 size_to_clusters(s
, offset_into_cluster(s
, guest_offset
) + *bytes
);
1059 l2_index
= offset_to_l2_index(s
, guest_offset
);
1060 nb_clusters
= MIN(nb_clusters
, s
->l2_size
- l2_index
);
1062 /* Find L2 entry for the first involved cluster */
1063 ret
= get_cluster_table(bs
, guest_offset
, &l2_table
, &l2_index
);
1068 entry
= be64_to_cpu(l2_table
[l2_index
]);
1070 /* For the moment, overwrite compressed clusters one by one */
1071 if (entry
& QCOW_OFLAG_COMPRESSED
) {
1074 nb_clusters
= count_cow_clusters(s
, nb_clusters
, l2_table
, l2_index
);
1077 /* This function is only called when there were no non-COW clusters, so if
1078 * we can't find any unallocated or COW clusters either, something is
1079 * wrong with our code. */
1080 assert(nb_clusters
> 0);
1082 ret
= qcow2_cache_put(bs
, s
->l2_table_cache
, (void**) &l2_table
);
1087 /* Allocate, if necessary at a given offset in the image file */
1088 alloc_cluster_offset
= start_of_cluster(s
, *host_offset
);
1089 ret
= do_alloc_cluster_offset(bs
, guest_offset
, &alloc_cluster_offset
,
1095 /* Can't extend contiguous allocation */
1096 if (nb_clusters
== 0) {
1102 * Save info needed for meta data update.
1104 * requested_sectors: Number of sectors from the start of the first
1105 * newly allocated cluster to the end of the (possibly shortened
1106 * before) write request.
1108 * avail_sectors: Number of sectors from the start of the first
1109 * newly allocated to the end of the last newly allocated cluster.
1111 * nb_sectors: The number of sectors from the start of the first
1112 * newly allocated cluster to the end of the area that the write
1113 * request actually writes to (excluding COW at the end)
1115 int requested_sectors
=
1116 (*bytes
+ offset_into_cluster(s
, guest_offset
))
1117 >> BDRV_SECTOR_BITS
;
1118 int avail_sectors
= nb_clusters
1119 << (s
->cluster_bits
- BDRV_SECTOR_BITS
);
1120 int alloc_n_start
= offset_into_cluster(s
, guest_offset
)
1121 >> BDRV_SECTOR_BITS
;
1122 int nb_sectors
= MIN(requested_sectors
, avail_sectors
);
1123 QCowL2Meta
*old_m
= *m
;
1125 *m
= g_malloc0(sizeof(**m
));
1127 **m
= (QCowL2Meta
) {
1130 .alloc_offset
= alloc_cluster_offset
,
1131 .offset
= start_of_cluster(s
, guest_offset
),
1132 .nb_clusters
= nb_clusters
,
1133 .nb_available
= nb_sectors
,
1137 .nb_sectors
= alloc_n_start
,
1140 .offset
= nb_sectors
* BDRV_SECTOR_SIZE
,
1141 .nb_sectors
= avail_sectors
- nb_sectors
,
1144 qemu_co_queue_init(&(*m
)->dependent_requests
);
1145 QLIST_INSERT_HEAD(&s
->cluster_allocs
, *m
, next_in_flight
);
1147 *host_offset
= alloc_cluster_offset
+ offset_into_cluster(s
, guest_offset
);
1148 *bytes
= MIN(*bytes
, (nb_sectors
* BDRV_SECTOR_SIZE
)
1149 - offset_into_cluster(s
, guest_offset
));
1150 assert(*bytes
!= 0);
1155 if (*m
&& (*m
)->nb_clusters
> 0) {
1156 QLIST_REMOVE(*m
, next_in_flight
);
1162 * alloc_cluster_offset
1164 * For a given offset on the virtual disk, find the cluster offset in qcow2
1165 * file. If the offset is not found, allocate a new cluster.
1167 * If the cluster was already allocated, m->nb_clusters is set to 0 and
1168 * other fields in m are meaningless.
1170 * If the cluster is newly allocated, m->nb_clusters is set to the number of
1171 * contiguous clusters that have been allocated. In this case, the other
1172 * fields of m are valid and contain information about the first allocated
1175 * If the request conflicts with another write request in flight, the coroutine
1176 * is queued and will be reentered when the dependency has completed.
1178 * Return 0 on success and -errno in error cases
1180 int qcow2_alloc_cluster_offset(BlockDriverState
*bs
, uint64_t offset
,
1181 int *num
, uint64_t *host_offset
, QCowL2Meta
**m
)
1183 BDRVQcowState
*s
= bs
->opaque
;
1184 uint64_t start
, remaining
;
1185 uint64_t cluster_offset
;
1189 trace_qcow2_alloc_clusters_offset(qemu_coroutine_self(), offset
, *num
);
1191 assert((offset
& ~BDRV_SECTOR_MASK
) == 0);
1195 remaining
= *num
<< BDRV_SECTOR_BITS
;
1203 if (!*host_offset
) {
1204 *host_offset
= start_of_cluster(s
, cluster_offset
);
1207 assert(remaining
>= cur_bytes
);
1210 remaining
-= cur_bytes
;
1211 cluster_offset
+= cur_bytes
;
1213 if (remaining
== 0) {
1217 cur_bytes
= remaining
;
1220 * Now start gathering as many contiguous clusters as possible:
1222 * 1. Check for overlaps with in-flight allocations
1224 * a) Overlap not in the first cluster -> shorten this request and
1225 * let the caller handle the rest in its next loop iteration.
1227 * b) Real overlaps of two requests. Yield and restart the search
1228 * for contiguous clusters (the situation could have changed
1229 * while we were sleeping)
1231 * c) TODO: Request starts in the same cluster as the in-flight
1232 * allocation ends. Shorten the COW of the in-fight allocation,
1233 * set cluster_offset to write to the same cluster and set up
1234 * the right synchronisation between the in-flight request and
1237 ret
= handle_dependencies(bs
, start
, &cur_bytes
, m
);
1238 if (ret
== -EAGAIN
) {
1239 /* Currently handle_dependencies() doesn't yield if we already had
1240 * an allocation. If it did, we would have to clean up the L2Meta
1241 * structs before starting over. */
1244 } else if (ret
< 0) {
1246 } else if (cur_bytes
== 0) {
1249 /* handle_dependencies() may have decreased cur_bytes (shortened
1250 * the allocations below) so that the next dependency is processed
1251 * correctly during the next loop iteration. */
1255 * 2. Count contiguous COPIED clusters.
1257 ret
= handle_copied(bs
, start
, &cluster_offset
, &cur_bytes
, m
);
1262 } else if (cur_bytes
== 0) {
1267 * 3. If the request still hasn't completed, allocate new clusters,
1268 * considering any cluster_offset of steps 1c or 2.
1270 ret
= handle_alloc(bs
, start
, &cluster_offset
, &cur_bytes
, m
);
1276 assert(cur_bytes
== 0);
1281 *num
-= remaining
>> BDRV_SECTOR_BITS
;
1283 assert(*host_offset
!= 0);
1288 static int decompress_buffer(uint8_t *out_buf
, int out_buf_size
,
1289 const uint8_t *buf
, int buf_size
)
1291 z_stream strm1
, *strm
= &strm1
;
1294 memset(strm
, 0, sizeof(*strm
));
1296 strm
->next_in
= (uint8_t *)buf
;
1297 strm
->avail_in
= buf_size
;
1298 strm
->next_out
= out_buf
;
1299 strm
->avail_out
= out_buf_size
;
1301 ret
= inflateInit2(strm
, -12);
1304 ret
= inflate(strm
, Z_FINISH
);
1305 out_len
= strm
->next_out
- out_buf
;
1306 if ((ret
!= Z_STREAM_END
&& ret
!= Z_BUF_ERROR
) ||
1307 out_len
!= out_buf_size
) {
1315 int qcow2_decompress_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
)
1317 BDRVQcowState
*s
= bs
->opaque
;
1318 int ret
, csize
, nb_csectors
, sector_offset
;
1321 coffset
= cluster_offset
& s
->cluster_offset_mask
;
1322 if (s
->cluster_cache_offset
!= coffset
) {
1323 nb_csectors
= ((cluster_offset
>> s
->csize_shift
) & s
->csize_mask
) + 1;
1324 sector_offset
= coffset
& 511;
1325 csize
= nb_csectors
* 512 - sector_offset
;
1326 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_COMPRESSED
);
1327 ret
= bdrv_read(bs
->file
, coffset
>> 9, s
->cluster_data
, nb_csectors
);
1331 if (decompress_buffer(s
->cluster_cache
, s
->cluster_size
,
1332 s
->cluster_data
+ sector_offset
, csize
) < 0) {
1335 s
->cluster_cache_offset
= coffset
;
1341 * This discards as many clusters of nb_clusters as possible at once (i.e.
1342 * all clusters in the same L2 table) and returns the number of discarded
1345 static int discard_single_l2(BlockDriverState
*bs
, uint64_t offset
,
1346 unsigned int nb_clusters
, enum qcow2_discard_type type
)
1348 BDRVQcowState
*s
= bs
->opaque
;
1354 ret
= get_cluster_table(bs
, offset
, &l2_table
, &l2_index
);
1359 /* Limit nb_clusters to one L2 table */
1360 nb_clusters
= MIN(nb_clusters
, s
->l2_size
- l2_index
);
1362 for (i
= 0; i
< nb_clusters
; i
++) {
1363 uint64_t old_offset
;
1365 old_offset
= be64_to_cpu(l2_table
[l2_index
+ i
]);
1368 * Make sure that a discarded area reads back as zeroes for v3 images
1369 * (we cannot do it for v2 without actually writing a zero-filled
1370 * buffer). We can skip the operation if the cluster is already marked
1371 * as zero, or if it's unallocated and we don't have a backing file.
1373 * TODO We might want to use bdrv_get_block_status(bs) here, but we're
1374 * holding s->lock, so that doesn't work today.
1376 if (old_offset
& QCOW_OFLAG_ZERO
) {
1380 if ((old_offset
& L2E_OFFSET_MASK
) == 0 && !bs
->backing_hd
) {
1384 /* First remove L2 entries */
1385 qcow2_cache_entry_mark_dirty(s
->l2_table_cache
, l2_table
);
1386 if (s
->qcow_version
>= 3) {
1387 l2_table
[l2_index
+ i
] = cpu_to_be64(QCOW_OFLAG_ZERO
);
1389 l2_table
[l2_index
+ i
] = cpu_to_be64(0);
1392 /* Then decrease the refcount */
1393 qcow2_free_any_clusters(bs
, old_offset
, 1, type
);
1396 ret
= qcow2_cache_put(bs
, s
->l2_table_cache
, (void**) &l2_table
);
1404 int qcow2_discard_clusters(BlockDriverState
*bs
, uint64_t offset
,
1405 int nb_sectors
, enum qcow2_discard_type type
)
1407 BDRVQcowState
*s
= bs
->opaque
;
1408 uint64_t end_offset
;
1409 unsigned int nb_clusters
;
1412 end_offset
= offset
+ (nb_sectors
<< BDRV_SECTOR_BITS
);
1414 /* Round start up and end down */
1415 offset
= align_offset(offset
, s
->cluster_size
);
1416 end_offset
= start_of_cluster(s
, end_offset
);
1418 if (offset
> end_offset
) {
1422 nb_clusters
= size_to_clusters(s
, end_offset
- offset
);
1424 s
->cache_discards
= true;
1426 /* Each L2 table is handled by its own loop iteration */
1427 while (nb_clusters
> 0) {
1428 ret
= discard_single_l2(bs
, offset
, nb_clusters
, type
);
1434 offset
+= (ret
* s
->cluster_size
);
1439 s
->cache_discards
= false;
1440 qcow2_process_discards(bs
, ret
);
1446 * This zeroes as many clusters of nb_clusters as possible at once (i.e.
1447 * all clusters in the same L2 table) and returns the number of zeroed
1450 static int zero_single_l2(BlockDriverState
*bs
, uint64_t offset
,
1451 unsigned int nb_clusters
)
1453 BDRVQcowState
*s
= bs
->opaque
;
1459 ret
= get_cluster_table(bs
, offset
, &l2_table
, &l2_index
);
1464 /* Limit nb_clusters to one L2 table */
1465 nb_clusters
= MIN(nb_clusters
, s
->l2_size
- l2_index
);
1467 for (i
= 0; i
< nb_clusters
; i
++) {
1468 uint64_t old_offset
;
1470 old_offset
= be64_to_cpu(l2_table
[l2_index
+ i
]);
1472 /* Update L2 entries */
1473 qcow2_cache_entry_mark_dirty(s
->l2_table_cache
, l2_table
);
1474 if (old_offset
& QCOW_OFLAG_COMPRESSED
) {
1475 l2_table
[l2_index
+ i
] = cpu_to_be64(QCOW_OFLAG_ZERO
);
1476 qcow2_free_any_clusters(bs
, old_offset
, 1, QCOW2_DISCARD_REQUEST
);
1478 l2_table
[l2_index
+ i
] |= cpu_to_be64(QCOW_OFLAG_ZERO
);
1482 ret
= qcow2_cache_put(bs
, s
->l2_table_cache
, (void**) &l2_table
);
1490 int qcow2_zero_clusters(BlockDriverState
*bs
, uint64_t offset
, int nb_sectors
)
1492 BDRVQcowState
*s
= bs
->opaque
;
1493 unsigned int nb_clusters
;
1496 /* The zero flag is only supported by version 3 and newer */
1497 if (s
->qcow_version
< 3) {
1501 /* Each L2 table is handled by its own loop iteration */
1502 nb_clusters
= size_to_clusters(s
, nb_sectors
<< BDRV_SECTOR_BITS
);
1504 s
->cache_discards
= true;
1506 while (nb_clusters
> 0) {
1507 ret
= zero_single_l2(bs
, offset
, nb_clusters
);
1513 offset
+= (ret
* s
->cluster_size
);
1518 s
->cache_discards
= false;
1519 qcow2_process_discards(bs
, ret
);
1525 * Expands all zero clusters in a specific L1 table (or deallocates them, for
1526 * non-backed non-pre-allocated zero clusters).
1528 * expanded_clusters is a bitmap where every bit corresponds to one cluster in
1529 * the image file; a bit gets set if the corresponding cluster has been used for
1530 * zero expansion (i.e., has been filled with zeroes and is referenced from an
1531 * L2 table). nb_clusters contains the total cluster count of the image file,
1532 * i.e., the number of bits in expanded_clusters.
1534 static int expand_zero_clusters_in_l1(BlockDriverState
*bs
, uint64_t *l1_table
,
1535 int l1_size
, uint8_t **expanded_clusters
,
1536 uint64_t *nb_clusters
)
1538 BDRVQcowState
*s
= bs
->opaque
;
1539 bool is_active_l1
= (l1_table
== s
->l1_table
);
1540 uint64_t *l2_table
= NULL
;
1544 if (!is_active_l1
) {
1545 /* inactive L2 tables require a buffer to be stored in when loading
1547 l2_table
= qemu_blockalign(bs
, s
->cluster_size
);
1550 for (i
= 0; i
< l1_size
; i
++) {
1551 uint64_t l2_offset
= l1_table
[i
] & L1E_OFFSET_MASK
;
1552 bool l2_dirty
= false;
1560 /* get active L2 tables from cache */
1561 ret
= qcow2_cache_get(bs
, s
->l2_table_cache
, l2_offset
,
1562 (void **)&l2_table
);
1564 /* load inactive L2 tables from disk */
1565 ret
= bdrv_read(bs
->file
, l2_offset
/ BDRV_SECTOR_SIZE
,
1566 (void *)l2_table
, s
->cluster_sectors
);
1572 for (j
= 0; j
< s
->l2_size
; j
++) {
1573 uint64_t l2_entry
= be64_to_cpu(l2_table
[j
]);
1574 int64_t offset
= l2_entry
& L2E_OFFSET_MASK
, cluster_index
;
1575 int cluster_type
= qcow2_get_cluster_type(l2_entry
);
1576 bool preallocated
= offset
!= 0;
1578 if (cluster_type
== QCOW2_CLUSTER_NORMAL
) {
1579 cluster_index
= offset
>> s
->cluster_bits
;
1580 assert((cluster_index
>= 0) && (cluster_index
< *nb_clusters
));
1581 if ((*expanded_clusters
)[cluster_index
/ 8] &
1582 (1 << (cluster_index
% 8))) {
1583 /* Probably a shared L2 table; this cluster was a zero
1584 * cluster which has been expanded, its refcount
1585 * therefore most likely requires an update. */
1586 ret
= qcow2_update_cluster_refcount(bs
, cluster_index
, 1,
1587 QCOW2_DISCARD_NEVER
);
1591 /* Since we just increased the refcount, the COPIED flag may
1592 * no longer be set. */
1593 l2_table
[j
] = cpu_to_be64(l2_entry
& ~QCOW_OFLAG_COPIED
);
1598 else if (qcow2_get_cluster_type(l2_entry
) != QCOW2_CLUSTER_ZERO
) {
1602 if (!preallocated
) {
1603 if (!bs
->backing_hd
) {
1604 /* not backed; therefore we can simply deallocate the
1611 offset
= qcow2_alloc_clusters(bs
, s
->cluster_size
);
1618 ret
= qcow2_pre_write_overlap_check(bs
, 0, offset
, s
->cluster_size
);
1620 if (!preallocated
) {
1621 qcow2_free_clusters(bs
, offset
, s
->cluster_size
,
1622 QCOW2_DISCARD_ALWAYS
);
1627 ret
= bdrv_write_zeroes(bs
->file
, offset
/ BDRV_SECTOR_SIZE
,
1628 s
->cluster_sectors
, 0);
1630 if (!preallocated
) {
1631 qcow2_free_clusters(bs
, offset
, s
->cluster_size
,
1632 QCOW2_DISCARD_ALWAYS
);
1637 l2_table
[j
] = cpu_to_be64(offset
| QCOW_OFLAG_COPIED
);
1640 cluster_index
= offset
>> s
->cluster_bits
;
1642 if (cluster_index
>= *nb_clusters
) {
1643 uint64_t old_bitmap_size
= (*nb_clusters
+ 7) / 8;
1644 uint64_t new_bitmap_size
;
1645 /* The offset may lie beyond the old end of the underlying image
1646 * file for growable files only */
1647 assert(bs
->file
->growable
);
1648 *nb_clusters
= size_to_clusters(s
, bs
->file
->total_sectors
*
1650 new_bitmap_size
= (*nb_clusters
+ 7) / 8;
1651 *expanded_clusters
= g_realloc(*expanded_clusters
,
1653 /* clear the newly allocated space */
1654 memset(&(*expanded_clusters
)[old_bitmap_size
], 0,
1655 new_bitmap_size
- old_bitmap_size
);
1658 assert((cluster_index
>= 0) && (cluster_index
< *nb_clusters
));
1659 (*expanded_clusters
)[cluster_index
/ 8] |= 1 << (cluster_index
% 8);
1664 qcow2_cache_entry_mark_dirty(s
->l2_table_cache
, l2_table
);
1665 qcow2_cache_depends_on_flush(s
->l2_table_cache
);
1667 ret
= qcow2_cache_put(bs
, s
->l2_table_cache
, (void **)&l2_table
);
1674 ret
= qcow2_pre_write_overlap_check(bs
,
1675 QCOW2_OL_INACTIVE_L2
| QCOW2_OL_ACTIVE_L2
, l2_offset
,
1681 ret
= bdrv_write(bs
->file
, l2_offset
/ BDRV_SECTOR_SIZE
,
1682 (void *)l2_table
, s
->cluster_sectors
);
1694 if (!is_active_l1
) {
1695 qemu_vfree(l2_table
);
1698 qcow2_cache_put(bs
, s
->l2_table_cache
, (void **)&l2_table
);
1700 ret
= qcow2_cache_put(bs
, s
->l2_table_cache
,
1701 (void **)&l2_table
);
1709 * For backed images, expands all zero clusters on the image. For non-backed
1710 * images, deallocates all non-pre-allocated zero clusters (and claims the
1711 * allocation for pre-allocated ones). This is important for downgrading to a
1712 * qcow2 version which doesn't yet support metadata zero clusters.
1714 int qcow2_expand_zero_clusters(BlockDriverState
*bs
)
1716 BDRVQcowState
*s
= bs
->opaque
;
1717 uint64_t *l1_table
= NULL
;
1718 uint64_t nb_clusters
;
1719 uint8_t *expanded_clusters
;
1723 nb_clusters
= size_to_clusters(s
, bs
->file
->total_sectors
*
1725 expanded_clusters
= g_malloc0((nb_clusters
+ 7) / 8);
1727 ret
= expand_zero_clusters_in_l1(bs
, s
->l1_table
, s
->l1_size
,
1728 &expanded_clusters
, &nb_clusters
);
1733 /* Inactive L1 tables may point to active L2 tables - therefore it is
1734 * necessary to flush the L2 table cache before trying to access the L2
1735 * tables pointed to by inactive L1 entries (else we might try to expand
1736 * zero clusters that have already been expanded); furthermore, it is also
1737 * necessary to empty the L2 table cache, since it may contain tables which
1738 * are now going to be modified directly on disk, bypassing the cache.
1739 * qcow2_cache_empty() does both for us. */
1740 ret
= qcow2_cache_empty(bs
, s
->l2_table_cache
);
1745 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
1746 int l1_sectors
= (s
->snapshots
[i
].l1_size
* sizeof(uint64_t) +
1747 BDRV_SECTOR_SIZE
- 1) / BDRV_SECTOR_SIZE
;
1749 l1_table
= g_realloc(l1_table
, l1_sectors
* BDRV_SECTOR_SIZE
);
1751 ret
= bdrv_read(bs
->file
, s
->snapshots
[i
].l1_table_offset
/
1752 BDRV_SECTOR_SIZE
, (void *)l1_table
, l1_sectors
);
1757 for (j
= 0; j
< s
->snapshots
[i
].l1_size
; j
++) {
1758 be64_to_cpus(&l1_table
[j
]);
1761 ret
= expand_zero_clusters_in_l1(bs
, l1_table
, s
->snapshots
[i
].l1_size
,
1762 &expanded_clusters
, &nb_clusters
);
1771 g_free(expanded_clusters
);