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
87 #define QCOW_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
90 typedef struct __attribute__((packed
)) QCowSnapshotHeader
{
91 /* header is 8 byte aligned */
92 uint64_t l1_table_offset
;
101 uint64_t vm_clock_nsec
;
103 uint32_t vm_state_size
;
104 uint32_t extra_data_size
; /* for extension */
105 /* extra data follows */
108 } QCowSnapshotHeader
;
110 #define L2_CACHE_SIZE 16
112 typedef struct QCowSnapshot
{
113 uint64_t l1_table_offset
;
117 uint32_t vm_state_size
;
120 uint64_t vm_clock_nsec
;
123 typedef struct BDRVQcowState
{
124 BlockDriverState
*hd
;
131 int l1_vm_state_index
;
134 uint64_t cluster_offset_mask
;
135 uint64_t l1_table_offset
;
138 uint64_t l2_cache_offsets
[L2_CACHE_SIZE
];
139 uint32_t l2_cache_counts
[L2_CACHE_SIZE
];
140 uint8_t *cluster_cache
;
141 uint8_t *cluster_data
;
142 uint64_t cluster_cache_offset
;
144 uint64_t *refcount_table
;
145 uint64_t refcount_table_offset
;
146 uint32_t refcount_table_size
;
147 uint64_t refcount_block_cache_offset
;
148 uint16_t *refcount_block_cache
;
149 int64_t free_cluster_index
;
150 int64_t free_byte_offset
;
152 uint32_t crypt_method
; /* current crypt method, 0 if no key yet */
153 uint32_t crypt_method_header
;
154 AES_KEY aes_encrypt_key
;
155 AES_KEY aes_decrypt_key
;
156 uint64_t snapshots_offset
;
159 QCowSnapshot
*snapshots
;
162 static int decompress_cluster(BDRVQcowState
*s
, uint64_t cluster_offset
);
163 static int qcow_read(BlockDriverState
*bs
, int64_t sector_num
,
164 uint8_t *buf
, int nb_sectors
);
165 static int qcow_read_snapshots(BlockDriverState
*bs
);
166 static void qcow_free_snapshots(BlockDriverState
*bs
);
167 static int refcount_init(BlockDriverState
*bs
);
168 static void refcount_close(BlockDriverState
*bs
);
169 static int get_refcount(BlockDriverState
*bs
, int64_t cluster_index
);
170 static int update_cluster_refcount(BlockDriverState
*bs
,
171 int64_t cluster_index
,
173 static void update_refcount(BlockDriverState
*bs
,
174 int64_t offset
, int64_t length
,
176 static int64_t alloc_clusters(BlockDriverState
*bs
, int64_t size
);
177 static int64_t alloc_bytes(BlockDriverState
*bs
, int size
);
178 static void free_clusters(BlockDriverState
*bs
,
179 int64_t offset
, int64_t size
);
181 static void check_refcounts(BlockDriverState
*bs
);
184 static int qcow_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
186 const QCowHeader
*cow_header
= (const void *)buf
;
188 if (buf_size
>= sizeof(QCowHeader
) &&
189 be32_to_cpu(cow_header
->magic
) == QCOW_MAGIC
&&
190 be32_to_cpu(cow_header
->version
) == QCOW_VERSION
)
198 * read qcow2 extension and fill bs
199 * start reading from start_offset
200 * finish reading upon magic of value 0 or when end_offset reached
201 * unknown magic is skipped (future extension this version knows nothing about)
202 * return 0 upon success, non-0 otherwise
204 static int qcow_read_extensions(BlockDriverState
*bs
, uint64_t start_offset
,
207 BDRVQcowState
*s
= bs
->opaque
;
212 printf("qcow_read_extensions: start=%ld end=%ld\n", start_offset
, end_offset
);
214 offset
= start_offset
;
215 while (offset
< end_offset
) {
219 if (offset
> s
->cluster_size
)
220 printf("qcow_handle_extension: suspicious offset %lu\n", offset
);
222 printf("attemting to read extended header in offset %lu\n", offset
);
225 if (bdrv_pread(s
->hd
, offset
, &ext
, sizeof(ext
)) != sizeof(ext
)) {
226 fprintf(stderr
, "qcow_handle_extension: ERROR: pread fail from offset %llu\n",
227 (unsigned long long)offset
);
230 be32_to_cpus(&ext
.magic
);
231 be32_to_cpus(&ext
.len
);
232 offset
+= sizeof(ext
);
234 printf("ext.magic = 0x%x\n", ext
.magic
);
237 case QCOW_EXT_MAGIC_END
:
240 case QCOW_EXT_MAGIC_BACKING_FORMAT
:
241 if (ext
.len
>= sizeof(bs
->backing_format
)) {
242 fprintf(stderr
, "ERROR: ext_backing_format: len=%u too large"
244 ext
.len
, sizeof(bs
->backing_format
));
247 if (bdrv_pread(s
->hd
, offset
, bs
->backing_format
,
250 bs
->backing_format
[ext
.len
] = '\0';
252 printf("Qcow2: Got format extension %s\n", bs
->backing_format
);
254 offset
+= ((ext
.len
+ 7) & ~7);
258 /* unknown magic -- just skip it */
259 offset
+= ((ext
.len
+ 7) & ~7);
268 static int qcow_open(BlockDriverState
*bs
, const char *filename
, int flags
)
270 BDRVQcowState
*s
= bs
->opaque
;
271 int len
, i
, shift
, ret
;
275 /* Performance is terrible right now with cache=writethrough due mainly
276 * to reference count updates. If the user does not explicitly specify
277 * a caching type, force to writeback caching.
279 if ((flags
& BDRV_O_CACHE_DEF
)) {
280 flags
|= BDRV_O_CACHE_WB
;
281 flags
&= ~BDRV_O_CACHE_DEF
;
283 ret
= bdrv_file_open(&s
->hd
, filename
, flags
);
286 if (bdrv_pread(s
->hd
, 0, &header
, sizeof(header
)) != sizeof(header
))
288 be32_to_cpus(&header
.magic
);
289 be32_to_cpus(&header
.version
);
290 be64_to_cpus(&header
.backing_file_offset
);
291 be32_to_cpus(&header
.backing_file_size
);
292 be64_to_cpus(&header
.size
);
293 be32_to_cpus(&header
.cluster_bits
);
294 be32_to_cpus(&header
.crypt_method
);
295 be64_to_cpus(&header
.l1_table_offset
);
296 be32_to_cpus(&header
.l1_size
);
297 be64_to_cpus(&header
.refcount_table_offset
);
298 be32_to_cpus(&header
.refcount_table_clusters
);
299 be64_to_cpus(&header
.snapshots_offset
);
300 be32_to_cpus(&header
.nb_snapshots
);
302 if (header
.magic
!= QCOW_MAGIC
|| header
.version
!= QCOW_VERSION
)
304 if (header
.size
<= 1 ||
305 header
.cluster_bits
< 9 ||
306 header
.cluster_bits
> 16)
308 if (header
.crypt_method
> QCOW_CRYPT_AES
)
310 s
->crypt_method_header
= header
.crypt_method
;
311 if (s
->crypt_method_header
)
313 s
->cluster_bits
= header
.cluster_bits
;
314 s
->cluster_size
= 1 << s
->cluster_bits
;
315 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
316 s
->l2_bits
= s
->cluster_bits
- 3; /* L2 is always one cluster */
317 s
->l2_size
= 1 << s
->l2_bits
;
318 bs
->total_sectors
= header
.size
/ 512;
319 s
->csize_shift
= (62 - (s
->cluster_bits
- 8));
320 s
->csize_mask
= (1 << (s
->cluster_bits
- 8)) - 1;
321 s
->cluster_offset_mask
= (1LL << s
->csize_shift
) - 1;
322 s
->refcount_table_offset
= header
.refcount_table_offset
;
323 s
->refcount_table_size
=
324 header
.refcount_table_clusters
<< (s
->cluster_bits
- 3);
326 s
->snapshots_offset
= header
.snapshots_offset
;
327 s
->nb_snapshots
= header
.nb_snapshots
;
329 /* read the level 1 table */
330 s
->l1_size
= header
.l1_size
;
331 shift
= s
->cluster_bits
+ s
->l2_bits
;
332 s
->l1_vm_state_index
= (header
.size
+ (1LL << shift
) - 1) >> shift
;
333 /* the L1 table must contain at least enough entries to put
335 if (s
->l1_size
< s
->l1_vm_state_index
)
337 s
->l1_table_offset
= header
.l1_table_offset
;
338 s
->l1_table
= qemu_malloc(s
->l1_size
* sizeof(uint64_t));
339 if (bdrv_pread(s
->hd
, s
->l1_table_offset
, s
->l1_table
, s
->l1_size
* sizeof(uint64_t)) !=
340 s
->l1_size
* sizeof(uint64_t))
342 for(i
= 0;i
< s
->l1_size
; i
++) {
343 be64_to_cpus(&s
->l1_table
[i
]);
346 s
->l2_cache
= qemu_malloc(s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
347 s
->cluster_cache
= qemu_malloc(s
->cluster_size
);
348 /* one more sector for decompressed data alignment */
349 s
->cluster_data
= qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
351 s
->cluster_cache_offset
= -1;
353 if (refcount_init(bs
) < 0)
356 /* read qcow2 extensions */
357 if (header
.backing_file_offset
)
358 ext_end
= header
.backing_file_offset
;
360 ext_end
= s
->cluster_size
;
361 if (qcow_read_extensions(bs
, sizeof(header
), ext_end
))
364 /* read the backing file name */
365 if (header
.backing_file_offset
!= 0) {
366 len
= header
.backing_file_size
;
369 if (bdrv_pread(s
->hd
, header
.backing_file_offset
, bs
->backing_file
, len
) != len
)
371 bs
->backing_file
[len
] = '\0';
373 if (qcow_read_snapshots(bs
) < 0)
382 qcow_free_snapshots(bs
);
384 qemu_free(s
->l1_table
);
385 qemu_free(s
->l2_cache
);
386 qemu_free(s
->cluster_cache
);
387 qemu_free(s
->cluster_data
);
392 static int qcow_set_key(BlockDriverState
*bs
, const char *key
)
394 BDRVQcowState
*s
= bs
->opaque
;
398 memset(keybuf
, 0, 16);
402 /* XXX: we could compress the chars to 7 bits to increase
404 for(i
= 0;i
< len
;i
++) {
407 s
->crypt_method
= s
->crypt_method_header
;
409 if (AES_set_encrypt_key(keybuf
, 128, &s
->aes_encrypt_key
) != 0)
411 if (AES_set_decrypt_key(keybuf
, 128, &s
->aes_decrypt_key
) != 0)
421 AES_encrypt(in
, tmp
, &s
->aes_encrypt_key
);
422 AES_decrypt(tmp
, out
, &s
->aes_decrypt_key
);
423 for(i
= 0; i
< 16; i
++)
424 printf(" %02x", tmp
[i
]);
426 for(i
= 0; i
< 16; i
++)
427 printf(" %02x", out
[i
]);
434 /* The crypt function is compatible with the linux cryptoloop
435 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
437 static void encrypt_sectors(BDRVQcowState
*s
, int64_t sector_num
,
438 uint8_t *out_buf
, const uint8_t *in_buf
,
439 int nb_sectors
, int enc
,
448 for(i
= 0; i
< nb_sectors
; i
++) {
449 ivec
.ll
[0] = cpu_to_le64(sector_num
);
451 AES_cbc_encrypt(in_buf
, out_buf
, 512, key
,
459 static int copy_sectors(BlockDriverState
*bs
, uint64_t start_sect
,
460 uint64_t cluster_offset
, int n_start
, int n_end
)
462 BDRVQcowState
*s
= bs
->opaque
;
468 ret
= qcow_read(bs
, start_sect
+ n_start
, s
->cluster_data
, n
);
471 if (s
->crypt_method
) {
472 encrypt_sectors(s
, start_sect
+ n_start
,
474 s
->cluster_data
, n
, 1,
475 &s
->aes_encrypt_key
);
477 ret
= bdrv_write(s
->hd
, (cluster_offset
>> 9) + n_start
,
484 static void l2_cache_reset(BlockDriverState
*bs
)
486 BDRVQcowState
*s
= bs
->opaque
;
488 memset(s
->l2_cache
, 0, s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
489 memset(s
->l2_cache_offsets
, 0, L2_CACHE_SIZE
* sizeof(uint64_t));
490 memset(s
->l2_cache_counts
, 0, L2_CACHE_SIZE
* sizeof(uint32_t));
493 static inline int l2_cache_new_entry(BlockDriverState
*bs
)
495 BDRVQcowState
*s
= bs
->opaque
;
499 /* find a new entry in the least used one */
501 min_count
= 0xffffffff;
502 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
503 if (s
->l2_cache_counts
[i
] < min_count
) {
504 min_count
= s
->l2_cache_counts
[i
];
511 static int64_t align_offset(int64_t offset
, int n
)
513 offset
= (offset
+ n
- 1) & ~(n
- 1);
517 static int grow_l1_table(BlockDriverState
*bs
, int min_size
)
519 BDRVQcowState
*s
= bs
->opaque
;
520 int new_l1_size
, new_l1_size2
, ret
, i
;
521 uint64_t *new_l1_table
;
522 uint64_t new_l1_table_offset
;
525 new_l1_size
= s
->l1_size
;
526 if (min_size
<= new_l1_size
)
528 while (min_size
> new_l1_size
) {
529 new_l1_size
= (new_l1_size
* 3 + 1) / 2;
532 printf("grow l1_table from %d to %d\n", s
->l1_size
, new_l1_size
);
535 new_l1_size2
= sizeof(uint64_t) * new_l1_size
;
536 new_l1_table
= qemu_mallocz(new_l1_size2
);
537 memcpy(new_l1_table
, s
->l1_table
, s
->l1_size
* sizeof(uint64_t));
539 /* write new table (align to cluster) */
540 new_l1_table_offset
= alloc_clusters(bs
, new_l1_size2
);
542 for(i
= 0; i
< s
->l1_size
; i
++)
543 new_l1_table
[i
] = cpu_to_be64(new_l1_table
[i
]);
544 ret
= bdrv_pwrite(s
->hd
, new_l1_table_offset
, new_l1_table
, new_l1_size2
);
545 if (ret
!= new_l1_size2
)
547 for(i
= 0; i
< s
->l1_size
; i
++)
548 new_l1_table
[i
] = be64_to_cpu(new_l1_table
[i
]);
551 cpu_to_be32w((uint32_t*)data
, new_l1_size
);
552 cpu_to_be64w((uint64_t*)(data
+ 4), new_l1_table_offset
);
553 if (bdrv_pwrite(s
->hd
, offsetof(QCowHeader
, l1_size
), data
,
554 sizeof(data
)) != sizeof(data
))
556 qemu_free(s
->l1_table
);
557 free_clusters(bs
, s
->l1_table_offset
, s
->l1_size
* sizeof(uint64_t));
558 s
->l1_table_offset
= new_l1_table_offset
;
559 s
->l1_table
= new_l1_table
;
560 s
->l1_size
= new_l1_size
;
563 qemu_free(s
->l1_table
);
570 * seek l2_offset in the l2_cache table
571 * if not found, return NULL,
573 * increments the l2 cache hit count of the entry,
574 * if counter overflow, divide by two all counters
575 * return the pointer to the l2 cache entry
579 static uint64_t *seek_l2_table(BDRVQcowState
*s
, uint64_t l2_offset
)
583 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
584 if (l2_offset
== s
->l2_cache_offsets
[i
]) {
585 /* increment the hit count */
586 if (++s
->l2_cache_counts
[i
] == 0xffffffff) {
587 for(j
= 0; j
< L2_CACHE_SIZE
; j
++) {
588 s
->l2_cache_counts
[j
] >>= 1;
591 return s
->l2_cache
+ (i
<< s
->l2_bits
);
600 * Loads a L2 table into memory. If the table is in the cache, the cache
601 * is used; otherwise the L2 table is loaded from the image file.
603 * Returns a pointer to the L2 table on success, or NULL if the read from
604 * the image file failed.
607 static uint64_t *l2_load(BlockDriverState
*bs
, uint64_t l2_offset
)
609 BDRVQcowState
*s
= bs
->opaque
;
613 /* seek if the table for the given offset is in the cache */
615 l2_table
= seek_l2_table(s
, l2_offset
);
616 if (l2_table
!= NULL
)
619 /* not found: load a new entry in the least used one */
621 min_index
= l2_cache_new_entry(bs
);
622 l2_table
= s
->l2_cache
+ (min_index
<< s
->l2_bits
);
623 if (bdrv_pread(s
->hd
, l2_offset
, l2_table
, s
->l2_size
* sizeof(uint64_t)) !=
624 s
->l2_size
* sizeof(uint64_t))
626 s
->l2_cache_offsets
[min_index
] = l2_offset
;
627 s
->l2_cache_counts
[min_index
] = 1;
635 * Allocate a new l2 entry in the file. If l1_index points to an already
636 * used entry in the L2 table (i.e. we are doing a copy on write for the L2
637 * table) copy the contents of the old L2 table into the newly allocated one.
638 * Otherwise the new table is initialized with zeros.
642 static uint64_t *l2_allocate(BlockDriverState
*bs
, int l1_index
)
644 BDRVQcowState
*s
= bs
->opaque
;
646 uint64_t old_l2_offset
, tmp
;
647 uint64_t *l2_table
, l2_offset
;
649 old_l2_offset
= s
->l1_table
[l1_index
];
651 /* allocate a new l2 entry */
653 l2_offset
= alloc_clusters(bs
, s
->l2_size
* sizeof(uint64_t));
655 /* update the L1 entry */
657 s
->l1_table
[l1_index
] = l2_offset
| QCOW_OFLAG_COPIED
;
659 tmp
= cpu_to_be64(l2_offset
| QCOW_OFLAG_COPIED
);
660 if (bdrv_pwrite(s
->hd
, s
->l1_table_offset
+ l1_index
* sizeof(tmp
),
661 &tmp
, sizeof(tmp
)) != sizeof(tmp
))
664 /* allocate a new entry in the l2 cache */
666 min_index
= l2_cache_new_entry(bs
);
667 l2_table
= s
->l2_cache
+ (min_index
<< s
->l2_bits
);
669 if (old_l2_offset
== 0) {
670 /* if there was no old l2 table, clear the new table */
671 memset(l2_table
, 0, s
->l2_size
* sizeof(uint64_t));
673 /* if there was an old l2 table, read it from the disk */
674 if (bdrv_pread(s
->hd
, old_l2_offset
,
675 l2_table
, s
->l2_size
* sizeof(uint64_t)) !=
676 s
->l2_size
* sizeof(uint64_t))
679 /* write the l2 table to the file */
680 if (bdrv_pwrite(s
->hd
, l2_offset
,
681 l2_table
, s
->l2_size
* sizeof(uint64_t)) !=
682 s
->l2_size
* sizeof(uint64_t))
685 /* update the l2 cache entry */
687 s
->l2_cache_offsets
[min_index
] = l2_offset
;
688 s
->l2_cache_counts
[min_index
] = 1;
693 static int size_to_clusters(BDRVQcowState
*s
, int64_t size
)
695 return (size
+ (s
->cluster_size
- 1)) >> s
->cluster_bits
;
698 static int count_contiguous_clusters(uint64_t nb_clusters
, int cluster_size
,
699 uint64_t *l2_table
, uint64_t start
, uint64_t mask
)
702 uint64_t offset
= be64_to_cpu(l2_table
[0]) & ~mask
;
707 for (i
= start
; i
< start
+ nb_clusters
; i
++)
708 if (offset
+ i
* cluster_size
!= (be64_to_cpu(l2_table
[i
]) & ~mask
))
714 static int count_contiguous_free_clusters(uint64_t nb_clusters
, uint64_t *l2_table
)
718 while(nb_clusters
-- && l2_table
[i
] == 0)
727 * For a given offset of the disk image, return cluster offset in
730 * on entry, *num is the number of contiguous clusters we'd like to
731 * access following offset.
733 * on exit, *num is the number of contiguous clusters we can read.
735 * Return 1, if the offset is found
736 * Return 0, otherwise.
740 static uint64_t get_cluster_offset(BlockDriverState
*bs
,
741 uint64_t offset
, int *num
)
743 BDRVQcowState
*s
= bs
->opaque
;
744 int l1_index
, l2_index
;
745 uint64_t l2_offset
, *l2_table
, cluster_offset
;
747 int index_in_cluster
, nb_available
, nb_needed
, nb_clusters
;
749 index_in_cluster
= (offset
>> 9) & (s
->cluster_sectors
- 1);
750 nb_needed
= *num
+ index_in_cluster
;
752 l1_bits
= s
->l2_bits
+ s
->cluster_bits
;
754 /* compute how many bytes there are between the offset and
755 * the end of the l1 entry
758 nb_available
= (1 << l1_bits
) - (offset
& ((1 << l1_bits
) - 1));
760 /* compute the number of available sectors */
762 nb_available
= (nb_available
>> 9) + index_in_cluster
;
764 if (nb_needed
> nb_available
) {
765 nb_needed
= nb_available
;
770 /* seek the the l2 offset in the l1 table */
772 l1_index
= offset
>> l1_bits
;
773 if (l1_index
>= s
->l1_size
)
776 l2_offset
= s
->l1_table
[l1_index
];
778 /* seek the l2 table of the given l2 offset */
783 /* load the l2 table in memory */
785 l2_offset
&= ~QCOW_OFLAG_COPIED
;
786 l2_table
= l2_load(bs
, l2_offset
);
787 if (l2_table
== NULL
)
790 /* find the cluster offset for the given disk offset */
792 l2_index
= (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
793 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
794 nb_clusters
= size_to_clusters(s
, nb_needed
<< 9);
796 if (!cluster_offset
) {
797 /* how many empty clusters ? */
798 c
= count_contiguous_free_clusters(nb_clusters
, &l2_table
[l2_index
]);
800 /* how many allocated clusters ? */
801 c
= count_contiguous_clusters(nb_clusters
, s
->cluster_size
,
802 &l2_table
[l2_index
], 0, QCOW_OFLAG_COPIED
);
805 nb_available
= (c
* s
->cluster_sectors
);
807 if (nb_available
> nb_needed
)
808 nb_available
= nb_needed
;
810 *num
= nb_available
- index_in_cluster
;
812 return cluster_offset
& ~QCOW_OFLAG_COPIED
;
818 * free clusters according to its type: compressed or not
822 static void free_any_clusters(BlockDriverState
*bs
,
823 uint64_t cluster_offset
, int nb_clusters
)
825 BDRVQcowState
*s
= bs
->opaque
;
827 /* free the cluster */
829 if (cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
831 nb_csectors
= ((cluster_offset
>> s
->csize_shift
) &
833 free_clusters(bs
, (cluster_offset
& s
->cluster_offset_mask
) & ~511,
838 free_clusters(bs
, cluster_offset
, nb_clusters
<< s
->cluster_bits
);
846 * for a given disk offset, load (and allocate if needed)
849 * the l2 table offset in the qcow2 file and the cluster index
850 * in the l2 table are given to the caller.
854 static int get_cluster_table(BlockDriverState
*bs
, uint64_t offset
,
855 uint64_t **new_l2_table
,
856 uint64_t *new_l2_offset
,
859 BDRVQcowState
*s
= bs
->opaque
;
860 int l1_index
, l2_index
, ret
;
861 uint64_t l2_offset
, *l2_table
;
863 /* seek the the l2 offset in the l1 table */
865 l1_index
= offset
>> (s
->l2_bits
+ s
->cluster_bits
);
866 if (l1_index
>= s
->l1_size
) {
867 ret
= grow_l1_table(bs
, l1_index
+ 1);
871 l2_offset
= s
->l1_table
[l1_index
];
873 /* seek the l2 table of the given l2 offset */
875 if (l2_offset
& QCOW_OFLAG_COPIED
) {
876 /* load the l2 table in memory */
877 l2_offset
&= ~QCOW_OFLAG_COPIED
;
878 l2_table
= l2_load(bs
, l2_offset
);
879 if (l2_table
== NULL
)
883 free_clusters(bs
, l2_offset
, s
->l2_size
* sizeof(uint64_t));
884 l2_table
= l2_allocate(bs
, l1_index
);
885 if (l2_table
== NULL
)
887 l2_offset
= s
->l1_table
[l1_index
] & ~QCOW_OFLAG_COPIED
;
890 /* find the cluster offset for the given disk offset */
892 l2_index
= (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
894 *new_l2_table
= l2_table
;
895 *new_l2_offset
= l2_offset
;
896 *new_l2_index
= l2_index
;
902 * alloc_compressed_cluster_offset
904 * For a given offset of the disk image, return cluster offset in
907 * If the offset is not found, allocate a new compressed cluster.
909 * Return the cluster offset if successful,
910 * Return 0, otherwise.
914 static uint64_t alloc_compressed_cluster_offset(BlockDriverState
*bs
,
918 BDRVQcowState
*s
= bs
->opaque
;
920 uint64_t l2_offset
, *l2_table
, cluster_offset
;
923 ret
= get_cluster_table(bs
, offset
, &l2_table
, &l2_offset
, &l2_index
);
927 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
928 if (cluster_offset
& QCOW_OFLAG_COPIED
)
929 return cluster_offset
& ~QCOW_OFLAG_COPIED
;
932 free_any_clusters(bs
, cluster_offset
, 1);
934 cluster_offset
= alloc_bytes(bs
, compressed_size
);
935 nb_csectors
= ((cluster_offset
+ compressed_size
- 1) >> 9) -
936 (cluster_offset
>> 9);
938 cluster_offset
|= QCOW_OFLAG_COMPRESSED
|
939 ((uint64_t)nb_csectors
<< s
->csize_shift
);
941 /* update L2 table */
943 /* compressed clusters never have the copied flag */
945 l2_table
[l2_index
] = cpu_to_be64(cluster_offset
);
946 if (bdrv_pwrite(s
->hd
,
947 l2_offset
+ l2_index
* sizeof(uint64_t),
949 sizeof(uint64_t)) != sizeof(uint64_t))
952 return cluster_offset
;
955 typedef struct QCowL2Meta
963 static int alloc_cluster_link_l2(BlockDriverState
*bs
, uint64_t cluster_offset
,
966 BDRVQcowState
*s
= bs
->opaque
;
967 int i
, j
= 0, l2_index
, ret
;
968 uint64_t *old_cluster
, start_sect
, l2_offset
, *l2_table
;
970 if (m
->nb_clusters
== 0)
973 old_cluster
= qemu_malloc(m
->nb_clusters
* sizeof(uint64_t));
975 /* copy content of unmodified sectors */
976 start_sect
= (m
->offset
& ~(s
->cluster_size
- 1)) >> 9;
978 ret
= copy_sectors(bs
, start_sect
, cluster_offset
, 0, m
->n_start
);
983 if (m
->nb_available
& (s
->cluster_sectors
- 1)) {
984 uint64_t end
= m
->nb_available
& ~(uint64_t)(s
->cluster_sectors
- 1);
985 ret
= copy_sectors(bs
, start_sect
+ end
, cluster_offset
+ (end
<< 9),
986 m
->nb_available
- end
, s
->cluster_sectors
);
992 /* update L2 table */
993 if (!get_cluster_table(bs
, m
->offset
, &l2_table
, &l2_offset
, &l2_index
))
996 for (i
= 0; i
< m
->nb_clusters
; i
++) {
997 if(l2_table
[l2_index
+ i
] != 0)
998 old_cluster
[j
++] = l2_table
[l2_index
+ i
];
1000 l2_table
[l2_index
+ i
] = cpu_to_be64((cluster_offset
+
1001 (i
<< s
->cluster_bits
)) | QCOW_OFLAG_COPIED
);
1004 if (bdrv_pwrite(s
->hd
, l2_offset
+ l2_index
* sizeof(uint64_t),
1005 l2_table
+ l2_index
, m
->nb_clusters
* sizeof(uint64_t)) !=
1006 m
->nb_clusters
* sizeof(uint64_t))
1009 for (i
= 0; i
< j
; i
++)
1010 free_any_clusters(bs
, old_cluster
[i
], 1);
1014 qemu_free(old_cluster
);
1019 * alloc_cluster_offset
1021 * For a given offset of the disk image, return cluster offset in
1024 * If the offset is not found, allocate a new cluster.
1026 * Return the cluster offset if successful,
1027 * Return 0, otherwise.
1031 static uint64_t alloc_cluster_offset(BlockDriverState
*bs
,
1033 int n_start
, int n_end
,
1034 int *num
, QCowL2Meta
*m
)
1036 BDRVQcowState
*s
= bs
->opaque
;
1038 uint64_t l2_offset
, *l2_table
, cluster_offset
;
1039 int nb_clusters
, i
= 0;
1041 ret
= get_cluster_table(bs
, offset
, &l2_table
, &l2_offset
, &l2_index
);
1045 nb_clusters
= size_to_clusters(s
, n_end
<< 9);
1047 nb_clusters
= MIN(nb_clusters
, s
->l2_size
- l2_index
);
1049 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
1051 /* We keep all QCOW_OFLAG_COPIED clusters */
1053 if (cluster_offset
& QCOW_OFLAG_COPIED
) {
1054 nb_clusters
= count_contiguous_clusters(nb_clusters
, s
->cluster_size
,
1055 &l2_table
[l2_index
], 0, 0);
1057 cluster_offset
&= ~QCOW_OFLAG_COPIED
;
1063 /* for the moment, multiple compressed clusters are not managed */
1065 if (cluster_offset
& QCOW_OFLAG_COMPRESSED
)
1068 /* how many available clusters ? */
1070 while (i
< nb_clusters
) {
1071 i
+= count_contiguous_clusters(nb_clusters
- i
, s
->cluster_size
,
1072 &l2_table
[l2_index
], i
, 0);
1074 if(be64_to_cpu(l2_table
[l2_index
+ i
]))
1077 i
+= count_contiguous_free_clusters(nb_clusters
- i
,
1078 &l2_table
[l2_index
+ i
]);
1080 cluster_offset
= be64_to_cpu(l2_table
[l2_index
+ i
]);
1082 if ((cluster_offset
& QCOW_OFLAG_COPIED
) ||
1083 (cluster_offset
& QCOW_OFLAG_COMPRESSED
))
1088 /* allocate a new cluster */
1090 cluster_offset
= alloc_clusters(bs
, nb_clusters
* s
->cluster_size
);
1092 /* save info needed for meta data update */
1094 m
->n_start
= n_start
;
1095 m
->nb_clusters
= nb_clusters
;
1098 m
->nb_available
= MIN(nb_clusters
<< (s
->cluster_bits
- 9), n_end
);
1100 *num
= m
->nb_available
- n_start
;
1102 return cluster_offset
;
1105 static int qcow_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
1106 int nb_sectors
, int *pnum
)
1108 uint64_t cluster_offset
;
1111 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, pnum
);
1113 return (cluster_offset
!= 0);
1116 static int decompress_buffer(uint8_t *out_buf
, int out_buf_size
,
1117 const uint8_t *buf
, int buf_size
)
1119 z_stream strm1
, *strm
= &strm1
;
1122 memset(strm
, 0, sizeof(*strm
));
1124 strm
->next_in
= (uint8_t *)buf
;
1125 strm
->avail_in
= buf_size
;
1126 strm
->next_out
= out_buf
;
1127 strm
->avail_out
= out_buf_size
;
1129 ret
= inflateInit2(strm
, -12);
1132 ret
= inflate(strm
, Z_FINISH
);
1133 out_len
= strm
->next_out
- out_buf
;
1134 if ((ret
!= Z_STREAM_END
&& ret
!= Z_BUF_ERROR
) ||
1135 out_len
!= out_buf_size
) {
1143 static int decompress_cluster(BDRVQcowState
*s
, uint64_t cluster_offset
)
1145 int ret
, csize
, nb_csectors
, sector_offset
;
1148 coffset
= cluster_offset
& s
->cluster_offset_mask
;
1149 if (s
->cluster_cache_offset
!= coffset
) {
1150 nb_csectors
= ((cluster_offset
>> s
->csize_shift
) & s
->csize_mask
) + 1;
1151 sector_offset
= coffset
& 511;
1152 csize
= nb_csectors
* 512 - sector_offset
;
1153 ret
= bdrv_read(s
->hd
, coffset
>> 9, s
->cluster_data
, nb_csectors
);
1157 if (decompress_buffer(s
->cluster_cache
, s
->cluster_size
,
1158 s
->cluster_data
+ sector_offset
, csize
) < 0) {
1161 s
->cluster_cache_offset
= coffset
;
1166 /* handle reading after the end of the backing file */
1167 static int backing_read1(BlockDriverState
*bs
,
1168 int64_t sector_num
, uint8_t *buf
, int nb_sectors
)
1171 if ((sector_num
+ nb_sectors
) <= bs
->total_sectors
)
1173 if (sector_num
>= bs
->total_sectors
)
1176 n1
= bs
->total_sectors
- sector_num
;
1177 memset(buf
+ n1
* 512, 0, 512 * (nb_sectors
- n1
));
1181 static int qcow_read(BlockDriverState
*bs
, int64_t sector_num
,
1182 uint8_t *buf
, int nb_sectors
)
1184 BDRVQcowState
*s
= bs
->opaque
;
1185 int ret
, index_in_cluster
, n
, n1
;
1186 uint64_t cluster_offset
;
1188 while (nb_sectors
> 0) {
1190 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, &n
);
1191 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
1192 if (!cluster_offset
) {
1193 if (bs
->backing_hd
) {
1194 /* read from the base image */
1195 n1
= backing_read1(bs
->backing_hd
, sector_num
, buf
, n
);
1197 ret
= bdrv_read(bs
->backing_hd
, sector_num
, buf
, n1
);
1202 memset(buf
, 0, 512 * n
);
1204 } else if (cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
1205 if (decompress_cluster(s
, cluster_offset
) < 0)
1207 memcpy(buf
, s
->cluster_cache
+ index_in_cluster
* 512, 512 * n
);
1209 ret
= bdrv_pread(s
->hd
, cluster_offset
+ index_in_cluster
* 512, buf
, n
* 512);
1212 if (s
->crypt_method
) {
1213 encrypt_sectors(s
, sector_num
, buf
, buf
, n
, 0,
1214 &s
->aes_decrypt_key
);
1224 static int qcow_write(BlockDriverState
*bs
, int64_t sector_num
,
1225 const uint8_t *buf
, int nb_sectors
)
1227 BDRVQcowState
*s
= bs
->opaque
;
1228 int ret
, index_in_cluster
, n
;
1229 uint64_t cluster_offset
;
1233 while (nb_sectors
> 0) {
1234 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
1235 n_end
= index_in_cluster
+ nb_sectors
;
1236 if (s
->crypt_method
&&
1237 n_end
> QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
)
1238 n_end
= QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
;
1239 cluster_offset
= alloc_cluster_offset(bs
, sector_num
<< 9,
1241 n_end
, &n
, &l2meta
);
1242 if (!cluster_offset
)
1244 if (s
->crypt_method
) {
1245 encrypt_sectors(s
, sector_num
, s
->cluster_data
, buf
, n
, 1,
1246 &s
->aes_encrypt_key
);
1247 ret
= bdrv_pwrite(s
->hd
, cluster_offset
+ index_in_cluster
* 512,
1248 s
->cluster_data
, n
* 512);
1250 ret
= bdrv_pwrite(s
->hd
, cluster_offset
+ index_in_cluster
* 512, buf
, n
* 512);
1252 if (ret
!= n
* 512 || alloc_cluster_link_l2(bs
, cluster_offset
, &l2meta
) < 0) {
1253 free_any_clusters(bs
, cluster_offset
, l2meta
.nb_clusters
);
1260 s
->cluster_cache_offset
= -1; /* disable compressed cache */
1264 typedef struct QCowAIOCB
{
1265 BlockDriverAIOCB common
;
1272 uint64_t cluster_offset
;
1273 uint8_t *cluster_data
;
1274 BlockDriverAIOCB
*hd_aiocb
;
1275 struct iovec hd_iov
;
1276 QEMUIOVector hd_qiov
;
1281 static void qcow_aio_read_cb(void *opaque
, int ret
);
1282 static void qcow_aio_read_bh(void *opaque
)
1284 QCowAIOCB
*acb
= opaque
;
1285 qemu_bh_delete(acb
->bh
);
1287 qcow_aio_read_cb(opaque
, 0);
1290 static int qcow_schedule_bh(QEMUBHFunc
*cb
, QCowAIOCB
*acb
)
1295 acb
->bh
= qemu_bh_new(cb
, acb
);
1299 qemu_bh_schedule(acb
->bh
);
1304 static void qcow_aio_read_cb(void *opaque
, int ret
)
1306 QCowAIOCB
*acb
= opaque
;
1307 BlockDriverState
*bs
= acb
->common
.bs
;
1308 BDRVQcowState
*s
= bs
->opaque
;
1309 int index_in_cluster
, n1
;
1311 acb
->hd_aiocb
= NULL
;
1315 /* post process the read buffer */
1316 if (!acb
->cluster_offset
) {
1318 } else if (acb
->cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
1321 if (s
->crypt_method
) {
1322 encrypt_sectors(s
, acb
->sector_num
, acb
->buf
, acb
->buf
,
1324 &s
->aes_decrypt_key
);
1328 acb
->nb_sectors
-= acb
->n
;
1329 acb
->sector_num
+= acb
->n
;
1330 acb
->buf
+= acb
->n
* 512;
1332 if (acb
->nb_sectors
== 0) {
1333 /* request completed */
1338 /* prepare next AIO request */
1339 acb
->n
= acb
->nb_sectors
;
1340 acb
->cluster_offset
= get_cluster_offset(bs
, acb
->sector_num
<< 9, &acb
->n
);
1341 index_in_cluster
= acb
->sector_num
& (s
->cluster_sectors
- 1);
1343 if (!acb
->cluster_offset
) {
1344 if (bs
->backing_hd
) {
1345 /* read from the base image */
1346 n1
= backing_read1(bs
->backing_hd
, acb
->sector_num
,
1349 acb
->hd_iov
.iov_base
= (void *)acb
->buf
;
1350 acb
->hd_iov
.iov_len
= acb
->n
* 512;
1351 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
1352 acb
->hd_aiocb
= bdrv_aio_readv(bs
->backing_hd
, acb
->sector_num
,
1353 &acb
->hd_qiov
, acb
->n
,
1354 qcow_aio_read_cb
, acb
);
1355 if (acb
->hd_aiocb
== NULL
)
1358 ret
= qcow_schedule_bh(qcow_aio_read_bh
, acb
);
1363 /* Note: in this case, no need to wait */
1364 memset(acb
->buf
, 0, 512 * acb
->n
);
1365 ret
= qcow_schedule_bh(qcow_aio_read_bh
, acb
);
1369 } else if (acb
->cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
1370 /* add AIO support for compressed blocks ? */
1371 if (decompress_cluster(s
, acb
->cluster_offset
) < 0)
1374 s
->cluster_cache
+ index_in_cluster
* 512, 512 * acb
->n
);
1375 ret
= qcow_schedule_bh(qcow_aio_read_bh
, acb
);
1379 if ((acb
->cluster_offset
& 511) != 0) {
1384 acb
->hd_iov
.iov_base
= (void *)acb
->buf
;
1385 acb
->hd_iov
.iov_len
= acb
->n
* 512;
1386 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
1387 acb
->hd_aiocb
= bdrv_aio_readv(s
->hd
,
1388 (acb
->cluster_offset
>> 9) + index_in_cluster
,
1389 &acb
->hd_qiov
, acb
->n
, qcow_aio_read_cb
, acb
);
1390 if (acb
->hd_aiocb
== NULL
)
1396 if (acb
->qiov
->niov
> 1) {
1397 qemu_iovec_from_buffer(acb
->qiov
, acb
->orig_buf
, acb
->qiov
->size
);
1398 qemu_vfree(acb
->orig_buf
);
1400 acb
->common
.cb(acb
->common
.opaque
, ret
);
1401 qemu_aio_release(acb
);
1404 static QCowAIOCB
*qcow_aio_setup(BlockDriverState
*bs
,
1405 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1406 BlockDriverCompletionFunc
*cb
, void *opaque
, int is_write
)
1410 acb
= qemu_aio_get(bs
, cb
, opaque
);
1413 acb
->hd_aiocb
= NULL
;
1414 acb
->sector_num
= sector_num
;
1416 if (qiov
->niov
> 1) {
1417 acb
->buf
= acb
->orig_buf
= qemu_memalign(512, qiov
->size
);
1419 qemu_iovec_to_buffer(qiov
, acb
->buf
);
1421 acb
->buf
= (uint8_t *)qiov
->iov
->iov_base
;
1423 acb
->nb_sectors
= nb_sectors
;
1425 acb
->cluster_offset
= 0;
1426 acb
->l2meta
.nb_clusters
= 0;
1430 static BlockDriverAIOCB
*qcow_aio_readv(BlockDriverState
*bs
,
1431 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1432 BlockDriverCompletionFunc
*cb
, void *opaque
)
1436 acb
= qcow_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
1440 qcow_aio_read_cb(acb
, 0);
1441 return &acb
->common
;
1444 static void qcow_aio_write_cb(void *opaque
, int ret
)
1446 QCowAIOCB
*acb
= opaque
;
1447 BlockDriverState
*bs
= acb
->common
.bs
;
1448 BDRVQcowState
*s
= bs
->opaque
;
1449 int index_in_cluster
;
1450 const uint8_t *src_buf
;
1453 acb
->hd_aiocb
= NULL
;
1458 if (alloc_cluster_link_l2(bs
, acb
->cluster_offset
, &acb
->l2meta
) < 0) {
1459 free_any_clusters(bs
, acb
->cluster_offset
, acb
->l2meta
.nb_clusters
);
1463 acb
->nb_sectors
-= acb
->n
;
1464 acb
->sector_num
+= acb
->n
;
1465 acb
->buf
+= acb
->n
* 512;
1467 if (acb
->nb_sectors
== 0) {
1468 /* request completed */
1473 index_in_cluster
= acb
->sector_num
& (s
->cluster_sectors
- 1);
1474 n_end
= index_in_cluster
+ acb
->nb_sectors
;
1475 if (s
->crypt_method
&&
1476 n_end
> QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
)
1477 n_end
= QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
;
1479 acb
->cluster_offset
= alloc_cluster_offset(bs
, acb
->sector_num
<< 9,
1481 n_end
, &acb
->n
, &acb
->l2meta
);
1482 if (!acb
->cluster_offset
|| (acb
->cluster_offset
& 511) != 0) {
1486 if (s
->crypt_method
) {
1487 if (!acb
->cluster_data
) {
1488 acb
->cluster_data
= qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS
*
1491 encrypt_sectors(s
, acb
->sector_num
, acb
->cluster_data
, acb
->buf
,
1492 acb
->n
, 1, &s
->aes_encrypt_key
);
1493 src_buf
= acb
->cluster_data
;
1497 acb
->hd_iov
.iov_base
= (void *)src_buf
;
1498 acb
->hd_iov
.iov_len
= acb
->n
* 512;
1499 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
1500 acb
->hd_aiocb
= bdrv_aio_writev(s
->hd
,
1501 (acb
->cluster_offset
>> 9) + index_in_cluster
,
1502 &acb
->hd_qiov
, acb
->n
,
1503 qcow_aio_write_cb
, acb
);
1504 if (acb
->hd_aiocb
== NULL
)
1510 if (acb
->qiov
->niov
> 1)
1511 qemu_vfree(acb
->orig_buf
);
1512 acb
->common
.cb(acb
->common
.opaque
, ret
);
1513 qemu_aio_release(acb
);
1516 static BlockDriverAIOCB
*qcow_aio_writev(BlockDriverState
*bs
,
1517 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1518 BlockDriverCompletionFunc
*cb
, void *opaque
)
1520 BDRVQcowState
*s
= bs
->opaque
;
1523 s
->cluster_cache_offset
= -1; /* disable compressed cache */
1525 acb
= qcow_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
1529 qcow_aio_write_cb(acb
, 0);
1530 return &acb
->common
;
1533 static void qcow_aio_cancel(BlockDriverAIOCB
*blockacb
)
1535 QCowAIOCB
*acb
= (QCowAIOCB
*)blockacb
;
1537 bdrv_aio_cancel(acb
->hd_aiocb
);
1538 qemu_aio_release(acb
);
1541 static void qcow_close(BlockDriverState
*bs
)
1543 BDRVQcowState
*s
= bs
->opaque
;
1544 qemu_free(s
->l1_table
);
1545 qemu_free(s
->l2_cache
);
1546 qemu_free(s
->cluster_cache
);
1547 qemu_free(s
->cluster_data
);
1552 /* XXX: use std qcow open function ? */
1553 typedef struct QCowCreateState
{
1556 uint16_t *refcount_block
;
1557 uint64_t *refcount_table
;
1558 int64_t l1_table_offset
;
1559 int64_t refcount_table_offset
;
1560 int64_t refcount_block_offset
;
1563 static void create_refcount_update(QCowCreateState
*s
,
1564 int64_t offset
, int64_t size
)
1567 int64_t start
, last
, cluster_offset
;
1570 start
= offset
& ~(s
->cluster_size
- 1);
1571 last
= (offset
+ size
- 1) & ~(s
->cluster_size
- 1);
1572 for(cluster_offset
= start
; cluster_offset
<= last
;
1573 cluster_offset
+= s
->cluster_size
) {
1574 p
= &s
->refcount_block
[cluster_offset
>> s
->cluster_bits
];
1575 refcount
= be16_to_cpu(*p
);
1577 *p
= cpu_to_be16(refcount
);
1581 static int qcow_create2(const char *filename
, int64_t total_size
,
1582 const char *backing_file
, const char *backing_format
,
1586 int fd
, header_size
, backing_filename_len
, l1_size
, i
, shift
, l2_bits
;
1587 int ref_clusters
, backing_format_len
= 0;
1589 uint64_t tmp
, offset
;
1590 QCowCreateState s1
, *s
= &s1
;
1591 QCowExtension ext_bf
= {0, 0};
1594 memset(s
, 0, sizeof(*s
));
1596 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0644);
1599 memset(&header
, 0, sizeof(header
));
1600 header
.magic
= cpu_to_be32(QCOW_MAGIC
);
1601 header
.version
= cpu_to_be32(QCOW_VERSION
);
1602 header
.size
= cpu_to_be64(total_size
* 512);
1603 header_size
= sizeof(header
);
1604 backing_filename_len
= 0;
1606 if (backing_format
) {
1607 ext_bf
.magic
= QCOW_EXT_MAGIC_BACKING_FORMAT
;
1608 backing_format_len
= strlen(backing_format
);
1609 ext_bf
.len
= (backing_format_len
+ 7) & ~7;
1610 header_size
+= ((sizeof(ext_bf
) + ext_bf
.len
+ 7) & ~7);
1612 header
.backing_file_offset
= cpu_to_be64(header_size
);
1613 backing_filename_len
= strlen(backing_file
);
1614 header
.backing_file_size
= cpu_to_be32(backing_filename_len
);
1615 header_size
+= backing_filename_len
;
1617 s
->cluster_bits
= 12; /* 4 KB clusters */
1618 s
->cluster_size
= 1 << s
->cluster_bits
;
1619 header
.cluster_bits
= cpu_to_be32(s
->cluster_bits
);
1620 header_size
= (header_size
+ 7) & ~7;
1621 if (flags
& BLOCK_FLAG_ENCRYPT
) {
1622 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
1624 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
1626 l2_bits
= s
->cluster_bits
- 3;
1627 shift
= s
->cluster_bits
+ l2_bits
;
1628 l1_size
= (((total_size
* 512) + (1LL << shift
) - 1) >> shift
);
1629 offset
= align_offset(header_size
, s
->cluster_size
);
1630 s
->l1_table_offset
= offset
;
1631 header
.l1_table_offset
= cpu_to_be64(s
->l1_table_offset
);
1632 header
.l1_size
= cpu_to_be32(l1_size
);
1633 offset
+= align_offset(l1_size
* sizeof(uint64_t), s
->cluster_size
);
1635 s
->refcount_table
= qemu_mallocz(s
->cluster_size
);
1637 s
->refcount_table_offset
= offset
;
1638 header
.refcount_table_offset
= cpu_to_be64(offset
);
1639 header
.refcount_table_clusters
= cpu_to_be32(1);
1640 offset
+= s
->cluster_size
;
1641 s
->refcount_block_offset
= offset
;
1643 /* count how many refcount blocks needed */
1644 tmp
= offset
>> s
->cluster_bits
;
1645 ref_clusters
= (tmp
>> (s
->cluster_bits
- REFCOUNT_SHIFT
)) + 1;
1646 for (i
=0; i
< ref_clusters
; i
++) {
1647 s
->refcount_table
[i
] = cpu_to_be64(offset
);
1648 offset
+= s
->cluster_size
;
1651 s
->refcount_block
= qemu_mallocz(ref_clusters
* s
->cluster_size
);
1653 /* update refcounts */
1654 create_refcount_update(s
, 0, header_size
);
1655 create_refcount_update(s
, s
->l1_table_offset
, l1_size
* sizeof(uint64_t));
1656 create_refcount_update(s
, s
->refcount_table_offset
, s
->cluster_size
);
1657 create_refcount_update(s
, s
->refcount_block_offset
, ref_clusters
* s
->cluster_size
);
1659 /* write all the data */
1660 write(fd
, &header
, sizeof(header
));
1662 if (backing_format_len
) {
1664 int d
= ext_bf
.len
- backing_format_len
;
1666 memset(zero
, 0, sizeof(zero
));
1667 cpu_to_be32s(&ext_bf
.magic
);
1668 cpu_to_be32s(&ext_bf
.len
);
1669 write(fd
, &ext_bf
, sizeof(ext_bf
));
1670 write(fd
, backing_format
, backing_format_len
);
1675 write(fd
, backing_file
, backing_filename_len
);
1677 lseek(fd
, s
->l1_table_offset
, SEEK_SET
);
1679 for(i
= 0;i
< l1_size
; i
++) {
1680 write(fd
, &tmp
, sizeof(tmp
));
1682 lseek(fd
, s
->refcount_table_offset
, SEEK_SET
);
1683 write(fd
, s
->refcount_table
, s
->cluster_size
);
1685 lseek(fd
, s
->refcount_block_offset
, SEEK_SET
);
1686 write(fd
, s
->refcount_block
, ref_clusters
* s
->cluster_size
);
1688 qemu_free(s
->refcount_table
);
1689 qemu_free(s
->refcount_block
);
1694 static int qcow_create(const char *filename
, int64_t total_size
,
1695 const char *backing_file
, int flags
)
1697 return qcow_create2(filename
, total_size
, backing_file
, NULL
, flags
);
1700 static int qcow_make_empty(BlockDriverState
*bs
)
1703 /* XXX: not correct */
1704 BDRVQcowState
*s
= bs
->opaque
;
1705 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
1708 memset(s
->l1_table
, 0, l1_length
);
1709 if (bdrv_pwrite(s
->hd
, s
->l1_table_offset
, s
->l1_table
, l1_length
) < 0)
1711 ret
= bdrv_truncate(s
->hd
, s
->l1_table_offset
+ l1_length
);
1720 /* XXX: put compressed sectors first, then all the cluster aligned
1721 tables to avoid losing bytes in alignment */
1722 static int qcow_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1723 const uint8_t *buf
, int nb_sectors
)
1725 BDRVQcowState
*s
= bs
->opaque
;
1729 uint64_t cluster_offset
;
1731 if (nb_sectors
== 0) {
1732 /* align end of file to a sector boundary to ease reading with
1733 sector based I/Os */
1734 cluster_offset
= bdrv_getlength(s
->hd
);
1735 cluster_offset
= (cluster_offset
+ 511) & ~511;
1736 bdrv_truncate(s
->hd
, cluster_offset
);
1740 if (nb_sectors
!= s
->cluster_sectors
)
1743 out_buf
= qemu_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
1745 /* best compression, small window, no zlib header */
1746 memset(&strm
, 0, sizeof(strm
));
1747 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
1749 9, Z_DEFAULT_STRATEGY
);
1755 strm
.avail_in
= s
->cluster_size
;
1756 strm
.next_in
= (uint8_t *)buf
;
1757 strm
.avail_out
= s
->cluster_size
;
1758 strm
.next_out
= out_buf
;
1760 ret
= deflate(&strm
, Z_FINISH
);
1761 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
1766 out_len
= strm
.next_out
- out_buf
;
1770 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
1771 /* could not compress: write normal cluster */
1772 qcow_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
1774 cluster_offset
= alloc_compressed_cluster_offset(bs
, sector_num
<< 9,
1776 if (!cluster_offset
)
1778 cluster_offset
&= s
->cluster_offset_mask
;
1779 if (bdrv_pwrite(s
->hd
, cluster_offset
, out_buf
, out_len
) != out_len
) {
1789 static void qcow_flush(BlockDriverState
*bs
)
1791 BDRVQcowState
*s
= bs
->opaque
;
1795 static int qcow_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1797 BDRVQcowState
*s
= bs
->opaque
;
1798 bdi
->cluster_size
= s
->cluster_size
;
1799 bdi
->vm_state_offset
= (int64_t)s
->l1_vm_state_index
<<
1800 (s
->cluster_bits
+ s
->l2_bits
);
1804 /*********************************************************/
1805 /* snapshot support */
1807 /* update the refcounts of snapshots and the copied flag */
1808 static int update_snapshot_refcount(BlockDriverState
*bs
,
1809 int64_t l1_table_offset
,
1813 BDRVQcowState
*s
= bs
->opaque
;
1814 uint64_t *l1_table
, *l2_table
, l2_offset
, offset
, l1_size2
, l1_allocated
;
1815 int64_t old_offset
, old_l2_offset
;
1816 int l2_size
, i
, j
, l1_modified
, l2_modified
, nb_csectors
, refcount
;
1822 l1_size2
= l1_size
* sizeof(uint64_t);
1824 if (l1_table_offset
!= s
->l1_table_offset
) {
1825 l1_table
= qemu_malloc(l1_size2
);
1827 if (bdrv_pread(s
->hd
, l1_table_offset
,
1828 l1_table
, l1_size2
) != l1_size2
)
1830 for(i
= 0;i
< l1_size
; i
++)
1831 be64_to_cpus(&l1_table
[i
]);
1833 assert(l1_size
== s
->l1_size
);
1834 l1_table
= s
->l1_table
;
1838 l2_size
= s
->l2_size
* sizeof(uint64_t);
1839 l2_table
= qemu_malloc(l2_size
);
1841 for(i
= 0; i
< l1_size
; i
++) {
1842 l2_offset
= l1_table
[i
];
1844 old_l2_offset
= l2_offset
;
1845 l2_offset
&= ~QCOW_OFLAG_COPIED
;
1847 if (bdrv_pread(s
->hd
, l2_offset
, l2_table
, l2_size
) != l2_size
)
1849 for(j
= 0; j
< s
->l2_size
; j
++) {
1850 offset
= be64_to_cpu(l2_table
[j
]);
1852 old_offset
= offset
;
1853 offset
&= ~QCOW_OFLAG_COPIED
;
1854 if (offset
& QCOW_OFLAG_COMPRESSED
) {
1855 nb_csectors
= ((offset
>> s
->csize_shift
) &
1858 update_refcount(bs
, (offset
& s
->cluster_offset_mask
) & ~511,
1859 nb_csectors
* 512, addend
);
1860 /* compressed clusters are never modified */
1864 refcount
= update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, addend
);
1866 refcount
= get_refcount(bs
, offset
>> s
->cluster_bits
);
1870 if (refcount
== 1) {
1871 offset
|= QCOW_OFLAG_COPIED
;
1873 if (offset
!= old_offset
) {
1874 l2_table
[j
] = cpu_to_be64(offset
);
1880 if (bdrv_pwrite(s
->hd
,
1881 l2_offset
, l2_table
, l2_size
) != l2_size
)
1886 refcount
= update_cluster_refcount(bs
, l2_offset
>> s
->cluster_bits
, addend
);
1888 refcount
= get_refcount(bs
, l2_offset
>> s
->cluster_bits
);
1890 if (refcount
== 1) {
1891 l2_offset
|= QCOW_OFLAG_COPIED
;
1893 if (l2_offset
!= old_l2_offset
) {
1894 l1_table
[i
] = l2_offset
;
1900 for(i
= 0; i
< l1_size
; i
++)
1901 cpu_to_be64s(&l1_table
[i
]);
1902 if (bdrv_pwrite(s
->hd
, l1_table_offset
, l1_table
,
1903 l1_size2
) != l1_size2
)
1905 for(i
= 0; i
< l1_size
; i
++)
1906 be64_to_cpus(&l1_table
[i
]);
1909 qemu_free(l1_table
);
1910 qemu_free(l2_table
);
1914 qemu_free(l1_table
);
1915 qemu_free(l2_table
);
1919 static void qcow_free_snapshots(BlockDriverState
*bs
)
1921 BDRVQcowState
*s
= bs
->opaque
;
1924 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1925 qemu_free(s
->snapshots
[i
].name
);
1926 qemu_free(s
->snapshots
[i
].id_str
);
1928 qemu_free(s
->snapshots
);
1929 s
->snapshots
= NULL
;
1930 s
->nb_snapshots
= 0;
1933 static int qcow_read_snapshots(BlockDriverState
*bs
)
1935 BDRVQcowState
*s
= bs
->opaque
;
1936 QCowSnapshotHeader h
;
1938 int i
, id_str_size
, name_size
;
1940 uint32_t extra_data_size
;
1942 if (!s
->nb_snapshots
) {
1943 s
->snapshots
= NULL
;
1944 s
->snapshots_size
= 0;
1948 offset
= s
->snapshots_offset
;
1949 s
->snapshots
= qemu_mallocz(s
->nb_snapshots
* sizeof(QCowSnapshot
));
1950 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1951 offset
= align_offset(offset
, 8);
1952 if (bdrv_pread(s
->hd
, offset
, &h
, sizeof(h
)) != sizeof(h
))
1954 offset
+= sizeof(h
);
1955 sn
= s
->snapshots
+ i
;
1956 sn
->l1_table_offset
= be64_to_cpu(h
.l1_table_offset
);
1957 sn
->l1_size
= be32_to_cpu(h
.l1_size
);
1958 sn
->vm_state_size
= be32_to_cpu(h
.vm_state_size
);
1959 sn
->date_sec
= be32_to_cpu(h
.date_sec
);
1960 sn
->date_nsec
= be32_to_cpu(h
.date_nsec
);
1961 sn
->vm_clock_nsec
= be64_to_cpu(h
.vm_clock_nsec
);
1962 extra_data_size
= be32_to_cpu(h
.extra_data_size
);
1964 id_str_size
= be16_to_cpu(h
.id_str_size
);
1965 name_size
= be16_to_cpu(h
.name_size
);
1967 offset
+= extra_data_size
;
1969 sn
->id_str
= qemu_malloc(id_str_size
+ 1);
1970 if (bdrv_pread(s
->hd
, offset
, sn
->id_str
, id_str_size
) != id_str_size
)
1972 offset
+= id_str_size
;
1973 sn
->id_str
[id_str_size
] = '\0';
1975 sn
->name
= qemu_malloc(name_size
+ 1);
1976 if (bdrv_pread(s
->hd
, offset
, sn
->name
, name_size
) != name_size
)
1978 offset
+= name_size
;
1979 sn
->name
[name_size
] = '\0';
1981 s
->snapshots_size
= offset
- s
->snapshots_offset
;
1984 qcow_free_snapshots(bs
);
1988 /* add at the end of the file a new list of snapshots */
1989 static int qcow_write_snapshots(BlockDriverState
*bs
)
1991 BDRVQcowState
*s
= bs
->opaque
;
1993 QCowSnapshotHeader h
;
1994 int i
, name_size
, id_str_size
, snapshots_size
;
1997 int64_t offset
, snapshots_offset
;
1999 /* compute the size of the snapshots */
2001 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
2002 sn
= s
->snapshots
+ i
;
2003 offset
= align_offset(offset
, 8);
2004 offset
+= sizeof(h
);
2005 offset
+= strlen(sn
->id_str
);
2006 offset
+= strlen(sn
->name
);
2008 snapshots_size
= offset
;
2010 snapshots_offset
= alloc_clusters(bs
, snapshots_size
);
2011 offset
= snapshots_offset
;
2013 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
2014 sn
= s
->snapshots
+ i
;
2015 memset(&h
, 0, sizeof(h
));
2016 h
.l1_table_offset
= cpu_to_be64(sn
->l1_table_offset
);
2017 h
.l1_size
= cpu_to_be32(sn
->l1_size
);
2018 h
.vm_state_size
= cpu_to_be32(sn
->vm_state_size
);
2019 h
.date_sec
= cpu_to_be32(sn
->date_sec
);
2020 h
.date_nsec
= cpu_to_be32(sn
->date_nsec
);
2021 h
.vm_clock_nsec
= cpu_to_be64(sn
->vm_clock_nsec
);
2023 id_str_size
= strlen(sn
->id_str
);
2024 name_size
= strlen(sn
->name
);
2025 h
.id_str_size
= cpu_to_be16(id_str_size
);
2026 h
.name_size
= cpu_to_be16(name_size
);
2027 offset
= align_offset(offset
, 8);
2028 if (bdrv_pwrite(s
->hd
, offset
, &h
, sizeof(h
)) != sizeof(h
))
2030 offset
+= sizeof(h
);
2031 if (bdrv_pwrite(s
->hd
, offset
, sn
->id_str
, id_str_size
) != id_str_size
)
2033 offset
+= id_str_size
;
2034 if (bdrv_pwrite(s
->hd
, offset
, sn
->name
, name_size
) != name_size
)
2036 offset
+= name_size
;
2039 /* update the various header fields */
2040 data64
= cpu_to_be64(snapshots_offset
);
2041 if (bdrv_pwrite(s
->hd
, offsetof(QCowHeader
, snapshots_offset
),
2042 &data64
, sizeof(data64
)) != sizeof(data64
))
2044 data32
= cpu_to_be32(s
->nb_snapshots
);
2045 if (bdrv_pwrite(s
->hd
, offsetof(QCowHeader
, nb_snapshots
),
2046 &data32
, sizeof(data32
)) != sizeof(data32
))
2049 /* free the old snapshot table */
2050 free_clusters(bs
, s
->snapshots_offset
, s
->snapshots_size
);
2051 s
->snapshots_offset
= snapshots_offset
;
2052 s
->snapshots_size
= snapshots_size
;
2058 static void find_new_snapshot_id(BlockDriverState
*bs
,
2059 char *id_str
, int id_str_size
)
2061 BDRVQcowState
*s
= bs
->opaque
;
2063 int i
, id
, id_max
= 0;
2065 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
2066 sn
= s
->snapshots
+ i
;
2067 id
= strtoul(sn
->id_str
, NULL
, 10);
2071 snprintf(id_str
, id_str_size
, "%d", id_max
+ 1);
2074 static int find_snapshot_by_id(BlockDriverState
*bs
, const char *id_str
)
2076 BDRVQcowState
*s
= bs
->opaque
;
2079 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
2080 if (!strcmp(s
->snapshots
[i
].id_str
, id_str
))
2086 static int find_snapshot_by_id_or_name(BlockDriverState
*bs
, const char *name
)
2088 BDRVQcowState
*s
= bs
->opaque
;
2091 ret
= find_snapshot_by_id(bs
, name
);
2094 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
2095 if (!strcmp(s
->snapshots
[i
].name
, name
))
2101 /* if no id is provided, a new one is constructed */
2102 static int qcow_snapshot_create(BlockDriverState
*bs
,
2103 QEMUSnapshotInfo
*sn_info
)
2105 BDRVQcowState
*s
= bs
->opaque
;
2106 QCowSnapshot
*snapshots1
, sn1
, *sn
= &sn1
;
2108 uint64_t *l1_table
= NULL
;
2110 memset(sn
, 0, sizeof(*sn
));
2112 if (sn_info
->id_str
[0] == '\0') {
2113 /* compute a new id */
2114 find_new_snapshot_id(bs
, sn_info
->id_str
, sizeof(sn_info
->id_str
));
2117 /* check that the ID is unique */
2118 if (find_snapshot_by_id(bs
, sn_info
->id_str
) >= 0)
2121 sn
->id_str
= qemu_strdup(sn_info
->id_str
);
2124 sn
->name
= qemu_strdup(sn_info
->name
);
2127 sn
->vm_state_size
= sn_info
->vm_state_size
;
2128 sn
->date_sec
= sn_info
->date_sec
;
2129 sn
->date_nsec
= sn_info
->date_nsec
;
2130 sn
->vm_clock_nsec
= sn_info
->vm_clock_nsec
;
2132 ret
= update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 1);
2136 /* create the L1 table of the snapshot */
2137 sn
->l1_table_offset
= alloc_clusters(bs
, s
->l1_size
* sizeof(uint64_t));
2138 sn
->l1_size
= s
->l1_size
;
2140 l1_table
= qemu_malloc(s
->l1_size
* sizeof(uint64_t));
2141 for(i
= 0; i
< s
->l1_size
; i
++) {
2142 l1_table
[i
] = cpu_to_be64(s
->l1_table
[i
]);
2144 if (bdrv_pwrite(s
->hd
, sn
->l1_table_offset
,
2145 l1_table
, s
->l1_size
* sizeof(uint64_t)) !=
2146 (s
->l1_size
* sizeof(uint64_t)))
2148 qemu_free(l1_table
);
2151 snapshots1
= qemu_malloc((s
->nb_snapshots
+ 1) * sizeof(QCowSnapshot
));
2153 memcpy(snapshots1
, s
->snapshots
, s
->nb_snapshots
* sizeof(QCowSnapshot
));
2154 qemu_free(s
->snapshots
);
2156 s
->snapshots
= snapshots1
;
2157 s
->snapshots
[s
->nb_snapshots
++] = *sn
;
2159 if (qcow_write_snapshots(bs
) < 0)
2162 check_refcounts(bs
);
2166 qemu_free(sn
->name
);
2167 qemu_free(l1_table
);
2171 /* copy the snapshot 'snapshot_name' into the current disk image */
2172 static int qcow_snapshot_goto(BlockDriverState
*bs
,
2173 const char *snapshot_id
)
2175 BDRVQcowState
*s
= bs
->opaque
;
2177 int i
, snapshot_index
, l1_size2
;
2179 snapshot_index
= find_snapshot_by_id_or_name(bs
, snapshot_id
);
2180 if (snapshot_index
< 0)
2182 sn
= &s
->snapshots
[snapshot_index
];
2184 if (update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, -1) < 0)
2187 if (grow_l1_table(bs
, sn
->l1_size
) < 0)
2190 s
->l1_size
= sn
->l1_size
;
2191 l1_size2
= s
->l1_size
* sizeof(uint64_t);
2192 /* copy the snapshot l1 table to the current l1 table */
2193 if (bdrv_pread(s
->hd
, sn
->l1_table_offset
,
2194 s
->l1_table
, l1_size2
) != l1_size2
)
2196 if (bdrv_pwrite(s
->hd
, s
->l1_table_offset
,
2197 s
->l1_table
, l1_size2
) != l1_size2
)
2199 for(i
= 0;i
< s
->l1_size
; i
++) {
2200 be64_to_cpus(&s
->l1_table
[i
]);
2203 if (update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 1) < 0)
2207 check_refcounts(bs
);
2214 static int qcow_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
2216 BDRVQcowState
*s
= bs
->opaque
;
2218 int snapshot_index
, ret
;
2220 snapshot_index
= find_snapshot_by_id_or_name(bs
, snapshot_id
);
2221 if (snapshot_index
< 0)
2223 sn
= &s
->snapshots
[snapshot_index
];
2225 ret
= update_snapshot_refcount(bs
, sn
->l1_table_offset
, sn
->l1_size
, -1);
2228 /* must update the copied flag on the current cluster offsets */
2229 ret
= update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 0);
2232 free_clusters(bs
, sn
->l1_table_offset
, sn
->l1_size
* sizeof(uint64_t));
2234 qemu_free(sn
->id_str
);
2235 qemu_free(sn
->name
);
2236 memmove(sn
, sn
+ 1, (s
->nb_snapshots
- snapshot_index
- 1) * sizeof(*sn
));
2238 ret
= qcow_write_snapshots(bs
);
2240 /* XXX: restore snapshot if error ? */
2244 check_refcounts(bs
);
2249 static int qcow_snapshot_list(BlockDriverState
*bs
,
2250 QEMUSnapshotInfo
**psn_tab
)
2252 BDRVQcowState
*s
= bs
->opaque
;
2253 QEMUSnapshotInfo
*sn_tab
, *sn_info
;
2257 sn_tab
= qemu_mallocz(s
->nb_snapshots
* sizeof(QEMUSnapshotInfo
));
2258 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
2259 sn_info
= sn_tab
+ i
;
2260 sn
= s
->snapshots
+ i
;
2261 pstrcpy(sn_info
->id_str
, sizeof(sn_info
->id_str
),
2263 pstrcpy(sn_info
->name
, sizeof(sn_info
->name
),
2265 sn_info
->vm_state_size
= sn
->vm_state_size
;
2266 sn_info
->date_sec
= sn
->date_sec
;
2267 sn_info
->date_nsec
= sn
->date_nsec
;
2268 sn_info
->vm_clock_nsec
= sn
->vm_clock_nsec
;
2271 return s
->nb_snapshots
;
2274 /*********************************************************/
2275 /* refcount handling */
2277 static int refcount_init(BlockDriverState
*bs
)
2279 BDRVQcowState
*s
= bs
->opaque
;
2280 int ret
, refcount_table_size2
, i
;
2282 s
->refcount_block_cache
= qemu_malloc(s
->cluster_size
);
2283 refcount_table_size2
= s
->refcount_table_size
* sizeof(uint64_t);
2284 s
->refcount_table
= qemu_malloc(refcount_table_size2
);
2285 if (s
->refcount_table_size
> 0) {
2286 ret
= bdrv_pread(s
->hd
, s
->refcount_table_offset
,
2287 s
->refcount_table
, refcount_table_size2
);
2288 if (ret
!= refcount_table_size2
)
2290 for(i
= 0; i
< s
->refcount_table_size
; i
++)
2291 be64_to_cpus(&s
->refcount_table
[i
]);
2298 static void refcount_close(BlockDriverState
*bs
)
2300 BDRVQcowState
*s
= bs
->opaque
;
2301 qemu_free(s
->refcount_block_cache
);
2302 qemu_free(s
->refcount_table
);
2306 static int load_refcount_block(BlockDriverState
*bs
,
2307 int64_t refcount_block_offset
)
2309 BDRVQcowState
*s
= bs
->opaque
;
2311 ret
= bdrv_pread(s
->hd
, refcount_block_offset
, s
->refcount_block_cache
,
2313 if (ret
!= s
->cluster_size
)
2315 s
->refcount_block_cache_offset
= refcount_block_offset
;
2319 static int get_refcount(BlockDriverState
*bs
, int64_t cluster_index
)
2321 BDRVQcowState
*s
= bs
->opaque
;
2322 int refcount_table_index
, block_index
;
2323 int64_t refcount_block_offset
;
2325 refcount_table_index
= cluster_index
>> (s
->cluster_bits
- REFCOUNT_SHIFT
);
2326 if (refcount_table_index
>= s
->refcount_table_size
)
2328 refcount_block_offset
= s
->refcount_table
[refcount_table_index
];
2329 if (!refcount_block_offset
)
2331 if (refcount_block_offset
!= s
->refcount_block_cache_offset
) {
2332 /* better than nothing: return allocated if read error */
2333 if (load_refcount_block(bs
, refcount_block_offset
) < 0)
2336 block_index
= cluster_index
&
2337 ((1 << (s
->cluster_bits
- REFCOUNT_SHIFT
)) - 1);
2338 return be16_to_cpu(s
->refcount_block_cache
[block_index
]);
2341 /* return < 0 if error */
2342 static int64_t alloc_clusters_noref(BlockDriverState
*bs
, int64_t size
)
2344 BDRVQcowState
*s
= bs
->opaque
;
2347 nb_clusters
= size_to_clusters(s
, size
);
2349 for(i
= 0; i
< nb_clusters
; i
++) {
2350 int64_t i
= s
->free_cluster_index
++;
2351 if (get_refcount(bs
, i
) != 0)
2355 printf("alloc_clusters: size=%lld -> %lld\n",
2357 (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
);
2359 return (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
;
2362 static int64_t alloc_clusters(BlockDriverState
*bs
, int64_t size
)
2366 offset
= alloc_clusters_noref(bs
, size
);
2367 update_refcount(bs
, offset
, size
, 1);
2371 /* only used to allocate compressed sectors. We try to allocate
2372 contiguous sectors. size must be <= cluster_size */
2373 static int64_t alloc_bytes(BlockDriverState
*bs
, int size
)
2375 BDRVQcowState
*s
= bs
->opaque
;
2376 int64_t offset
, cluster_offset
;
2377 int free_in_cluster
;
2379 assert(size
> 0 && size
<= s
->cluster_size
);
2380 if (s
->free_byte_offset
== 0) {
2381 s
->free_byte_offset
= alloc_clusters(bs
, s
->cluster_size
);
2384 free_in_cluster
= s
->cluster_size
-
2385 (s
->free_byte_offset
& (s
->cluster_size
- 1));
2386 if (size
<= free_in_cluster
) {
2387 /* enough space in current cluster */
2388 offset
= s
->free_byte_offset
;
2389 s
->free_byte_offset
+= size
;
2390 free_in_cluster
-= size
;
2391 if (free_in_cluster
== 0)
2392 s
->free_byte_offset
= 0;
2393 if ((offset
& (s
->cluster_size
- 1)) != 0)
2394 update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, 1);
2396 offset
= alloc_clusters(bs
, s
->cluster_size
);
2397 cluster_offset
= s
->free_byte_offset
& ~(s
->cluster_size
- 1);
2398 if ((cluster_offset
+ s
->cluster_size
) == offset
) {
2399 /* we are lucky: contiguous data */
2400 offset
= s
->free_byte_offset
;
2401 update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, 1);
2402 s
->free_byte_offset
+= size
;
2404 s
->free_byte_offset
= offset
;
2411 static void free_clusters(BlockDriverState
*bs
,
2412 int64_t offset
, int64_t size
)
2414 update_refcount(bs
, offset
, size
, -1);
2417 static int grow_refcount_table(BlockDriverState
*bs
, int min_size
)
2419 BDRVQcowState
*s
= bs
->opaque
;
2420 int new_table_size
, new_table_size2
, refcount_table_clusters
, i
, ret
;
2421 uint64_t *new_table
;
2422 int64_t table_offset
;
2425 int64_t old_table_offset
;
2427 if (min_size
<= s
->refcount_table_size
)
2429 /* compute new table size */
2430 refcount_table_clusters
= s
->refcount_table_size
>> (s
->cluster_bits
- 3);
2432 if (refcount_table_clusters
== 0) {
2433 refcount_table_clusters
= 1;
2435 refcount_table_clusters
= (refcount_table_clusters
* 3 + 1) / 2;
2437 new_table_size
= refcount_table_clusters
<< (s
->cluster_bits
- 3);
2438 if (min_size
<= new_table_size
)
2442 printf("grow_refcount_table from %d to %d\n",
2443 s
->refcount_table_size
,
2446 new_table_size2
= new_table_size
* sizeof(uint64_t);
2447 new_table
= qemu_mallocz(new_table_size2
);
2448 memcpy(new_table
, s
->refcount_table
,
2449 s
->refcount_table_size
* sizeof(uint64_t));
2450 for(i
= 0; i
< s
->refcount_table_size
; i
++)
2451 cpu_to_be64s(&new_table
[i
]);
2452 /* Note: we cannot update the refcount now to avoid recursion */
2453 table_offset
= alloc_clusters_noref(bs
, new_table_size2
);
2454 ret
= bdrv_pwrite(s
->hd
, table_offset
, new_table
, new_table_size2
);
2455 if (ret
!= new_table_size2
)
2457 for(i
= 0; i
< s
->refcount_table_size
; i
++)
2458 be64_to_cpus(&new_table
[i
]);
2460 cpu_to_be64w((uint64_t*)data
, table_offset
);
2461 cpu_to_be32w((uint32_t*)(data
+ 8), refcount_table_clusters
);
2462 if (bdrv_pwrite(s
->hd
, offsetof(QCowHeader
, refcount_table_offset
),
2463 data
, sizeof(data
)) != sizeof(data
))
2465 qemu_free(s
->refcount_table
);
2466 old_table_offset
= s
->refcount_table_offset
;
2467 old_table_size
= s
->refcount_table_size
;
2468 s
->refcount_table
= new_table
;
2469 s
->refcount_table_size
= new_table_size
;
2470 s
->refcount_table_offset
= table_offset
;
2472 update_refcount(bs
, table_offset
, new_table_size2
, 1);
2473 free_clusters(bs
, old_table_offset
, old_table_size
* sizeof(uint64_t));
2476 free_clusters(bs
, table_offset
, new_table_size2
);
2477 qemu_free(new_table
);
2481 /* addend must be 1 or -1 */
2482 /* XXX: cache several refcount block clusters ? */
2483 static int update_cluster_refcount(BlockDriverState
*bs
,
2484 int64_t cluster_index
,
2487 BDRVQcowState
*s
= bs
->opaque
;
2488 int64_t offset
, refcount_block_offset
;
2489 int ret
, refcount_table_index
, block_index
, refcount
;
2492 refcount_table_index
= cluster_index
>> (s
->cluster_bits
- REFCOUNT_SHIFT
);
2493 if (refcount_table_index
>= s
->refcount_table_size
) {
2496 ret
= grow_refcount_table(bs
, refcount_table_index
+ 1);
2500 refcount_block_offset
= s
->refcount_table
[refcount_table_index
];
2501 if (!refcount_block_offset
) {
2504 /* create a new refcount block */
2505 /* Note: we cannot update the refcount now to avoid recursion */
2506 offset
= alloc_clusters_noref(bs
, s
->cluster_size
);
2507 memset(s
->refcount_block_cache
, 0, s
->cluster_size
);
2508 ret
= bdrv_pwrite(s
->hd
, offset
, s
->refcount_block_cache
, s
->cluster_size
);
2509 if (ret
!= s
->cluster_size
)
2511 s
->refcount_table
[refcount_table_index
] = offset
;
2512 data64
= cpu_to_be64(offset
);
2513 ret
= bdrv_pwrite(s
->hd
, s
->refcount_table_offset
+
2514 refcount_table_index
* sizeof(uint64_t),
2515 &data64
, sizeof(data64
));
2516 if (ret
!= sizeof(data64
))
2519 refcount_block_offset
= offset
;
2520 s
->refcount_block_cache_offset
= offset
;
2521 update_refcount(bs
, offset
, s
->cluster_size
, 1);
2523 if (refcount_block_offset
!= s
->refcount_block_cache_offset
) {
2524 if (load_refcount_block(bs
, refcount_block_offset
) < 0)
2528 /* we can update the count and save it */
2529 block_index
= cluster_index
&
2530 ((1 << (s
->cluster_bits
- REFCOUNT_SHIFT
)) - 1);
2531 refcount
= be16_to_cpu(s
->refcount_block_cache
[block_index
]);
2533 if (refcount
< 0 || refcount
> 0xffff)
2535 if (refcount
== 0 && cluster_index
< s
->free_cluster_index
) {
2536 s
->free_cluster_index
= cluster_index
;
2538 s
->refcount_block_cache
[block_index
] = cpu_to_be16(refcount
);
2539 if (bdrv_pwrite(s
->hd
,
2540 refcount_block_offset
+ (block_index
<< REFCOUNT_SHIFT
),
2541 &s
->refcount_block_cache
[block_index
], 2) != 2)
2546 static void update_refcount(BlockDriverState
*bs
,
2547 int64_t offset
, int64_t length
,
2550 BDRVQcowState
*s
= bs
->opaque
;
2551 int64_t start
, last
, cluster_offset
;
2554 printf("update_refcount: offset=%lld size=%lld addend=%d\n",
2555 offset
, length
, addend
);
2559 start
= offset
& ~(s
->cluster_size
- 1);
2560 last
= (offset
+ length
- 1) & ~(s
->cluster_size
- 1);
2561 for(cluster_offset
= start
; cluster_offset
<= last
;
2562 cluster_offset
+= s
->cluster_size
) {
2563 update_cluster_refcount(bs
, cluster_offset
>> s
->cluster_bits
, addend
);
2568 static void inc_refcounts(BlockDriverState
*bs
,
2569 uint16_t *refcount_table
,
2570 int refcount_table_size
,
2571 int64_t offset
, int64_t size
)
2573 BDRVQcowState
*s
= bs
->opaque
;
2574 int64_t start
, last
, cluster_offset
;
2580 start
= offset
& ~(s
->cluster_size
- 1);
2581 last
= (offset
+ size
- 1) & ~(s
->cluster_size
- 1);
2582 for(cluster_offset
= start
; cluster_offset
<= last
;
2583 cluster_offset
+= s
->cluster_size
) {
2584 k
= cluster_offset
>> s
->cluster_bits
;
2585 if (k
< 0 || k
>= refcount_table_size
) {
2586 printf("ERROR: invalid cluster offset=0x%llx\n", cluster_offset
);
2588 if (++refcount_table
[k
] == 0) {
2589 printf("ERROR: overflow cluster offset=0x%llx\n", cluster_offset
);
2595 static int check_refcounts_l1(BlockDriverState
*bs
,
2596 uint16_t *refcount_table
,
2597 int refcount_table_size
,
2598 int64_t l1_table_offset
, int l1_size
,
2601 BDRVQcowState
*s
= bs
->opaque
;
2602 uint64_t *l1_table
, *l2_table
, l2_offset
, offset
, l1_size2
;
2603 int l2_size
, i
, j
, nb_csectors
, refcount
;
2606 l1_size2
= l1_size
* sizeof(uint64_t);
2608 inc_refcounts(bs
, refcount_table
, refcount_table_size
,
2609 l1_table_offset
, l1_size2
);
2611 l1_table
= qemu_malloc(l1_size2
);
2612 if (bdrv_pread(s
->hd
, l1_table_offset
,
2613 l1_table
, l1_size2
) != l1_size2
)
2615 for(i
= 0;i
< l1_size
; i
++)
2616 be64_to_cpus(&l1_table
[i
]);
2618 l2_size
= s
->l2_size
* sizeof(uint64_t);
2619 l2_table
= qemu_malloc(l2_size
);
2620 for(i
= 0; i
< l1_size
; i
++) {
2621 l2_offset
= l1_table
[i
];
2624 refcount
= get_refcount(bs
, (l2_offset
& ~QCOW_OFLAG_COPIED
) >> s
->cluster_bits
);
2625 if ((refcount
== 1) != ((l2_offset
& QCOW_OFLAG_COPIED
) != 0)) {
2626 printf("ERROR OFLAG_COPIED: l2_offset=%llx refcount=%d\n",
2627 l2_offset
, refcount
);
2630 l2_offset
&= ~QCOW_OFLAG_COPIED
;
2631 if (bdrv_pread(s
->hd
, l2_offset
, l2_table
, l2_size
) != l2_size
)
2633 for(j
= 0; j
< s
->l2_size
; j
++) {
2634 offset
= be64_to_cpu(l2_table
[j
]);
2636 if (offset
& QCOW_OFLAG_COMPRESSED
) {
2637 if (offset
& QCOW_OFLAG_COPIED
) {
2638 printf("ERROR: cluster %lld: copied flag must never be set for compressed clusters\n",
2639 offset
>> s
->cluster_bits
);
2640 offset
&= ~QCOW_OFLAG_COPIED
;
2642 nb_csectors
= ((offset
>> s
->csize_shift
) &
2644 offset
&= s
->cluster_offset_mask
;
2645 inc_refcounts(bs
, refcount_table
,
2646 refcount_table_size
,
2647 offset
& ~511, nb_csectors
* 512);
2650 refcount
= get_refcount(bs
, (offset
& ~QCOW_OFLAG_COPIED
) >> s
->cluster_bits
);
2651 if ((refcount
== 1) != ((offset
& QCOW_OFLAG_COPIED
) != 0)) {
2652 printf("ERROR OFLAG_COPIED: offset=%llx refcount=%d\n",
2656 offset
&= ~QCOW_OFLAG_COPIED
;
2657 inc_refcounts(bs
, refcount_table
,
2658 refcount_table_size
,
2659 offset
, s
->cluster_size
);
2663 inc_refcounts(bs
, refcount_table
,
2664 refcount_table_size
,
2669 qemu_free(l1_table
);
2670 qemu_free(l2_table
);
2673 printf("ERROR: I/O error in check_refcounts_l1\n");
2674 qemu_free(l1_table
);
2675 qemu_free(l2_table
);
2679 static void check_refcounts(BlockDriverState
*bs
)
2681 BDRVQcowState
*s
= bs
->opaque
;
2683 int nb_clusters
, refcount1
, refcount2
, i
;
2685 uint16_t *refcount_table
;
2687 size
= bdrv_getlength(s
->hd
);
2688 nb_clusters
= size_to_clusters(s
, size
);
2689 refcount_table
= qemu_mallocz(nb_clusters
* sizeof(uint16_t));
2692 inc_refcounts(bs
, refcount_table
, nb_clusters
,
2693 0, s
->cluster_size
);
2695 check_refcounts_l1(bs
, refcount_table
, nb_clusters
,
2696 s
->l1_table_offset
, s
->l1_size
, 1);
2699 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
2700 sn
= s
->snapshots
+ i
;
2701 check_refcounts_l1(bs
, refcount_table
, nb_clusters
,
2702 sn
->l1_table_offset
, sn
->l1_size
, 0);
2704 inc_refcounts(bs
, refcount_table
, nb_clusters
,
2705 s
->snapshots_offset
, s
->snapshots_size
);
2708 inc_refcounts(bs
, refcount_table
, nb_clusters
,
2709 s
->refcount_table_offset
,
2710 s
->refcount_table_size
* sizeof(uint64_t));
2711 for(i
= 0; i
< s
->refcount_table_size
; i
++) {
2713 offset
= s
->refcount_table
[i
];
2715 inc_refcounts(bs
, refcount_table
, nb_clusters
,
2716 offset
, s
->cluster_size
);
2720 /* compare ref counts */
2721 for(i
= 0; i
< nb_clusters
; i
++) {
2722 refcount1
= get_refcount(bs
, i
);
2723 refcount2
= refcount_table
[i
];
2724 if (refcount1
!= refcount2
)
2725 printf("ERROR cluster %d refcount=%d reference=%d\n",
2726 i
, refcount1
, refcount2
);
2729 qemu_free(refcount_table
);
2733 static void dump_refcounts(BlockDriverState
*bs
)
2735 BDRVQcowState
*s
= bs
->opaque
;
2736 int64_t nb_clusters
, k
, k1
, size
;
2739 size
= bdrv_getlength(s
->hd
);
2740 nb_clusters
= size_to_clusters(s
, size
);
2741 for(k
= 0; k
< nb_clusters
;) {
2743 refcount
= get_refcount(bs
, k
);
2745 while (k
< nb_clusters
&& get_refcount(bs
, k
) == refcount
)
2747 printf("%lld: refcount=%d nb=%lld\n", k
, refcount
, k
- k1
);
2753 static int qcow_put_buffer(BlockDriverState
*bs
, const uint8_t *buf
,
2754 int64_t pos
, int size
)
2756 int growable
= bs
->growable
;
2759 bdrv_pwrite(bs
, pos
, buf
, size
);
2760 bs
->growable
= growable
;
2765 static int qcow_get_buffer(BlockDriverState
*bs
, uint8_t *buf
,
2766 int64_t pos
, int size
)
2768 int growable
= bs
->growable
;
2772 ret
= bdrv_pread(bs
, pos
, buf
, size
);
2773 bs
->growable
= growable
;
2778 BlockDriver bdrv_qcow2
= {
2779 .format_name
= "qcow2",
2780 .instance_size
= sizeof(BDRVQcowState
),
2781 .bdrv_probe
= qcow_probe
,
2782 .bdrv_open
= qcow_open
,
2783 .bdrv_close
= qcow_close
,
2784 .bdrv_create
= qcow_create
,
2785 .bdrv_flush
= qcow_flush
,
2786 .bdrv_is_allocated
= qcow_is_allocated
,
2787 .bdrv_set_key
= qcow_set_key
,
2788 .bdrv_make_empty
= qcow_make_empty
,
2790 .bdrv_aio_readv
= qcow_aio_readv
,
2791 .bdrv_aio_writev
= qcow_aio_writev
,
2792 .bdrv_aio_cancel
= qcow_aio_cancel
,
2793 .aiocb_size
= sizeof(QCowAIOCB
),
2794 .bdrv_write_compressed
= qcow_write_compressed
,
2796 .bdrv_snapshot_create
= qcow_snapshot_create
,
2797 .bdrv_snapshot_goto
= qcow_snapshot_goto
,
2798 .bdrv_snapshot_delete
= qcow_snapshot_delete
,
2799 .bdrv_snapshot_list
= qcow_snapshot_list
,
2800 .bdrv_get_info
= qcow_get_info
,
2802 .bdrv_put_buffer
= qcow_put_buffer
,
2803 .bdrv_get_buffer
= qcow_get_buffer
,
2805 .bdrv_create2
= qcow_create2
,