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_int.h"
29 #include "block/qcow2.h"
31 int qcow2_grow_l1_table(BlockDriverState
*bs
, int min_size
)
33 BDRVQcowState
*s
= bs
->opaque
;
34 int new_l1_size
, new_l1_size2
, ret
, i
;
35 uint64_t *new_l1_table
;
36 int64_t new_l1_table_offset
;
39 new_l1_size
= s
->l1_size
;
40 if (min_size
<= new_l1_size
)
42 if (new_l1_size
== 0) {
45 while (min_size
> new_l1_size
) {
46 new_l1_size
= (new_l1_size
* 3 + 1) / 2;
49 printf("grow l1_table from %d to %d\n", s
->l1_size
, new_l1_size
);
52 new_l1_size2
= sizeof(uint64_t) * new_l1_size
;
53 new_l1_table
= qemu_mallocz(align_offset(new_l1_size2
, 512));
54 memcpy(new_l1_table
, s
->l1_table
, s
->l1_size
* sizeof(uint64_t));
56 /* write new table (align to cluster) */
57 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_GROW_ALLOC_TABLE
);
58 new_l1_table_offset
= qcow2_alloc_clusters(bs
, new_l1_size2
);
59 if (new_l1_table_offset
< 0) {
60 qemu_free(new_l1_table
);
61 return new_l1_table_offset
;
64 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_GROW_WRITE_TABLE
);
65 for(i
= 0; i
< s
->l1_size
; i
++)
66 new_l1_table
[i
] = cpu_to_be64(new_l1_table
[i
]);
67 ret
= bdrv_pwrite(bs
->file
, new_l1_table_offset
, new_l1_table
, new_l1_size2
);
68 if (ret
!= new_l1_size2
)
70 for(i
= 0; i
< s
->l1_size
; i
++)
71 new_l1_table
[i
] = be64_to_cpu(new_l1_table
[i
]);
74 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_GROW_ACTIVATE_TABLE
);
75 cpu_to_be32w((uint32_t*)data
, new_l1_size
);
76 cpu_to_be64w((uint64_t*)(data
+ 4), new_l1_table_offset
);
77 ret
= bdrv_pwrite(bs
->file
, offsetof(QCowHeader
, l1_size
), data
,sizeof(data
));
78 if (ret
!= sizeof(data
)) {
81 qemu_free(s
->l1_table
);
82 qcow2_free_clusters(bs
, s
->l1_table_offset
, s
->l1_size
* sizeof(uint64_t));
83 s
->l1_table_offset
= new_l1_table_offset
;
84 s
->l1_table
= new_l1_table
;
85 s
->l1_size
= new_l1_size
;
88 qemu_free(new_l1_table
);
89 qcow2_free_clusters(bs
, new_l1_table_offset
, new_l1_size2
);
90 return ret
< 0 ? ret
: -EIO
;
93 void qcow2_l2_cache_reset(BlockDriverState
*bs
)
95 BDRVQcowState
*s
= bs
->opaque
;
97 memset(s
->l2_cache
, 0, s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
98 memset(s
->l2_cache_offsets
, 0, L2_CACHE_SIZE
* sizeof(uint64_t));
99 memset(s
->l2_cache_counts
, 0, L2_CACHE_SIZE
* sizeof(uint32_t));
102 static inline int l2_cache_new_entry(BlockDriverState
*bs
)
104 BDRVQcowState
*s
= bs
->opaque
;
108 /* find a new entry in the least used one */
110 min_count
= 0xffffffff;
111 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
112 if (s
->l2_cache_counts
[i
] < min_count
) {
113 min_count
= s
->l2_cache_counts
[i
];
123 * seek l2_offset in the l2_cache table
124 * if not found, return NULL,
126 * increments the l2 cache hit count of the entry,
127 * if counter overflow, divide by two all counters
128 * return the pointer to the l2 cache entry
132 static uint64_t *seek_l2_table(BDRVQcowState
*s
, uint64_t l2_offset
)
136 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
137 if (l2_offset
== s
->l2_cache_offsets
[i
]) {
138 /* increment the hit count */
139 if (++s
->l2_cache_counts
[i
] == 0xffffffff) {
140 for(j
= 0; j
< L2_CACHE_SIZE
; j
++) {
141 s
->l2_cache_counts
[j
] >>= 1;
144 return s
->l2_cache
+ (i
<< s
->l2_bits
);
153 * Loads a L2 table into memory. If the table is in the cache, the cache
154 * is used; otherwise the L2 table is loaded from the image file.
156 * Returns a pointer to the L2 table on success, or NULL if the read from
157 * the image file failed.
160 static uint64_t *l2_load(BlockDriverState
*bs
, uint64_t l2_offset
)
162 BDRVQcowState
*s
= bs
->opaque
;
166 /* seek if the table for the given offset is in the cache */
168 l2_table
= seek_l2_table(s
, l2_offset
);
169 if (l2_table
!= NULL
)
172 /* not found: load a new entry in the least used one */
174 min_index
= l2_cache_new_entry(bs
);
175 l2_table
= s
->l2_cache
+ (min_index
<< s
->l2_bits
);
177 BLKDBG_EVENT(bs
->file
, BLKDBG_L2_LOAD
);
178 if (bdrv_pread(bs
->file
, l2_offset
, l2_table
, s
->l2_size
* sizeof(uint64_t)) !=
179 s
->l2_size
* sizeof(uint64_t))
181 s
->l2_cache_offsets
[min_index
] = l2_offset
;
182 s
->l2_cache_counts
[min_index
] = 1;
188 * Writes one sector of the L1 table to the disk (can't update single entries
189 * and we really don't want bdrv_pread to perform a read-modify-write)
191 #define L1_ENTRIES_PER_SECTOR (512 / 8)
192 static int write_l1_entry(BlockDriverState
*bs
, int l1_index
)
194 BDRVQcowState
*s
= bs
->opaque
;
195 uint64_t buf
[L1_ENTRIES_PER_SECTOR
];
199 l1_start_index
= l1_index
& ~(L1_ENTRIES_PER_SECTOR
- 1);
200 for (i
= 0; i
< L1_ENTRIES_PER_SECTOR
; i
++) {
201 buf
[i
] = cpu_to_be64(s
->l1_table
[l1_start_index
+ i
]);
204 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_UPDATE
);
205 ret
= bdrv_pwrite(bs
->file
, s
->l1_table_offset
+ 8 * l1_start_index
,
217 * Allocate a new l2 entry in the file. If l1_index points to an already
218 * used entry in the L2 table (i.e. we are doing a copy on write for the L2
219 * table) copy the contents of the old L2 table into the newly allocated one.
220 * Otherwise the new table is initialized with zeros.
224 static int l2_allocate(BlockDriverState
*bs
, int l1_index
, uint64_t **table
)
226 BDRVQcowState
*s
= bs
->opaque
;
228 uint64_t old_l2_offset
;
233 old_l2_offset
= s
->l1_table
[l1_index
];
235 /* allocate a new l2 entry */
237 l2_offset
= qcow2_alloc_clusters(bs
, s
->l2_size
* sizeof(uint64_t));
242 /* allocate a new entry in the l2 cache */
244 min_index
= l2_cache_new_entry(bs
);
245 l2_table
= s
->l2_cache
+ (min_index
<< s
->l2_bits
);
247 if (old_l2_offset
== 0) {
248 /* if there was no old l2 table, clear the new table */
249 memset(l2_table
, 0, s
->l2_size
* sizeof(uint64_t));
251 /* if there was an old l2 table, read it from the disk */
252 BLKDBG_EVENT(bs
->file
, BLKDBG_L2_ALLOC_COW_READ
);
253 ret
= bdrv_pread(bs
->file
, old_l2_offset
, l2_table
,
254 s
->l2_size
* sizeof(uint64_t));
259 /* write the l2 table to the file */
260 BLKDBG_EVENT(bs
->file
, BLKDBG_L2_ALLOC_WRITE
);
261 ret
= bdrv_pwrite(bs
->file
, l2_offset
, l2_table
,
262 s
->l2_size
* sizeof(uint64_t));
267 /* update the L1 entry */
268 s
->l1_table
[l1_index
] = l2_offset
| QCOW_OFLAG_COPIED
;
269 ret
= write_l1_entry(bs
, l1_index
);
274 /* update the l2 cache entry */
276 s
->l2_cache_offsets
[min_index
] = l2_offset
;
277 s
->l2_cache_counts
[min_index
] = 1;
283 qcow2_l2_cache_reset(bs
);
287 static int count_contiguous_clusters(uint64_t nb_clusters
, int cluster_size
,
288 uint64_t *l2_table
, uint64_t start
, uint64_t mask
)
291 uint64_t offset
= be64_to_cpu(l2_table
[0]) & ~mask
;
296 for (i
= start
; i
< start
+ nb_clusters
; i
++)
297 if (offset
+ (uint64_t) i
* cluster_size
!= (be64_to_cpu(l2_table
[i
]) & ~mask
))
303 static int count_contiguous_free_clusters(uint64_t nb_clusters
, uint64_t *l2_table
)
307 while(nb_clusters
-- && l2_table
[i
] == 0)
313 /* The crypt function is compatible with the linux cryptoloop
314 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
316 void qcow2_encrypt_sectors(BDRVQcowState
*s
, int64_t sector_num
,
317 uint8_t *out_buf
, const uint8_t *in_buf
,
318 int nb_sectors
, int enc
,
327 for(i
= 0; i
< nb_sectors
; i
++) {
328 ivec
.ll
[0] = cpu_to_le64(sector_num
);
330 AES_cbc_encrypt(in_buf
, out_buf
, 512, key
,
339 static int qcow_read(BlockDriverState
*bs
, int64_t sector_num
,
340 uint8_t *buf
, int nb_sectors
)
342 BDRVQcowState
*s
= bs
->opaque
;
343 int ret
, index_in_cluster
, n
, n1
;
344 uint64_t cluster_offset
;
346 while (nb_sectors
> 0) {
348 cluster_offset
= qcow2_get_cluster_offset(bs
, sector_num
<< 9, &n
);
349 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
350 if (!cluster_offset
) {
351 if (bs
->backing_hd
) {
352 /* read from the base image */
353 n1
= qcow2_backing_read1(bs
->backing_hd
, sector_num
, buf
, n
);
355 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_BACKING
);
356 ret
= bdrv_read(bs
->backing_hd
, sector_num
, buf
, n1
);
361 memset(buf
, 0, 512 * n
);
363 } else if (cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
364 if (qcow2_decompress_cluster(bs
, cluster_offset
) < 0)
366 memcpy(buf
, s
->cluster_cache
+ index_in_cluster
* 512, 512 * n
);
368 BLKDBG_EVENT(bs
->file
, BLKDBG_READ
);
369 ret
= bdrv_pread(bs
->file
, cluster_offset
+ index_in_cluster
* 512, buf
, n
* 512);
372 if (s
->crypt_method
) {
373 qcow2_encrypt_sectors(s
, sector_num
, buf
, buf
, n
, 0,
374 &s
->aes_decrypt_key
);
384 static int copy_sectors(BlockDriverState
*bs
, uint64_t start_sect
,
385 uint64_t cluster_offset
, int n_start
, int n_end
)
387 BDRVQcowState
*s
= bs
->opaque
;
393 BLKDBG_EVENT(bs
->file
, BLKDBG_COW_READ
);
394 ret
= qcow_read(bs
, start_sect
+ n_start
, s
->cluster_data
, n
);
397 if (s
->crypt_method
) {
398 qcow2_encrypt_sectors(s
, start_sect
+ n_start
,
400 s
->cluster_data
, n
, 1,
401 &s
->aes_encrypt_key
);
403 BLKDBG_EVENT(bs
->file
, BLKDBG_COW_WRITE
);
404 ret
= bdrv_write(bs
->file
, (cluster_offset
>> 9) + n_start
,
415 * For a given offset of the disk image, return cluster offset in
418 * on entry, *num is the number of contiguous clusters we'd like to
419 * access following offset.
421 * on exit, *num is the number of contiguous clusters we can read.
423 * Return 1, if the offset is found
424 * Return 0, otherwise.
428 uint64_t qcow2_get_cluster_offset(BlockDriverState
*bs
, uint64_t offset
,
431 BDRVQcowState
*s
= bs
->opaque
;
432 unsigned int l1_index
, l2_index
;
433 uint64_t l2_offset
, *l2_table
, cluster_offset
;
435 unsigned int index_in_cluster
, nb_clusters
;
436 uint64_t nb_available
, nb_needed
;
438 index_in_cluster
= (offset
>> 9) & (s
->cluster_sectors
- 1);
439 nb_needed
= *num
+ index_in_cluster
;
441 l1_bits
= s
->l2_bits
+ s
->cluster_bits
;
443 /* compute how many bytes there are between the offset and
444 * the end of the l1 entry
447 nb_available
= (1ULL << l1_bits
) - (offset
& ((1ULL << l1_bits
) - 1));
449 /* compute the number of available sectors */
451 nb_available
= (nb_available
>> 9) + index_in_cluster
;
453 if (nb_needed
> nb_available
) {
454 nb_needed
= nb_available
;
459 /* seek the the l2 offset in the l1 table */
461 l1_index
= offset
>> l1_bits
;
462 if (l1_index
>= s
->l1_size
)
465 l2_offset
= s
->l1_table
[l1_index
];
467 /* seek the l2 table of the given l2 offset */
472 /* load the l2 table in memory */
474 l2_offset
&= ~QCOW_OFLAG_COPIED
;
475 l2_table
= l2_load(bs
, l2_offset
);
476 if (l2_table
== NULL
)
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 if (!cluster_offset
) {
486 /* how many empty clusters ? */
487 c
= count_contiguous_free_clusters(nb_clusters
, &l2_table
[l2_index
]);
489 /* how many allocated clusters ? */
490 c
= count_contiguous_clusters(nb_clusters
, s
->cluster_size
,
491 &l2_table
[l2_index
], 0, QCOW_OFLAG_COPIED
);
494 nb_available
= (c
* s
->cluster_sectors
);
496 if (nb_available
> nb_needed
)
497 nb_available
= nb_needed
;
499 *num
= nb_available
- index_in_cluster
;
501 return cluster_offset
& ~QCOW_OFLAG_COPIED
;
507 * for a given disk offset, load (and allocate if needed)
510 * the l2 table offset in the qcow2 file and the cluster index
511 * in the l2 table are given to the caller.
513 * Returns 0 on success, -errno in failure case
515 static int get_cluster_table(BlockDriverState
*bs
, uint64_t offset
,
516 uint64_t **new_l2_table
,
517 uint64_t *new_l2_offset
,
520 BDRVQcowState
*s
= bs
->opaque
;
521 unsigned int l1_index
, l2_index
;
523 uint64_t *l2_table
= NULL
;
526 /* seek the the l2 offset in the l1 table */
528 l1_index
= offset
>> (s
->l2_bits
+ s
->cluster_bits
);
529 if (l1_index
>= s
->l1_size
) {
530 ret
= qcow2_grow_l1_table(bs
, l1_index
+ 1);
535 l2_offset
= s
->l1_table
[l1_index
];
537 /* seek the l2 table of the given l2 offset */
539 if (l2_offset
& QCOW_OFLAG_COPIED
) {
540 /* load the l2 table in memory */
541 l2_offset
&= ~QCOW_OFLAG_COPIED
;
542 l2_table
= l2_load(bs
, l2_offset
);
543 if (l2_table
== NULL
) {
548 qcow2_free_clusters(bs
, l2_offset
, s
->l2_size
* sizeof(uint64_t));
549 ret
= l2_allocate(bs
, l1_index
, &l2_table
);
553 l2_offset
= s
->l1_table
[l1_index
] & ~QCOW_OFLAG_COPIED
;
556 /* find the cluster offset for the given disk offset */
558 l2_index
= (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
560 *new_l2_table
= l2_table
;
561 *new_l2_offset
= l2_offset
;
562 *new_l2_index
= l2_index
;
568 * alloc_compressed_cluster_offset
570 * For a given offset of the disk image, return cluster offset in
573 * If the offset is not found, allocate a new compressed cluster.
575 * Return the cluster offset if successful,
576 * Return 0, otherwise.
580 uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState
*bs
,
584 BDRVQcowState
*s
= bs
->opaque
;
586 uint64_t l2_offset
, *l2_table
;
587 int64_t cluster_offset
;
590 ret
= get_cluster_table(bs
, offset
, &l2_table
, &l2_offset
, &l2_index
);
595 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
596 if (cluster_offset
& QCOW_OFLAG_COPIED
)
597 return cluster_offset
& ~QCOW_OFLAG_COPIED
;
600 qcow2_free_any_clusters(bs
, cluster_offset
, 1);
602 cluster_offset
= qcow2_alloc_bytes(bs
, compressed_size
);
603 if (cluster_offset
< 0) {
607 nb_csectors
= ((cluster_offset
+ compressed_size
- 1) >> 9) -
608 (cluster_offset
>> 9);
610 cluster_offset
|= QCOW_OFLAG_COMPRESSED
|
611 ((uint64_t)nb_csectors
<< s
->csize_shift
);
613 /* update L2 table */
615 /* compressed clusters never have the copied flag */
617 BLKDBG_EVENT(bs
->file
, BLKDBG_L2_UPDATE_COMPRESSED
);
618 l2_table
[l2_index
] = cpu_to_be64(cluster_offset
);
619 if (bdrv_pwrite(bs
->file
,
620 l2_offset
+ l2_index
* sizeof(uint64_t),
622 sizeof(uint64_t)) != sizeof(uint64_t))
625 return cluster_offset
;
629 * Write L2 table updates to disk, writing whole sectors to avoid a
630 * read-modify-write in bdrv_pwrite
632 #define L2_ENTRIES_PER_SECTOR (512 / 8)
633 static int write_l2_entries(BlockDriverState
*bs
, uint64_t *l2_table
,
634 uint64_t l2_offset
, int l2_index
, int num
)
636 int l2_start_index
= l2_index
& ~(L1_ENTRIES_PER_SECTOR
- 1);
637 int start_offset
= (8 * l2_index
) & ~511;
638 int end_offset
= (8 * (l2_index
+ num
) + 511) & ~511;
639 size_t len
= end_offset
- start_offset
;
642 BLKDBG_EVENT(bs
->file
, BLKDBG_L2_UPDATE
);
643 ret
= bdrv_pwrite(bs
->file
, l2_offset
+ start_offset
,
644 &l2_table
[l2_start_index
], len
);
652 int qcow2_alloc_cluster_link_l2(BlockDriverState
*bs
, QCowL2Meta
*m
)
654 BDRVQcowState
*s
= bs
->opaque
;
655 int i
, j
= 0, l2_index
, ret
;
656 uint64_t *old_cluster
, start_sect
, l2_offset
, *l2_table
;
657 uint64_t cluster_offset
= m
->cluster_offset
;
659 if (m
->nb_clusters
== 0)
662 old_cluster
= qemu_malloc(m
->nb_clusters
* sizeof(uint64_t));
664 /* copy content of unmodified sectors */
665 start_sect
= (m
->offset
& ~(s
->cluster_size
- 1)) >> 9;
667 ret
= copy_sectors(bs
, start_sect
, cluster_offset
, 0, m
->n_start
);
672 if (m
->nb_available
& (s
->cluster_sectors
- 1)) {
673 uint64_t end
= m
->nb_available
& ~(uint64_t)(s
->cluster_sectors
- 1);
674 ret
= copy_sectors(bs
, start_sect
+ end
, cluster_offset
+ (end
<< 9),
675 m
->nb_available
- end
, s
->cluster_sectors
);
680 /* update L2 table */
681 ret
= get_cluster_table(bs
, m
->offset
, &l2_table
, &l2_offset
, &l2_index
);
686 for (i
= 0; i
< m
->nb_clusters
; i
++) {
687 /* if two concurrent writes happen to the same unallocated cluster
688 * each write allocates separate cluster and writes data concurrently.
689 * The first one to complete updates l2 table with pointer to its
690 * cluster the second one has to do RMW (which is done above by
691 * copy_sectors()), update l2 table with its cluster pointer and free
692 * old cluster. This is what this loop does */
693 if(l2_table
[l2_index
+ i
] != 0)
694 old_cluster
[j
++] = l2_table
[l2_index
+ i
];
696 l2_table
[l2_index
+ i
] = cpu_to_be64((cluster_offset
+
697 (i
<< s
->cluster_bits
)) | QCOW_OFLAG_COPIED
);
700 ret
= write_l2_entries(bs
, l2_table
, l2_offset
, l2_index
, m
->nb_clusters
);
702 qcow2_l2_cache_reset(bs
);
706 for (i
= 0; i
< j
; i
++)
707 qcow2_free_any_clusters(bs
,
708 be64_to_cpu(old_cluster
[i
]) & ~QCOW_OFLAG_COPIED
, 1);
712 qemu_free(old_cluster
);
717 * alloc_cluster_offset
719 * For a given offset of the disk image, return cluster offset in qcow2 file.
720 * If the offset is not found, allocate a new cluster.
722 * If the cluster was already allocated, m->nb_clusters is set to 0,
723 * m->depends_on is set to NULL and the other fields in m are meaningless.
725 * If the cluster is newly allocated, m->nb_clusters is set to the number of
726 * contiguous clusters that have been allocated. This may be 0 if the request
727 * conflict with another write request in flight; in this case, m->depends_on
728 * is set and the remaining fields of m are meaningless.
730 * If m->nb_clusters is non-zero, the other fields of m are valid and contain
731 * information about the first allocated cluster.
733 * Return 0 on success and -errno in error cases
735 int qcow2_alloc_cluster_offset(BlockDriverState
*bs
, uint64_t offset
,
736 int n_start
, int n_end
, int *num
, QCowL2Meta
*m
)
738 BDRVQcowState
*s
= bs
->opaque
;
740 uint64_t l2_offset
, *l2_table
;
741 int64_t cluster_offset
;
742 unsigned int nb_clusters
, i
= 0;
743 QCowL2Meta
*old_alloc
;
745 ret
= get_cluster_table(bs
, offset
, &l2_table
, &l2_offset
, &l2_index
);
750 nb_clusters
= size_to_clusters(s
, n_end
<< 9);
752 nb_clusters
= MIN(nb_clusters
, s
->l2_size
- l2_index
);
754 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
756 /* We keep all QCOW_OFLAG_COPIED clusters */
758 if (cluster_offset
& QCOW_OFLAG_COPIED
) {
759 nb_clusters
= count_contiguous_clusters(nb_clusters
, s
->cluster_size
,
760 &l2_table
[l2_index
], 0, 0);
762 cluster_offset
&= ~QCOW_OFLAG_COPIED
;
764 m
->depends_on
= NULL
;
769 /* for the moment, multiple compressed clusters are not managed */
771 if (cluster_offset
& QCOW_OFLAG_COMPRESSED
)
774 /* how many available clusters ? */
776 while (i
< nb_clusters
) {
777 i
+= count_contiguous_clusters(nb_clusters
- i
, s
->cluster_size
,
778 &l2_table
[l2_index
], i
, 0);
779 if ((i
>= nb_clusters
) || be64_to_cpu(l2_table
[l2_index
+ i
])) {
783 i
+= count_contiguous_free_clusters(nb_clusters
- i
,
784 &l2_table
[l2_index
+ i
]);
785 if (i
>= nb_clusters
) {
789 cluster_offset
= be64_to_cpu(l2_table
[l2_index
+ i
]);
791 if ((cluster_offset
& QCOW_OFLAG_COPIED
) ||
792 (cluster_offset
& QCOW_OFLAG_COMPRESSED
))
795 assert(i
<= nb_clusters
);
799 * Check if there already is an AIO write request in flight which allocates
800 * the same cluster. In this case we need to wait until the previous
801 * request has completed and updated the L2 table accordingly.
803 QLIST_FOREACH(old_alloc
, &s
->cluster_allocs
, next_in_flight
) {
805 uint64_t end_offset
= offset
+ nb_clusters
* s
->cluster_size
;
806 uint64_t old_offset
= old_alloc
->offset
;
807 uint64_t old_end_offset
= old_alloc
->offset
+
808 old_alloc
->nb_clusters
* s
->cluster_size
;
810 if (end_offset
< old_offset
|| offset
> old_end_offset
) {
811 /* No intersection */
813 if (offset
< old_offset
) {
814 /* Stop at the start of a running allocation */
815 nb_clusters
= (old_offset
- offset
) >> s
->cluster_bits
;
820 if (nb_clusters
== 0) {
821 /* Set dependency and wait for a callback */
822 m
->depends_on
= old_alloc
;
834 QLIST_INSERT_HEAD(&s
->cluster_allocs
, m
, next_in_flight
);
836 /* allocate a new cluster */
838 cluster_offset
= qcow2_alloc_clusters(bs
, nb_clusters
* s
->cluster_size
);
839 if (cluster_offset
< 0) {
840 QLIST_REMOVE(m
, next_in_flight
);
841 return cluster_offset
;
844 /* save info needed for meta data update */
846 m
->n_start
= n_start
;
847 m
->nb_clusters
= nb_clusters
;
850 m
->nb_available
= MIN(nb_clusters
<< (s
->cluster_bits
- 9), n_end
);
851 m
->cluster_offset
= cluster_offset
;
853 *num
= m
->nb_available
- n_start
;
858 static int decompress_buffer(uint8_t *out_buf
, int out_buf_size
,
859 const uint8_t *buf
, int buf_size
)
861 z_stream strm1
, *strm
= &strm1
;
864 memset(strm
, 0, sizeof(*strm
));
866 strm
->next_in
= (uint8_t *)buf
;
867 strm
->avail_in
= buf_size
;
868 strm
->next_out
= out_buf
;
869 strm
->avail_out
= out_buf_size
;
871 ret
= inflateInit2(strm
, -12);
874 ret
= inflate(strm
, Z_FINISH
);
875 out_len
= strm
->next_out
- out_buf
;
876 if ((ret
!= Z_STREAM_END
&& ret
!= Z_BUF_ERROR
) ||
877 out_len
!= out_buf_size
) {
885 int qcow2_decompress_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
)
887 BDRVQcowState
*s
= bs
->opaque
;
888 int ret
, csize
, nb_csectors
, sector_offset
;
891 coffset
= cluster_offset
& s
->cluster_offset_mask
;
892 if (s
->cluster_cache_offset
!= coffset
) {
893 nb_csectors
= ((cluster_offset
>> s
->csize_shift
) & s
->csize_mask
) + 1;
894 sector_offset
= coffset
& 511;
895 csize
= nb_csectors
* 512 - sector_offset
;
896 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_COMPRESSED
);
897 ret
= bdrv_read(bs
->file
, coffset
>> 9, s
->cluster_data
, nb_csectors
);
901 if (decompress_buffer(s
->cluster_cache
, s
->cluster_size
,
902 s
->cluster_data
+ sector_offset
, csize
) < 0) {
905 s
->cluster_cache_offset
= coffset
;