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
50 #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
51 #define QCOW_VERSION 2
53 #define QCOW_CRYPT_NONE 0
54 #define QCOW_CRYPT_AES 1
56 #define QCOW_MAX_CRYPT_CLUSTERS 32
58 /* indicate that the refcount of the referenced cluster is exactly one. */
59 #define QCOW_OFLAG_COPIED (1LL << 63)
60 /* indicate that the cluster is compressed (they never have the copied flag) */
61 #define QCOW_OFLAG_COMPRESSED (1LL << 62)
63 #define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
65 typedef struct QCowHeader
{
68 uint64_t backing_file_offset
;
69 uint32_t backing_file_size
;
70 uint32_t cluster_bits
;
71 uint64_t size
; /* in bytes */
72 uint32_t crypt_method
;
73 uint32_t l1_size
; /* XXX: save number of clusters instead ? */
74 uint64_t l1_table_offset
;
75 uint64_t refcount_table_offset
;
76 uint32_t refcount_table_clusters
;
77 uint32_t nb_snapshots
;
78 uint64_t snapshots_offset
;
86 #define QCOW_EXT_MAGIC_END 0
89 typedef struct __attribute__((packed
)) QCowSnapshotHeader
{
90 /* header is 8 byte aligned */
91 uint64_t l1_table_offset
;
100 uint64_t vm_clock_nsec
;
102 uint32_t vm_state_size
;
103 uint32_t extra_data_size
; /* for extension */
104 /* extra data follows */
107 } QCowSnapshotHeader
;
109 #define L2_CACHE_SIZE 16
111 typedef struct QCowSnapshot
{
112 uint64_t l1_table_offset
;
116 uint32_t vm_state_size
;
119 uint64_t vm_clock_nsec
;
122 typedef struct BDRVQcowState
{
123 BlockDriverState
*hd
;
130 int l1_vm_state_index
;
133 uint64_t cluster_offset_mask
;
134 uint64_t l1_table_offset
;
137 uint64_t l2_cache_offsets
[L2_CACHE_SIZE
];
138 uint32_t l2_cache_counts
[L2_CACHE_SIZE
];
139 uint8_t *cluster_cache
;
140 uint8_t *cluster_data
;
141 uint64_t cluster_cache_offset
;
143 uint64_t *refcount_table
;
144 uint64_t refcount_table_offset
;
145 uint32_t refcount_table_size
;
146 uint64_t refcount_block_cache_offset
;
147 uint16_t *refcount_block_cache
;
148 int64_t free_cluster_index
;
149 int64_t free_byte_offset
;
151 uint32_t crypt_method
; /* current crypt method, 0 if no key yet */
152 uint32_t crypt_method_header
;
153 AES_KEY aes_encrypt_key
;
154 AES_KEY aes_decrypt_key
;
155 uint64_t snapshots_offset
;
158 QCowSnapshot
*snapshots
;
161 static int decompress_cluster(BDRVQcowState
*s
, uint64_t cluster_offset
);
162 static int qcow_read(BlockDriverState
*bs
, int64_t sector_num
,
163 uint8_t *buf
, int nb_sectors
);
164 static int qcow_read_snapshots(BlockDriverState
*bs
);
165 static void qcow_free_snapshots(BlockDriverState
*bs
);
166 static int refcount_init(BlockDriverState
*bs
);
167 static void refcount_close(BlockDriverState
*bs
);
168 static int get_refcount(BlockDriverState
*bs
, int64_t cluster_index
);
169 static int update_cluster_refcount(BlockDriverState
*bs
,
170 int64_t cluster_index
,
172 static void update_refcount(BlockDriverState
*bs
,
173 int64_t offset
, int64_t length
,
175 static int64_t alloc_clusters(BlockDriverState
*bs
, int64_t size
);
176 static int64_t alloc_bytes(BlockDriverState
*bs
, int size
);
177 static void free_clusters(BlockDriverState
*bs
,
178 int64_t offset
, int64_t size
);
180 static void check_refcounts(BlockDriverState
*bs
);
183 static int qcow_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
185 const QCowHeader
*cow_header
= (const void *)buf
;
187 if (buf_size
>= sizeof(QCowHeader
) &&
188 be32_to_cpu(cow_header
->magic
) == QCOW_MAGIC
&&
189 be32_to_cpu(cow_header
->version
) == QCOW_VERSION
)
197 * read qcow2 extension and fill bs
198 * start reading from start_offset
199 * finish reading upon magic of value 0 or when end_offset reached
200 * unknown magic is skipped (future extension this version knows nothing about)
201 * return 0 upon success, non-0 otherwise
203 static int qcow_read_extensions(BlockDriverState
*bs
, uint64_t start_offset
,
206 BDRVQcowState
*s
= bs
->opaque
;
211 printf("qcow_read_extensions: start=%ld end=%ld\n", start_offset
, end_offset
);
213 offset
= start_offset
;
214 while (offset
< end_offset
) {
218 if (offset
> s
->cluster_size
)
219 printf("qcow_handle_extension: suspicious offset %lu\n", offset
);
221 printf("attemting to read extended header in offset %lu\n", offset
);
224 if (bdrv_pread(s
->hd
, offset
, &ext
, sizeof(ext
)) != sizeof(ext
)) {
225 fprintf(stderr
, "qcow_handle_extension: ERROR: pread fail from offset %lu\n",
229 be32_to_cpus(&ext
.magic
);
230 be32_to_cpus(&ext
.len
);
231 offset
+= sizeof(ext
);
233 printf("ext.magic = 0x%x\n", ext
.magic
);
236 case QCOW_EXT_MAGIC_END
:
239 /* unknown magic -- just skip it */
240 offset
+= ((ext
.len
+ 7) & ~7);
249 static int qcow_open(BlockDriverState
*bs
, const char *filename
, int flags
)
251 BDRVQcowState
*s
= bs
->opaque
;
252 int len
, i
, shift
, ret
;
256 /* Performance is terrible right now with cache=writethrough due mainly
257 * to reference count updates. If the user does not explicitly specify
258 * a caching type, force to writeback caching.
260 if ((flags
& BDRV_O_CACHE_DEF
)) {
261 flags
|= BDRV_O_CACHE_WB
;
262 flags
&= ~BDRV_O_CACHE_DEF
;
264 ret
= bdrv_file_open(&s
->hd
, filename
, flags
);
267 if (bdrv_pread(s
->hd
, 0, &header
, sizeof(header
)) != sizeof(header
))
269 be32_to_cpus(&header
.magic
);
270 be32_to_cpus(&header
.version
);
271 be64_to_cpus(&header
.backing_file_offset
);
272 be32_to_cpus(&header
.backing_file_size
);
273 be64_to_cpus(&header
.size
);
274 be32_to_cpus(&header
.cluster_bits
);
275 be32_to_cpus(&header
.crypt_method
);
276 be64_to_cpus(&header
.l1_table_offset
);
277 be32_to_cpus(&header
.l1_size
);
278 be64_to_cpus(&header
.refcount_table_offset
);
279 be32_to_cpus(&header
.refcount_table_clusters
);
280 be64_to_cpus(&header
.snapshots_offset
);
281 be32_to_cpus(&header
.nb_snapshots
);
283 if (header
.magic
!= QCOW_MAGIC
|| header
.version
!= QCOW_VERSION
)
285 if (header
.size
<= 1 ||
286 header
.cluster_bits
< 9 ||
287 header
.cluster_bits
> 16)
289 if (header
.crypt_method
> QCOW_CRYPT_AES
)
291 s
->crypt_method_header
= header
.crypt_method
;
292 if (s
->crypt_method_header
)
294 s
->cluster_bits
= header
.cluster_bits
;
295 s
->cluster_size
= 1 << s
->cluster_bits
;
296 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
297 s
->l2_bits
= s
->cluster_bits
- 3; /* L2 is always one cluster */
298 s
->l2_size
= 1 << s
->l2_bits
;
299 bs
->total_sectors
= header
.size
/ 512;
300 s
->csize_shift
= (62 - (s
->cluster_bits
- 8));
301 s
->csize_mask
= (1 << (s
->cluster_bits
- 8)) - 1;
302 s
->cluster_offset_mask
= (1LL << s
->csize_shift
) - 1;
303 s
->refcount_table_offset
= header
.refcount_table_offset
;
304 s
->refcount_table_size
=
305 header
.refcount_table_clusters
<< (s
->cluster_bits
- 3);
307 s
->snapshots_offset
= header
.snapshots_offset
;
308 s
->nb_snapshots
= header
.nb_snapshots
;
310 /* read the level 1 table */
311 s
->l1_size
= header
.l1_size
;
312 shift
= s
->cluster_bits
+ s
->l2_bits
;
313 s
->l1_vm_state_index
= (header
.size
+ (1LL << shift
) - 1) >> shift
;
314 /* the L1 table must contain at least enough entries to put
316 if (s
->l1_size
< s
->l1_vm_state_index
)
318 s
->l1_table_offset
= header
.l1_table_offset
;
319 s
->l1_table
= qemu_malloc(s
->l1_size
* sizeof(uint64_t));
320 if (bdrv_pread(s
->hd
, s
->l1_table_offset
, s
->l1_table
, s
->l1_size
* sizeof(uint64_t)) !=
321 s
->l1_size
* sizeof(uint64_t))
323 for(i
= 0;i
< s
->l1_size
; i
++) {
324 be64_to_cpus(&s
->l1_table
[i
]);
327 s
->l2_cache
= qemu_malloc(s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
328 s
->cluster_cache
= qemu_malloc(s
->cluster_size
);
329 /* one more sector for decompressed data alignment */
330 s
->cluster_data
= qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
332 s
->cluster_cache_offset
= -1;
334 if (refcount_init(bs
) < 0)
337 /* read qcow2 extensions */
338 if (header
.backing_file_offset
)
339 ext_end
= header
.backing_file_offset
;
341 ext_end
= s
->cluster_size
;
342 if (qcow_read_extensions(bs
, sizeof(header
), ext_end
))
345 /* read the backing file name */
346 if (header
.backing_file_offset
!= 0) {
347 len
= header
.backing_file_size
;
350 if (bdrv_pread(s
->hd
, header
.backing_file_offset
, bs
->backing_file
, len
) != len
)
352 bs
->backing_file
[len
] = '\0';
354 if (qcow_read_snapshots(bs
) < 0)
363 qcow_free_snapshots(bs
);
365 qemu_free(s
->l1_table
);
366 qemu_free(s
->l2_cache
);
367 qemu_free(s
->cluster_cache
);
368 qemu_free(s
->cluster_data
);
373 static int qcow_set_key(BlockDriverState
*bs
, const char *key
)
375 BDRVQcowState
*s
= bs
->opaque
;
379 memset(keybuf
, 0, 16);
383 /* XXX: we could compress the chars to 7 bits to increase
385 for(i
= 0;i
< len
;i
++) {
388 s
->crypt_method
= s
->crypt_method_header
;
390 if (AES_set_encrypt_key(keybuf
, 128, &s
->aes_encrypt_key
) != 0)
392 if (AES_set_decrypt_key(keybuf
, 128, &s
->aes_decrypt_key
) != 0)
402 AES_encrypt(in
, tmp
, &s
->aes_encrypt_key
);
403 AES_decrypt(tmp
, out
, &s
->aes_decrypt_key
);
404 for(i
= 0; i
< 16; i
++)
405 printf(" %02x", tmp
[i
]);
407 for(i
= 0; i
< 16; i
++)
408 printf(" %02x", out
[i
]);
415 /* The crypt function is compatible with the linux cryptoloop
416 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
418 static void encrypt_sectors(BDRVQcowState
*s
, int64_t sector_num
,
419 uint8_t *out_buf
, const uint8_t *in_buf
,
420 int nb_sectors
, int enc
,
429 for(i
= 0; i
< nb_sectors
; i
++) {
430 ivec
.ll
[0] = cpu_to_le64(sector_num
);
432 AES_cbc_encrypt(in_buf
, out_buf
, 512, key
,
440 static int copy_sectors(BlockDriverState
*bs
, uint64_t start_sect
,
441 uint64_t cluster_offset
, int n_start
, int n_end
)
443 BDRVQcowState
*s
= bs
->opaque
;
449 ret
= qcow_read(bs
, start_sect
+ n_start
, s
->cluster_data
, n
);
452 if (s
->crypt_method
) {
453 encrypt_sectors(s
, start_sect
+ n_start
,
455 s
->cluster_data
, n
, 1,
456 &s
->aes_encrypt_key
);
458 ret
= bdrv_write(s
->hd
, (cluster_offset
>> 9) + n_start
,
465 static void l2_cache_reset(BlockDriverState
*bs
)
467 BDRVQcowState
*s
= bs
->opaque
;
469 memset(s
->l2_cache
, 0, s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
470 memset(s
->l2_cache_offsets
, 0, L2_CACHE_SIZE
* sizeof(uint64_t));
471 memset(s
->l2_cache_counts
, 0, L2_CACHE_SIZE
* sizeof(uint32_t));
474 static inline int l2_cache_new_entry(BlockDriverState
*bs
)
476 BDRVQcowState
*s
= bs
->opaque
;
480 /* find a new entry in the least used one */
482 min_count
= 0xffffffff;
483 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
484 if (s
->l2_cache_counts
[i
] < min_count
) {
485 min_count
= s
->l2_cache_counts
[i
];
492 static int64_t align_offset(int64_t offset
, int n
)
494 offset
= (offset
+ n
- 1) & ~(n
- 1);
498 static int grow_l1_table(BlockDriverState
*bs
, int min_size
)
500 BDRVQcowState
*s
= bs
->opaque
;
501 int new_l1_size
, new_l1_size2
, ret
, i
;
502 uint64_t *new_l1_table
;
503 uint64_t new_l1_table_offset
;
506 new_l1_size
= s
->l1_size
;
507 if (min_size
<= new_l1_size
)
509 while (min_size
> new_l1_size
) {
510 new_l1_size
= (new_l1_size
* 3 + 1) / 2;
513 printf("grow l1_table from %d to %d\n", s
->l1_size
, new_l1_size
);
516 new_l1_size2
= sizeof(uint64_t) * new_l1_size
;
517 new_l1_table
= qemu_mallocz(new_l1_size2
);
518 memcpy(new_l1_table
, s
->l1_table
, s
->l1_size
* sizeof(uint64_t));
520 /* write new table (align to cluster) */
521 new_l1_table_offset
= alloc_clusters(bs
, new_l1_size2
);
523 for(i
= 0; i
< s
->l1_size
; i
++)
524 new_l1_table
[i
] = cpu_to_be64(new_l1_table
[i
]);
525 ret
= bdrv_pwrite(s
->hd
, new_l1_table_offset
, new_l1_table
, new_l1_size2
);
526 if (ret
!= new_l1_size2
)
528 for(i
= 0; i
< s
->l1_size
; i
++)
529 new_l1_table
[i
] = be64_to_cpu(new_l1_table
[i
]);
532 cpu_to_be32w((uint32_t*)data
, new_l1_size
);
533 cpu_to_be64w((uint64_t*)(data
+ 4), new_l1_table_offset
);
534 if (bdrv_pwrite(s
->hd
, offsetof(QCowHeader
, l1_size
), data
,
535 sizeof(data
)) != sizeof(data
))
537 qemu_free(s
->l1_table
);
538 free_clusters(bs
, s
->l1_table_offset
, s
->l1_size
* sizeof(uint64_t));
539 s
->l1_table_offset
= new_l1_table_offset
;
540 s
->l1_table
= new_l1_table
;
541 s
->l1_size
= new_l1_size
;
544 qemu_free(s
->l1_table
);
551 * seek l2_offset in the l2_cache table
552 * if not found, return NULL,
554 * increments the l2 cache hit count of the entry,
555 * if counter overflow, divide by two all counters
556 * return the pointer to the l2 cache entry
560 static uint64_t *seek_l2_table(BDRVQcowState
*s
, uint64_t l2_offset
)
564 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
565 if (l2_offset
== s
->l2_cache_offsets
[i
]) {
566 /* increment the hit count */
567 if (++s
->l2_cache_counts
[i
] == 0xffffffff) {
568 for(j
= 0; j
< L2_CACHE_SIZE
; j
++) {
569 s
->l2_cache_counts
[j
] >>= 1;
572 return s
->l2_cache
+ (i
<< s
->l2_bits
);
581 * Loads a L2 table into memory. If the table is in the cache, the cache
582 * is used; otherwise the L2 table is loaded from the image file.
584 * Returns a pointer to the L2 table on success, or NULL if the read from
585 * the image file failed.
588 static uint64_t *l2_load(BlockDriverState
*bs
, uint64_t l2_offset
)
590 BDRVQcowState
*s
= bs
->opaque
;
594 /* seek if the table for the given offset is in the cache */
596 l2_table
= seek_l2_table(s
, l2_offset
);
597 if (l2_table
!= NULL
)
600 /* not found: load a new entry in the least used one */
602 min_index
= l2_cache_new_entry(bs
);
603 l2_table
= s
->l2_cache
+ (min_index
<< s
->l2_bits
);
604 if (bdrv_pread(s
->hd
, l2_offset
, l2_table
, s
->l2_size
* sizeof(uint64_t)) !=
605 s
->l2_size
* sizeof(uint64_t))
607 s
->l2_cache_offsets
[min_index
] = l2_offset
;
608 s
->l2_cache_counts
[min_index
] = 1;
616 * Allocate a new l2 entry in the file. If l1_index points to an already
617 * used entry in the L2 table (i.e. we are doing a copy on write for the L2
618 * table) copy the contents of the old L2 table into the newly allocated one.
619 * Otherwise the new table is initialized with zeros.
623 static uint64_t *l2_allocate(BlockDriverState
*bs
, int l1_index
)
625 BDRVQcowState
*s
= bs
->opaque
;
627 uint64_t old_l2_offset
, tmp
;
628 uint64_t *l2_table
, l2_offset
;
630 old_l2_offset
= s
->l1_table
[l1_index
];
632 /* allocate a new l2 entry */
634 l2_offset
= alloc_clusters(bs
, s
->l2_size
* sizeof(uint64_t));
636 /* update the L1 entry */
638 s
->l1_table
[l1_index
] = l2_offset
| QCOW_OFLAG_COPIED
;
640 tmp
= cpu_to_be64(l2_offset
| QCOW_OFLAG_COPIED
);
641 if (bdrv_pwrite(s
->hd
, s
->l1_table_offset
+ l1_index
* sizeof(tmp
),
642 &tmp
, sizeof(tmp
)) != sizeof(tmp
))
645 /* allocate a new entry in the l2 cache */
647 min_index
= l2_cache_new_entry(bs
);
648 l2_table
= s
->l2_cache
+ (min_index
<< s
->l2_bits
);
650 if (old_l2_offset
== 0) {
651 /* if there was no old l2 table, clear the new table */
652 memset(l2_table
, 0, s
->l2_size
* sizeof(uint64_t));
654 /* if there was an old l2 table, read it from the disk */
655 if (bdrv_pread(s
->hd
, old_l2_offset
,
656 l2_table
, s
->l2_size
* sizeof(uint64_t)) !=
657 s
->l2_size
* sizeof(uint64_t))
660 /* write the l2 table to the file */
661 if (bdrv_pwrite(s
->hd
, l2_offset
,
662 l2_table
, s
->l2_size
* sizeof(uint64_t)) !=
663 s
->l2_size
* sizeof(uint64_t))
666 /* update the l2 cache entry */
668 s
->l2_cache_offsets
[min_index
] = l2_offset
;
669 s
->l2_cache_counts
[min_index
] = 1;
674 static int size_to_clusters(BDRVQcowState
*s
, int64_t size
)
676 return (size
+ (s
->cluster_size
- 1)) >> s
->cluster_bits
;
679 static int count_contiguous_clusters(uint64_t nb_clusters
, int cluster_size
,
680 uint64_t *l2_table
, uint64_t start
, uint64_t mask
)
683 uint64_t offset
= be64_to_cpu(l2_table
[0]) & ~mask
;
688 for (i
= start
; i
< start
+ nb_clusters
; i
++)
689 if (offset
+ i
* cluster_size
!= (be64_to_cpu(l2_table
[i
]) & ~mask
))
695 static int count_contiguous_free_clusters(uint64_t nb_clusters
, uint64_t *l2_table
)
699 while(nb_clusters
-- && l2_table
[i
] == 0)
708 * For a given offset of the disk image, return cluster offset in
711 * on entry, *num is the number of contiguous clusters we'd like to
712 * access following offset.
714 * on exit, *num is the number of contiguous clusters we can read.
716 * Return 1, if the offset is found
717 * Return 0, otherwise.
721 static uint64_t get_cluster_offset(BlockDriverState
*bs
,
722 uint64_t offset
, int *num
)
724 BDRVQcowState
*s
= bs
->opaque
;
725 int l1_index
, l2_index
;
726 uint64_t l2_offset
, *l2_table
, cluster_offset
;
728 int index_in_cluster
, nb_available
, nb_needed
, nb_clusters
;
730 index_in_cluster
= (offset
>> 9) & (s
->cluster_sectors
- 1);
731 nb_needed
= *num
+ index_in_cluster
;
733 l1_bits
= s
->l2_bits
+ s
->cluster_bits
;
735 /* compute how many bytes there are between the offset and
736 * the end of the l1 entry
739 nb_available
= (1 << l1_bits
) - (offset
& ((1 << l1_bits
) - 1));
741 /* compute the number of available sectors */
743 nb_available
= (nb_available
>> 9) + index_in_cluster
;
747 /* seek the the l2 offset in the l1 table */
749 l1_index
= offset
>> l1_bits
;
750 if (l1_index
>= s
->l1_size
)
753 l2_offset
= s
->l1_table
[l1_index
];
755 /* seek the l2 table of the given l2 offset */
760 /* load the l2 table in memory */
762 l2_offset
&= ~QCOW_OFLAG_COPIED
;
763 l2_table
= l2_load(bs
, l2_offset
);
764 if (l2_table
== NULL
)
767 /* find the cluster offset for the given disk offset */
769 l2_index
= (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
770 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
771 nb_clusters
= size_to_clusters(s
, nb_needed
<< 9);
773 if (!cluster_offset
) {
774 /* how many empty clusters ? */
775 c
= count_contiguous_free_clusters(nb_clusters
, &l2_table
[l2_index
]);
777 /* how many allocated clusters ? */
778 c
= count_contiguous_clusters(nb_clusters
, s
->cluster_size
,
779 &l2_table
[l2_index
], 0, QCOW_OFLAG_COPIED
);
782 nb_available
= (c
* s
->cluster_sectors
);
784 if (nb_available
> nb_needed
)
785 nb_available
= nb_needed
;
787 *num
= nb_available
- index_in_cluster
;
789 return cluster_offset
& ~QCOW_OFLAG_COPIED
;
795 * free clusters according to its type: compressed or not
799 static void free_any_clusters(BlockDriverState
*bs
,
800 uint64_t cluster_offset
, int nb_clusters
)
802 BDRVQcowState
*s
= bs
->opaque
;
804 /* free the cluster */
806 if (cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
808 nb_csectors
= ((cluster_offset
>> s
->csize_shift
) &
810 free_clusters(bs
, (cluster_offset
& s
->cluster_offset_mask
) & ~511,
815 free_clusters(bs
, cluster_offset
, nb_clusters
<< s
->cluster_bits
);
823 * for a given disk offset, load (and allocate if needed)
826 * the l2 table offset in the qcow2 file and the cluster index
827 * in the l2 table are given to the caller.
831 static int get_cluster_table(BlockDriverState
*bs
, uint64_t offset
,
832 uint64_t **new_l2_table
,
833 uint64_t *new_l2_offset
,
836 BDRVQcowState
*s
= bs
->opaque
;
837 int l1_index
, l2_index
, ret
;
838 uint64_t l2_offset
, *l2_table
;
840 /* seek the the l2 offset in the l1 table */
842 l1_index
= offset
>> (s
->l2_bits
+ s
->cluster_bits
);
843 if (l1_index
>= s
->l1_size
) {
844 ret
= grow_l1_table(bs
, l1_index
+ 1);
848 l2_offset
= s
->l1_table
[l1_index
];
850 /* seek the l2 table of the given l2 offset */
852 if (l2_offset
& QCOW_OFLAG_COPIED
) {
853 /* load the l2 table in memory */
854 l2_offset
&= ~QCOW_OFLAG_COPIED
;
855 l2_table
= l2_load(bs
, l2_offset
);
856 if (l2_table
== NULL
)
860 free_clusters(bs
, l2_offset
, s
->l2_size
* sizeof(uint64_t));
861 l2_table
= l2_allocate(bs
, l1_index
);
862 if (l2_table
== NULL
)
864 l2_offset
= s
->l1_table
[l1_index
] & ~QCOW_OFLAG_COPIED
;
867 /* find the cluster offset for the given disk offset */
869 l2_index
= (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
871 *new_l2_table
= l2_table
;
872 *new_l2_offset
= l2_offset
;
873 *new_l2_index
= l2_index
;
879 * alloc_compressed_cluster_offset
881 * For a given offset of the disk image, return cluster offset in
884 * If the offset is not found, allocate a new compressed cluster.
886 * Return the cluster offset if successful,
887 * Return 0, otherwise.
891 static uint64_t alloc_compressed_cluster_offset(BlockDriverState
*bs
,
895 BDRVQcowState
*s
= bs
->opaque
;
897 uint64_t l2_offset
, *l2_table
, cluster_offset
;
900 ret
= get_cluster_table(bs
, offset
, &l2_table
, &l2_offset
, &l2_index
);
904 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
905 if (cluster_offset
& QCOW_OFLAG_COPIED
)
906 return cluster_offset
& ~QCOW_OFLAG_COPIED
;
909 free_any_clusters(bs
, cluster_offset
, 1);
911 cluster_offset
= alloc_bytes(bs
, compressed_size
);
912 nb_csectors
= ((cluster_offset
+ compressed_size
- 1) >> 9) -
913 (cluster_offset
>> 9);
915 cluster_offset
|= QCOW_OFLAG_COMPRESSED
|
916 ((uint64_t)nb_csectors
<< s
->csize_shift
);
918 /* update L2 table */
920 /* compressed clusters never have the copied flag */
922 l2_table
[l2_index
] = cpu_to_be64(cluster_offset
);
923 if (bdrv_pwrite(s
->hd
,
924 l2_offset
+ l2_index
* sizeof(uint64_t),
926 sizeof(uint64_t)) != sizeof(uint64_t))
929 return cluster_offset
;
932 typedef struct QCowL2Meta
940 static int alloc_cluster_link_l2(BlockDriverState
*bs
, uint64_t cluster_offset
,
943 BDRVQcowState
*s
= bs
->opaque
;
944 int i
, j
= 0, l2_index
, ret
;
945 uint64_t *old_cluster
, start_sect
, l2_offset
, *l2_table
;
947 if (m
->nb_clusters
== 0)
950 old_cluster
= qemu_malloc(m
->nb_clusters
* sizeof(uint64_t));
952 /* copy content of unmodified sectors */
953 start_sect
= (m
->offset
& ~(s
->cluster_size
- 1)) >> 9;
955 ret
= copy_sectors(bs
, start_sect
, cluster_offset
, 0, m
->n_start
);
960 if (m
->nb_available
& (s
->cluster_sectors
- 1)) {
961 uint64_t end
= m
->nb_available
& ~(uint64_t)(s
->cluster_sectors
- 1);
962 ret
= copy_sectors(bs
, start_sect
+ end
, cluster_offset
+ (end
<< 9),
963 m
->nb_available
- end
, s
->cluster_sectors
);
969 /* update L2 table */
970 if (!get_cluster_table(bs
, m
->offset
, &l2_table
, &l2_offset
, &l2_index
))
973 for (i
= 0; i
< m
->nb_clusters
; i
++) {
974 if(l2_table
[l2_index
+ i
] != 0)
975 old_cluster
[j
++] = l2_table
[l2_index
+ i
];
977 l2_table
[l2_index
+ i
] = cpu_to_be64((cluster_offset
+
978 (i
<< s
->cluster_bits
)) | QCOW_OFLAG_COPIED
);
981 if (bdrv_pwrite(s
->hd
, l2_offset
+ l2_index
* sizeof(uint64_t),
982 l2_table
+ l2_index
, m
->nb_clusters
* sizeof(uint64_t)) !=
983 m
->nb_clusters
* sizeof(uint64_t))
986 for (i
= 0; i
< j
; i
++)
987 free_any_clusters(bs
, old_cluster
[i
], 1);
991 qemu_free(old_cluster
);
996 * alloc_cluster_offset
998 * For a given offset of the disk image, return cluster offset in
1001 * If the offset is not found, allocate a new cluster.
1003 * Return the cluster offset if successful,
1004 * Return 0, otherwise.
1008 static uint64_t alloc_cluster_offset(BlockDriverState
*bs
,
1010 int n_start
, int n_end
,
1011 int *num
, QCowL2Meta
*m
)
1013 BDRVQcowState
*s
= bs
->opaque
;
1015 uint64_t l2_offset
, *l2_table
, cluster_offset
;
1016 int nb_clusters
, i
= 0;
1018 ret
= get_cluster_table(bs
, offset
, &l2_table
, &l2_offset
, &l2_index
);
1022 nb_clusters
= size_to_clusters(s
, n_end
<< 9);
1024 nb_clusters
= MIN(nb_clusters
, s
->l2_size
- l2_index
);
1026 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
1028 /* We keep all QCOW_OFLAG_COPIED clusters */
1030 if (cluster_offset
& QCOW_OFLAG_COPIED
) {
1031 nb_clusters
= count_contiguous_clusters(nb_clusters
, s
->cluster_size
,
1032 &l2_table
[l2_index
], 0, 0);
1034 cluster_offset
&= ~QCOW_OFLAG_COPIED
;
1040 /* for the moment, multiple compressed clusters are not managed */
1042 if (cluster_offset
& QCOW_OFLAG_COMPRESSED
)
1045 /* how many available clusters ? */
1047 while (i
< nb_clusters
) {
1048 i
+= count_contiguous_clusters(nb_clusters
- i
, s
->cluster_size
,
1049 &l2_table
[l2_index
], i
, 0);
1051 if(be64_to_cpu(l2_table
[l2_index
+ i
]))
1054 i
+= count_contiguous_free_clusters(nb_clusters
- i
,
1055 &l2_table
[l2_index
+ i
]);
1057 cluster_offset
= be64_to_cpu(l2_table
[l2_index
+ i
]);
1059 if ((cluster_offset
& QCOW_OFLAG_COPIED
) ||
1060 (cluster_offset
& QCOW_OFLAG_COMPRESSED
))
1065 /* allocate a new cluster */
1067 cluster_offset
= alloc_clusters(bs
, nb_clusters
* s
->cluster_size
);
1069 /* save info needed for meta data update */
1071 m
->n_start
= n_start
;
1072 m
->nb_clusters
= nb_clusters
;
1075 m
->nb_available
= MIN(nb_clusters
<< (s
->cluster_bits
- 9), n_end
);
1077 *num
= m
->nb_available
- n_start
;
1079 return cluster_offset
;
1082 static int qcow_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
1083 int nb_sectors
, int *pnum
)
1085 uint64_t cluster_offset
;
1088 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, pnum
);
1090 return (cluster_offset
!= 0);
1093 static int decompress_buffer(uint8_t *out_buf
, int out_buf_size
,
1094 const uint8_t *buf
, int buf_size
)
1096 z_stream strm1
, *strm
= &strm1
;
1099 memset(strm
, 0, sizeof(*strm
));
1101 strm
->next_in
= (uint8_t *)buf
;
1102 strm
->avail_in
= buf_size
;
1103 strm
->next_out
= out_buf
;
1104 strm
->avail_out
= out_buf_size
;
1106 ret
= inflateInit2(strm
, -12);
1109 ret
= inflate(strm
, Z_FINISH
);
1110 out_len
= strm
->next_out
- out_buf
;
1111 if ((ret
!= Z_STREAM_END
&& ret
!= Z_BUF_ERROR
) ||
1112 out_len
!= out_buf_size
) {
1120 static int decompress_cluster(BDRVQcowState
*s
, uint64_t cluster_offset
)
1122 int ret
, csize
, nb_csectors
, sector_offset
;
1125 coffset
= cluster_offset
& s
->cluster_offset_mask
;
1126 if (s
->cluster_cache_offset
!= coffset
) {
1127 nb_csectors
= ((cluster_offset
>> s
->csize_shift
) & s
->csize_mask
) + 1;
1128 sector_offset
= coffset
& 511;
1129 csize
= nb_csectors
* 512 - sector_offset
;
1130 ret
= bdrv_read(s
->hd
, coffset
>> 9, s
->cluster_data
, nb_csectors
);
1134 if (decompress_buffer(s
->cluster_cache
, s
->cluster_size
,
1135 s
->cluster_data
+ sector_offset
, csize
) < 0) {
1138 s
->cluster_cache_offset
= coffset
;
1143 /* handle reading after the end of the backing file */
1144 static int backing_read1(BlockDriverState
*bs
,
1145 int64_t sector_num
, uint8_t *buf
, int nb_sectors
)
1148 if ((sector_num
+ nb_sectors
) <= bs
->total_sectors
)
1150 if (sector_num
>= bs
->total_sectors
)
1153 n1
= bs
->total_sectors
- sector_num
;
1154 memset(buf
+ n1
* 512, 0, 512 * (nb_sectors
- n1
));
1158 static int qcow_read(BlockDriverState
*bs
, int64_t sector_num
,
1159 uint8_t *buf
, int nb_sectors
)
1161 BDRVQcowState
*s
= bs
->opaque
;
1162 int ret
, index_in_cluster
, n
, n1
;
1163 uint64_t cluster_offset
;
1165 while (nb_sectors
> 0) {
1167 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, &n
);
1168 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
1169 if (!cluster_offset
) {
1170 if (bs
->backing_hd
) {
1171 /* read from the base image */
1172 n1
= backing_read1(bs
->backing_hd
, sector_num
, buf
, n
);
1174 ret
= bdrv_read(bs
->backing_hd
, sector_num
, buf
, n1
);
1179 memset(buf
, 0, 512 * n
);
1181 } else if (cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
1182 if (decompress_cluster(s
, cluster_offset
) < 0)
1184 memcpy(buf
, s
->cluster_cache
+ index_in_cluster
* 512, 512 * n
);
1186 ret
= bdrv_pread(s
->hd
, cluster_offset
+ index_in_cluster
* 512, buf
, n
* 512);
1189 if (s
->crypt_method
) {
1190 encrypt_sectors(s
, sector_num
, buf
, buf
, n
, 0,
1191 &s
->aes_decrypt_key
);
1201 static int qcow_write(BlockDriverState
*bs
, int64_t sector_num
,
1202 const uint8_t *buf
, int nb_sectors
)
1204 BDRVQcowState
*s
= bs
->opaque
;
1205 int ret
, index_in_cluster
, n
;
1206 uint64_t cluster_offset
;
1210 while (nb_sectors
> 0) {
1211 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
1212 n_end
= index_in_cluster
+ nb_sectors
;
1213 if (s
->crypt_method
&&
1214 n_end
> QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
)
1215 n_end
= QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
;
1216 cluster_offset
= alloc_cluster_offset(bs
, sector_num
<< 9,
1218 n_end
, &n
, &l2meta
);
1219 if (!cluster_offset
)
1221 if (s
->crypt_method
) {
1222 encrypt_sectors(s
, sector_num
, s
->cluster_data
, buf
, n
, 1,
1223 &s
->aes_encrypt_key
);
1224 ret
= bdrv_pwrite(s
->hd
, cluster_offset
+ index_in_cluster
* 512,
1225 s
->cluster_data
, n
* 512);
1227 ret
= bdrv_pwrite(s
->hd
, cluster_offset
+ index_in_cluster
* 512, buf
, n
* 512);
1229 if (ret
!= n
* 512 || alloc_cluster_link_l2(bs
, cluster_offset
, &l2meta
) < 0) {
1230 free_any_clusters(bs
, cluster_offset
, l2meta
.nb_clusters
);
1237 s
->cluster_cache_offset
= -1; /* disable compressed cache */
1241 typedef struct QCowAIOCB
{
1242 BlockDriverAIOCB common
;
1247 uint64_t cluster_offset
;
1248 uint8_t *cluster_data
;
1249 BlockDriverAIOCB
*hd_aiocb
;
1254 static void qcow_aio_read_cb(void *opaque
, int ret
);
1255 static void qcow_aio_read_bh(void *opaque
)
1257 QCowAIOCB
*acb
= opaque
;
1258 qemu_bh_delete(acb
->bh
);
1260 qcow_aio_read_cb(opaque
, 0);
1263 static int qcow_schedule_bh(QEMUBHFunc
*cb
, QCowAIOCB
*acb
)
1268 acb
->bh
= qemu_bh_new(cb
, acb
);
1272 qemu_bh_schedule(acb
->bh
);
1277 static void qcow_aio_read_cb(void *opaque
, int ret
)
1279 QCowAIOCB
*acb
= opaque
;
1280 BlockDriverState
*bs
= acb
->common
.bs
;
1281 BDRVQcowState
*s
= bs
->opaque
;
1282 int index_in_cluster
, n1
;
1284 acb
->hd_aiocb
= NULL
;
1287 acb
->common
.cb(acb
->common
.opaque
, ret
);
1288 qemu_aio_release(acb
);
1292 /* post process the read buffer */
1293 if (!acb
->cluster_offset
) {
1295 } else if (acb
->cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
1298 if (s
->crypt_method
) {
1299 encrypt_sectors(s
, acb
->sector_num
, acb
->buf
, acb
->buf
,
1301 &s
->aes_decrypt_key
);
1305 acb
->nb_sectors
-= acb
->n
;
1306 acb
->sector_num
+= acb
->n
;
1307 acb
->buf
+= acb
->n
* 512;
1309 if (acb
->nb_sectors
== 0) {
1310 /* request completed */
1311 acb
->common
.cb(acb
->common
.opaque
, 0);
1312 qemu_aio_release(acb
);
1316 /* prepare next AIO request */
1317 acb
->n
= acb
->nb_sectors
;
1318 acb
->cluster_offset
= get_cluster_offset(bs
, acb
->sector_num
<< 9, &acb
->n
);
1319 index_in_cluster
= acb
->sector_num
& (s
->cluster_sectors
- 1);
1321 if (!acb
->cluster_offset
) {
1322 if (bs
->backing_hd
) {
1323 /* read from the base image */
1324 n1
= backing_read1(bs
->backing_hd
, acb
->sector_num
,
1327 acb
->hd_aiocb
= bdrv_aio_read(bs
->backing_hd
, acb
->sector_num
,
1328 acb
->buf
, acb
->n
, qcow_aio_read_cb
, acb
);
1329 if (acb
->hd_aiocb
== NULL
)
1332 ret
= qcow_schedule_bh(qcow_aio_read_bh
, acb
);
1337 /* Note: in this case, no need to wait */
1338 memset(acb
->buf
, 0, 512 * acb
->n
);
1339 ret
= qcow_schedule_bh(qcow_aio_read_bh
, acb
);
1343 } else if (acb
->cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
1344 /* add AIO support for compressed blocks ? */
1345 if (decompress_cluster(s
, acb
->cluster_offset
) < 0)
1348 s
->cluster_cache
+ index_in_cluster
* 512, 512 * acb
->n
);
1349 ret
= qcow_schedule_bh(qcow_aio_read_bh
, acb
);
1353 if ((acb
->cluster_offset
& 511) != 0) {
1357 acb
->hd_aiocb
= bdrv_aio_read(s
->hd
,
1358 (acb
->cluster_offset
>> 9) + index_in_cluster
,
1359 acb
->buf
, acb
->n
, qcow_aio_read_cb
, acb
);
1360 if (acb
->hd_aiocb
== NULL
)
1365 static QCowAIOCB
*qcow_aio_setup(BlockDriverState
*bs
,
1366 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1367 BlockDriverCompletionFunc
*cb
, void *opaque
)
1371 acb
= qemu_aio_get(bs
, cb
, opaque
);
1374 acb
->hd_aiocb
= NULL
;
1375 acb
->sector_num
= sector_num
;
1377 acb
->nb_sectors
= nb_sectors
;
1379 acb
->cluster_offset
= 0;
1380 acb
->l2meta
.nb_clusters
= 0;
1384 static BlockDriverAIOCB
*qcow_aio_read(BlockDriverState
*bs
,
1385 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1386 BlockDriverCompletionFunc
*cb
, void *opaque
)
1390 acb
= qcow_aio_setup(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1394 qcow_aio_read_cb(acb
, 0);
1395 return &acb
->common
;
1398 static void qcow_aio_write_cb(void *opaque
, int ret
)
1400 QCowAIOCB
*acb
= opaque
;
1401 BlockDriverState
*bs
= acb
->common
.bs
;
1402 BDRVQcowState
*s
= bs
->opaque
;
1403 int index_in_cluster
;
1404 const uint8_t *src_buf
;
1407 acb
->hd_aiocb
= NULL
;
1411 acb
->common
.cb(acb
->common
.opaque
, ret
);
1412 qemu_aio_release(acb
);
1416 if (alloc_cluster_link_l2(bs
, acb
->cluster_offset
, &acb
->l2meta
) < 0) {
1417 free_any_clusters(bs
, acb
->cluster_offset
, acb
->l2meta
.nb_clusters
);
1421 acb
->nb_sectors
-= acb
->n
;
1422 acb
->sector_num
+= acb
->n
;
1423 acb
->buf
+= acb
->n
* 512;
1425 if (acb
->nb_sectors
== 0) {
1426 /* request completed */
1427 acb
->common
.cb(acb
->common
.opaque
, 0);
1428 qemu_aio_release(acb
);
1432 index_in_cluster
= acb
->sector_num
& (s
->cluster_sectors
- 1);
1433 n_end
= index_in_cluster
+ acb
->nb_sectors
;
1434 if (s
->crypt_method
&&
1435 n_end
> QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
)
1436 n_end
= QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
;
1438 acb
->cluster_offset
= alloc_cluster_offset(bs
, acb
->sector_num
<< 9,
1440 n_end
, &acb
->n
, &acb
->l2meta
);
1441 if (!acb
->cluster_offset
|| (acb
->cluster_offset
& 511) != 0) {
1445 if (s
->crypt_method
) {
1446 if (!acb
->cluster_data
) {
1447 acb
->cluster_data
= qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS
*
1450 encrypt_sectors(s
, acb
->sector_num
, acb
->cluster_data
, acb
->buf
,
1451 acb
->n
, 1, &s
->aes_encrypt_key
);
1452 src_buf
= acb
->cluster_data
;
1456 acb
->hd_aiocb
= bdrv_aio_write(s
->hd
,
1457 (acb
->cluster_offset
>> 9) + index_in_cluster
,
1459 qcow_aio_write_cb
, acb
);
1460 if (acb
->hd_aiocb
== NULL
)
1464 static BlockDriverAIOCB
*qcow_aio_write(BlockDriverState
*bs
,
1465 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1466 BlockDriverCompletionFunc
*cb
, void *opaque
)
1468 BDRVQcowState
*s
= bs
->opaque
;
1471 s
->cluster_cache_offset
= -1; /* disable compressed cache */
1473 acb
= qcow_aio_setup(bs
, sector_num
, (uint8_t*)buf
, nb_sectors
, cb
, opaque
);
1477 qcow_aio_write_cb(acb
, 0);
1478 return &acb
->common
;
1481 static void qcow_aio_cancel(BlockDriverAIOCB
*blockacb
)
1483 QCowAIOCB
*acb
= (QCowAIOCB
*)blockacb
;
1485 bdrv_aio_cancel(acb
->hd_aiocb
);
1486 qemu_aio_release(acb
);
1489 static void qcow_close(BlockDriverState
*bs
)
1491 BDRVQcowState
*s
= bs
->opaque
;
1492 qemu_free(s
->l1_table
);
1493 qemu_free(s
->l2_cache
);
1494 qemu_free(s
->cluster_cache
);
1495 qemu_free(s
->cluster_data
);
1500 /* XXX: use std qcow open function ? */
1501 typedef struct QCowCreateState
{
1504 uint16_t *refcount_block
;
1505 uint64_t *refcount_table
;
1506 int64_t l1_table_offset
;
1507 int64_t refcount_table_offset
;
1508 int64_t refcount_block_offset
;
1511 static void create_refcount_update(QCowCreateState
*s
,
1512 int64_t offset
, int64_t size
)
1515 int64_t start
, last
, cluster_offset
;
1518 start
= offset
& ~(s
->cluster_size
- 1);
1519 last
= (offset
+ size
- 1) & ~(s
->cluster_size
- 1);
1520 for(cluster_offset
= start
; cluster_offset
<= last
;
1521 cluster_offset
+= s
->cluster_size
) {
1522 p
= &s
->refcount_block
[cluster_offset
>> s
->cluster_bits
];
1523 refcount
= be16_to_cpu(*p
);
1525 *p
= cpu_to_be16(refcount
);
1529 static int qcow_create(const char *filename
, int64_t total_size
,
1530 const char *backing_file
, int flags
)
1532 int fd
, header_size
, backing_filename_len
, l1_size
, i
, shift
, l2_bits
;
1534 uint64_t tmp
, offset
;
1535 QCowCreateState s1
, *s
= &s1
;
1537 memset(s
, 0, sizeof(*s
));
1539 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0644);
1542 memset(&header
, 0, sizeof(header
));
1543 header
.magic
= cpu_to_be32(QCOW_MAGIC
);
1544 header
.version
= cpu_to_be32(QCOW_VERSION
);
1545 header
.size
= cpu_to_be64(total_size
* 512);
1546 header_size
= sizeof(header
);
1547 backing_filename_len
= 0;
1549 header
.backing_file_offset
= cpu_to_be64(header_size
);
1550 backing_filename_len
= strlen(backing_file
);
1551 header
.backing_file_size
= cpu_to_be32(backing_filename_len
);
1552 header_size
+= backing_filename_len
;
1554 s
->cluster_bits
= 12; /* 4 KB clusters */
1555 s
->cluster_size
= 1 << s
->cluster_bits
;
1556 header
.cluster_bits
= cpu_to_be32(s
->cluster_bits
);
1557 header_size
= (header_size
+ 7) & ~7;
1558 if (flags
& BLOCK_FLAG_ENCRYPT
) {
1559 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
1561 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
1563 l2_bits
= s
->cluster_bits
- 3;
1564 shift
= s
->cluster_bits
+ l2_bits
;
1565 l1_size
= (((total_size
* 512) + (1LL << shift
) - 1) >> shift
);
1566 offset
= align_offset(header_size
, s
->cluster_size
);
1567 s
->l1_table_offset
= offset
;
1568 header
.l1_table_offset
= cpu_to_be64(s
->l1_table_offset
);
1569 header
.l1_size
= cpu_to_be32(l1_size
);
1570 offset
+= align_offset(l1_size
* sizeof(uint64_t), s
->cluster_size
);
1572 s
->refcount_table
= qemu_mallocz(s
->cluster_size
);
1573 s
->refcount_block
= qemu_mallocz(s
->cluster_size
);
1575 s
->refcount_table_offset
= offset
;
1576 header
.refcount_table_offset
= cpu_to_be64(offset
);
1577 header
.refcount_table_clusters
= cpu_to_be32(1);
1578 offset
+= s
->cluster_size
;
1580 s
->refcount_table
[0] = cpu_to_be64(offset
);
1581 s
->refcount_block_offset
= offset
;
1582 offset
+= s
->cluster_size
;
1584 /* update refcounts */
1585 create_refcount_update(s
, 0, header_size
);
1586 create_refcount_update(s
, s
->l1_table_offset
, l1_size
* sizeof(uint64_t));
1587 create_refcount_update(s
, s
->refcount_table_offset
, s
->cluster_size
);
1588 create_refcount_update(s
, s
->refcount_block_offset
, s
->cluster_size
);
1590 /* write all the data */
1591 write(fd
, &header
, sizeof(header
));
1593 write(fd
, backing_file
, backing_filename_len
);
1595 lseek(fd
, s
->l1_table_offset
, SEEK_SET
);
1597 for(i
= 0;i
< l1_size
; i
++) {
1598 write(fd
, &tmp
, sizeof(tmp
));
1600 lseek(fd
, s
->refcount_table_offset
, SEEK_SET
);
1601 write(fd
, s
->refcount_table
, s
->cluster_size
);
1603 lseek(fd
, s
->refcount_block_offset
, SEEK_SET
);
1604 write(fd
, s
->refcount_block
, s
->cluster_size
);
1606 qemu_free(s
->refcount_table
);
1607 qemu_free(s
->refcount_block
);
1612 static int qcow_make_empty(BlockDriverState
*bs
)
1615 /* XXX: not correct */
1616 BDRVQcowState
*s
= bs
->opaque
;
1617 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
1620 memset(s
->l1_table
, 0, l1_length
);
1621 if (bdrv_pwrite(s
->hd
, s
->l1_table_offset
, s
->l1_table
, l1_length
) < 0)
1623 ret
= bdrv_truncate(s
->hd
, s
->l1_table_offset
+ l1_length
);
1632 /* XXX: put compressed sectors first, then all the cluster aligned
1633 tables to avoid losing bytes in alignment */
1634 static int qcow_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1635 const uint8_t *buf
, int nb_sectors
)
1637 BDRVQcowState
*s
= bs
->opaque
;
1641 uint64_t cluster_offset
;
1643 if (nb_sectors
== 0) {
1644 /* align end of file to a sector boundary to ease reading with
1645 sector based I/Os */
1646 cluster_offset
= bdrv_getlength(s
->hd
);
1647 cluster_offset
= (cluster_offset
+ 511) & ~511;
1648 bdrv_truncate(s
->hd
, cluster_offset
);
1652 if (nb_sectors
!= s
->cluster_sectors
)
1655 out_buf
= qemu_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
1657 /* best compression, small window, no zlib header */
1658 memset(&strm
, 0, sizeof(strm
));
1659 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
1661 9, Z_DEFAULT_STRATEGY
);
1667 strm
.avail_in
= s
->cluster_size
;
1668 strm
.next_in
= (uint8_t *)buf
;
1669 strm
.avail_out
= s
->cluster_size
;
1670 strm
.next_out
= out_buf
;
1672 ret
= deflate(&strm
, Z_FINISH
);
1673 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
1678 out_len
= strm
.next_out
- out_buf
;
1682 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
1683 /* could not compress: write normal cluster */
1684 qcow_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
1686 cluster_offset
= alloc_compressed_cluster_offset(bs
, sector_num
<< 9,
1688 if (!cluster_offset
)
1690 cluster_offset
&= s
->cluster_offset_mask
;
1691 if (bdrv_pwrite(s
->hd
, cluster_offset
, out_buf
, out_len
) != out_len
) {
1701 static void qcow_flush(BlockDriverState
*bs
)
1703 BDRVQcowState
*s
= bs
->opaque
;
1707 static int qcow_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1709 BDRVQcowState
*s
= bs
->opaque
;
1710 bdi
->cluster_size
= s
->cluster_size
;
1711 bdi
->vm_state_offset
= (int64_t)s
->l1_vm_state_index
<<
1712 (s
->cluster_bits
+ s
->l2_bits
);
1716 /*********************************************************/
1717 /* snapshot support */
1719 /* update the refcounts of snapshots and the copied flag */
1720 static int update_snapshot_refcount(BlockDriverState
*bs
,
1721 int64_t l1_table_offset
,
1725 BDRVQcowState
*s
= bs
->opaque
;
1726 uint64_t *l1_table
, *l2_table
, l2_offset
, offset
, l1_size2
, l1_allocated
;
1727 int64_t old_offset
, old_l2_offset
;
1728 int l2_size
, i
, j
, l1_modified
, l2_modified
, nb_csectors
, refcount
;
1734 l1_size2
= l1_size
* sizeof(uint64_t);
1736 if (l1_table_offset
!= s
->l1_table_offset
) {
1737 l1_table
= qemu_malloc(l1_size2
);
1739 if (bdrv_pread(s
->hd
, l1_table_offset
,
1740 l1_table
, l1_size2
) != l1_size2
)
1742 for(i
= 0;i
< l1_size
; i
++)
1743 be64_to_cpus(&l1_table
[i
]);
1745 assert(l1_size
== s
->l1_size
);
1746 l1_table
= s
->l1_table
;
1750 l2_size
= s
->l2_size
* sizeof(uint64_t);
1751 l2_table
= qemu_malloc(l2_size
);
1753 for(i
= 0; i
< l1_size
; i
++) {
1754 l2_offset
= l1_table
[i
];
1756 old_l2_offset
= l2_offset
;
1757 l2_offset
&= ~QCOW_OFLAG_COPIED
;
1759 if (bdrv_pread(s
->hd
, l2_offset
, l2_table
, l2_size
) != l2_size
)
1761 for(j
= 0; j
< s
->l2_size
; j
++) {
1762 offset
= be64_to_cpu(l2_table
[j
]);
1764 old_offset
= offset
;
1765 offset
&= ~QCOW_OFLAG_COPIED
;
1766 if (offset
& QCOW_OFLAG_COMPRESSED
) {
1767 nb_csectors
= ((offset
>> s
->csize_shift
) &
1770 update_refcount(bs
, (offset
& s
->cluster_offset_mask
) & ~511,
1771 nb_csectors
* 512, addend
);
1772 /* compressed clusters are never modified */
1776 refcount
= update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, addend
);
1778 refcount
= get_refcount(bs
, offset
>> s
->cluster_bits
);
1782 if (refcount
== 1) {
1783 offset
|= QCOW_OFLAG_COPIED
;
1785 if (offset
!= old_offset
) {
1786 l2_table
[j
] = cpu_to_be64(offset
);
1792 if (bdrv_pwrite(s
->hd
,
1793 l2_offset
, l2_table
, l2_size
) != l2_size
)
1798 refcount
= update_cluster_refcount(bs
, l2_offset
>> s
->cluster_bits
, addend
);
1800 refcount
= get_refcount(bs
, l2_offset
>> s
->cluster_bits
);
1802 if (refcount
== 1) {
1803 l2_offset
|= QCOW_OFLAG_COPIED
;
1805 if (l2_offset
!= old_l2_offset
) {
1806 l1_table
[i
] = l2_offset
;
1812 for(i
= 0; i
< l1_size
; i
++)
1813 cpu_to_be64s(&l1_table
[i
]);
1814 if (bdrv_pwrite(s
->hd
, l1_table_offset
, l1_table
,
1815 l1_size2
) != l1_size2
)
1817 for(i
= 0; i
< l1_size
; i
++)
1818 be64_to_cpus(&l1_table
[i
]);
1821 qemu_free(l1_table
);
1822 qemu_free(l2_table
);
1826 qemu_free(l1_table
);
1827 qemu_free(l2_table
);
1831 static void qcow_free_snapshots(BlockDriverState
*bs
)
1833 BDRVQcowState
*s
= bs
->opaque
;
1836 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1837 qemu_free(s
->snapshots
[i
].name
);
1838 qemu_free(s
->snapshots
[i
].id_str
);
1840 qemu_free(s
->snapshots
);
1841 s
->snapshots
= NULL
;
1842 s
->nb_snapshots
= 0;
1845 static int qcow_read_snapshots(BlockDriverState
*bs
)
1847 BDRVQcowState
*s
= bs
->opaque
;
1848 QCowSnapshotHeader h
;
1850 int i
, id_str_size
, name_size
;
1852 uint32_t extra_data_size
;
1854 if (!s
->nb_snapshots
) {
1855 s
->snapshots
= NULL
;
1856 s
->snapshots_size
= 0;
1860 offset
= s
->snapshots_offset
;
1861 s
->snapshots
= qemu_mallocz(s
->nb_snapshots
* sizeof(QCowSnapshot
));
1862 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1863 offset
= align_offset(offset
, 8);
1864 if (bdrv_pread(s
->hd
, offset
, &h
, sizeof(h
)) != sizeof(h
))
1866 offset
+= sizeof(h
);
1867 sn
= s
->snapshots
+ i
;
1868 sn
->l1_table_offset
= be64_to_cpu(h
.l1_table_offset
);
1869 sn
->l1_size
= be32_to_cpu(h
.l1_size
);
1870 sn
->vm_state_size
= be32_to_cpu(h
.vm_state_size
);
1871 sn
->date_sec
= be32_to_cpu(h
.date_sec
);
1872 sn
->date_nsec
= be32_to_cpu(h
.date_nsec
);
1873 sn
->vm_clock_nsec
= be64_to_cpu(h
.vm_clock_nsec
);
1874 extra_data_size
= be32_to_cpu(h
.extra_data_size
);
1876 id_str_size
= be16_to_cpu(h
.id_str_size
);
1877 name_size
= be16_to_cpu(h
.name_size
);
1879 offset
+= extra_data_size
;
1881 sn
->id_str
= qemu_malloc(id_str_size
+ 1);
1882 if (bdrv_pread(s
->hd
, offset
, sn
->id_str
, id_str_size
) != id_str_size
)
1884 offset
+= id_str_size
;
1885 sn
->id_str
[id_str_size
] = '\0';
1887 sn
->name
= qemu_malloc(name_size
+ 1);
1888 if (bdrv_pread(s
->hd
, offset
, sn
->name
, name_size
) != name_size
)
1890 offset
+= name_size
;
1891 sn
->name
[name_size
] = '\0';
1893 s
->snapshots_size
= offset
- s
->snapshots_offset
;
1896 qcow_free_snapshots(bs
);
1900 /* add at the end of the file a new list of snapshots */
1901 static int qcow_write_snapshots(BlockDriverState
*bs
)
1903 BDRVQcowState
*s
= bs
->opaque
;
1905 QCowSnapshotHeader h
;
1906 int i
, name_size
, id_str_size
, snapshots_size
;
1909 int64_t offset
, snapshots_offset
;
1911 /* compute the size of the snapshots */
1913 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1914 sn
= s
->snapshots
+ i
;
1915 offset
= align_offset(offset
, 8);
1916 offset
+= sizeof(h
);
1917 offset
+= strlen(sn
->id_str
);
1918 offset
+= strlen(sn
->name
);
1920 snapshots_size
= offset
;
1922 snapshots_offset
= alloc_clusters(bs
, snapshots_size
);
1923 offset
= snapshots_offset
;
1925 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1926 sn
= s
->snapshots
+ i
;
1927 memset(&h
, 0, sizeof(h
));
1928 h
.l1_table_offset
= cpu_to_be64(sn
->l1_table_offset
);
1929 h
.l1_size
= cpu_to_be32(sn
->l1_size
);
1930 h
.vm_state_size
= cpu_to_be32(sn
->vm_state_size
);
1931 h
.date_sec
= cpu_to_be32(sn
->date_sec
);
1932 h
.date_nsec
= cpu_to_be32(sn
->date_nsec
);
1933 h
.vm_clock_nsec
= cpu_to_be64(sn
->vm_clock_nsec
);
1935 id_str_size
= strlen(sn
->id_str
);
1936 name_size
= strlen(sn
->name
);
1937 h
.id_str_size
= cpu_to_be16(id_str_size
);
1938 h
.name_size
= cpu_to_be16(name_size
);
1939 offset
= align_offset(offset
, 8);
1940 if (bdrv_pwrite(s
->hd
, offset
, &h
, sizeof(h
)) != sizeof(h
))
1942 offset
+= sizeof(h
);
1943 if (bdrv_pwrite(s
->hd
, offset
, sn
->id_str
, id_str_size
) != id_str_size
)
1945 offset
+= id_str_size
;
1946 if (bdrv_pwrite(s
->hd
, offset
, sn
->name
, name_size
) != name_size
)
1948 offset
+= name_size
;
1951 /* update the various header fields */
1952 data64
= cpu_to_be64(snapshots_offset
);
1953 if (bdrv_pwrite(s
->hd
, offsetof(QCowHeader
, snapshots_offset
),
1954 &data64
, sizeof(data64
)) != sizeof(data64
))
1956 data32
= cpu_to_be32(s
->nb_snapshots
);
1957 if (bdrv_pwrite(s
->hd
, offsetof(QCowHeader
, nb_snapshots
),
1958 &data32
, sizeof(data32
)) != sizeof(data32
))
1961 /* free the old snapshot table */
1962 free_clusters(bs
, s
->snapshots_offset
, s
->snapshots_size
);
1963 s
->snapshots_offset
= snapshots_offset
;
1964 s
->snapshots_size
= snapshots_size
;
1970 static void find_new_snapshot_id(BlockDriverState
*bs
,
1971 char *id_str
, int id_str_size
)
1973 BDRVQcowState
*s
= bs
->opaque
;
1975 int i
, id
, id_max
= 0;
1977 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1978 sn
= s
->snapshots
+ i
;
1979 id
= strtoul(sn
->id_str
, NULL
, 10);
1983 snprintf(id_str
, id_str_size
, "%d", id_max
+ 1);
1986 static int find_snapshot_by_id(BlockDriverState
*bs
, const char *id_str
)
1988 BDRVQcowState
*s
= bs
->opaque
;
1991 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1992 if (!strcmp(s
->snapshots
[i
].id_str
, id_str
))
1998 static int find_snapshot_by_id_or_name(BlockDriverState
*bs
, const char *name
)
2000 BDRVQcowState
*s
= bs
->opaque
;
2003 ret
= find_snapshot_by_id(bs
, name
);
2006 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
2007 if (!strcmp(s
->snapshots
[i
].name
, name
))
2013 /* if no id is provided, a new one is constructed */
2014 static int qcow_snapshot_create(BlockDriverState
*bs
,
2015 QEMUSnapshotInfo
*sn_info
)
2017 BDRVQcowState
*s
= bs
->opaque
;
2018 QCowSnapshot
*snapshots1
, sn1
, *sn
= &sn1
;
2020 uint64_t *l1_table
= NULL
;
2022 memset(sn
, 0, sizeof(*sn
));
2024 if (sn_info
->id_str
[0] == '\0') {
2025 /* compute a new id */
2026 find_new_snapshot_id(bs
, sn_info
->id_str
, sizeof(sn_info
->id_str
));
2029 /* check that the ID is unique */
2030 if (find_snapshot_by_id(bs
, sn_info
->id_str
) >= 0)
2033 sn
->id_str
= qemu_strdup(sn_info
->id_str
);
2036 sn
->name
= qemu_strdup(sn_info
->name
);
2039 sn
->vm_state_size
= sn_info
->vm_state_size
;
2040 sn
->date_sec
= sn_info
->date_sec
;
2041 sn
->date_nsec
= sn_info
->date_nsec
;
2042 sn
->vm_clock_nsec
= sn_info
->vm_clock_nsec
;
2044 ret
= update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 1);
2048 /* create the L1 table of the snapshot */
2049 sn
->l1_table_offset
= alloc_clusters(bs
, s
->l1_size
* sizeof(uint64_t));
2050 sn
->l1_size
= s
->l1_size
;
2052 l1_table
= qemu_malloc(s
->l1_size
* sizeof(uint64_t));
2053 for(i
= 0; i
< s
->l1_size
; i
++) {
2054 l1_table
[i
] = cpu_to_be64(s
->l1_table
[i
]);
2056 if (bdrv_pwrite(s
->hd
, sn
->l1_table_offset
,
2057 l1_table
, s
->l1_size
* sizeof(uint64_t)) !=
2058 (s
->l1_size
* sizeof(uint64_t)))
2060 qemu_free(l1_table
);
2063 snapshots1
= qemu_malloc((s
->nb_snapshots
+ 1) * sizeof(QCowSnapshot
));
2065 memcpy(snapshots1
, s
->snapshots
, s
->nb_snapshots
* sizeof(QCowSnapshot
));
2066 qemu_free(s
->snapshots
);
2068 s
->snapshots
= snapshots1
;
2069 s
->snapshots
[s
->nb_snapshots
++] = *sn
;
2071 if (qcow_write_snapshots(bs
) < 0)
2074 check_refcounts(bs
);
2078 qemu_free(sn
->name
);
2079 qemu_free(l1_table
);
2083 /* copy the snapshot 'snapshot_name' into the current disk image */
2084 static int qcow_snapshot_goto(BlockDriverState
*bs
,
2085 const char *snapshot_id
)
2087 BDRVQcowState
*s
= bs
->opaque
;
2089 int i
, snapshot_index
, l1_size2
;
2091 snapshot_index
= find_snapshot_by_id_or_name(bs
, snapshot_id
);
2092 if (snapshot_index
< 0)
2094 sn
= &s
->snapshots
[snapshot_index
];
2096 if (update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, -1) < 0)
2099 if (grow_l1_table(bs
, sn
->l1_size
) < 0)
2102 s
->l1_size
= sn
->l1_size
;
2103 l1_size2
= s
->l1_size
* sizeof(uint64_t);
2104 /* copy the snapshot l1 table to the current l1 table */
2105 if (bdrv_pread(s
->hd
, sn
->l1_table_offset
,
2106 s
->l1_table
, l1_size2
) != l1_size2
)
2108 if (bdrv_pwrite(s
->hd
, s
->l1_table_offset
,
2109 s
->l1_table
, l1_size2
) != l1_size2
)
2111 for(i
= 0;i
< s
->l1_size
; i
++) {
2112 be64_to_cpus(&s
->l1_table
[i
]);
2115 if (update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 1) < 0)
2119 check_refcounts(bs
);
2126 static int qcow_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
2128 BDRVQcowState
*s
= bs
->opaque
;
2130 int snapshot_index
, ret
;
2132 snapshot_index
= find_snapshot_by_id_or_name(bs
, snapshot_id
);
2133 if (snapshot_index
< 0)
2135 sn
= &s
->snapshots
[snapshot_index
];
2137 ret
= update_snapshot_refcount(bs
, sn
->l1_table_offset
, sn
->l1_size
, -1);
2140 /* must update the copied flag on the current cluster offsets */
2141 ret
= update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 0);
2144 free_clusters(bs
, sn
->l1_table_offset
, sn
->l1_size
* sizeof(uint64_t));
2146 qemu_free(sn
->id_str
);
2147 qemu_free(sn
->name
);
2148 memmove(sn
, sn
+ 1, (s
->nb_snapshots
- snapshot_index
- 1) * sizeof(*sn
));
2150 ret
= qcow_write_snapshots(bs
);
2152 /* XXX: restore snapshot if error ? */
2156 check_refcounts(bs
);
2161 static int qcow_snapshot_list(BlockDriverState
*bs
,
2162 QEMUSnapshotInfo
**psn_tab
)
2164 BDRVQcowState
*s
= bs
->opaque
;
2165 QEMUSnapshotInfo
*sn_tab
, *sn_info
;
2169 sn_tab
= qemu_mallocz(s
->nb_snapshots
* sizeof(QEMUSnapshotInfo
));
2170 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
2171 sn_info
= sn_tab
+ i
;
2172 sn
= s
->snapshots
+ i
;
2173 pstrcpy(sn_info
->id_str
, sizeof(sn_info
->id_str
),
2175 pstrcpy(sn_info
->name
, sizeof(sn_info
->name
),
2177 sn_info
->vm_state_size
= sn
->vm_state_size
;
2178 sn_info
->date_sec
= sn
->date_sec
;
2179 sn_info
->date_nsec
= sn
->date_nsec
;
2180 sn_info
->vm_clock_nsec
= sn
->vm_clock_nsec
;
2183 return s
->nb_snapshots
;
2186 /*********************************************************/
2187 /* refcount handling */
2189 static int refcount_init(BlockDriverState
*bs
)
2191 BDRVQcowState
*s
= bs
->opaque
;
2192 int ret
, refcount_table_size2
, i
;
2194 s
->refcount_block_cache
= qemu_malloc(s
->cluster_size
);
2195 refcount_table_size2
= s
->refcount_table_size
* sizeof(uint64_t);
2196 s
->refcount_table
= qemu_malloc(refcount_table_size2
);
2197 if (s
->refcount_table_size
> 0) {
2198 ret
= bdrv_pread(s
->hd
, s
->refcount_table_offset
,
2199 s
->refcount_table
, refcount_table_size2
);
2200 if (ret
!= refcount_table_size2
)
2202 for(i
= 0; i
< s
->refcount_table_size
; i
++)
2203 be64_to_cpus(&s
->refcount_table
[i
]);
2210 static void refcount_close(BlockDriverState
*bs
)
2212 BDRVQcowState
*s
= bs
->opaque
;
2213 qemu_free(s
->refcount_block_cache
);
2214 qemu_free(s
->refcount_table
);
2218 static int load_refcount_block(BlockDriverState
*bs
,
2219 int64_t refcount_block_offset
)
2221 BDRVQcowState
*s
= bs
->opaque
;
2223 ret
= bdrv_pread(s
->hd
, refcount_block_offset
, s
->refcount_block_cache
,
2225 if (ret
!= s
->cluster_size
)
2227 s
->refcount_block_cache_offset
= refcount_block_offset
;
2231 static int get_refcount(BlockDriverState
*bs
, int64_t cluster_index
)
2233 BDRVQcowState
*s
= bs
->opaque
;
2234 int refcount_table_index
, block_index
;
2235 int64_t refcount_block_offset
;
2237 refcount_table_index
= cluster_index
>> (s
->cluster_bits
- REFCOUNT_SHIFT
);
2238 if (refcount_table_index
>= s
->refcount_table_size
)
2240 refcount_block_offset
= s
->refcount_table
[refcount_table_index
];
2241 if (!refcount_block_offset
)
2243 if (refcount_block_offset
!= s
->refcount_block_cache_offset
) {
2244 /* better than nothing: return allocated if read error */
2245 if (load_refcount_block(bs
, refcount_block_offset
) < 0)
2248 block_index
= cluster_index
&
2249 ((1 << (s
->cluster_bits
- REFCOUNT_SHIFT
)) - 1);
2250 return be16_to_cpu(s
->refcount_block_cache
[block_index
]);
2253 /* return < 0 if error */
2254 static int64_t alloc_clusters_noref(BlockDriverState
*bs
, int64_t size
)
2256 BDRVQcowState
*s
= bs
->opaque
;
2259 nb_clusters
= size_to_clusters(s
, size
);
2261 for(i
= 0; i
< nb_clusters
; i
++) {
2262 int64_t i
= s
->free_cluster_index
++;
2263 if (get_refcount(bs
, i
) != 0)
2267 printf("alloc_clusters: size=%lld -> %lld\n",
2269 (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
);
2271 return (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
;
2274 static int64_t alloc_clusters(BlockDriverState
*bs
, int64_t size
)
2278 offset
= alloc_clusters_noref(bs
, size
);
2279 update_refcount(bs
, offset
, size
, 1);
2283 /* only used to allocate compressed sectors. We try to allocate
2284 contiguous sectors. size must be <= cluster_size */
2285 static int64_t alloc_bytes(BlockDriverState
*bs
, int size
)
2287 BDRVQcowState
*s
= bs
->opaque
;
2288 int64_t offset
, cluster_offset
;
2289 int free_in_cluster
;
2291 assert(size
> 0 && size
<= s
->cluster_size
);
2292 if (s
->free_byte_offset
== 0) {
2293 s
->free_byte_offset
= alloc_clusters(bs
, s
->cluster_size
);
2296 free_in_cluster
= s
->cluster_size
-
2297 (s
->free_byte_offset
& (s
->cluster_size
- 1));
2298 if (size
<= free_in_cluster
) {
2299 /* enough space in current cluster */
2300 offset
= s
->free_byte_offset
;
2301 s
->free_byte_offset
+= size
;
2302 free_in_cluster
-= size
;
2303 if (free_in_cluster
== 0)
2304 s
->free_byte_offset
= 0;
2305 if ((offset
& (s
->cluster_size
- 1)) != 0)
2306 update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, 1);
2308 offset
= alloc_clusters(bs
, s
->cluster_size
);
2309 cluster_offset
= s
->free_byte_offset
& ~(s
->cluster_size
- 1);
2310 if ((cluster_offset
+ s
->cluster_size
) == offset
) {
2311 /* we are lucky: contiguous data */
2312 offset
= s
->free_byte_offset
;
2313 update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, 1);
2314 s
->free_byte_offset
+= size
;
2316 s
->free_byte_offset
= offset
;
2323 static void free_clusters(BlockDriverState
*bs
,
2324 int64_t offset
, int64_t size
)
2326 update_refcount(bs
, offset
, size
, -1);
2329 static int grow_refcount_table(BlockDriverState
*bs
, int min_size
)
2331 BDRVQcowState
*s
= bs
->opaque
;
2332 int new_table_size
, new_table_size2
, refcount_table_clusters
, i
, ret
;
2333 uint64_t *new_table
;
2334 int64_t table_offset
;
2337 int64_t old_table_offset
;
2339 if (min_size
<= s
->refcount_table_size
)
2341 /* compute new table size */
2342 refcount_table_clusters
= s
->refcount_table_size
>> (s
->cluster_bits
- 3);
2344 if (refcount_table_clusters
== 0) {
2345 refcount_table_clusters
= 1;
2347 refcount_table_clusters
= (refcount_table_clusters
* 3 + 1) / 2;
2349 new_table_size
= refcount_table_clusters
<< (s
->cluster_bits
- 3);
2350 if (min_size
<= new_table_size
)
2354 printf("grow_refcount_table from %d to %d\n",
2355 s
->refcount_table_size
,
2358 new_table_size2
= new_table_size
* sizeof(uint64_t);
2359 new_table
= qemu_mallocz(new_table_size2
);
2360 memcpy(new_table
, s
->refcount_table
,
2361 s
->refcount_table_size
* sizeof(uint64_t));
2362 for(i
= 0; i
< s
->refcount_table_size
; i
++)
2363 cpu_to_be64s(&new_table
[i
]);
2364 /* Note: we cannot update the refcount now to avoid recursion */
2365 table_offset
= alloc_clusters_noref(bs
, new_table_size2
);
2366 ret
= bdrv_pwrite(s
->hd
, table_offset
, new_table
, new_table_size2
);
2367 if (ret
!= new_table_size2
)
2369 for(i
= 0; i
< s
->refcount_table_size
; i
++)
2370 be64_to_cpus(&new_table
[i
]);
2372 cpu_to_be64w((uint64_t*)data
, table_offset
);
2373 cpu_to_be32w((uint32_t*)(data
+ 8), refcount_table_clusters
);
2374 if (bdrv_pwrite(s
->hd
, offsetof(QCowHeader
, refcount_table_offset
),
2375 data
, sizeof(data
)) != sizeof(data
))
2377 qemu_free(s
->refcount_table
);
2378 old_table_offset
= s
->refcount_table_offset
;
2379 old_table_size
= s
->refcount_table_size
;
2380 s
->refcount_table
= new_table
;
2381 s
->refcount_table_size
= new_table_size
;
2382 s
->refcount_table_offset
= table_offset
;
2384 update_refcount(bs
, table_offset
, new_table_size2
, 1);
2385 free_clusters(bs
, old_table_offset
, old_table_size
* sizeof(uint64_t));
2388 free_clusters(bs
, table_offset
, new_table_size2
);
2389 qemu_free(new_table
);
2393 /* addend must be 1 or -1 */
2394 /* XXX: cache several refcount block clusters ? */
2395 static int update_cluster_refcount(BlockDriverState
*bs
,
2396 int64_t cluster_index
,
2399 BDRVQcowState
*s
= bs
->opaque
;
2400 int64_t offset
, refcount_block_offset
;
2401 int ret
, refcount_table_index
, block_index
, refcount
;
2404 refcount_table_index
= cluster_index
>> (s
->cluster_bits
- REFCOUNT_SHIFT
);
2405 if (refcount_table_index
>= s
->refcount_table_size
) {
2408 ret
= grow_refcount_table(bs
, refcount_table_index
+ 1);
2412 refcount_block_offset
= s
->refcount_table
[refcount_table_index
];
2413 if (!refcount_block_offset
) {
2416 /* create a new refcount block */
2417 /* Note: we cannot update the refcount now to avoid recursion */
2418 offset
= alloc_clusters_noref(bs
, s
->cluster_size
);
2419 memset(s
->refcount_block_cache
, 0, s
->cluster_size
);
2420 ret
= bdrv_pwrite(s
->hd
, offset
, s
->refcount_block_cache
, s
->cluster_size
);
2421 if (ret
!= s
->cluster_size
)
2423 s
->refcount_table
[refcount_table_index
] = offset
;
2424 data64
= cpu_to_be64(offset
);
2425 ret
= bdrv_pwrite(s
->hd
, s
->refcount_table_offset
+
2426 refcount_table_index
* sizeof(uint64_t),
2427 &data64
, sizeof(data64
));
2428 if (ret
!= sizeof(data64
))
2431 refcount_block_offset
= offset
;
2432 s
->refcount_block_cache_offset
= offset
;
2433 update_refcount(bs
, offset
, s
->cluster_size
, 1);
2435 if (refcount_block_offset
!= s
->refcount_block_cache_offset
) {
2436 if (load_refcount_block(bs
, refcount_block_offset
) < 0)
2440 /* we can update the count and save it */
2441 block_index
= cluster_index
&
2442 ((1 << (s
->cluster_bits
- REFCOUNT_SHIFT
)) - 1);
2443 refcount
= be16_to_cpu(s
->refcount_block_cache
[block_index
]);
2445 if (refcount
< 0 || refcount
> 0xffff)
2447 if (refcount
== 0 && cluster_index
< s
->free_cluster_index
) {
2448 s
->free_cluster_index
= cluster_index
;
2450 s
->refcount_block_cache
[block_index
] = cpu_to_be16(refcount
);
2451 if (bdrv_pwrite(s
->hd
,
2452 refcount_block_offset
+ (block_index
<< REFCOUNT_SHIFT
),
2453 &s
->refcount_block_cache
[block_index
], 2) != 2)
2458 static void update_refcount(BlockDriverState
*bs
,
2459 int64_t offset
, int64_t length
,
2462 BDRVQcowState
*s
= bs
->opaque
;
2463 int64_t start
, last
, cluster_offset
;
2466 printf("update_refcount: offset=%lld size=%lld addend=%d\n",
2467 offset
, length
, addend
);
2471 start
= offset
& ~(s
->cluster_size
- 1);
2472 last
= (offset
+ length
- 1) & ~(s
->cluster_size
- 1);
2473 for(cluster_offset
= start
; cluster_offset
<= last
;
2474 cluster_offset
+= s
->cluster_size
) {
2475 update_cluster_refcount(bs
, cluster_offset
>> s
->cluster_bits
, addend
);
2480 static void inc_refcounts(BlockDriverState
*bs
,
2481 uint16_t *refcount_table
,
2482 int refcount_table_size
,
2483 int64_t offset
, int64_t size
)
2485 BDRVQcowState
*s
= bs
->opaque
;
2486 int64_t start
, last
, cluster_offset
;
2492 start
= offset
& ~(s
->cluster_size
- 1);
2493 last
= (offset
+ size
- 1) & ~(s
->cluster_size
- 1);
2494 for(cluster_offset
= start
; cluster_offset
<= last
;
2495 cluster_offset
+= s
->cluster_size
) {
2496 k
= cluster_offset
>> s
->cluster_bits
;
2497 if (k
< 0 || k
>= refcount_table_size
) {
2498 printf("ERROR: invalid cluster offset=0x%llx\n", cluster_offset
);
2500 if (++refcount_table
[k
] == 0) {
2501 printf("ERROR: overflow cluster offset=0x%llx\n", cluster_offset
);
2507 static int check_refcounts_l1(BlockDriverState
*bs
,
2508 uint16_t *refcount_table
,
2509 int refcount_table_size
,
2510 int64_t l1_table_offset
, int l1_size
,
2513 BDRVQcowState
*s
= bs
->opaque
;
2514 uint64_t *l1_table
, *l2_table
, l2_offset
, offset
, l1_size2
;
2515 int l2_size
, i
, j
, nb_csectors
, refcount
;
2518 l1_size2
= l1_size
* sizeof(uint64_t);
2520 inc_refcounts(bs
, refcount_table
, refcount_table_size
,
2521 l1_table_offset
, l1_size2
);
2523 l1_table
= qemu_malloc(l1_size2
);
2524 if (bdrv_pread(s
->hd
, l1_table_offset
,
2525 l1_table
, l1_size2
) != l1_size2
)
2527 for(i
= 0;i
< l1_size
; i
++)
2528 be64_to_cpus(&l1_table
[i
]);
2530 l2_size
= s
->l2_size
* sizeof(uint64_t);
2531 l2_table
= qemu_malloc(l2_size
);
2532 for(i
= 0; i
< l1_size
; i
++) {
2533 l2_offset
= l1_table
[i
];
2536 refcount
= get_refcount(bs
, (l2_offset
& ~QCOW_OFLAG_COPIED
) >> s
->cluster_bits
);
2537 if ((refcount
== 1) != ((l2_offset
& QCOW_OFLAG_COPIED
) != 0)) {
2538 printf("ERROR OFLAG_COPIED: l2_offset=%llx refcount=%d\n",
2539 l2_offset
, refcount
);
2542 l2_offset
&= ~QCOW_OFLAG_COPIED
;
2543 if (bdrv_pread(s
->hd
, l2_offset
, l2_table
, l2_size
) != l2_size
)
2545 for(j
= 0; j
< s
->l2_size
; j
++) {
2546 offset
= be64_to_cpu(l2_table
[j
]);
2548 if (offset
& QCOW_OFLAG_COMPRESSED
) {
2549 if (offset
& QCOW_OFLAG_COPIED
) {
2550 printf("ERROR: cluster %lld: copied flag must never be set for compressed clusters\n",
2551 offset
>> s
->cluster_bits
);
2552 offset
&= ~QCOW_OFLAG_COPIED
;
2554 nb_csectors
= ((offset
>> s
->csize_shift
) &
2556 offset
&= s
->cluster_offset_mask
;
2557 inc_refcounts(bs
, refcount_table
,
2558 refcount_table_size
,
2559 offset
& ~511, nb_csectors
* 512);
2562 refcount
= get_refcount(bs
, (offset
& ~QCOW_OFLAG_COPIED
) >> s
->cluster_bits
);
2563 if ((refcount
== 1) != ((offset
& QCOW_OFLAG_COPIED
) != 0)) {
2564 printf("ERROR OFLAG_COPIED: offset=%llx refcount=%d\n",
2568 offset
&= ~QCOW_OFLAG_COPIED
;
2569 inc_refcounts(bs
, refcount_table
,
2570 refcount_table_size
,
2571 offset
, s
->cluster_size
);
2575 inc_refcounts(bs
, refcount_table
,
2576 refcount_table_size
,
2581 qemu_free(l1_table
);
2582 qemu_free(l2_table
);
2585 printf("ERROR: I/O error in check_refcounts_l1\n");
2586 qemu_free(l1_table
);
2587 qemu_free(l2_table
);
2591 static void check_refcounts(BlockDriverState
*bs
)
2593 BDRVQcowState
*s
= bs
->opaque
;
2595 int nb_clusters
, refcount1
, refcount2
, i
;
2597 uint16_t *refcount_table
;
2599 size
= bdrv_getlength(s
->hd
);
2600 nb_clusters
= size_to_clusters(s
, size
);
2601 refcount_table
= qemu_mallocz(nb_clusters
* sizeof(uint16_t));
2604 inc_refcounts(bs
, refcount_table
, nb_clusters
,
2605 0, s
->cluster_size
);
2607 check_refcounts_l1(bs
, refcount_table
, nb_clusters
,
2608 s
->l1_table_offset
, s
->l1_size
, 1);
2611 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
2612 sn
= s
->snapshots
+ i
;
2613 check_refcounts_l1(bs
, refcount_table
, nb_clusters
,
2614 sn
->l1_table_offset
, sn
->l1_size
, 0);
2616 inc_refcounts(bs
, refcount_table
, nb_clusters
,
2617 s
->snapshots_offset
, s
->snapshots_size
);
2620 inc_refcounts(bs
, refcount_table
, nb_clusters
,
2621 s
->refcount_table_offset
,
2622 s
->refcount_table_size
* sizeof(uint64_t));
2623 for(i
= 0; i
< s
->refcount_table_size
; i
++) {
2625 offset
= s
->refcount_table
[i
];
2627 inc_refcounts(bs
, refcount_table
, nb_clusters
,
2628 offset
, s
->cluster_size
);
2632 /* compare ref counts */
2633 for(i
= 0; i
< nb_clusters
; i
++) {
2634 refcount1
= get_refcount(bs
, i
);
2635 refcount2
= refcount_table
[i
];
2636 if (refcount1
!= refcount2
)
2637 printf("ERROR cluster %d refcount=%d reference=%d\n",
2638 i
, refcount1
, refcount2
);
2641 qemu_free(refcount_table
);
2645 static void dump_refcounts(BlockDriverState
*bs
)
2647 BDRVQcowState
*s
= bs
->opaque
;
2648 int64_t nb_clusters
, k
, k1
, size
;
2651 size
= bdrv_getlength(s
->hd
);
2652 nb_clusters
= size_to_clusters(s
, size
);
2653 for(k
= 0; k
< nb_clusters
;) {
2655 refcount
= get_refcount(bs
, k
);
2657 while (k
< nb_clusters
&& get_refcount(bs
, k
) == refcount
)
2659 printf("%lld: refcount=%d nb=%lld\n", k
, refcount
, k
- k1
);
2665 BlockDriver bdrv_qcow2
= {
2666 .format_name
= "qcow2",
2667 .instance_size
= sizeof(BDRVQcowState
),
2668 .bdrv_probe
= qcow_probe
,
2669 .bdrv_open
= qcow_open
,
2670 .bdrv_close
= qcow_close
,
2671 .bdrv_create
= qcow_create
,
2672 .bdrv_flush
= qcow_flush
,
2673 .bdrv_is_allocated
= qcow_is_allocated
,
2674 .bdrv_set_key
= qcow_set_key
,
2675 .bdrv_make_empty
= qcow_make_empty
,
2677 .bdrv_aio_read
= qcow_aio_read
,
2678 .bdrv_aio_write
= qcow_aio_write
,
2679 .bdrv_aio_cancel
= qcow_aio_cancel
,
2680 .aiocb_size
= sizeof(QCowAIOCB
),
2681 .bdrv_write_compressed
= qcow_write_compressed
,
2683 .bdrv_snapshot_create
= qcow_snapshot_create
,
2684 .bdrv_snapshot_goto
= qcow_snapshot_goto
,
2685 .bdrv_snapshot_delete
= qcow_snapshot_delete
,
2686 .bdrv_snapshot_list
= qcow_snapshot_list
,
2687 .bdrv_get_info
= qcow_get_info
,