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
24 #include "qemu-common.h"
25 #include "block_int.h"
31 Differences with QCOW:
33 - Support for multiple incremental snapshots.
34 - Memory management by reference counts.
35 - Clusters which have a reference count of one have the bit
36 QCOW_OFLAG_COPIED to optimize write performance.
37 - Size of compressed clusters is stored in sectors to reduce bit usage
38 in the cluster offsets.
39 - Support for storing additional data (such as the VM state) in the
41 - If a backing store is used, the cluster size is not constrained
42 (could be backported to QCOW).
43 - L2 tables have always a size of one cluster.
47 //#define DEBUG_ALLOC2
49 #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
50 #define QCOW_VERSION 2
52 #define QCOW_CRYPT_NONE 0
53 #define QCOW_CRYPT_AES 1
55 #define QCOW_MAX_CRYPT_CLUSTERS 32
57 /* indicate that the refcount of the referenced cluster is exactly one. */
58 #define QCOW_OFLAG_COPIED (1LL << 63)
59 /* indicate that the cluster is compressed (they never have the copied flag) */
60 #define QCOW_OFLAG_COMPRESSED (1LL << 62)
62 #define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
64 typedef struct QCowHeader
{
67 uint64_t backing_file_offset
;
68 uint32_t backing_file_size
;
69 uint32_t cluster_bits
;
70 uint64_t size
; /* in bytes */
71 uint32_t crypt_method
;
72 uint32_t l1_size
; /* XXX: save number of clusters instead ? */
73 uint64_t l1_table_offset
;
74 uint64_t refcount_table_offset
;
75 uint32_t refcount_table_clusters
;
76 uint32_t nb_snapshots
;
77 uint64_t snapshots_offset
;
80 typedef struct __attribute__((packed
)) QCowSnapshotHeader
{
81 /* header is 8 byte aligned */
82 uint64_t l1_table_offset
;
91 uint64_t vm_clock_nsec
;
93 uint32_t vm_state_size
;
94 uint32_t extra_data_size
; /* for extension */
95 /* extra data follows */
100 #define L2_CACHE_SIZE 16
102 typedef struct QCowSnapshot
{
103 uint64_t l1_table_offset
;
107 uint32_t vm_state_size
;
110 uint64_t vm_clock_nsec
;
113 typedef struct BDRVQcowState
{
114 BlockDriverState
*hd
;
121 int l1_vm_state_index
;
124 uint64_t cluster_offset_mask
;
125 uint64_t l1_table_offset
;
128 uint64_t l2_cache_offsets
[L2_CACHE_SIZE
];
129 uint32_t l2_cache_counts
[L2_CACHE_SIZE
];
130 uint8_t *cluster_cache
;
131 uint8_t *cluster_data
;
132 uint64_t cluster_cache_offset
;
134 uint64_t *refcount_table
;
135 uint64_t refcount_table_offset
;
136 uint32_t refcount_table_size
;
137 uint64_t refcount_block_cache_offset
;
138 uint16_t *refcount_block_cache
;
139 int64_t free_cluster_index
;
140 int64_t free_byte_offset
;
142 uint32_t crypt_method
; /* current crypt method, 0 if no key yet */
143 uint32_t crypt_method_header
;
144 AES_KEY aes_encrypt_key
;
145 AES_KEY aes_decrypt_key
;
147 int64_t highest_alloc
; /* highest cluester allocated (in clusters) */
148 int64_t nc_free
; /* num of free clusters below highest_alloc */
150 uint64_t snapshots_offset
;
153 QCowSnapshot
*snapshots
;
156 static int decompress_cluster(BDRVQcowState
*s
, uint64_t cluster_offset
);
157 static int qcow_read(BlockDriverState
*bs
, int64_t sector_num
,
158 uint8_t *buf
, int nb_sectors
);
159 static int qcow_read_snapshots(BlockDriverState
*bs
);
160 static void qcow_free_snapshots(BlockDriverState
*bs
);
161 static int refcount_init(BlockDriverState
*bs
);
162 static void refcount_close(BlockDriverState
*bs
);
163 static int get_refcount(BlockDriverState
*bs
, int64_t cluster_index
);
164 static int update_cluster_refcount(BlockDriverState
*bs
,
165 int64_t cluster_index
,
167 static void update_refcount(BlockDriverState
*bs
,
168 int64_t offset
, int64_t length
,
170 static int64_t alloc_clusters(BlockDriverState
*bs
, int64_t size
);
171 static int64_t alloc_bytes(BlockDriverState
*bs
, int size
);
172 static void free_clusters(BlockDriverState
*bs
,
173 int64_t offset
, int64_t size
);
175 static void check_refcounts(BlockDriverState
*bs
);
177 static void scan_refcount(BlockDriverState
*bs
, int64_t *high
, int64_t *free
);
180 static int qcow_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
182 const QCowHeader
*cow_header
= (const void *)buf
;
184 if (buf_size
>= sizeof(QCowHeader
) &&
185 be32_to_cpu(cow_header
->magic
) == QCOW_MAGIC
&&
186 be32_to_cpu(cow_header
->version
) == QCOW_VERSION
)
192 static int qcow_open(BlockDriverState
*bs
, const char *filename
, int flags
)
194 BDRVQcowState
*s
= bs
->opaque
;
195 int len
, i
, shift
, ret
;
198 /* Performance is terrible right now with cache=writethrough due mainly
199 * to reference count updates. If the user does not explicitly specify
200 * a caching type, force to writeback caching.
202 if ((flags
& BDRV_O_CACHE_DEF
)) {
203 flags
|= BDRV_O_CACHE_WB
;
204 flags
&= ~BDRV_O_CACHE_DEF
;
206 ret
= bdrv_file_open(&s
->hd
, filename
, flags
);
209 if (bdrv_pread(s
->hd
, 0, &header
, sizeof(header
)) != sizeof(header
))
211 be32_to_cpus(&header
.magic
);
212 be32_to_cpus(&header
.version
);
213 be64_to_cpus(&header
.backing_file_offset
);
214 be32_to_cpus(&header
.backing_file_size
);
215 be64_to_cpus(&header
.size
);
216 be32_to_cpus(&header
.cluster_bits
);
217 be32_to_cpus(&header
.crypt_method
);
218 be64_to_cpus(&header
.l1_table_offset
);
219 be32_to_cpus(&header
.l1_size
);
220 be64_to_cpus(&header
.refcount_table_offset
);
221 be32_to_cpus(&header
.refcount_table_clusters
);
222 be64_to_cpus(&header
.snapshots_offset
);
223 be32_to_cpus(&header
.nb_snapshots
);
225 if (header
.magic
!= QCOW_MAGIC
|| header
.version
!= QCOW_VERSION
)
227 if (header
.size
<= 1 ||
228 header
.cluster_bits
< 9 ||
229 header
.cluster_bits
> 16)
231 if (header
.crypt_method
> QCOW_CRYPT_AES
)
233 s
->crypt_method_header
= header
.crypt_method
;
234 if (s
->crypt_method_header
)
236 s
->cluster_bits
= header
.cluster_bits
;
237 s
->cluster_size
= 1 << s
->cluster_bits
;
238 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
239 s
->l2_bits
= s
->cluster_bits
- 3; /* L2 is always one cluster */
240 s
->l2_size
= 1 << s
->l2_bits
;
241 bs
->total_sectors
= header
.size
/ 512;
242 s
->csize_shift
= (62 - (s
->cluster_bits
- 8));
243 s
->csize_mask
= (1 << (s
->cluster_bits
- 8)) - 1;
244 s
->cluster_offset_mask
= (1LL << s
->csize_shift
) - 1;
245 s
->refcount_table_offset
= header
.refcount_table_offset
;
246 s
->refcount_table_size
=
247 header
.refcount_table_clusters
<< (s
->cluster_bits
- 3);
249 s
->snapshots_offset
= header
.snapshots_offset
;
250 s
->nb_snapshots
= header
.nb_snapshots
;
252 /* read the level 1 table */
253 s
->l1_size
= header
.l1_size
;
254 shift
= s
->cluster_bits
+ s
->l2_bits
;
255 s
->l1_vm_state_index
= (header
.size
+ (1LL << shift
) - 1) >> shift
;
256 /* the L1 table must contain at least enough entries to put
258 if (s
->l1_size
< s
->l1_vm_state_index
)
260 s
->l1_table_offset
= header
.l1_table_offset
;
261 s
->l1_table
= qemu_malloc(s
->l1_size
* sizeof(uint64_t));
264 if (bdrv_pread(s
->hd
, s
->l1_table_offset
, s
->l1_table
, s
->l1_size
* sizeof(uint64_t)) !=
265 s
->l1_size
* sizeof(uint64_t))
267 for(i
= 0;i
< s
->l1_size
; i
++) {
268 be64_to_cpus(&s
->l1_table
[i
]);
271 s
->l2_cache
= qemu_malloc(s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
274 s
->cluster_cache
= qemu_malloc(s
->cluster_size
);
275 if (!s
->cluster_cache
)
277 /* one more sector for decompressed data alignment */
278 s
->cluster_data
= qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
280 if (!s
->cluster_data
)
282 s
->cluster_cache_offset
= -1;
284 if (refcount_init(bs
) < 0)
287 scan_refcount(bs
, &s
->highest_alloc
, &s
->nc_free
);
289 /* read the backing file name */
290 if (header
.backing_file_offset
!= 0) {
291 len
= header
.backing_file_size
;
294 if (bdrv_pread(s
->hd
, header
.backing_file_offset
, bs
->backing_file
, len
) != len
)
296 bs
->backing_file
[len
] = '\0';
298 if (qcow_read_snapshots(bs
) < 0)
307 qcow_free_snapshots(bs
);
309 qemu_free(s
->l1_table
);
310 qemu_free(s
->l2_cache
);
311 qemu_free(s
->cluster_cache
);
312 qemu_free(s
->cluster_data
);
317 static int qcow_set_key(BlockDriverState
*bs
, const char *key
)
319 BDRVQcowState
*s
= bs
->opaque
;
323 memset(keybuf
, 0, 16);
327 /* XXX: we could compress the chars to 7 bits to increase
329 for(i
= 0;i
< len
;i
++) {
332 s
->crypt_method
= s
->crypt_method_header
;
334 if (AES_set_encrypt_key(keybuf
, 128, &s
->aes_encrypt_key
) != 0)
336 if (AES_set_decrypt_key(keybuf
, 128, &s
->aes_decrypt_key
) != 0)
346 AES_encrypt(in
, tmp
, &s
->aes_encrypt_key
);
347 AES_decrypt(tmp
, out
, &s
->aes_decrypt_key
);
348 for(i
= 0; i
< 16; i
++)
349 printf(" %02x", tmp
[i
]);
351 for(i
= 0; i
< 16; i
++)
352 printf(" %02x", out
[i
]);
359 /* The crypt function is compatible with the linux cryptoloop
360 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
362 static void encrypt_sectors(BDRVQcowState
*s
, int64_t sector_num
,
363 uint8_t *out_buf
, const uint8_t *in_buf
,
364 int nb_sectors
, int enc
,
373 for(i
= 0; i
< nb_sectors
; i
++) {
374 ivec
.ll
[0] = cpu_to_le64(sector_num
);
376 AES_cbc_encrypt(in_buf
, out_buf
, 512, 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 ret
= qcow_read(bs
, start_sect
+ n_start
, s
->cluster_data
, n
);
396 if (s
->crypt_method
) {
397 encrypt_sectors(s
, start_sect
+ n_start
,
399 s
->cluster_data
, n
, 1,
400 &s
->aes_encrypt_key
);
402 ret
= bdrv_write(s
->hd
, (cluster_offset
>> 9) + n_start
,
409 static void l2_cache_reset(BlockDriverState
*bs
)
411 BDRVQcowState
*s
= bs
->opaque
;
413 memset(s
->l2_cache
, 0, s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
414 memset(s
->l2_cache_offsets
, 0, L2_CACHE_SIZE
* sizeof(uint64_t));
415 memset(s
->l2_cache_counts
, 0, L2_CACHE_SIZE
* sizeof(uint32_t));
418 static inline int l2_cache_new_entry(BlockDriverState
*bs
)
420 BDRVQcowState
*s
= bs
->opaque
;
424 /* find a new entry in the least used one */
426 min_count
= 0xffffffff;
427 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
428 if (s
->l2_cache_counts
[i
] < min_count
) {
429 min_count
= s
->l2_cache_counts
[i
];
436 static int64_t align_offset(int64_t offset
, int n
)
438 offset
= (offset
+ n
- 1) & ~(n
- 1);
442 static int grow_l1_table(BlockDriverState
*bs
, int min_size
)
444 BDRVQcowState
*s
= bs
->opaque
;
445 int new_l1_size
, new_l1_size2
, ret
, i
;
446 uint64_t *new_l1_table
;
447 uint64_t new_l1_table_offset
;
450 new_l1_size
= s
->l1_size
;
451 if (min_size
<= new_l1_size
)
453 while (min_size
> new_l1_size
) {
454 new_l1_size
= (new_l1_size
* 3 + 1) / 2;
457 printf("grow l1_table from %d to %d\n", s
->l1_size
, new_l1_size
);
460 new_l1_size2
= sizeof(uint64_t) * new_l1_size
;
461 new_l1_table
= qemu_mallocz(new_l1_size2
);
464 memcpy(new_l1_table
, s
->l1_table
, s
->l1_size
* sizeof(uint64_t));
466 /* write new table (align to cluster) */
467 new_l1_table_offset
= alloc_clusters(bs
, new_l1_size2
);
469 for(i
= 0; i
< s
->l1_size
; i
++)
470 new_l1_table
[i
] = cpu_to_be64(new_l1_table
[i
]);
471 ret
= bdrv_pwrite(s
->hd
, new_l1_table_offset
, new_l1_table
, new_l1_size2
);
472 if (ret
!= new_l1_size2
)
474 for(i
= 0; i
< s
->l1_size
; i
++)
475 new_l1_table
[i
] = be64_to_cpu(new_l1_table
[i
]);
478 cpu_to_be32w((uint32_t*)data
, new_l1_size
);
479 cpu_to_be64w((uint64_t*)(data
+ 4), new_l1_table_offset
);
480 if (bdrv_pwrite(s
->hd
, offsetof(QCowHeader
, l1_size
), data
,
481 sizeof(data
)) != sizeof(data
))
483 qemu_free(s
->l1_table
);
484 free_clusters(bs
, s
->l1_table_offset
, s
->l1_size
* sizeof(uint64_t));
485 s
->l1_table_offset
= new_l1_table_offset
;
486 s
->l1_table
= new_l1_table
;
487 s
->l1_size
= new_l1_size
;
490 qemu_free(s
->l1_table
);
497 * seek l2_offset in the l2_cache table
498 * if not found, return NULL,
500 * increments the l2 cache hit count of the entry,
501 * if counter overflow, divide by two all counters
502 * return the pointer to the l2 cache entry
506 static uint64_t *seek_l2_table(BDRVQcowState
*s
, uint64_t l2_offset
)
510 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
511 if (l2_offset
== s
->l2_cache_offsets
[i
]) {
512 /* increment the hit count */
513 if (++s
->l2_cache_counts
[i
] == 0xffffffff) {
514 for(j
= 0; j
< L2_CACHE_SIZE
; j
++) {
515 s
->l2_cache_counts
[j
] >>= 1;
518 return s
->l2_cache
+ (i
<< s
->l2_bits
);
527 * Loads a L2 table into memory. If the table is in the cache, the cache
528 * is used; otherwise the L2 table is loaded from the image file.
530 * Returns a pointer to the L2 table on success, or NULL if the read from
531 * the image file failed.
534 static uint64_t *l2_load(BlockDriverState
*bs
, uint64_t l2_offset
)
536 BDRVQcowState
*s
= bs
->opaque
;
540 /* seek if the table for the given offset is in the cache */
542 l2_table
= seek_l2_table(s
, l2_offset
);
543 if (l2_table
!= NULL
)
546 /* not found: load a new entry in the least used one */
548 min_index
= l2_cache_new_entry(bs
);
549 l2_table
= s
->l2_cache
+ (min_index
<< s
->l2_bits
);
550 if (bdrv_pread(s
->hd
, l2_offset
, l2_table
, s
->l2_size
* sizeof(uint64_t)) !=
551 s
->l2_size
* sizeof(uint64_t))
553 s
->l2_cache_offsets
[min_index
] = l2_offset
;
554 s
->l2_cache_counts
[min_index
] = 1;
562 * Allocate a new l2 entry in the file. If l1_index points to an already
563 * used entry in the L2 table (i.e. we are doing a copy on write for the L2
564 * table) copy the contents of the old L2 table into the newly allocated one.
565 * Otherwise the new table is initialized with zeros.
569 static uint64_t *l2_allocate(BlockDriverState
*bs
, int l1_index
)
571 BDRVQcowState
*s
= bs
->opaque
;
573 uint64_t old_l2_offset
, tmp
;
574 uint64_t *l2_table
, l2_offset
;
576 old_l2_offset
= s
->l1_table
[l1_index
];
578 /* allocate a new l2 entry */
580 l2_offset
= alloc_clusters(bs
, s
->l2_size
* sizeof(uint64_t));
582 /* update the L1 entry */
584 s
->l1_table
[l1_index
] = l2_offset
| QCOW_OFLAG_COPIED
;
586 tmp
= cpu_to_be64(l2_offset
| QCOW_OFLAG_COPIED
);
587 if (bdrv_pwrite(s
->hd
, s
->l1_table_offset
+ l1_index
* sizeof(tmp
),
588 &tmp
, sizeof(tmp
)) != sizeof(tmp
))
591 /* allocate a new entry in the l2 cache */
593 min_index
= l2_cache_new_entry(bs
);
594 l2_table
= s
->l2_cache
+ (min_index
<< s
->l2_bits
);
596 if (old_l2_offset
== 0) {
597 /* if there was no old l2 table, clear the new table */
598 memset(l2_table
, 0, s
->l2_size
* sizeof(uint64_t));
600 /* if there was an old l2 table, read it from the disk */
601 if (bdrv_pread(s
->hd
, old_l2_offset
,
602 l2_table
, s
->l2_size
* sizeof(uint64_t)) !=
603 s
->l2_size
* sizeof(uint64_t))
606 /* write the l2 table to the file */
607 if (bdrv_pwrite(s
->hd
, l2_offset
,
608 l2_table
, s
->l2_size
* sizeof(uint64_t)) !=
609 s
->l2_size
* sizeof(uint64_t))
612 /* update the l2 cache entry */
614 s
->l2_cache_offsets
[min_index
] = l2_offset
;
615 s
->l2_cache_counts
[min_index
] = 1;
620 static int size_to_clusters(BDRVQcowState
*s
, int64_t size
)
622 return (size
+ (s
->cluster_size
- 1)) >> s
->cluster_bits
;
625 static int count_contiguous_clusters(uint64_t nb_clusters
, int cluster_size
,
626 uint64_t *l2_table
, uint64_t start
, uint64_t mask
)
629 uint64_t offset
= be64_to_cpu(l2_table
[0]) & ~mask
;
634 for (i
= start
; i
< start
+ nb_clusters
; i
++)
635 if (offset
+ i
* cluster_size
!= (be64_to_cpu(l2_table
[i
]) & ~mask
))
641 static int count_contiguous_free_clusters(uint64_t nb_clusters
, uint64_t *l2_table
)
645 while(nb_clusters
-- && l2_table
[i
] == 0)
654 * For a given offset of the disk image, return cluster offset in
657 * on entry, *num is the number of contiguous clusters we'd like to
658 * access following offset.
660 * on exit, *num is the number of contiguous clusters we can read.
662 * Return 1, if the offset is found
663 * Return 0, otherwise.
667 static uint64_t get_cluster_offset(BlockDriverState
*bs
,
668 uint64_t offset
, int *num
)
670 BDRVQcowState
*s
= bs
->opaque
;
671 int l1_index
, l2_index
;
672 uint64_t l2_offset
, *l2_table
, cluster_offset
;
674 int index_in_cluster
, nb_available
, nb_needed
, nb_clusters
;
676 index_in_cluster
= (offset
>> 9) & (s
->cluster_sectors
- 1);
677 nb_needed
= *num
+ index_in_cluster
;
679 l1_bits
= s
->l2_bits
+ s
->cluster_bits
;
681 /* compute how many bytes there are between the offset and
682 * the end of the l1 entry
685 nb_available
= (1 << l1_bits
) - (offset
& ((1 << l1_bits
) - 1));
687 /* compute the number of available sectors */
689 nb_available
= (nb_available
>> 9) + index_in_cluster
;
693 /* seek the the l2 offset in the l1 table */
695 l1_index
= offset
>> l1_bits
;
696 if (l1_index
>= s
->l1_size
)
699 l2_offset
= s
->l1_table
[l1_index
];
701 /* seek the l2 table of the given l2 offset */
706 /* load the l2 table in memory */
708 l2_offset
&= ~QCOW_OFLAG_COPIED
;
709 l2_table
= l2_load(bs
, l2_offset
);
710 if (l2_table
== NULL
)
713 /* find the cluster offset for the given disk offset */
715 l2_index
= (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
716 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
717 nb_clusters
= size_to_clusters(s
, nb_needed
<< 9);
719 if (!cluster_offset
) {
720 /* how many empty clusters ? */
721 c
= count_contiguous_free_clusters(nb_clusters
, &l2_table
[l2_index
]);
723 /* how many allocated clusters ? */
724 c
= count_contiguous_clusters(nb_clusters
, s
->cluster_size
,
725 &l2_table
[l2_index
], 0, QCOW_OFLAG_COPIED
);
728 nb_available
= (c
* s
->cluster_sectors
);
730 if (nb_available
> nb_needed
)
731 nb_available
= nb_needed
;
733 *num
= nb_available
- index_in_cluster
;
735 return cluster_offset
& ~QCOW_OFLAG_COPIED
;
741 * free clusters according to its type: compressed or not
745 static void free_any_clusters(BlockDriverState
*bs
,
746 uint64_t cluster_offset
, int nb_clusters
)
748 BDRVQcowState
*s
= bs
->opaque
;
750 /* free the cluster */
752 if (cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
754 nb_csectors
= ((cluster_offset
>> s
->csize_shift
) &
756 free_clusters(bs
, (cluster_offset
& s
->cluster_offset_mask
) & ~511,
761 free_clusters(bs
, cluster_offset
, nb_clusters
<< s
->cluster_bits
);
769 * for a given disk offset, load (and allocate if needed)
772 * the l2 table offset in the qcow2 file and the cluster index
773 * in the l2 table are given to the caller.
777 static int get_cluster_table(BlockDriverState
*bs
, uint64_t offset
,
778 uint64_t **new_l2_table
,
779 uint64_t *new_l2_offset
,
782 BDRVQcowState
*s
= bs
->opaque
;
783 int l1_index
, l2_index
, ret
;
784 uint64_t l2_offset
, *l2_table
;
786 /* seek the the l2 offset in the l1 table */
788 l1_index
= offset
>> (s
->l2_bits
+ s
->cluster_bits
);
789 if (l1_index
>= s
->l1_size
) {
790 ret
= grow_l1_table(bs
, l1_index
+ 1);
794 l2_offset
= s
->l1_table
[l1_index
];
796 /* seek the l2 table of the given l2 offset */
798 if (l2_offset
& QCOW_OFLAG_COPIED
) {
799 /* load the l2 table in memory */
800 l2_offset
&= ~QCOW_OFLAG_COPIED
;
801 l2_table
= l2_load(bs
, l2_offset
);
802 if (l2_table
== NULL
)
806 free_clusters(bs
, l2_offset
, s
->l2_size
* sizeof(uint64_t));
807 l2_table
= l2_allocate(bs
, l1_index
);
808 if (l2_table
== NULL
)
810 l2_offset
= s
->l1_table
[l1_index
] & ~QCOW_OFLAG_COPIED
;
813 /* find the cluster offset for the given disk offset */
815 l2_index
= (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
817 *new_l2_table
= l2_table
;
818 *new_l2_offset
= l2_offset
;
819 *new_l2_index
= l2_index
;
825 * alloc_compressed_cluster_offset
827 * For a given offset of the disk image, return cluster offset in
830 * If the offset is not found, allocate a new compressed cluster.
832 * Return the cluster offset if successful,
833 * Return 0, otherwise.
837 static uint64_t alloc_compressed_cluster_offset(BlockDriverState
*bs
,
841 BDRVQcowState
*s
= bs
->opaque
;
843 uint64_t l2_offset
, *l2_table
, cluster_offset
;
846 ret
= get_cluster_table(bs
, offset
, &l2_table
, &l2_offset
, &l2_index
);
850 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
851 if (cluster_offset
& QCOW_OFLAG_COPIED
)
852 return cluster_offset
& ~QCOW_OFLAG_COPIED
;
855 free_any_clusters(bs
, cluster_offset
, 1);
857 cluster_offset
= alloc_bytes(bs
, compressed_size
);
858 nb_csectors
= ((cluster_offset
+ compressed_size
- 1) >> 9) -
859 (cluster_offset
>> 9);
861 cluster_offset
|= QCOW_OFLAG_COMPRESSED
|
862 ((uint64_t)nb_csectors
<< s
->csize_shift
);
864 /* update L2 table */
866 /* compressed clusters never have the copied flag */
868 l2_table
[l2_index
] = cpu_to_be64(cluster_offset
);
869 if (bdrv_pwrite(s
->hd
,
870 l2_offset
+ l2_index
* sizeof(uint64_t),
872 sizeof(uint64_t)) != sizeof(uint64_t))
875 return cluster_offset
;
878 typedef struct QCowL2Meta
886 static int alloc_cluster_link_l2(BlockDriverState
*bs
, uint64_t cluster_offset
,
889 BDRVQcowState
*s
= bs
->opaque
;
890 int i
, j
= 0, l2_index
, ret
;
891 uint64_t *old_cluster
, start_sect
, l2_offset
, *l2_table
;
893 if (m
->nb_clusters
== 0)
896 if (!(old_cluster
= qemu_malloc(m
->nb_clusters
* sizeof(uint64_t))))
899 /* copy content of unmodified sectors */
900 start_sect
= (m
->offset
& ~(s
->cluster_size
- 1)) >> 9;
902 ret
= copy_sectors(bs
, start_sect
, cluster_offset
, 0, m
->n_start
);
907 if (m
->nb_available
& (s
->cluster_sectors
- 1)) {
908 uint64_t end
= m
->nb_available
& ~(uint64_t)(s
->cluster_sectors
- 1);
909 ret
= copy_sectors(bs
, start_sect
+ end
, cluster_offset
+ (end
<< 9),
910 m
->nb_available
- end
, s
->cluster_sectors
);
916 /* update L2 table */
917 if (!get_cluster_table(bs
, m
->offset
, &l2_table
, &l2_offset
, &l2_index
))
920 for (i
= 0; i
< m
->nb_clusters
; i
++) {
921 if(l2_table
[l2_index
+ i
] != 0)
922 old_cluster
[j
++] = l2_table
[l2_index
+ i
];
924 l2_table
[l2_index
+ i
] = cpu_to_be64((cluster_offset
+
925 (i
<< s
->cluster_bits
)) | QCOW_OFLAG_COPIED
);
928 if (bdrv_pwrite(s
->hd
, l2_offset
+ l2_index
* sizeof(uint64_t),
929 l2_table
+ l2_index
, m
->nb_clusters
* sizeof(uint64_t)) !=
930 m
->nb_clusters
* sizeof(uint64_t))
933 for (i
= 0; i
< j
; i
++)
934 free_any_clusters(bs
, old_cluster
[i
], 1);
938 qemu_free(old_cluster
);
943 * alloc_cluster_offset
945 * For a given offset of the disk image, return cluster offset in
948 * If the offset is not found, allocate a new cluster.
950 * Return the cluster offset if successful,
951 * Return 0, otherwise.
955 static uint64_t alloc_cluster_offset(BlockDriverState
*bs
,
957 int n_start
, int n_end
,
958 int *num
, QCowL2Meta
*m
)
960 BDRVQcowState
*s
= bs
->opaque
;
962 uint64_t l2_offset
, *l2_table
, cluster_offset
;
963 int nb_clusters
, i
= 0;
965 ret
= get_cluster_table(bs
, offset
, &l2_table
, &l2_offset
, &l2_index
);
969 nb_clusters
= size_to_clusters(s
, n_end
<< 9);
971 nb_clusters
= MIN(nb_clusters
, s
->l2_size
- l2_index
);
973 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
975 /* We keep all QCOW_OFLAG_COPIED clusters */
977 if (cluster_offset
& QCOW_OFLAG_COPIED
) {
978 nb_clusters
= count_contiguous_clusters(nb_clusters
, s
->cluster_size
,
979 &l2_table
[l2_index
], 0, 0);
981 cluster_offset
&= ~QCOW_OFLAG_COPIED
;
987 /* for the moment, multiple compressed clusters are not managed */
989 if (cluster_offset
& QCOW_OFLAG_COMPRESSED
)
992 /* how many available clusters ? */
994 while (i
< nb_clusters
) {
995 i
+= count_contiguous_clusters(nb_clusters
- i
, s
->cluster_size
,
996 &l2_table
[l2_index
], i
, 0);
998 if(be64_to_cpu(l2_table
[l2_index
+ i
]))
1001 i
+= count_contiguous_free_clusters(nb_clusters
- i
,
1002 &l2_table
[l2_index
+ i
]);
1004 cluster_offset
= be64_to_cpu(l2_table
[l2_index
+ i
]);
1006 if ((cluster_offset
& QCOW_OFLAG_COPIED
) ||
1007 (cluster_offset
& QCOW_OFLAG_COMPRESSED
))
1012 /* allocate a new cluster */
1014 cluster_offset
= alloc_clusters(bs
, nb_clusters
* s
->cluster_size
);
1016 /* save info needed for meta data update */
1018 m
->n_start
= n_start
;
1019 m
->nb_clusters
= nb_clusters
;
1022 m
->nb_available
= MIN(nb_clusters
<< (s
->cluster_bits
- 9), n_end
);
1024 *num
= m
->nb_available
- n_start
;
1026 return cluster_offset
;
1029 static int qcow_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
1030 int nb_sectors
, int *pnum
)
1032 uint64_t cluster_offset
;
1035 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, pnum
);
1037 return (cluster_offset
!= 0);
1040 static int decompress_buffer(uint8_t *out_buf
, int out_buf_size
,
1041 const uint8_t *buf
, int buf_size
)
1043 z_stream strm1
, *strm
= &strm1
;
1046 memset(strm
, 0, sizeof(*strm
));
1048 strm
->next_in
= (uint8_t *)buf
;
1049 strm
->avail_in
= buf_size
;
1050 strm
->next_out
= out_buf
;
1051 strm
->avail_out
= out_buf_size
;
1053 ret
= inflateInit2(strm
, -12);
1056 ret
= inflate(strm
, Z_FINISH
);
1057 out_len
= strm
->next_out
- out_buf
;
1058 if ((ret
!= Z_STREAM_END
&& ret
!= Z_BUF_ERROR
) ||
1059 out_len
!= out_buf_size
) {
1067 static int decompress_cluster(BDRVQcowState
*s
, uint64_t cluster_offset
)
1069 int ret
, csize
, nb_csectors
, sector_offset
;
1072 coffset
= cluster_offset
& s
->cluster_offset_mask
;
1073 if (s
->cluster_cache_offset
!= coffset
) {
1074 nb_csectors
= ((cluster_offset
>> s
->csize_shift
) & s
->csize_mask
) + 1;
1075 sector_offset
= coffset
& 511;
1076 csize
= nb_csectors
* 512 - sector_offset
;
1077 ret
= bdrv_read(s
->hd
, coffset
>> 9, s
->cluster_data
, nb_csectors
);
1081 if (decompress_buffer(s
->cluster_cache
, s
->cluster_size
,
1082 s
->cluster_data
+ sector_offset
, csize
) < 0) {
1085 s
->cluster_cache_offset
= coffset
;
1090 /* handle reading after the end of the backing file */
1091 static int backing_read1(BlockDriverState
*bs
,
1092 int64_t sector_num
, uint8_t *buf
, int nb_sectors
)
1095 if ((sector_num
+ nb_sectors
) <= bs
->total_sectors
)
1097 if (sector_num
>= bs
->total_sectors
)
1100 n1
= bs
->total_sectors
- sector_num
;
1101 memset(buf
+ n1
* 512, 0, 512 * (nb_sectors
- n1
));
1105 static int qcow_read(BlockDriverState
*bs
, int64_t sector_num
,
1106 uint8_t *buf
, int nb_sectors
)
1108 BDRVQcowState
*s
= bs
->opaque
;
1109 int ret
, index_in_cluster
, n
, n1
;
1110 uint64_t cluster_offset
;
1112 while (nb_sectors
> 0) {
1114 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, &n
);
1115 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
1116 if (!cluster_offset
) {
1117 if (bs
->backing_hd
) {
1118 /* read from the base image */
1119 n1
= backing_read1(bs
->backing_hd
, sector_num
, buf
, n
);
1121 ret
= bdrv_read(bs
->backing_hd
, sector_num
, buf
, n1
);
1126 memset(buf
, 0, 512 * n
);
1128 } else if (cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
1129 if (decompress_cluster(s
, cluster_offset
) < 0)
1131 memcpy(buf
, s
->cluster_cache
+ index_in_cluster
* 512, 512 * n
);
1133 ret
= bdrv_pread(s
->hd
, cluster_offset
+ index_in_cluster
* 512, buf
, n
* 512);
1136 if (s
->crypt_method
) {
1137 encrypt_sectors(s
, sector_num
, buf
, buf
, n
, 0,
1138 &s
->aes_decrypt_key
);
1148 static int qcow_write(BlockDriverState
*bs
, int64_t sector_num
,
1149 const uint8_t *buf
, int nb_sectors
)
1151 BDRVQcowState
*s
= bs
->opaque
;
1152 int ret
, index_in_cluster
, n
;
1153 uint64_t cluster_offset
;
1157 while (nb_sectors
> 0) {
1158 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
1159 n_end
= index_in_cluster
+ nb_sectors
;
1160 if (s
->crypt_method
&&
1161 n_end
> QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
)
1162 n_end
= QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
;
1163 cluster_offset
= alloc_cluster_offset(bs
, sector_num
<< 9,
1165 n_end
, &n
, &l2meta
);
1166 if (!cluster_offset
)
1168 if (s
->crypt_method
) {
1169 encrypt_sectors(s
, sector_num
, s
->cluster_data
, buf
, n
, 1,
1170 &s
->aes_encrypt_key
);
1171 ret
= bdrv_pwrite(s
->hd
, cluster_offset
+ index_in_cluster
* 512,
1172 s
->cluster_data
, n
* 512);
1174 ret
= bdrv_pwrite(s
->hd
, cluster_offset
+ index_in_cluster
* 512, buf
, n
* 512);
1176 if (ret
!= n
* 512 || alloc_cluster_link_l2(bs
, cluster_offset
, &l2meta
) < 0) {
1177 free_any_clusters(bs
, cluster_offset
, l2meta
.nb_clusters
);
1184 s
->cluster_cache_offset
= -1; /* disable compressed cache */
1188 typedef struct QCowAIOCB
{
1189 BlockDriverAIOCB common
;
1194 uint64_t cluster_offset
;
1195 uint8_t *cluster_data
;
1196 BlockDriverAIOCB
*hd_aiocb
;
1201 static void qcow_aio_read_cb(void *opaque
, int ret
);
1202 static void qcow_aio_read_bh(void *opaque
)
1204 QCowAIOCB
*acb
= opaque
;
1205 qemu_bh_delete(acb
->bh
);
1207 qcow_aio_read_cb(opaque
, 0);
1210 static int qcow_schedule_bh(QEMUBHFunc
*cb
, QCowAIOCB
*acb
)
1215 acb
->bh
= qemu_bh_new(cb
, acb
);
1219 qemu_bh_schedule(acb
->bh
);
1224 static void qcow_aio_read_cb(void *opaque
, int ret
)
1226 QCowAIOCB
*acb
= opaque
;
1227 BlockDriverState
*bs
= acb
->common
.bs
;
1228 BDRVQcowState
*s
= bs
->opaque
;
1229 int index_in_cluster
, n1
;
1231 acb
->hd_aiocb
= NULL
;
1234 acb
->common
.cb(acb
->common
.opaque
, ret
);
1235 qemu_aio_release(acb
);
1239 /* post process the read buffer */
1240 if (!acb
->cluster_offset
) {
1242 } else if (acb
->cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
1245 if (s
->crypt_method
) {
1246 encrypt_sectors(s
, acb
->sector_num
, acb
->buf
, acb
->buf
,
1248 &s
->aes_decrypt_key
);
1252 acb
->nb_sectors
-= acb
->n
;
1253 acb
->sector_num
+= acb
->n
;
1254 acb
->buf
+= acb
->n
* 512;
1256 if (acb
->nb_sectors
== 0) {
1257 /* request completed */
1258 acb
->common
.cb(acb
->common
.opaque
, 0);
1259 qemu_aio_release(acb
);
1263 /* prepare next AIO request */
1264 acb
->n
= acb
->nb_sectors
;
1265 acb
->cluster_offset
= get_cluster_offset(bs
, acb
->sector_num
<< 9, &acb
->n
);
1266 index_in_cluster
= acb
->sector_num
& (s
->cluster_sectors
- 1);
1268 if (!acb
->cluster_offset
) {
1269 if (bs
->backing_hd
) {
1270 /* read from the base image */
1271 n1
= backing_read1(bs
->backing_hd
, acb
->sector_num
,
1274 acb
->hd_aiocb
= bdrv_aio_read(bs
->backing_hd
, acb
->sector_num
,
1275 acb
->buf
, acb
->n
, qcow_aio_read_cb
, acb
);
1276 if (acb
->hd_aiocb
== NULL
)
1279 ret
= qcow_schedule_bh(qcow_aio_read_bh
, acb
);
1284 /* Note: in this case, no need to wait */
1285 memset(acb
->buf
, 0, 512 * acb
->n
);
1286 ret
= qcow_schedule_bh(qcow_aio_read_bh
, acb
);
1290 } else if (acb
->cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
1291 /* add AIO support for compressed blocks ? */
1292 if (decompress_cluster(s
, acb
->cluster_offset
) < 0)
1295 s
->cluster_cache
+ index_in_cluster
* 512, 512 * acb
->n
);
1296 ret
= qcow_schedule_bh(qcow_aio_read_bh
, acb
);
1300 if ((acb
->cluster_offset
& 511) != 0) {
1304 acb
->hd_aiocb
= bdrv_aio_read(s
->hd
,
1305 (acb
->cluster_offset
>> 9) + index_in_cluster
,
1306 acb
->buf
, acb
->n
, qcow_aio_read_cb
, acb
);
1307 if (acb
->hd_aiocb
== NULL
)
1312 static QCowAIOCB
*qcow_aio_setup(BlockDriverState
*bs
,
1313 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1314 BlockDriverCompletionFunc
*cb
, void *opaque
)
1318 acb
= qemu_aio_get(bs
, cb
, opaque
);
1321 acb
->hd_aiocb
= NULL
;
1322 acb
->sector_num
= sector_num
;
1324 acb
->nb_sectors
= nb_sectors
;
1326 acb
->cluster_offset
= 0;
1327 acb
->l2meta
.nb_clusters
= 0;
1331 static BlockDriverAIOCB
*qcow_aio_read(BlockDriverState
*bs
,
1332 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1333 BlockDriverCompletionFunc
*cb
, void *opaque
)
1337 acb
= qcow_aio_setup(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1341 qcow_aio_read_cb(acb
, 0);
1342 return &acb
->common
;
1345 static void qcow_aio_write_cb(void *opaque
, int ret
)
1347 QCowAIOCB
*acb
= opaque
;
1348 BlockDriverState
*bs
= acb
->common
.bs
;
1349 BDRVQcowState
*s
= bs
->opaque
;
1350 int index_in_cluster
;
1351 const uint8_t *src_buf
;
1354 acb
->hd_aiocb
= NULL
;
1358 acb
->common
.cb(acb
->common
.opaque
, ret
);
1359 qemu_aio_release(acb
);
1363 if (alloc_cluster_link_l2(bs
, acb
->cluster_offset
, &acb
->l2meta
) < 0) {
1364 free_any_clusters(bs
, acb
->cluster_offset
, acb
->l2meta
.nb_clusters
);
1368 acb
->nb_sectors
-= acb
->n
;
1369 acb
->sector_num
+= acb
->n
;
1370 acb
->buf
+= acb
->n
* 512;
1372 if (acb
->nb_sectors
== 0) {
1373 /* request completed */
1374 acb
->common
.cb(acb
->common
.opaque
, 0);
1375 qemu_aio_release(acb
);
1379 index_in_cluster
= acb
->sector_num
& (s
->cluster_sectors
- 1);
1380 n_end
= index_in_cluster
+ acb
->nb_sectors
;
1381 if (s
->crypt_method
&&
1382 n_end
> QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
)
1383 n_end
= QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
;
1385 acb
->cluster_offset
= alloc_cluster_offset(bs
, acb
->sector_num
<< 9,
1387 n_end
, &acb
->n
, &acb
->l2meta
);
1388 if (!acb
->cluster_offset
|| (acb
->cluster_offset
& 511) != 0) {
1392 if (s
->crypt_method
) {
1393 if (!acb
->cluster_data
) {
1394 acb
->cluster_data
= qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS
*
1396 if (!acb
->cluster_data
) {
1401 encrypt_sectors(s
, acb
->sector_num
, acb
->cluster_data
, acb
->buf
,
1402 acb
->n
, 1, &s
->aes_encrypt_key
);
1403 src_buf
= acb
->cluster_data
;
1407 acb
->hd_aiocb
= bdrv_aio_write(s
->hd
,
1408 (acb
->cluster_offset
>> 9) + index_in_cluster
,
1410 qcow_aio_write_cb
, acb
);
1411 if (acb
->hd_aiocb
== NULL
)
1415 static BlockDriverAIOCB
*qcow_aio_write(BlockDriverState
*bs
,
1416 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1417 BlockDriverCompletionFunc
*cb
, void *opaque
)
1419 BDRVQcowState
*s
= bs
->opaque
;
1422 s
->cluster_cache_offset
= -1; /* disable compressed cache */
1424 acb
= qcow_aio_setup(bs
, sector_num
, (uint8_t*)buf
, nb_sectors
, cb
, opaque
);
1428 qcow_aio_write_cb(acb
, 0);
1429 return &acb
->common
;
1432 static void qcow_aio_cancel(BlockDriverAIOCB
*blockacb
)
1434 QCowAIOCB
*acb
= (QCowAIOCB
*)blockacb
;
1436 bdrv_aio_cancel(acb
->hd_aiocb
);
1437 qemu_aio_release(acb
);
1440 static void qcow_close(BlockDriverState
*bs
)
1442 BDRVQcowState
*s
= bs
->opaque
;
1443 qemu_free(s
->l1_table
);
1444 qemu_free(s
->l2_cache
);
1445 qemu_free(s
->cluster_cache
);
1446 qemu_free(s
->cluster_data
);
1451 /* XXX: use std qcow open function ? */
1452 typedef struct QCowCreateState
{
1455 uint16_t *refcount_block
;
1456 uint64_t *refcount_table
;
1457 int64_t l1_table_offset
;
1458 int64_t refcount_table_offset
;
1459 int64_t refcount_block_offset
;
1462 static void create_refcount_update(QCowCreateState
*s
,
1463 int64_t offset
, int64_t size
)
1466 int64_t start
, last
, cluster_offset
;
1469 start
= offset
& ~(s
->cluster_size
- 1);
1470 last
= (offset
+ size
- 1) & ~(s
->cluster_size
- 1);
1471 for(cluster_offset
= start
; cluster_offset
<= last
;
1472 cluster_offset
+= s
->cluster_size
) {
1473 p
= &s
->refcount_block
[cluster_offset
>> s
->cluster_bits
];
1474 refcount
= be16_to_cpu(*p
);
1476 *p
= cpu_to_be16(refcount
);
1480 static int qcow_create(const char *filename
, int64_t total_size
,
1481 const char *backing_file
, int flags
)
1483 int fd
, header_size
, backing_filename_len
, l1_size
, i
, shift
, l2_bits
;
1485 uint64_t tmp
, offset
;
1486 QCowCreateState s1
, *s
= &s1
;
1488 memset(s
, 0, sizeof(*s
));
1490 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0644);
1493 memset(&header
, 0, sizeof(header
));
1494 header
.magic
= cpu_to_be32(QCOW_MAGIC
);
1495 header
.version
= cpu_to_be32(QCOW_VERSION
);
1496 header
.size
= cpu_to_be64(total_size
* 512);
1497 header_size
= sizeof(header
);
1498 backing_filename_len
= 0;
1500 header
.backing_file_offset
= cpu_to_be64(header_size
);
1501 backing_filename_len
= strlen(backing_file
);
1502 header
.backing_file_size
= cpu_to_be32(backing_filename_len
);
1503 header_size
+= backing_filename_len
;
1505 s
->cluster_bits
= 12; /* 4 KB clusters */
1506 s
->cluster_size
= 1 << s
->cluster_bits
;
1507 header
.cluster_bits
= cpu_to_be32(s
->cluster_bits
);
1508 header_size
= (header_size
+ 7) & ~7;
1509 if (flags
& BLOCK_FLAG_ENCRYPT
) {
1510 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
1512 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
1514 l2_bits
= s
->cluster_bits
- 3;
1515 shift
= s
->cluster_bits
+ l2_bits
;
1516 l1_size
= (((total_size
* 512) + (1LL << shift
) - 1) >> shift
);
1517 offset
= align_offset(header_size
, s
->cluster_size
);
1518 s
->l1_table_offset
= offset
;
1519 header
.l1_table_offset
= cpu_to_be64(s
->l1_table_offset
);
1520 header
.l1_size
= cpu_to_be32(l1_size
);
1521 offset
+= align_offset(l1_size
* sizeof(uint64_t), s
->cluster_size
);
1523 s
->refcount_table
= qemu_mallocz(s
->cluster_size
);
1524 if (!s
->refcount_table
)
1526 s
->refcount_block
= qemu_mallocz(s
->cluster_size
);
1527 if (!s
->refcount_block
)
1530 s
->refcount_table_offset
= offset
;
1531 header
.refcount_table_offset
= cpu_to_be64(offset
);
1532 header
.refcount_table_clusters
= cpu_to_be32(1);
1533 offset
+= s
->cluster_size
;
1535 s
->refcount_table
[0] = cpu_to_be64(offset
);
1536 s
->refcount_block_offset
= offset
;
1537 offset
+= s
->cluster_size
;
1539 /* update refcounts */
1540 create_refcount_update(s
, 0, header_size
);
1541 create_refcount_update(s
, s
->l1_table_offset
, l1_size
* sizeof(uint64_t));
1542 create_refcount_update(s
, s
->refcount_table_offset
, s
->cluster_size
);
1543 create_refcount_update(s
, s
->refcount_block_offset
, s
->cluster_size
);
1545 /* write all the data */
1546 write(fd
, &header
, sizeof(header
));
1548 write(fd
, backing_file
, backing_filename_len
);
1550 lseek(fd
, s
->l1_table_offset
, SEEK_SET
);
1552 for(i
= 0;i
< l1_size
; i
++) {
1553 write(fd
, &tmp
, sizeof(tmp
));
1555 lseek(fd
, s
->refcount_table_offset
, SEEK_SET
);
1556 write(fd
, s
->refcount_table
, s
->cluster_size
);
1558 lseek(fd
, s
->refcount_block_offset
, SEEK_SET
);
1559 write(fd
, s
->refcount_block
, s
->cluster_size
);
1561 qemu_free(s
->refcount_table
);
1562 qemu_free(s
->refcount_block
);
1566 qemu_free(s
->refcount_table
);
1567 qemu_free(s
->refcount_block
);
1572 static int qcow_make_empty(BlockDriverState
*bs
)
1575 /* XXX: not correct */
1576 BDRVQcowState
*s
= bs
->opaque
;
1577 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
1580 memset(s
->l1_table
, 0, l1_length
);
1581 if (bdrv_pwrite(s
->hd
, s
->l1_table_offset
, s
->l1_table
, l1_length
) < 0)
1583 ret
= bdrv_truncate(s
->hd
, s
->l1_table_offset
+ l1_length
);
1592 /* XXX: put compressed sectors first, then all the cluster aligned
1593 tables to avoid losing bytes in alignment */
1594 static int qcow_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1595 const uint8_t *buf
, int nb_sectors
)
1597 BDRVQcowState
*s
= bs
->opaque
;
1601 uint64_t cluster_offset
;
1603 if (nb_sectors
== 0) {
1604 /* align end of file to a sector boundary to ease reading with
1605 sector based I/Os */
1606 cluster_offset
= bdrv_getlength(s
->hd
);
1607 cluster_offset
= (cluster_offset
+ 511) & ~511;
1608 bdrv_truncate(s
->hd
, cluster_offset
);
1612 if (nb_sectors
!= s
->cluster_sectors
)
1615 out_buf
= qemu_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
1619 /* best compression, small window, no zlib header */
1620 memset(&strm
, 0, sizeof(strm
));
1621 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
1623 9, Z_DEFAULT_STRATEGY
);
1629 strm
.avail_in
= s
->cluster_size
;
1630 strm
.next_in
= (uint8_t *)buf
;
1631 strm
.avail_out
= s
->cluster_size
;
1632 strm
.next_out
= out_buf
;
1634 ret
= deflate(&strm
, Z_FINISH
);
1635 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
1640 out_len
= strm
.next_out
- out_buf
;
1644 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
1645 /* could not compress: write normal cluster */
1646 qcow_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
1648 cluster_offset
= alloc_compressed_cluster_offset(bs
, sector_num
<< 9,
1650 if (!cluster_offset
)
1652 cluster_offset
&= s
->cluster_offset_mask
;
1653 if (bdrv_pwrite(s
->hd
, cluster_offset
, out_buf
, out_len
) != out_len
) {
1663 static void qcow_flush(BlockDriverState
*bs
)
1665 BDRVQcowState
*s
= bs
->opaque
;
1669 static int qcow_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1671 BDRVQcowState
*s
= bs
->opaque
;
1672 bdi
->cluster_size
= s
->cluster_size
;
1673 bdi
->vm_state_offset
= (int64_t)s
->l1_vm_state_index
<<
1674 (s
->cluster_bits
+ s
->l2_bits
);
1675 bdi
->highest_alloc
= s
->highest_alloc
<< s
->cluster_bits
;
1676 bdi
->num_free_bytes
= s
->nc_free
<< s
->cluster_bits
;
1680 /*********************************************************/
1681 /* snapshot support */
1683 /* update the refcounts of snapshots and the copied flag */
1684 static int update_snapshot_refcount(BlockDriverState
*bs
,
1685 int64_t l1_table_offset
,
1689 BDRVQcowState
*s
= bs
->opaque
;
1690 uint64_t *l1_table
, *l2_table
, l2_offset
, offset
, l1_size2
, l1_allocated
;
1691 int64_t old_offset
, old_l2_offset
;
1692 int l2_size
, i
, j
, l1_modified
, l2_modified
, nb_csectors
, refcount
;
1698 l1_size2
= l1_size
* sizeof(uint64_t);
1700 if (l1_table_offset
!= s
->l1_table_offset
) {
1701 l1_table
= qemu_malloc(l1_size2
);
1705 if (bdrv_pread(s
->hd
, l1_table_offset
,
1706 l1_table
, l1_size2
) != l1_size2
)
1708 for(i
= 0;i
< l1_size
; i
++)
1709 be64_to_cpus(&l1_table
[i
]);
1711 assert(l1_size
== s
->l1_size
);
1712 l1_table
= s
->l1_table
;
1716 l2_size
= s
->l2_size
* sizeof(uint64_t);
1717 l2_table
= qemu_malloc(l2_size
);
1721 for(i
= 0; i
< l1_size
; i
++) {
1722 l2_offset
= l1_table
[i
];
1724 old_l2_offset
= l2_offset
;
1725 l2_offset
&= ~QCOW_OFLAG_COPIED
;
1727 if (bdrv_pread(s
->hd
, l2_offset
, l2_table
, l2_size
) != l2_size
)
1729 for(j
= 0; j
< s
->l2_size
; j
++) {
1730 offset
= be64_to_cpu(l2_table
[j
]);
1732 old_offset
= offset
;
1733 offset
&= ~QCOW_OFLAG_COPIED
;
1734 if (offset
& QCOW_OFLAG_COMPRESSED
) {
1735 nb_csectors
= ((offset
>> s
->csize_shift
) &
1738 update_refcount(bs
, (offset
& s
->cluster_offset_mask
) & ~511,
1739 nb_csectors
* 512, addend
);
1740 /* compressed clusters are never modified */
1744 refcount
= update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, addend
);
1746 refcount
= get_refcount(bs
, offset
>> s
->cluster_bits
);
1750 if (refcount
== 1) {
1751 offset
|= QCOW_OFLAG_COPIED
;
1753 if (offset
!= old_offset
) {
1754 l2_table
[j
] = cpu_to_be64(offset
);
1760 if (bdrv_pwrite(s
->hd
,
1761 l2_offset
, l2_table
, l2_size
) != l2_size
)
1766 refcount
= update_cluster_refcount(bs
, l2_offset
>> s
->cluster_bits
, addend
);
1768 refcount
= get_refcount(bs
, l2_offset
>> s
->cluster_bits
);
1770 if (refcount
== 1) {
1771 l2_offset
|= QCOW_OFLAG_COPIED
;
1773 if (l2_offset
!= old_l2_offset
) {
1774 l1_table
[i
] = l2_offset
;
1780 for(i
= 0; i
< l1_size
; i
++)
1781 cpu_to_be64s(&l1_table
[i
]);
1782 if (bdrv_pwrite(s
->hd
, l1_table_offset
, l1_table
,
1783 l1_size2
) != l1_size2
)
1785 for(i
= 0; i
< l1_size
; i
++)
1786 be64_to_cpus(&l1_table
[i
]);
1789 qemu_free(l1_table
);
1790 qemu_free(l2_table
);
1794 qemu_free(l1_table
);
1795 qemu_free(l2_table
);
1799 static void qcow_free_snapshots(BlockDriverState
*bs
)
1801 BDRVQcowState
*s
= bs
->opaque
;
1804 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1805 qemu_free(s
->snapshots
[i
].name
);
1806 qemu_free(s
->snapshots
[i
].id_str
);
1808 qemu_free(s
->snapshots
);
1809 s
->snapshots
= NULL
;
1810 s
->nb_snapshots
= 0;
1813 static int qcow_read_snapshots(BlockDriverState
*bs
)
1815 BDRVQcowState
*s
= bs
->opaque
;
1816 QCowSnapshotHeader h
;
1818 int i
, id_str_size
, name_size
;
1820 uint32_t extra_data_size
;
1822 if (!s
->nb_snapshots
) {
1823 s
->snapshots
= NULL
;
1824 s
->snapshots_size
= 0;
1828 offset
= s
->snapshots_offset
;
1829 s
->snapshots
= qemu_mallocz(s
->nb_snapshots
* sizeof(QCowSnapshot
));
1832 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1833 offset
= align_offset(offset
, 8);
1834 if (bdrv_pread(s
->hd
, offset
, &h
, sizeof(h
)) != sizeof(h
))
1836 offset
+= sizeof(h
);
1837 sn
= s
->snapshots
+ i
;
1838 sn
->l1_table_offset
= be64_to_cpu(h
.l1_table_offset
);
1839 sn
->l1_size
= be32_to_cpu(h
.l1_size
);
1840 sn
->vm_state_size
= be32_to_cpu(h
.vm_state_size
);
1841 sn
->date_sec
= be32_to_cpu(h
.date_sec
);
1842 sn
->date_nsec
= be32_to_cpu(h
.date_nsec
);
1843 sn
->vm_clock_nsec
= be64_to_cpu(h
.vm_clock_nsec
);
1844 extra_data_size
= be32_to_cpu(h
.extra_data_size
);
1846 id_str_size
= be16_to_cpu(h
.id_str_size
);
1847 name_size
= be16_to_cpu(h
.name_size
);
1849 offset
+= extra_data_size
;
1851 sn
->id_str
= qemu_malloc(id_str_size
+ 1);
1854 if (bdrv_pread(s
->hd
, offset
, sn
->id_str
, id_str_size
) != id_str_size
)
1856 offset
+= id_str_size
;
1857 sn
->id_str
[id_str_size
] = '\0';
1859 sn
->name
= qemu_malloc(name_size
+ 1);
1862 if (bdrv_pread(s
->hd
, offset
, sn
->name
, name_size
) != name_size
)
1864 offset
+= name_size
;
1865 sn
->name
[name_size
] = '\0';
1867 s
->snapshots_size
= offset
- s
->snapshots_offset
;
1870 qcow_free_snapshots(bs
);
1874 /* add at the end of the file a new list of snapshots */
1875 static int qcow_write_snapshots(BlockDriverState
*bs
)
1877 BDRVQcowState
*s
= bs
->opaque
;
1879 QCowSnapshotHeader h
;
1880 int i
, name_size
, id_str_size
, snapshots_size
;
1883 int64_t offset
, snapshots_offset
;
1885 /* compute the size of the snapshots */
1887 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1888 sn
= s
->snapshots
+ i
;
1889 offset
= align_offset(offset
, 8);
1890 offset
+= sizeof(h
);
1891 offset
+= strlen(sn
->id_str
);
1892 offset
+= strlen(sn
->name
);
1894 snapshots_size
= offset
;
1896 snapshots_offset
= alloc_clusters(bs
, snapshots_size
);
1897 offset
= snapshots_offset
;
1899 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1900 sn
= s
->snapshots
+ i
;
1901 memset(&h
, 0, sizeof(h
));
1902 h
.l1_table_offset
= cpu_to_be64(sn
->l1_table_offset
);
1903 h
.l1_size
= cpu_to_be32(sn
->l1_size
);
1904 h
.vm_state_size
= cpu_to_be32(sn
->vm_state_size
);
1905 h
.date_sec
= cpu_to_be32(sn
->date_sec
);
1906 h
.date_nsec
= cpu_to_be32(sn
->date_nsec
);
1907 h
.vm_clock_nsec
= cpu_to_be64(sn
->vm_clock_nsec
);
1909 id_str_size
= strlen(sn
->id_str
);
1910 name_size
= strlen(sn
->name
);
1911 h
.id_str_size
= cpu_to_be16(id_str_size
);
1912 h
.name_size
= cpu_to_be16(name_size
);
1913 offset
= align_offset(offset
, 8);
1914 if (bdrv_pwrite(s
->hd
, offset
, &h
, sizeof(h
)) != sizeof(h
))
1916 offset
+= sizeof(h
);
1917 if (bdrv_pwrite(s
->hd
, offset
, sn
->id_str
, id_str_size
) != id_str_size
)
1919 offset
+= id_str_size
;
1920 if (bdrv_pwrite(s
->hd
, offset
, sn
->name
, name_size
) != name_size
)
1922 offset
+= name_size
;
1925 /* update the various header fields */
1926 data64
= cpu_to_be64(snapshots_offset
);
1927 if (bdrv_pwrite(s
->hd
, offsetof(QCowHeader
, snapshots_offset
),
1928 &data64
, sizeof(data64
)) != sizeof(data64
))
1930 data32
= cpu_to_be32(s
->nb_snapshots
);
1931 if (bdrv_pwrite(s
->hd
, offsetof(QCowHeader
, nb_snapshots
),
1932 &data32
, sizeof(data32
)) != sizeof(data32
))
1935 /* free the old snapshot table */
1936 free_clusters(bs
, s
->snapshots_offset
, s
->snapshots_size
);
1937 s
->snapshots_offset
= snapshots_offset
;
1938 s
->snapshots_size
= snapshots_size
;
1944 static void find_new_snapshot_id(BlockDriverState
*bs
,
1945 char *id_str
, int id_str_size
)
1947 BDRVQcowState
*s
= bs
->opaque
;
1949 int i
, id
, id_max
= 0;
1951 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1952 sn
= s
->snapshots
+ i
;
1953 id
= strtoul(sn
->id_str
, NULL
, 10);
1957 snprintf(id_str
, id_str_size
, "%d", id_max
+ 1);
1960 static int find_snapshot_by_id(BlockDriverState
*bs
, const char *id_str
)
1962 BDRVQcowState
*s
= bs
->opaque
;
1965 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1966 if (!strcmp(s
->snapshots
[i
].id_str
, id_str
))
1972 static int find_snapshot_by_id_or_name(BlockDriverState
*bs
, const char *name
)
1974 BDRVQcowState
*s
= bs
->opaque
;
1977 ret
= find_snapshot_by_id(bs
, name
);
1980 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1981 if (!strcmp(s
->snapshots
[i
].name
, name
))
1987 /* if no id is provided, a new one is constructed */
1988 static int qcow_snapshot_create(BlockDriverState
*bs
,
1989 QEMUSnapshotInfo
*sn_info
)
1991 BDRVQcowState
*s
= bs
->opaque
;
1992 QCowSnapshot
*snapshots1
, sn1
, *sn
= &sn1
;
1994 uint64_t *l1_table
= NULL
;
1996 memset(sn
, 0, sizeof(*sn
));
1998 if (sn_info
->id_str
[0] == '\0') {
1999 /* compute a new id */
2000 find_new_snapshot_id(bs
, sn_info
->id_str
, sizeof(sn_info
->id_str
));
2003 /* check that the ID is unique */
2004 if (find_snapshot_by_id(bs
, sn_info
->id_str
) >= 0)
2007 sn
->id_str
= qemu_strdup(sn_info
->id_str
);
2010 sn
->name
= qemu_strdup(sn_info
->name
);
2013 sn
->vm_state_size
= sn_info
->vm_state_size
;
2014 sn
->date_sec
= sn_info
->date_sec
;
2015 sn
->date_nsec
= sn_info
->date_nsec
;
2016 sn
->vm_clock_nsec
= sn_info
->vm_clock_nsec
;
2018 ret
= update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 1);
2022 /* create the L1 table of the snapshot */
2023 sn
->l1_table_offset
= alloc_clusters(bs
, s
->l1_size
* sizeof(uint64_t));
2024 sn
->l1_size
= s
->l1_size
;
2026 l1_table
= qemu_malloc(s
->l1_size
* sizeof(uint64_t));
2029 for(i
= 0; i
< s
->l1_size
; i
++) {
2030 l1_table
[i
] = cpu_to_be64(s
->l1_table
[i
]);
2032 if (bdrv_pwrite(s
->hd
, sn
->l1_table_offset
,
2033 l1_table
, s
->l1_size
* sizeof(uint64_t)) !=
2034 (s
->l1_size
* sizeof(uint64_t)))
2036 qemu_free(l1_table
);
2039 snapshots1
= qemu_malloc((s
->nb_snapshots
+ 1) * sizeof(QCowSnapshot
));
2043 memcpy(snapshots1
, s
->snapshots
, s
->nb_snapshots
* sizeof(QCowSnapshot
));
2044 qemu_free(s
->snapshots
);
2046 s
->snapshots
= snapshots1
;
2047 s
->snapshots
[s
->nb_snapshots
++] = *sn
;
2049 if (qcow_write_snapshots(bs
) < 0)
2052 check_refcounts(bs
);
2056 qemu_free(sn
->name
);
2057 qemu_free(l1_table
);
2061 /* copy the snapshot 'snapshot_name' into the current disk image */
2062 static int qcow_snapshot_goto(BlockDriverState
*bs
,
2063 const char *snapshot_id
)
2065 BDRVQcowState
*s
= bs
->opaque
;
2067 int i
, snapshot_index
, l1_size2
;
2069 snapshot_index
= find_snapshot_by_id_or_name(bs
, snapshot_id
);
2070 if (snapshot_index
< 0)
2072 sn
= &s
->snapshots
[snapshot_index
];
2074 if (update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, -1) < 0)
2077 if (grow_l1_table(bs
, sn
->l1_size
) < 0)
2080 s
->l1_size
= sn
->l1_size
;
2081 l1_size2
= s
->l1_size
* sizeof(uint64_t);
2082 /* copy the snapshot l1 table to the current l1 table */
2083 if (bdrv_pread(s
->hd
, sn
->l1_table_offset
,
2084 s
->l1_table
, l1_size2
) != l1_size2
)
2086 if (bdrv_pwrite(s
->hd
, s
->l1_table_offset
,
2087 s
->l1_table
, l1_size2
) != l1_size2
)
2089 for(i
= 0;i
< s
->l1_size
; i
++) {
2090 be64_to_cpus(&s
->l1_table
[i
]);
2093 if (update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 1) < 0)
2097 check_refcounts(bs
);
2104 static int qcow_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
2106 BDRVQcowState
*s
= bs
->opaque
;
2108 int snapshot_index
, ret
;
2110 snapshot_index
= find_snapshot_by_id_or_name(bs
, snapshot_id
);
2111 if (snapshot_index
< 0)
2113 sn
= &s
->snapshots
[snapshot_index
];
2115 ret
= update_snapshot_refcount(bs
, sn
->l1_table_offset
, sn
->l1_size
, -1);
2118 /* must update the copied flag on the current cluster offsets */
2119 ret
= update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 0);
2122 free_clusters(bs
, sn
->l1_table_offset
, sn
->l1_size
* sizeof(uint64_t));
2124 qemu_free(sn
->id_str
);
2125 qemu_free(sn
->name
);
2126 memmove(sn
, sn
+ 1, (s
->nb_snapshots
- snapshot_index
- 1) * sizeof(*sn
));
2128 ret
= qcow_write_snapshots(bs
);
2130 /* XXX: restore snapshot if error ? */
2134 check_refcounts(bs
);
2139 static int qcow_snapshot_list(BlockDriverState
*bs
,
2140 QEMUSnapshotInfo
**psn_tab
)
2142 BDRVQcowState
*s
= bs
->opaque
;
2143 QEMUSnapshotInfo
*sn_tab
, *sn_info
;
2147 sn_tab
= qemu_mallocz(s
->nb_snapshots
* sizeof(QEMUSnapshotInfo
));
2150 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
2151 sn_info
= sn_tab
+ i
;
2152 sn
= s
->snapshots
+ i
;
2153 pstrcpy(sn_info
->id_str
, sizeof(sn_info
->id_str
),
2155 pstrcpy(sn_info
->name
, sizeof(sn_info
->name
),
2157 sn_info
->vm_state_size
= sn
->vm_state_size
;
2158 sn_info
->date_sec
= sn
->date_sec
;
2159 sn_info
->date_nsec
= sn
->date_nsec
;
2160 sn_info
->vm_clock_nsec
= sn
->vm_clock_nsec
;
2163 return s
->nb_snapshots
;
2170 /*********************************************************/
2171 /* refcount handling */
2173 static int refcount_init(BlockDriverState
*bs
)
2175 BDRVQcowState
*s
= bs
->opaque
;
2176 int ret
, refcount_table_size2
, i
;
2178 s
->refcount_block_cache
= qemu_malloc(s
->cluster_size
);
2179 if (!s
->refcount_block_cache
)
2181 refcount_table_size2
= s
->refcount_table_size
* sizeof(uint64_t);
2182 s
->refcount_table
= qemu_malloc(refcount_table_size2
);
2183 if (!s
->refcount_table
)
2185 if (s
->refcount_table_size
> 0) {
2186 ret
= bdrv_pread(s
->hd
, s
->refcount_table_offset
,
2187 s
->refcount_table
, refcount_table_size2
);
2188 if (ret
!= refcount_table_size2
)
2190 for(i
= 0; i
< s
->refcount_table_size
; i
++)
2191 be64_to_cpus(&s
->refcount_table
[i
]);
2198 static void refcount_close(BlockDriverState
*bs
)
2200 BDRVQcowState
*s
= bs
->opaque
;
2201 qemu_free(s
->refcount_block_cache
);
2202 qemu_free(s
->refcount_table
);
2206 static int load_refcount_block(BlockDriverState
*bs
,
2207 int64_t refcount_block_offset
)
2209 BDRVQcowState
*s
= bs
->opaque
;
2211 ret
= bdrv_pread(s
->hd
, refcount_block_offset
, s
->refcount_block_cache
,
2213 if (ret
!= s
->cluster_size
)
2215 s
->refcount_block_cache_offset
= refcount_block_offset
;
2219 static void scan_refcount(BlockDriverState
*bs
, int64_t *high
, int64_t *free
)
2221 BDRVQcowState
*s
= bs
->opaque
;
2222 int64_t refcnt_index
, cluster_index
, cluster_end
, h
= 0, f
= 0;
2223 int64_t tail
= 0; /* do not count last consecutive free entries */
2225 for (refcnt_index
=0; refcnt_index
< s
->refcount_table_size
; refcnt_index
++){
2226 if (s
->refcount_table
[refcnt_index
] == 0) {
2227 f
+= 1 << (s
->cluster_bits
- REFCOUNT_SHIFT
);
2228 tail
+= 1 << (s
->cluster_bits
- REFCOUNT_SHIFT
);
2231 cluster_index
= refcnt_index
<< (s
->cluster_bits
- REFCOUNT_SHIFT
);
2232 cluster_end
= (refcnt_index
+ 1) << (s
->cluster_bits
- REFCOUNT_SHIFT
);
2233 for ( ; cluster_index
< cluster_end
; cluster_index
++) {
2234 if (get_refcount(bs
, cluster_index
) == 0) {
2252 static int get_refcount(BlockDriverState
*bs
, int64_t cluster_index
)
2254 BDRVQcowState
*s
= bs
->opaque
;
2255 int refcount_table_index
, block_index
;
2256 int64_t refcount_block_offset
;
2258 refcount_table_index
= cluster_index
>> (s
->cluster_bits
- REFCOUNT_SHIFT
);
2259 if (refcount_table_index
>= s
->refcount_table_size
)
2261 refcount_block_offset
= s
->refcount_table
[refcount_table_index
];
2262 if (!refcount_block_offset
)
2264 if (refcount_block_offset
!= s
->refcount_block_cache_offset
) {
2265 /* better than nothing: return allocated if read error */
2266 if (load_refcount_block(bs
, refcount_block_offset
) < 0)
2269 block_index
= cluster_index
&
2270 ((1 << (s
->cluster_bits
- REFCOUNT_SHIFT
)) - 1);
2271 return be16_to_cpu(s
->refcount_block_cache
[block_index
]);
2274 /* return < 0 if error */
2275 static int64_t alloc_clusters_noref(BlockDriverState
*bs
, int64_t size
)
2277 BDRVQcowState
*s
= bs
->opaque
;
2280 nb_clusters
= size_to_clusters(s
, size
);
2282 for(i
= 0; i
< nb_clusters
; i
++) {
2283 int64_t i
= s
->free_cluster_index
++;
2284 if (get_refcount(bs
, i
) != 0)
2288 printf("alloc_clusters: size=%lld -> %lld\n",
2290 (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
);
2293 if (s
->highest_alloc
< s
->free_cluster_index
) {
2294 s
->nc_free
+= (s
->free_cluster_index
- s
->highest_alloc
);
2295 s
->highest_alloc
= s
->free_cluster_index
;
2298 return (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
;
2301 static int64_t alloc_clusters(BlockDriverState
*bs
, int64_t size
)
2305 offset
= alloc_clusters_noref(bs
, size
);
2306 update_refcount(bs
, offset
, size
, 1);
2310 /* only used to allocate compressed sectors. We try to allocate
2311 contiguous sectors. size must be <= cluster_size */
2312 static int64_t alloc_bytes(BlockDriverState
*bs
, int size
)
2314 BDRVQcowState
*s
= bs
->opaque
;
2315 int64_t offset
, cluster_offset
;
2316 int free_in_cluster
;
2318 assert(size
> 0 && size
<= s
->cluster_size
);
2319 if (s
->free_byte_offset
== 0) {
2320 s
->free_byte_offset
= alloc_clusters(bs
, s
->cluster_size
);
2323 free_in_cluster
= s
->cluster_size
-
2324 (s
->free_byte_offset
& (s
->cluster_size
- 1));
2325 if (size
<= free_in_cluster
) {
2326 /* enough space in current cluster */
2327 offset
= s
->free_byte_offset
;
2328 s
->free_byte_offset
+= size
;
2329 free_in_cluster
-= size
;
2330 if (free_in_cluster
== 0)
2331 s
->free_byte_offset
= 0;
2332 if ((offset
& (s
->cluster_size
- 1)) != 0)
2333 update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, 1);
2335 offset
= alloc_clusters(bs
, s
->cluster_size
);
2336 cluster_offset
= s
->free_byte_offset
& ~(s
->cluster_size
- 1);
2337 if ((cluster_offset
+ s
->cluster_size
) == offset
) {
2338 /* we are lucky: contiguous data */
2339 offset
= s
->free_byte_offset
;
2340 update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, 1);
2341 s
->free_byte_offset
+= size
;
2343 s
->free_byte_offset
= offset
;
2350 static void free_clusters(BlockDriverState
*bs
,
2351 int64_t offset
, int64_t size
)
2353 update_refcount(bs
, offset
, size
, -1);
2356 static int grow_refcount_table(BlockDriverState
*bs
, int min_size
)
2358 BDRVQcowState
*s
= bs
->opaque
;
2359 int new_table_size
, new_table_size2
, refcount_table_clusters
, i
, ret
;
2360 uint64_t *new_table
;
2361 int64_t table_offset
;
2364 int64_t old_table_offset
;
2366 if (min_size
<= s
->refcount_table_size
)
2368 /* compute new table size */
2369 refcount_table_clusters
= s
->refcount_table_size
>> (s
->cluster_bits
- 3);
2371 if (refcount_table_clusters
== 0) {
2372 refcount_table_clusters
= 1;
2374 refcount_table_clusters
= (refcount_table_clusters
* 3 + 1) / 2;
2376 new_table_size
= refcount_table_clusters
<< (s
->cluster_bits
- 3);
2377 if (min_size
<= new_table_size
)
2381 printf("grow_refcount_table from %d to %d\n",
2382 s
->refcount_table_size
,
2385 new_table_size2
= new_table_size
* sizeof(uint64_t);
2386 new_table
= qemu_mallocz(new_table_size2
);
2389 memcpy(new_table
, s
->refcount_table
,
2390 s
->refcount_table_size
* sizeof(uint64_t));
2391 for(i
= 0; i
< s
->refcount_table_size
; i
++)
2392 cpu_to_be64s(&new_table
[i
]);
2393 /* Note: we cannot update the refcount now to avoid recursion */
2394 table_offset
= alloc_clusters_noref(bs
, new_table_size2
);
2395 ret
= bdrv_pwrite(s
->hd
, table_offset
, new_table
, new_table_size2
);
2396 if (ret
!= new_table_size2
)
2398 for(i
= 0; i
< s
->refcount_table_size
; i
++)
2399 be64_to_cpus(&new_table
[i
]);
2401 cpu_to_be64w((uint64_t*)data
, table_offset
);
2402 cpu_to_be32w((uint32_t*)(data
+ 8), refcount_table_clusters
);
2403 if (bdrv_pwrite(s
->hd
, offsetof(QCowHeader
, refcount_table_offset
),
2404 data
, sizeof(data
)) != sizeof(data
))
2406 qemu_free(s
->refcount_table
);
2407 old_table_offset
= s
->refcount_table_offset
;
2408 old_table_size
= s
->refcount_table_size
;
2409 s
->refcount_table
= new_table
;
2410 s
->refcount_table_size
= new_table_size
;
2411 s
->refcount_table_offset
= table_offset
;
2413 update_refcount(bs
, table_offset
, new_table_size2
, 1);
2414 free_clusters(bs
, old_table_offset
, old_table_size
* sizeof(uint64_t));
2417 free_clusters(bs
, table_offset
, new_table_size2
);
2418 qemu_free(new_table
);
2422 /* addend must be 1 or -1 */
2423 /* XXX: cache several refcount block clusters ? */
2424 static int update_cluster_refcount(BlockDriverState
*bs
,
2425 int64_t cluster_index
,
2428 BDRVQcowState
*s
= bs
->opaque
;
2429 int64_t offset
, refcount_block_offset
;
2430 int ret
, refcount_table_index
, block_index
, refcount
;
2433 refcount_table_index
= cluster_index
>> (s
->cluster_bits
- REFCOUNT_SHIFT
);
2434 if (refcount_table_index
>= s
->refcount_table_size
) {
2437 ret
= grow_refcount_table(bs
, refcount_table_index
+ 1);
2441 refcount_block_offset
= s
->refcount_table
[refcount_table_index
];
2442 if (!refcount_block_offset
) {
2445 /* create a new refcount block */
2446 /* Note: we cannot update the refcount now to avoid recursion */
2447 offset
= alloc_clusters_noref(bs
, s
->cluster_size
);
2448 memset(s
->refcount_block_cache
, 0, s
->cluster_size
);
2449 ret
= bdrv_pwrite(s
->hd
, offset
, s
->refcount_block_cache
, s
->cluster_size
);
2450 if (ret
!= s
->cluster_size
)
2452 s
->refcount_table
[refcount_table_index
] = offset
;
2453 data64
= cpu_to_be64(offset
);
2454 ret
= bdrv_pwrite(s
->hd
, s
->refcount_table_offset
+
2455 refcount_table_index
* sizeof(uint64_t),
2456 &data64
, sizeof(data64
));
2457 if (ret
!= sizeof(data64
))
2460 refcount_block_offset
= offset
;
2461 s
->refcount_block_cache_offset
= offset
;
2462 update_refcount(bs
, offset
, s
->cluster_size
, 1);
2464 if (refcount_block_offset
!= s
->refcount_block_cache_offset
) {
2465 if (load_refcount_block(bs
, refcount_block_offset
) < 0)
2469 /* we can update the count and save it */
2470 block_index
= cluster_index
&
2471 ((1 << (s
->cluster_bits
- REFCOUNT_SHIFT
)) - 1);
2472 refcount
= be16_to_cpu(s
->refcount_block_cache
[block_index
]);
2474 if (refcount
== 1 && addend
== -1)
2476 else if (refcount
== 0 && addend
== 1)
2480 if (refcount
< 0 || refcount
> 0xffff)
2482 if (refcount
== 0 && cluster_index
< s
->free_cluster_index
) {
2483 s
->free_cluster_index
= cluster_index
;
2485 s
->refcount_block_cache
[block_index
] = cpu_to_be16(refcount
);
2486 if (bdrv_pwrite(s
->hd
,
2487 refcount_block_offset
+ (block_index
<< REFCOUNT_SHIFT
),
2488 &s
->refcount_block_cache
[block_index
], 2) != 2)
2493 static void update_refcount(BlockDriverState
*bs
,
2494 int64_t offset
, int64_t length
,
2497 BDRVQcowState
*s
= bs
->opaque
;
2498 int64_t start
, last
, cluster_offset
;
2501 printf("update_refcount: offset=%lld size=%lld addend=%d\n",
2502 offset
, length
, addend
);
2506 start
= offset
& ~(s
->cluster_size
- 1);
2507 last
= (offset
+ length
- 1) & ~(s
->cluster_size
- 1);
2508 for(cluster_offset
= start
; cluster_offset
<= last
;
2509 cluster_offset
+= s
->cluster_size
) {
2510 update_cluster_refcount(bs
, cluster_offset
>> s
->cluster_bits
, addend
);
2515 static void inc_refcounts(BlockDriverState
*bs
,
2516 uint16_t *refcount_table
,
2517 int refcount_table_size
,
2518 int64_t offset
, int64_t size
)
2520 BDRVQcowState
*s
= bs
->opaque
;
2521 int64_t start
, last
, cluster_offset
;
2527 start
= offset
& ~(s
->cluster_size
- 1);
2528 last
= (offset
+ size
- 1) & ~(s
->cluster_size
- 1);
2529 for(cluster_offset
= start
; cluster_offset
<= last
;
2530 cluster_offset
+= s
->cluster_size
) {
2531 k
= cluster_offset
>> s
->cluster_bits
;
2532 if (k
< 0 || k
>= refcount_table_size
) {
2533 printf("ERROR: invalid cluster offset=0x%llx\n", cluster_offset
);
2535 if (++refcount_table
[k
] == 0) {
2536 printf("ERROR: overflow cluster offset=0x%llx\n", cluster_offset
);
2542 static int check_refcounts_l1(BlockDriverState
*bs
,
2543 uint16_t *refcount_table
,
2544 int refcount_table_size
,
2545 int64_t l1_table_offset
, int l1_size
,
2548 BDRVQcowState
*s
= bs
->opaque
;
2549 uint64_t *l1_table
, *l2_table
, l2_offset
, offset
, l1_size2
;
2550 int l2_size
, i
, j
, nb_csectors
, refcount
;
2553 l1_size2
= l1_size
* sizeof(uint64_t);
2555 inc_refcounts(bs
, refcount_table
, refcount_table_size
,
2556 l1_table_offset
, l1_size2
);
2558 l1_table
= qemu_malloc(l1_size2
);
2561 if (bdrv_pread(s
->hd
, l1_table_offset
,
2562 l1_table
, l1_size2
) != l1_size2
)
2564 for(i
= 0;i
< l1_size
; i
++)
2565 be64_to_cpus(&l1_table
[i
]);
2567 l2_size
= s
->l2_size
* sizeof(uint64_t);
2568 l2_table
= qemu_malloc(l2_size
);
2571 for(i
= 0; i
< l1_size
; i
++) {
2572 l2_offset
= l1_table
[i
];
2575 refcount
= get_refcount(bs
, (l2_offset
& ~QCOW_OFLAG_COPIED
) >> s
->cluster_bits
);
2576 if ((refcount
== 1) != ((l2_offset
& QCOW_OFLAG_COPIED
) != 0)) {
2577 printf("ERROR OFLAG_COPIED: l2_offset=%llx refcount=%d\n",
2578 l2_offset
, refcount
);
2581 l2_offset
&= ~QCOW_OFLAG_COPIED
;
2582 if (bdrv_pread(s
->hd
, l2_offset
, l2_table
, l2_size
) != l2_size
)
2584 for(j
= 0; j
< s
->l2_size
; j
++) {
2585 offset
= be64_to_cpu(l2_table
[j
]);
2587 if (offset
& QCOW_OFLAG_COMPRESSED
) {
2588 if (offset
& QCOW_OFLAG_COPIED
) {
2589 printf("ERROR: cluster %lld: copied flag must never be set for compressed clusters\n",
2590 offset
>> s
->cluster_bits
);
2591 offset
&= ~QCOW_OFLAG_COPIED
;
2593 nb_csectors
= ((offset
>> s
->csize_shift
) &
2595 offset
&= s
->cluster_offset_mask
;
2596 inc_refcounts(bs
, refcount_table
,
2597 refcount_table_size
,
2598 offset
& ~511, nb_csectors
* 512);
2601 refcount
= get_refcount(bs
, (offset
& ~QCOW_OFLAG_COPIED
) >> s
->cluster_bits
);
2602 if ((refcount
== 1) != ((offset
& QCOW_OFLAG_COPIED
) != 0)) {
2603 printf("ERROR OFLAG_COPIED: offset=%llx refcount=%d\n",
2607 offset
&= ~QCOW_OFLAG_COPIED
;
2608 inc_refcounts(bs
, refcount_table
,
2609 refcount_table_size
,
2610 offset
, s
->cluster_size
);
2614 inc_refcounts(bs
, refcount_table
,
2615 refcount_table_size
,
2620 qemu_free(l1_table
);
2621 qemu_free(l2_table
);
2624 printf("ERROR: I/O error in check_refcounts_l1\n");
2625 qemu_free(l1_table
);
2626 qemu_free(l2_table
);
2630 static void check_refcounts(BlockDriverState
*bs
)
2632 BDRVQcowState
*s
= bs
->opaque
;
2634 int nb_clusters
, refcount1
, refcount2
, i
;
2636 uint16_t *refcount_table
;
2638 size
= bdrv_getlength(s
->hd
);
2639 nb_clusters
= size_to_clusters(s
, size
);
2640 refcount_table
= qemu_mallocz(nb_clusters
* sizeof(uint16_t));
2643 inc_refcounts(bs
, refcount_table
, nb_clusters
,
2644 0, s
->cluster_size
);
2646 check_refcounts_l1(bs
, refcount_table
, nb_clusters
,
2647 s
->l1_table_offset
, s
->l1_size
, 1);
2650 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
2651 sn
= s
->snapshots
+ i
;
2652 check_refcounts_l1(bs
, refcount_table
, nb_clusters
,
2653 sn
->l1_table_offset
, sn
->l1_size
, 0);
2655 inc_refcounts(bs
, refcount_table
, nb_clusters
,
2656 s
->snapshots_offset
, s
->snapshots_size
);
2659 inc_refcounts(bs
, refcount_table
, nb_clusters
,
2660 s
->refcount_table_offset
,
2661 s
->refcount_table_size
* sizeof(uint64_t));
2662 for(i
= 0; i
< s
->refcount_table_size
; i
++) {
2664 offset
= s
->refcount_table
[i
];
2666 inc_refcounts(bs
, refcount_table
, nb_clusters
,
2667 offset
, s
->cluster_size
);
2671 /* compare ref counts */
2672 for(i
= 0; i
< nb_clusters
; i
++) {
2673 refcount1
= get_refcount(bs
, i
);
2674 refcount2
= refcount_table
[i
];
2675 if (refcount1
!= refcount2
)
2676 printf("ERROR cluster %d refcount=%d reference=%d\n",
2677 i
, refcount1
, refcount2
);
2680 qemu_free(refcount_table
);
2684 static void dump_refcounts(BlockDriverState
*bs
)
2686 BDRVQcowState
*s
= bs
->opaque
;
2687 int64_t nb_clusters
, k
, k1
, size
;
2690 size
= bdrv_getlength(s
->hd
);
2691 nb_clusters
= size_to_clusters(s
, size
);
2692 for(k
= 0; k
< nb_clusters
;) {
2694 refcount
= get_refcount(bs
, k
);
2696 while (k
< nb_clusters
&& get_refcount(bs
, k
) == refcount
)
2698 printf("%lld: refcount=%d nb=%lld\n", k
, refcount
, k
- k1
);
2704 BlockDriver bdrv_qcow2
= {
2706 sizeof(BDRVQcowState
),
2718 .bdrv_aio_read
= qcow_aio_read
,
2719 .bdrv_aio_write
= qcow_aio_write
,
2720 .bdrv_aio_cancel
= qcow_aio_cancel
,
2721 .aiocb_size
= sizeof(QCowAIOCB
),
2722 .bdrv_write_compressed
= qcow_write_compressed
,
2724 .bdrv_snapshot_create
= qcow_snapshot_create
,
2725 .bdrv_snapshot_goto
= qcow_snapshot_goto
,
2726 .bdrv_snapshot_delete
= qcow_snapshot_delete
,
2727 .bdrv_snapshot_list
= qcow_snapshot_list
,
2728 .bdrv_get_info
= qcow_get_info
,