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
);
180 static int check_refcounts(BlockDriverState
*bs
);
182 static int qcow_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
184 const QCowHeader
*cow_header
= (const void *)buf
;
186 if (buf_size
>= sizeof(QCowHeader
) &&
187 be32_to_cpu(cow_header
->magic
) == QCOW_MAGIC
&&
188 be32_to_cpu(cow_header
->version
) == QCOW_VERSION
)
196 * read qcow2 extension and fill bs
197 * start reading from start_offset
198 * finish reading upon magic of value 0 or when end_offset reached
199 * unknown magic is skipped (future extension this version knows nothing about)
200 * return 0 upon success, non-0 otherwise
202 static int qcow_read_extensions(BlockDriverState
*bs
, uint64_t start_offset
,
205 BDRVQcowState
*s
= bs
->opaque
;
210 printf("qcow_read_extensions: start=%ld end=%ld\n", start_offset
, end_offset
);
212 offset
= start_offset
;
213 while (offset
< end_offset
) {
217 if (offset
> s
->cluster_size
)
218 printf("qcow_handle_extension: suspicious offset %lu\n", offset
);
220 printf("attemting to read extended header in offset %lu\n", offset
);
223 if (bdrv_pread(s
->hd
, offset
, &ext
, sizeof(ext
)) != sizeof(ext
)) {
224 fprintf(stderr
, "qcow_handle_extension: ERROR: pread fail from offset %llu\n",
225 (unsigned long long)offset
);
228 be32_to_cpus(&ext
.magic
);
229 be32_to_cpus(&ext
.len
);
230 offset
+= sizeof(ext
);
232 printf("ext.magic = 0x%x\n", ext
.magic
);
235 case QCOW_EXT_MAGIC_END
:
238 case QCOW_EXT_MAGIC_BACKING_FORMAT
:
239 if (ext
.len
>= sizeof(bs
->backing_format
)) {
240 fprintf(stderr
, "ERROR: ext_backing_format: len=%u too large"
242 ext
.len
, sizeof(bs
->backing_format
));
245 if (bdrv_pread(s
->hd
, offset
, bs
->backing_format
,
248 bs
->backing_format
[ext
.len
] = '\0';
250 printf("Qcow2: Got format extension %s\n", bs
->backing_format
);
252 offset
+= ((ext
.len
+ 7) & ~7);
256 /* unknown magic -- just skip it */
257 offset
+= ((ext
.len
+ 7) & ~7);
266 static int qcow_open(BlockDriverState
*bs
, const char *filename
, int flags
)
268 BDRVQcowState
*s
= bs
->opaque
;
269 int len
, i
, shift
, ret
;
273 /* Performance is terrible right now with cache=writethrough due mainly
274 * to reference count updates. If the user does not explicitly specify
275 * a caching type, force to writeback caching.
277 if ((flags
& BDRV_O_CACHE_DEF
)) {
278 flags
|= BDRV_O_CACHE_WB
;
279 flags
&= ~BDRV_O_CACHE_DEF
;
281 ret
= bdrv_file_open(&s
->hd
, filename
, flags
);
284 if (bdrv_pread(s
->hd
, 0, &header
, sizeof(header
)) != sizeof(header
))
286 be32_to_cpus(&header
.magic
);
287 be32_to_cpus(&header
.version
);
288 be64_to_cpus(&header
.backing_file_offset
);
289 be32_to_cpus(&header
.backing_file_size
);
290 be64_to_cpus(&header
.size
);
291 be32_to_cpus(&header
.cluster_bits
);
292 be32_to_cpus(&header
.crypt_method
);
293 be64_to_cpus(&header
.l1_table_offset
);
294 be32_to_cpus(&header
.l1_size
);
295 be64_to_cpus(&header
.refcount_table_offset
);
296 be32_to_cpus(&header
.refcount_table_clusters
);
297 be64_to_cpus(&header
.snapshots_offset
);
298 be32_to_cpus(&header
.nb_snapshots
);
300 if (header
.magic
!= QCOW_MAGIC
|| header
.version
!= QCOW_VERSION
)
302 if (header
.size
<= 1 ||
303 header
.cluster_bits
< 9 ||
304 header
.cluster_bits
> 16)
306 if (header
.crypt_method
> QCOW_CRYPT_AES
)
308 s
->crypt_method_header
= header
.crypt_method
;
309 if (s
->crypt_method_header
)
311 s
->cluster_bits
= header
.cluster_bits
;
312 s
->cluster_size
= 1 << s
->cluster_bits
;
313 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
314 s
->l2_bits
= s
->cluster_bits
- 3; /* L2 is always one cluster */
315 s
->l2_size
= 1 << s
->l2_bits
;
316 bs
->total_sectors
= header
.size
/ 512;
317 s
->csize_shift
= (62 - (s
->cluster_bits
- 8));
318 s
->csize_mask
= (1 << (s
->cluster_bits
- 8)) - 1;
319 s
->cluster_offset_mask
= (1LL << s
->csize_shift
) - 1;
320 s
->refcount_table_offset
= header
.refcount_table_offset
;
321 s
->refcount_table_size
=
322 header
.refcount_table_clusters
<< (s
->cluster_bits
- 3);
324 s
->snapshots_offset
= header
.snapshots_offset
;
325 s
->nb_snapshots
= header
.nb_snapshots
;
327 /* read the level 1 table */
328 s
->l1_size
= header
.l1_size
;
329 shift
= s
->cluster_bits
+ s
->l2_bits
;
330 s
->l1_vm_state_index
= (header
.size
+ (1LL << shift
) - 1) >> shift
;
331 /* the L1 table must contain at least enough entries to put
333 if (s
->l1_size
< s
->l1_vm_state_index
)
335 s
->l1_table_offset
= header
.l1_table_offset
;
336 s
->l1_table
= qemu_malloc(s
->l1_size
* sizeof(uint64_t));
337 if (bdrv_pread(s
->hd
, s
->l1_table_offset
, s
->l1_table
, s
->l1_size
* sizeof(uint64_t)) !=
338 s
->l1_size
* sizeof(uint64_t))
340 for(i
= 0;i
< s
->l1_size
; i
++) {
341 be64_to_cpus(&s
->l1_table
[i
]);
344 s
->l2_cache
= qemu_malloc(s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
345 s
->cluster_cache
= qemu_malloc(s
->cluster_size
);
346 /* one more sector for decompressed data alignment */
347 s
->cluster_data
= qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
349 s
->cluster_cache_offset
= -1;
351 if (refcount_init(bs
) < 0)
354 /* read qcow2 extensions */
355 if (header
.backing_file_offset
)
356 ext_end
= header
.backing_file_offset
;
358 ext_end
= s
->cluster_size
;
359 if (qcow_read_extensions(bs
, sizeof(header
), ext_end
))
362 /* read the backing file name */
363 if (header
.backing_file_offset
!= 0) {
364 len
= header
.backing_file_size
;
367 if (bdrv_pread(s
->hd
, header
.backing_file_offset
, bs
->backing_file
, len
) != len
)
369 bs
->backing_file
[len
] = '\0';
371 if (qcow_read_snapshots(bs
) < 0)
380 qcow_free_snapshots(bs
);
382 qemu_free(s
->l1_table
);
383 qemu_free(s
->l2_cache
);
384 qemu_free(s
->cluster_cache
);
385 qemu_free(s
->cluster_data
);
390 static int qcow_set_key(BlockDriverState
*bs
, const char *key
)
392 BDRVQcowState
*s
= bs
->opaque
;
396 memset(keybuf
, 0, 16);
400 /* XXX: we could compress the chars to 7 bits to increase
402 for(i
= 0;i
< len
;i
++) {
405 s
->crypt_method
= s
->crypt_method_header
;
407 if (AES_set_encrypt_key(keybuf
, 128, &s
->aes_encrypt_key
) != 0)
409 if (AES_set_decrypt_key(keybuf
, 128, &s
->aes_decrypt_key
) != 0)
419 AES_encrypt(in
, tmp
, &s
->aes_encrypt_key
);
420 AES_decrypt(tmp
, out
, &s
->aes_decrypt_key
);
421 for(i
= 0; i
< 16; i
++)
422 printf(" %02x", tmp
[i
]);
424 for(i
= 0; i
< 16; i
++)
425 printf(" %02x", out
[i
]);
432 /* The crypt function is compatible with the linux cryptoloop
433 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
435 static void encrypt_sectors(BDRVQcowState
*s
, int64_t sector_num
,
436 uint8_t *out_buf
, const uint8_t *in_buf
,
437 int nb_sectors
, int enc
,
446 for(i
= 0; i
< nb_sectors
; i
++) {
447 ivec
.ll
[0] = cpu_to_le64(sector_num
);
449 AES_cbc_encrypt(in_buf
, out_buf
, 512, key
,
457 static int copy_sectors(BlockDriverState
*bs
, uint64_t start_sect
,
458 uint64_t cluster_offset
, int n_start
, int n_end
)
460 BDRVQcowState
*s
= bs
->opaque
;
466 ret
= qcow_read(bs
, start_sect
+ n_start
, s
->cluster_data
, n
);
469 if (s
->crypt_method
) {
470 encrypt_sectors(s
, start_sect
+ n_start
,
472 s
->cluster_data
, n
, 1,
473 &s
->aes_encrypt_key
);
475 ret
= bdrv_write(s
->hd
, (cluster_offset
>> 9) + n_start
,
482 static void l2_cache_reset(BlockDriverState
*bs
)
484 BDRVQcowState
*s
= bs
->opaque
;
486 memset(s
->l2_cache
, 0, s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
487 memset(s
->l2_cache_offsets
, 0, L2_CACHE_SIZE
* sizeof(uint64_t));
488 memset(s
->l2_cache_counts
, 0, L2_CACHE_SIZE
* sizeof(uint32_t));
491 static inline int l2_cache_new_entry(BlockDriverState
*bs
)
493 BDRVQcowState
*s
= bs
->opaque
;
497 /* find a new entry in the least used one */
499 min_count
= 0xffffffff;
500 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
501 if (s
->l2_cache_counts
[i
] < min_count
) {
502 min_count
= s
->l2_cache_counts
[i
];
509 static int64_t align_offset(int64_t offset
, int n
)
511 offset
= (offset
+ n
- 1) & ~(n
- 1);
515 static int grow_l1_table(BlockDriverState
*bs
, int min_size
)
517 BDRVQcowState
*s
= bs
->opaque
;
518 int new_l1_size
, new_l1_size2
, ret
, i
;
519 uint64_t *new_l1_table
;
520 uint64_t new_l1_table_offset
;
523 new_l1_size
= s
->l1_size
;
524 if (min_size
<= new_l1_size
)
526 while (min_size
> new_l1_size
) {
527 new_l1_size
= (new_l1_size
* 3 + 1) / 2;
530 printf("grow l1_table from %d to %d\n", s
->l1_size
, new_l1_size
);
533 new_l1_size2
= sizeof(uint64_t) * new_l1_size
;
534 new_l1_table
= qemu_mallocz(new_l1_size2
);
535 memcpy(new_l1_table
, s
->l1_table
, s
->l1_size
* sizeof(uint64_t));
537 /* write new table (align to cluster) */
538 new_l1_table_offset
= alloc_clusters(bs
, new_l1_size2
);
540 for(i
= 0; i
< s
->l1_size
; i
++)
541 new_l1_table
[i
] = cpu_to_be64(new_l1_table
[i
]);
542 ret
= bdrv_pwrite(s
->hd
, new_l1_table_offset
, new_l1_table
, new_l1_size2
);
543 if (ret
!= new_l1_size2
)
545 for(i
= 0; i
< s
->l1_size
; i
++)
546 new_l1_table
[i
] = be64_to_cpu(new_l1_table
[i
]);
549 cpu_to_be32w((uint32_t*)data
, new_l1_size
);
550 cpu_to_be64w((uint64_t*)(data
+ 4), new_l1_table_offset
);
551 if (bdrv_pwrite(s
->hd
, offsetof(QCowHeader
, l1_size
), data
,
552 sizeof(data
)) != sizeof(data
))
554 qemu_free(s
->l1_table
);
555 free_clusters(bs
, s
->l1_table_offset
, s
->l1_size
* sizeof(uint64_t));
556 s
->l1_table_offset
= new_l1_table_offset
;
557 s
->l1_table
= new_l1_table
;
558 s
->l1_size
= new_l1_size
;
561 qemu_free(s
->l1_table
);
568 * seek l2_offset in the l2_cache table
569 * if not found, return NULL,
571 * increments the l2 cache hit count of the entry,
572 * if counter overflow, divide by two all counters
573 * return the pointer to the l2 cache entry
577 static uint64_t *seek_l2_table(BDRVQcowState
*s
, uint64_t l2_offset
)
581 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
582 if (l2_offset
== s
->l2_cache_offsets
[i
]) {
583 /* increment the hit count */
584 if (++s
->l2_cache_counts
[i
] == 0xffffffff) {
585 for(j
= 0; j
< L2_CACHE_SIZE
; j
++) {
586 s
->l2_cache_counts
[j
] >>= 1;
589 return s
->l2_cache
+ (i
<< s
->l2_bits
);
598 * Loads a L2 table into memory. If the table is in the cache, the cache
599 * is used; otherwise the L2 table is loaded from the image file.
601 * Returns a pointer to the L2 table on success, or NULL if the read from
602 * the image file failed.
605 static uint64_t *l2_load(BlockDriverState
*bs
, uint64_t l2_offset
)
607 BDRVQcowState
*s
= bs
->opaque
;
611 /* seek if the table for the given offset is in the cache */
613 l2_table
= seek_l2_table(s
, l2_offset
);
614 if (l2_table
!= NULL
)
617 /* not found: load a new entry in the least used one */
619 min_index
= l2_cache_new_entry(bs
);
620 l2_table
= s
->l2_cache
+ (min_index
<< s
->l2_bits
);
621 if (bdrv_pread(s
->hd
, l2_offset
, l2_table
, s
->l2_size
* sizeof(uint64_t)) !=
622 s
->l2_size
* sizeof(uint64_t))
624 s
->l2_cache_offsets
[min_index
] = l2_offset
;
625 s
->l2_cache_counts
[min_index
] = 1;
633 * Allocate a new l2 entry in the file. If l1_index points to an already
634 * used entry in the L2 table (i.e. we are doing a copy on write for the L2
635 * table) copy the contents of the old L2 table into the newly allocated one.
636 * Otherwise the new table is initialized with zeros.
640 static uint64_t *l2_allocate(BlockDriverState
*bs
, int l1_index
)
642 BDRVQcowState
*s
= bs
->opaque
;
644 uint64_t old_l2_offset
, tmp
;
645 uint64_t *l2_table
, l2_offset
;
647 old_l2_offset
= s
->l1_table
[l1_index
];
649 /* allocate a new l2 entry */
651 l2_offset
= alloc_clusters(bs
, s
->l2_size
* sizeof(uint64_t));
653 /* update the L1 entry */
655 s
->l1_table
[l1_index
] = l2_offset
| QCOW_OFLAG_COPIED
;
657 tmp
= cpu_to_be64(l2_offset
| QCOW_OFLAG_COPIED
);
658 if (bdrv_pwrite(s
->hd
, s
->l1_table_offset
+ l1_index
* sizeof(tmp
),
659 &tmp
, sizeof(tmp
)) != sizeof(tmp
))
662 /* allocate a new entry in the l2 cache */
664 min_index
= l2_cache_new_entry(bs
);
665 l2_table
= s
->l2_cache
+ (min_index
<< s
->l2_bits
);
667 if (old_l2_offset
== 0) {
668 /* if there was no old l2 table, clear the new table */
669 memset(l2_table
, 0, s
->l2_size
* sizeof(uint64_t));
671 /* if there was an old l2 table, read it from the disk */
672 if (bdrv_pread(s
->hd
, old_l2_offset
,
673 l2_table
, s
->l2_size
* sizeof(uint64_t)) !=
674 s
->l2_size
* sizeof(uint64_t))
677 /* write the l2 table to the file */
678 if (bdrv_pwrite(s
->hd
, l2_offset
,
679 l2_table
, s
->l2_size
* sizeof(uint64_t)) !=
680 s
->l2_size
* sizeof(uint64_t))
683 /* update the l2 cache entry */
685 s
->l2_cache_offsets
[min_index
] = l2_offset
;
686 s
->l2_cache_counts
[min_index
] = 1;
691 static int size_to_clusters(BDRVQcowState
*s
, int64_t size
)
693 return (size
+ (s
->cluster_size
- 1)) >> s
->cluster_bits
;
696 static int count_contiguous_clusters(uint64_t nb_clusters
, int cluster_size
,
697 uint64_t *l2_table
, uint64_t start
, uint64_t mask
)
700 uint64_t offset
= be64_to_cpu(l2_table
[0]) & ~mask
;
705 for (i
= start
; i
< start
+ nb_clusters
; i
++)
706 if (offset
+ i
* cluster_size
!= (be64_to_cpu(l2_table
[i
]) & ~mask
))
712 static int count_contiguous_free_clusters(uint64_t nb_clusters
, uint64_t *l2_table
)
716 while(nb_clusters
-- && l2_table
[i
] == 0)
725 * For a given offset of the disk image, return cluster offset in
728 * on entry, *num is the number of contiguous clusters we'd like to
729 * access following offset.
731 * on exit, *num is the number of contiguous clusters we can read.
733 * Return 1, if the offset is found
734 * Return 0, otherwise.
738 static uint64_t get_cluster_offset(BlockDriverState
*bs
,
739 uint64_t offset
, int *num
)
741 BDRVQcowState
*s
= bs
->opaque
;
742 int l1_index
, l2_index
;
743 uint64_t l2_offset
, *l2_table
, cluster_offset
;
745 int index_in_cluster
, nb_available
, nb_needed
, nb_clusters
;
747 index_in_cluster
= (offset
>> 9) & (s
->cluster_sectors
- 1);
748 nb_needed
= *num
+ index_in_cluster
;
750 l1_bits
= s
->l2_bits
+ s
->cluster_bits
;
752 /* compute how many bytes there are between the offset and
753 * the end of the l1 entry
756 nb_available
= (1 << l1_bits
) - (offset
& ((1 << l1_bits
) - 1));
758 /* compute the number of available sectors */
760 nb_available
= (nb_available
>> 9) + index_in_cluster
;
762 if (nb_needed
> nb_available
) {
763 nb_needed
= nb_available
;
768 /* seek the the l2 offset in the l1 table */
770 l1_index
= offset
>> l1_bits
;
771 if (l1_index
>= s
->l1_size
)
774 l2_offset
= s
->l1_table
[l1_index
];
776 /* seek the l2 table of the given l2 offset */
781 /* load the l2 table in memory */
783 l2_offset
&= ~QCOW_OFLAG_COPIED
;
784 l2_table
= l2_load(bs
, l2_offset
);
785 if (l2_table
== NULL
)
788 /* find the cluster offset for the given disk offset */
790 l2_index
= (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
791 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
792 nb_clusters
= size_to_clusters(s
, nb_needed
<< 9);
794 if (!cluster_offset
) {
795 /* how many empty clusters ? */
796 c
= count_contiguous_free_clusters(nb_clusters
, &l2_table
[l2_index
]);
798 /* how many allocated clusters ? */
799 c
= count_contiguous_clusters(nb_clusters
, s
->cluster_size
,
800 &l2_table
[l2_index
], 0, QCOW_OFLAG_COPIED
);
803 nb_available
= (c
* s
->cluster_sectors
);
805 if (nb_available
> nb_needed
)
806 nb_available
= nb_needed
;
808 *num
= nb_available
- index_in_cluster
;
810 return cluster_offset
& ~QCOW_OFLAG_COPIED
;
816 * free clusters according to its type: compressed or not
820 static void free_any_clusters(BlockDriverState
*bs
,
821 uint64_t cluster_offset
, int nb_clusters
)
823 BDRVQcowState
*s
= bs
->opaque
;
825 /* free the cluster */
827 if (cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
829 nb_csectors
= ((cluster_offset
>> s
->csize_shift
) &
831 free_clusters(bs
, (cluster_offset
& s
->cluster_offset_mask
) & ~511,
836 free_clusters(bs
, cluster_offset
, nb_clusters
<< s
->cluster_bits
);
844 * for a given disk offset, load (and allocate if needed)
847 * the l2 table offset in the qcow2 file and the cluster index
848 * in the l2 table are given to the caller.
852 static int get_cluster_table(BlockDriverState
*bs
, uint64_t offset
,
853 uint64_t **new_l2_table
,
854 uint64_t *new_l2_offset
,
857 BDRVQcowState
*s
= bs
->opaque
;
858 int l1_index
, l2_index
, ret
;
859 uint64_t l2_offset
, *l2_table
;
861 /* seek the the l2 offset in the l1 table */
863 l1_index
= offset
>> (s
->l2_bits
+ s
->cluster_bits
);
864 if (l1_index
>= s
->l1_size
) {
865 ret
= grow_l1_table(bs
, l1_index
+ 1);
869 l2_offset
= s
->l1_table
[l1_index
];
871 /* seek the l2 table of the given l2 offset */
873 if (l2_offset
& QCOW_OFLAG_COPIED
) {
874 /* load the l2 table in memory */
875 l2_offset
&= ~QCOW_OFLAG_COPIED
;
876 l2_table
= l2_load(bs
, l2_offset
);
877 if (l2_table
== NULL
)
881 free_clusters(bs
, l2_offset
, s
->l2_size
* sizeof(uint64_t));
882 l2_table
= l2_allocate(bs
, l1_index
);
883 if (l2_table
== NULL
)
885 l2_offset
= s
->l1_table
[l1_index
] & ~QCOW_OFLAG_COPIED
;
888 /* find the cluster offset for the given disk offset */
890 l2_index
= (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
892 *new_l2_table
= l2_table
;
893 *new_l2_offset
= l2_offset
;
894 *new_l2_index
= l2_index
;
900 * alloc_compressed_cluster_offset
902 * For a given offset of the disk image, return cluster offset in
905 * If the offset is not found, allocate a new compressed cluster.
907 * Return the cluster offset if successful,
908 * Return 0, otherwise.
912 static uint64_t alloc_compressed_cluster_offset(BlockDriverState
*bs
,
916 BDRVQcowState
*s
= bs
->opaque
;
918 uint64_t l2_offset
, *l2_table
, cluster_offset
;
921 ret
= get_cluster_table(bs
, offset
, &l2_table
, &l2_offset
, &l2_index
);
925 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
926 if (cluster_offset
& QCOW_OFLAG_COPIED
)
927 return cluster_offset
& ~QCOW_OFLAG_COPIED
;
930 free_any_clusters(bs
, cluster_offset
, 1);
932 cluster_offset
= alloc_bytes(bs
, compressed_size
);
933 nb_csectors
= ((cluster_offset
+ compressed_size
- 1) >> 9) -
934 (cluster_offset
>> 9);
936 cluster_offset
|= QCOW_OFLAG_COMPRESSED
|
937 ((uint64_t)nb_csectors
<< s
->csize_shift
);
939 /* update L2 table */
941 /* compressed clusters never have the copied flag */
943 l2_table
[l2_index
] = cpu_to_be64(cluster_offset
);
944 if (bdrv_pwrite(s
->hd
,
945 l2_offset
+ l2_index
* sizeof(uint64_t),
947 sizeof(uint64_t)) != sizeof(uint64_t))
950 return cluster_offset
;
953 typedef struct QCowL2Meta
961 static int alloc_cluster_link_l2(BlockDriverState
*bs
, uint64_t cluster_offset
,
964 BDRVQcowState
*s
= bs
->opaque
;
965 int i
, j
= 0, l2_index
, ret
;
966 uint64_t *old_cluster
, start_sect
, l2_offset
, *l2_table
;
968 if (m
->nb_clusters
== 0)
971 old_cluster
= qemu_malloc(m
->nb_clusters
* sizeof(uint64_t));
973 /* copy content of unmodified sectors */
974 start_sect
= (m
->offset
& ~(s
->cluster_size
- 1)) >> 9;
976 ret
= copy_sectors(bs
, start_sect
, cluster_offset
, 0, m
->n_start
);
981 if (m
->nb_available
& (s
->cluster_sectors
- 1)) {
982 uint64_t end
= m
->nb_available
& ~(uint64_t)(s
->cluster_sectors
- 1);
983 ret
= copy_sectors(bs
, start_sect
+ end
, cluster_offset
+ (end
<< 9),
984 m
->nb_available
- end
, s
->cluster_sectors
);
990 /* update L2 table */
991 if (!get_cluster_table(bs
, m
->offset
, &l2_table
, &l2_offset
, &l2_index
))
994 for (i
= 0; i
< m
->nb_clusters
; i
++) {
995 /* if two concurrent writes happen to the same unallocated cluster
996 * each write allocates separate cluster and writes data concurrently.
997 * The first one to complete updates l2 table with pointer to its
998 * cluster the second one has to do RMW (which is done above by
999 * copy_sectors()), update l2 table with its cluster pointer and free
1000 * old cluster. This is what this loop does */
1001 if(l2_table
[l2_index
+ i
] != 0)
1002 old_cluster
[j
++] = l2_table
[l2_index
+ i
];
1004 l2_table
[l2_index
+ i
] = cpu_to_be64((cluster_offset
+
1005 (i
<< s
->cluster_bits
)) | QCOW_OFLAG_COPIED
);
1008 if (bdrv_pwrite(s
->hd
, l2_offset
+ l2_index
* sizeof(uint64_t),
1009 l2_table
+ l2_index
, m
->nb_clusters
* sizeof(uint64_t)) !=
1010 m
->nb_clusters
* sizeof(uint64_t))
1013 for (i
= 0; i
< j
; i
++)
1014 free_any_clusters(bs
, be64_to_cpu(old_cluster
[i
]) & ~QCOW_OFLAG_COPIED
,
1019 qemu_free(old_cluster
);
1024 * alloc_cluster_offset
1026 * For a given offset of the disk image, return cluster offset in
1029 * If the offset is not found, allocate a new cluster.
1031 * Return the cluster offset if successful,
1032 * Return 0, otherwise.
1036 static uint64_t alloc_cluster_offset(BlockDriverState
*bs
,
1038 int n_start
, int n_end
,
1039 int *num
, QCowL2Meta
*m
)
1041 BDRVQcowState
*s
= bs
->opaque
;
1043 uint64_t l2_offset
, *l2_table
, cluster_offset
;
1044 int nb_clusters
, i
= 0;
1046 ret
= get_cluster_table(bs
, offset
, &l2_table
, &l2_offset
, &l2_index
);
1050 nb_clusters
= size_to_clusters(s
, n_end
<< 9);
1052 nb_clusters
= MIN(nb_clusters
, s
->l2_size
- l2_index
);
1054 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
1056 /* We keep all QCOW_OFLAG_COPIED clusters */
1058 if (cluster_offset
& QCOW_OFLAG_COPIED
) {
1059 nb_clusters
= count_contiguous_clusters(nb_clusters
, s
->cluster_size
,
1060 &l2_table
[l2_index
], 0, 0);
1062 cluster_offset
&= ~QCOW_OFLAG_COPIED
;
1068 /* for the moment, multiple compressed clusters are not managed */
1070 if (cluster_offset
& QCOW_OFLAG_COMPRESSED
)
1073 /* how many available clusters ? */
1075 while (i
< nb_clusters
) {
1076 i
+= count_contiguous_clusters(nb_clusters
- i
, s
->cluster_size
,
1077 &l2_table
[l2_index
], i
, 0);
1079 if(be64_to_cpu(l2_table
[l2_index
+ i
]))
1082 i
+= count_contiguous_free_clusters(nb_clusters
- i
,
1083 &l2_table
[l2_index
+ i
]);
1085 cluster_offset
= be64_to_cpu(l2_table
[l2_index
+ i
]);
1087 if ((cluster_offset
& QCOW_OFLAG_COPIED
) ||
1088 (cluster_offset
& QCOW_OFLAG_COMPRESSED
))
1093 /* allocate a new cluster */
1095 cluster_offset
= alloc_clusters(bs
, nb_clusters
* s
->cluster_size
);
1097 /* save info needed for meta data update */
1099 m
->n_start
= n_start
;
1100 m
->nb_clusters
= nb_clusters
;
1103 m
->nb_available
= MIN(nb_clusters
<< (s
->cluster_bits
- 9), n_end
);
1105 *num
= m
->nb_available
- n_start
;
1107 return cluster_offset
;
1110 static int qcow_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
1111 int nb_sectors
, int *pnum
)
1113 uint64_t cluster_offset
;
1116 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, pnum
);
1118 return (cluster_offset
!= 0);
1121 static int decompress_buffer(uint8_t *out_buf
, int out_buf_size
,
1122 const uint8_t *buf
, int buf_size
)
1124 z_stream strm1
, *strm
= &strm1
;
1127 memset(strm
, 0, sizeof(*strm
));
1129 strm
->next_in
= (uint8_t *)buf
;
1130 strm
->avail_in
= buf_size
;
1131 strm
->next_out
= out_buf
;
1132 strm
->avail_out
= out_buf_size
;
1134 ret
= inflateInit2(strm
, -12);
1137 ret
= inflate(strm
, Z_FINISH
);
1138 out_len
= strm
->next_out
- out_buf
;
1139 if ((ret
!= Z_STREAM_END
&& ret
!= Z_BUF_ERROR
) ||
1140 out_len
!= out_buf_size
) {
1148 static int decompress_cluster(BDRVQcowState
*s
, uint64_t cluster_offset
)
1150 int ret
, csize
, nb_csectors
, sector_offset
;
1153 coffset
= cluster_offset
& s
->cluster_offset_mask
;
1154 if (s
->cluster_cache_offset
!= coffset
) {
1155 nb_csectors
= ((cluster_offset
>> s
->csize_shift
) & s
->csize_mask
) + 1;
1156 sector_offset
= coffset
& 511;
1157 csize
= nb_csectors
* 512 - sector_offset
;
1158 ret
= bdrv_read(s
->hd
, coffset
>> 9, s
->cluster_data
, nb_csectors
);
1162 if (decompress_buffer(s
->cluster_cache
, s
->cluster_size
,
1163 s
->cluster_data
+ sector_offset
, csize
) < 0) {
1166 s
->cluster_cache_offset
= coffset
;
1171 /* handle reading after the end of the backing file */
1172 static int backing_read1(BlockDriverState
*bs
,
1173 int64_t sector_num
, uint8_t *buf
, int nb_sectors
)
1176 if ((sector_num
+ nb_sectors
) <= bs
->total_sectors
)
1178 if (sector_num
>= bs
->total_sectors
)
1181 n1
= bs
->total_sectors
- sector_num
;
1182 memset(buf
+ n1
* 512, 0, 512 * (nb_sectors
- n1
));
1186 static int qcow_read(BlockDriverState
*bs
, int64_t sector_num
,
1187 uint8_t *buf
, int nb_sectors
)
1189 BDRVQcowState
*s
= bs
->opaque
;
1190 int ret
, index_in_cluster
, n
, n1
;
1191 uint64_t cluster_offset
;
1193 while (nb_sectors
> 0) {
1195 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, &n
);
1196 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
1197 if (!cluster_offset
) {
1198 if (bs
->backing_hd
) {
1199 /* read from the base image */
1200 n1
= backing_read1(bs
->backing_hd
, sector_num
, buf
, n
);
1202 ret
= bdrv_read(bs
->backing_hd
, sector_num
, buf
, n1
);
1207 memset(buf
, 0, 512 * n
);
1209 } else if (cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
1210 if (decompress_cluster(s
, cluster_offset
) < 0)
1212 memcpy(buf
, s
->cluster_cache
+ index_in_cluster
* 512, 512 * n
);
1214 ret
= bdrv_pread(s
->hd
, cluster_offset
+ index_in_cluster
* 512, buf
, n
* 512);
1217 if (s
->crypt_method
) {
1218 encrypt_sectors(s
, sector_num
, buf
, buf
, n
, 0,
1219 &s
->aes_decrypt_key
);
1229 static int qcow_write(BlockDriverState
*bs
, int64_t sector_num
,
1230 const uint8_t *buf
, int nb_sectors
)
1232 BDRVQcowState
*s
= bs
->opaque
;
1233 int ret
, index_in_cluster
, n
;
1234 uint64_t cluster_offset
;
1238 while (nb_sectors
> 0) {
1239 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
1240 n_end
= index_in_cluster
+ nb_sectors
;
1241 if (s
->crypt_method
&&
1242 n_end
> QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
)
1243 n_end
= QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
;
1244 cluster_offset
= alloc_cluster_offset(bs
, sector_num
<< 9,
1246 n_end
, &n
, &l2meta
);
1247 if (!cluster_offset
)
1249 if (s
->crypt_method
) {
1250 encrypt_sectors(s
, sector_num
, s
->cluster_data
, buf
, n
, 1,
1251 &s
->aes_encrypt_key
);
1252 ret
= bdrv_pwrite(s
->hd
, cluster_offset
+ index_in_cluster
* 512,
1253 s
->cluster_data
, n
* 512);
1255 ret
= bdrv_pwrite(s
->hd
, cluster_offset
+ index_in_cluster
* 512, buf
, n
* 512);
1257 if (ret
!= n
* 512 || alloc_cluster_link_l2(bs
, cluster_offset
, &l2meta
) < 0) {
1258 free_any_clusters(bs
, cluster_offset
, l2meta
.nb_clusters
);
1265 s
->cluster_cache_offset
= -1; /* disable compressed cache */
1269 typedef struct QCowAIOCB
{
1270 BlockDriverAIOCB common
;
1277 uint64_t cluster_offset
;
1278 uint8_t *cluster_data
;
1279 BlockDriverAIOCB
*hd_aiocb
;
1280 struct iovec hd_iov
;
1281 QEMUIOVector hd_qiov
;
1286 static void qcow_aio_read_cb(void *opaque
, int ret
);
1287 static void qcow_aio_read_bh(void *opaque
)
1289 QCowAIOCB
*acb
= opaque
;
1290 qemu_bh_delete(acb
->bh
);
1292 qcow_aio_read_cb(opaque
, 0);
1295 static int qcow_schedule_bh(QEMUBHFunc
*cb
, QCowAIOCB
*acb
)
1300 acb
->bh
= qemu_bh_new(cb
, acb
);
1304 qemu_bh_schedule(acb
->bh
);
1309 static void qcow_aio_read_cb(void *opaque
, int ret
)
1311 QCowAIOCB
*acb
= opaque
;
1312 BlockDriverState
*bs
= acb
->common
.bs
;
1313 BDRVQcowState
*s
= bs
->opaque
;
1314 int index_in_cluster
, n1
;
1316 acb
->hd_aiocb
= NULL
;
1320 /* post process the read buffer */
1321 if (!acb
->cluster_offset
) {
1323 } else if (acb
->cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
1326 if (s
->crypt_method
) {
1327 encrypt_sectors(s
, acb
->sector_num
, acb
->buf
, acb
->buf
,
1329 &s
->aes_decrypt_key
);
1333 acb
->nb_sectors
-= acb
->n
;
1334 acb
->sector_num
+= acb
->n
;
1335 acb
->buf
+= acb
->n
* 512;
1337 if (acb
->nb_sectors
== 0) {
1338 /* request completed */
1343 /* prepare next AIO request */
1344 acb
->n
= acb
->nb_sectors
;
1345 acb
->cluster_offset
= get_cluster_offset(bs
, acb
->sector_num
<< 9, &acb
->n
);
1346 index_in_cluster
= acb
->sector_num
& (s
->cluster_sectors
- 1);
1348 if (!acb
->cluster_offset
) {
1349 if (bs
->backing_hd
) {
1350 /* read from the base image */
1351 n1
= backing_read1(bs
->backing_hd
, acb
->sector_num
,
1354 acb
->hd_iov
.iov_base
= (void *)acb
->buf
;
1355 acb
->hd_iov
.iov_len
= acb
->n
* 512;
1356 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
1357 acb
->hd_aiocb
= bdrv_aio_readv(bs
->backing_hd
, acb
->sector_num
,
1358 &acb
->hd_qiov
, acb
->n
,
1359 qcow_aio_read_cb
, acb
);
1360 if (acb
->hd_aiocb
== NULL
)
1363 ret
= qcow_schedule_bh(qcow_aio_read_bh
, acb
);
1368 /* Note: in this case, no need to wait */
1369 memset(acb
->buf
, 0, 512 * acb
->n
);
1370 ret
= qcow_schedule_bh(qcow_aio_read_bh
, acb
);
1374 } else if (acb
->cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
1375 /* add AIO support for compressed blocks ? */
1376 if (decompress_cluster(s
, acb
->cluster_offset
) < 0)
1379 s
->cluster_cache
+ index_in_cluster
* 512, 512 * acb
->n
);
1380 ret
= qcow_schedule_bh(qcow_aio_read_bh
, acb
);
1384 if ((acb
->cluster_offset
& 511) != 0) {
1389 acb
->hd_iov
.iov_base
= (void *)acb
->buf
;
1390 acb
->hd_iov
.iov_len
= acb
->n
* 512;
1391 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
1392 acb
->hd_aiocb
= bdrv_aio_readv(s
->hd
,
1393 (acb
->cluster_offset
>> 9) + index_in_cluster
,
1394 &acb
->hd_qiov
, acb
->n
, qcow_aio_read_cb
, acb
);
1395 if (acb
->hd_aiocb
== NULL
)
1401 if (acb
->qiov
->niov
> 1) {
1402 qemu_iovec_from_buffer(acb
->qiov
, acb
->orig_buf
, acb
->qiov
->size
);
1403 qemu_vfree(acb
->orig_buf
);
1405 acb
->common
.cb(acb
->common
.opaque
, ret
);
1406 qemu_aio_release(acb
);
1409 static QCowAIOCB
*qcow_aio_setup(BlockDriverState
*bs
,
1410 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1411 BlockDriverCompletionFunc
*cb
, void *opaque
, int is_write
)
1415 acb
= qemu_aio_get(bs
, cb
, opaque
);
1418 acb
->hd_aiocb
= NULL
;
1419 acb
->sector_num
= sector_num
;
1421 if (qiov
->niov
> 1) {
1422 acb
->buf
= acb
->orig_buf
= qemu_blockalign(bs
, qiov
->size
);
1424 qemu_iovec_to_buffer(qiov
, acb
->buf
);
1426 acb
->buf
= (uint8_t *)qiov
->iov
->iov_base
;
1428 acb
->nb_sectors
= nb_sectors
;
1430 acb
->cluster_offset
= 0;
1431 acb
->l2meta
.nb_clusters
= 0;
1435 static BlockDriverAIOCB
*qcow_aio_readv(BlockDriverState
*bs
,
1436 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1437 BlockDriverCompletionFunc
*cb
, void *opaque
)
1441 acb
= qcow_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
1445 qcow_aio_read_cb(acb
, 0);
1446 return &acb
->common
;
1449 static void qcow_aio_write_cb(void *opaque
, int ret
)
1451 QCowAIOCB
*acb
= opaque
;
1452 BlockDriverState
*bs
= acb
->common
.bs
;
1453 BDRVQcowState
*s
= bs
->opaque
;
1454 int index_in_cluster
;
1455 const uint8_t *src_buf
;
1458 acb
->hd_aiocb
= NULL
;
1463 if (alloc_cluster_link_l2(bs
, acb
->cluster_offset
, &acb
->l2meta
) < 0) {
1464 free_any_clusters(bs
, acb
->cluster_offset
, acb
->l2meta
.nb_clusters
);
1468 acb
->nb_sectors
-= acb
->n
;
1469 acb
->sector_num
+= acb
->n
;
1470 acb
->buf
+= acb
->n
* 512;
1472 if (acb
->nb_sectors
== 0) {
1473 /* request completed */
1478 index_in_cluster
= acb
->sector_num
& (s
->cluster_sectors
- 1);
1479 n_end
= index_in_cluster
+ acb
->nb_sectors
;
1480 if (s
->crypt_method
&&
1481 n_end
> QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
)
1482 n_end
= QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
;
1484 acb
->cluster_offset
= alloc_cluster_offset(bs
, acb
->sector_num
<< 9,
1486 n_end
, &acb
->n
, &acb
->l2meta
);
1487 if (!acb
->cluster_offset
|| (acb
->cluster_offset
& 511) != 0) {
1491 if (s
->crypt_method
) {
1492 if (!acb
->cluster_data
) {
1493 acb
->cluster_data
= qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS
*
1496 encrypt_sectors(s
, acb
->sector_num
, acb
->cluster_data
, acb
->buf
,
1497 acb
->n
, 1, &s
->aes_encrypt_key
);
1498 src_buf
= acb
->cluster_data
;
1502 acb
->hd_iov
.iov_base
= (void *)src_buf
;
1503 acb
->hd_iov
.iov_len
= acb
->n
* 512;
1504 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
1505 acb
->hd_aiocb
= bdrv_aio_writev(s
->hd
,
1506 (acb
->cluster_offset
>> 9) + index_in_cluster
,
1507 &acb
->hd_qiov
, acb
->n
,
1508 qcow_aio_write_cb
, acb
);
1509 if (acb
->hd_aiocb
== NULL
)
1515 if (acb
->qiov
->niov
> 1)
1516 qemu_vfree(acb
->orig_buf
);
1517 acb
->common
.cb(acb
->common
.opaque
, ret
);
1518 qemu_aio_release(acb
);
1521 static BlockDriverAIOCB
*qcow_aio_writev(BlockDriverState
*bs
,
1522 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1523 BlockDriverCompletionFunc
*cb
, void *opaque
)
1525 BDRVQcowState
*s
= bs
->opaque
;
1528 s
->cluster_cache_offset
= -1; /* disable compressed cache */
1530 acb
= qcow_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
1534 qcow_aio_write_cb(acb
, 0);
1535 return &acb
->common
;
1538 static void qcow_aio_cancel(BlockDriverAIOCB
*blockacb
)
1540 QCowAIOCB
*acb
= (QCowAIOCB
*)blockacb
;
1542 bdrv_aio_cancel(acb
->hd_aiocb
);
1543 qemu_aio_release(acb
);
1546 static void qcow_close(BlockDriverState
*bs
)
1548 BDRVQcowState
*s
= bs
->opaque
;
1549 qemu_free(s
->l1_table
);
1550 qemu_free(s
->l2_cache
);
1551 qemu_free(s
->cluster_cache
);
1552 qemu_free(s
->cluster_data
);
1557 /* XXX: use std qcow open function ? */
1558 typedef struct QCowCreateState
{
1561 uint16_t *refcount_block
;
1562 uint64_t *refcount_table
;
1563 int64_t l1_table_offset
;
1564 int64_t refcount_table_offset
;
1565 int64_t refcount_block_offset
;
1568 static void create_refcount_update(QCowCreateState
*s
,
1569 int64_t offset
, int64_t size
)
1572 int64_t start
, last
, cluster_offset
;
1575 start
= offset
& ~(s
->cluster_size
- 1);
1576 last
= (offset
+ size
- 1) & ~(s
->cluster_size
- 1);
1577 for(cluster_offset
= start
; cluster_offset
<= last
;
1578 cluster_offset
+= s
->cluster_size
) {
1579 p
= &s
->refcount_block
[cluster_offset
>> s
->cluster_bits
];
1580 refcount
= be16_to_cpu(*p
);
1582 *p
= cpu_to_be16(refcount
);
1586 static int qcow_create2(const char *filename
, int64_t total_size
,
1587 const char *backing_file
, const char *backing_format
,
1591 int fd
, header_size
, backing_filename_len
, l1_size
, i
, shift
, l2_bits
;
1592 int ref_clusters
, backing_format_len
= 0;
1594 uint64_t tmp
, offset
;
1595 QCowCreateState s1
, *s
= &s1
;
1596 QCowExtension ext_bf
= {0, 0};
1599 memset(s
, 0, sizeof(*s
));
1601 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0644);
1604 memset(&header
, 0, sizeof(header
));
1605 header
.magic
= cpu_to_be32(QCOW_MAGIC
);
1606 header
.version
= cpu_to_be32(QCOW_VERSION
);
1607 header
.size
= cpu_to_be64(total_size
* 512);
1608 header_size
= sizeof(header
);
1609 backing_filename_len
= 0;
1611 if (backing_format
) {
1612 ext_bf
.magic
= QCOW_EXT_MAGIC_BACKING_FORMAT
;
1613 backing_format_len
= strlen(backing_format
);
1614 ext_bf
.len
= (backing_format_len
+ 7) & ~7;
1615 header_size
+= ((sizeof(ext_bf
) + ext_bf
.len
+ 7) & ~7);
1617 header
.backing_file_offset
= cpu_to_be64(header_size
);
1618 backing_filename_len
= strlen(backing_file
);
1619 header
.backing_file_size
= cpu_to_be32(backing_filename_len
);
1620 header_size
+= backing_filename_len
;
1622 s
->cluster_bits
= 12; /* 4 KB clusters */
1623 s
->cluster_size
= 1 << s
->cluster_bits
;
1624 header
.cluster_bits
= cpu_to_be32(s
->cluster_bits
);
1625 header_size
= (header_size
+ 7) & ~7;
1626 if (flags
& BLOCK_FLAG_ENCRYPT
) {
1627 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
1629 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
1631 l2_bits
= s
->cluster_bits
- 3;
1632 shift
= s
->cluster_bits
+ l2_bits
;
1633 l1_size
= (((total_size
* 512) + (1LL << shift
) - 1) >> shift
);
1634 offset
= align_offset(header_size
, s
->cluster_size
);
1635 s
->l1_table_offset
= offset
;
1636 header
.l1_table_offset
= cpu_to_be64(s
->l1_table_offset
);
1637 header
.l1_size
= cpu_to_be32(l1_size
);
1638 offset
+= align_offset(l1_size
* sizeof(uint64_t), s
->cluster_size
);
1640 s
->refcount_table
= qemu_mallocz(s
->cluster_size
);
1642 s
->refcount_table_offset
= offset
;
1643 header
.refcount_table_offset
= cpu_to_be64(offset
);
1644 header
.refcount_table_clusters
= cpu_to_be32(1);
1645 offset
+= s
->cluster_size
;
1646 s
->refcount_block_offset
= offset
;
1648 /* count how many refcount blocks needed */
1649 tmp
= offset
>> s
->cluster_bits
;
1650 ref_clusters
= (tmp
>> (s
->cluster_bits
- REFCOUNT_SHIFT
)) + 1;
1651 for (i
=0; i
< ref_clusters
; i
++) {
1652 s
->refcount_table
[i
] = cpu_to_be64(offset
);
1653 offset
+= s
->cluster_size
;
1656 s
->refcount_block
= qemu_mallocz(ref_clusters
* s
->cluster_size
);
1658 /* update refcounts */
1659 create_refcount_update(s
, 0, header_size
);
1660 create_refcount_update(s
, s
->l1_table_offset
, l1_size
* sizeof(uint64_t));
1661 create_refcount_update(s
, s
->refcount_table_offset
, s
->cluster_size
);
1662 create_refcount_update(s
, s
->refcount_block_offset
, ref_clusters
* s
->cluster_size
);
1664 /* write all the data */
1665 write(fd
, &header
, sizeof(header
));
1667 if (backing_format_len
) {
1669 int d
= ext_bf
.len
- backing_format_len
;
1671 memset(zero
, 0, sizeof(zero
));
1672 cpu_to_be32s(&ext_bf
.magic
);
1673 cpu_to_be32s(&ext_bf
.len
);
1674 write(fd
, &ext_bf
, sizeof(ext_bf
));
1675 write(fd
, backing_format
, backing_format_len
);
1680 write(fd
, backing_file
, backing_filename_len
);
1682 lseek(fd
, s
->l1_table_offset
, SEEK_SET
);
1684 for(i
= 0;i
< l1_size
; i
++) {
1685 write(fd
, &tmp
, sizeof(tmp
));
1687 lseek(fd
, s
->refcount_table_offset
, SEEK_SET
);
1688 write(fd
, s
->refcount_table
, s
->cluster_size
);
1690 lseek(fd
, s
->refcount_block_offset
, SEEK_SET
);
1691 write(fd
, s
->refcount_block
, ref_clusters
* s
->cluster_size
);
1693 qemu_free(s
->refcount_table
);
1694 qemu_free(s
->refcount_block
);
1699 static int qcow_create(const char *filename
, int64_t total_size
,
1700 const char *backing_file
, int flags
)
1702 return qcow_create2(filename
, total_size
, backing_file
, NULL
, flags
);
1705 static int qcow_make_empty(BlockDriverState
*bs
)
1708 /* XXX: not correct */
1709 BDRVQcowState
*s
= bs
->opaque
;
1710 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
1713 memset(s
->l1_table
, 0, l1_length
);
1714 if (bdrv_pwrite(s
->hd
, s
->l1_table_offset
, s
->l1_table
, l1_length
) < 0)
1716 ret
= bdrv_truncate(s
->hd
, s
->l1_table_offset
+ l1_length
);
1725 /* XXX: put compressed sectors first, then all the cluster aligned
1726 tables to avoid losing bytes in alignment */
1727 static int qcow_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1728 const uint8_t *buf
, int nb_sectors
)
1730 BDRVQcowState
*s
= bs
->opaque
;
1734 uint64_t cluster_offset
;
1736 if (nb_sectors
== 0) {
1737 /* align end of file to a sector boundary to ease reading with
1738 sector based I/Os */
1739 cluster_offset
= bdrv_getlength(s
->hd
);
1740 cluster_offset
= (cluster_offset
+ 511) & ~511;
1741 bdrv_truncate(s
->hd
, cluster_offset
);
1745 if (nb_sectors
!= s
->cluster_sectors
)
1748 out_buf
= qemu_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
1750 /* best compression, small window, no zlib header */
1751 memset(&strm
, 0, sizeof(strm
));
1752 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
1754 9, Z_DEFAULT_STRATEGY
);
1760 strm
.avail_in
= s
->cluster_size
;
1761 strm
.next_in
= (uint8_t *)buf
;
1762 strm
.avail_out
= s
->cluster_size
;
1763 strm
.next_out
= out_buf
;
1765 ret
= deflate(&strm
, Z_FINISH
);
1766 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
1771 out_len
= strm
.next_out
- out_buf
;
1775 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
1776 /* could not compress: write normal cluster */
1777 qcow_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
1779 cluster_offset
= alloc_compressed_cluster_offset(bs
, sector_num
<< 9,
1781 if (!cluster_offset
)
1783 cluster_offset
&= s
->cluster_offset_mask
;
1784 if (bdrv_pwrite(s
->hd
, cluster_offset
, out_buf
, out_len
) != out_len
) {
1794 static void qcow_flush(BlockDriverState
*bs
)
1796 BDRVQcowState
*s
= bs
->opaque
;
1800 static int qcow_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1802 BDRVQcowState
*s
= bs
->opaque
;
1803 bdi
->cluster_size
= s
->cluster_size
;
1804 bdi
->vm_state_offset
= (int64_t)s
->l1_vm_state_index
<<
1805 (s
->cluster_bits
+ s
->l2_bits
);
1809 /*********************************************************/
1810 /* snapshot support */
1812 /* update the refcounts of snapshots and the copied flag */
1813 static int update_snapshot_refcount(BlockDriverState
*bs
,
1814 int64_t l1_table_offset
,
1818 BDRVQcowState
*s
= bs
->opaque
;
1819 uint64_t *l1_table
, *l2_table
, l2_offset
, offset
, l1_size2
, l1_allocated
;
1820 int64_t old_offset
, old_l2_offset
;
1821 int l2_size
, i
, j
, l1_modified
, l2_modified
, nb_csectors
, refcount
;
1827 l1_size2
= l1_size
* sizeof(uint64_t);
1829 if (l1_table_offset
!= s
->l1_table_offset
) {
1830 l1_table
= qemu_malloc(l1_size2
);
1832 if (bdrv_pread(s
->hd
, l1_table_offset
,
1833 l1_table
, l1_size2
) != l1_size2
)
1835 for(i
= 0;i
< l1_size
; i
++)
1836 be64_to_cpus(&l1_table
[i
]);
1838 assert(l1_size
== s
->l1_size
);
1839 l1_table
= s
->l1_table
;
1843 l2_size
= s
->l2_size
* sizeof(uint64_t);
1844 l2_table
= qemu_malloc(l2_size
);
1846 for(i
= 0; i
< l1_size
; i
++) {
1847 l2_offset
= l1_table
[i
];
1849 old_l2_offset
= l2_offset
;
1850 l2_offset
&= ~QCOW_OFLAG_COPIED
;
1852 if (bdrv_pread(s
->hd
, l2_offset
, l2_table
, l2_size
) != l2_size
)
1854 for(j
= 0; j
< s
->l2_size
; j
++) {
1855 offset
= be64_to_cpu(l2_table
[j
]);
1857 old_offset
= offset
;
1858 offset
&= ~QCOW_OFLAG_COPIED
;
1859 if (offset
& QCOW_OFLAG_COMPRESSED
) {
1860 nb_csectors
= ((offset
>> s
->csize_shift
) &
1863 update_refcount(bs
, (offset
& s
->cluster_offset_mask
) & ~511,
1864 nb_csectors
* 512, addend
);
1865 /* compressed clusters are never modified */
1869 refcount
= update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, addend
);
1871 refcount
= get_refcount(bs
, offset
>> s
->cluster_bits
);
1875 if (refcount
== 1) {
1876 offset
|= QCOW_OFLAG_COPIED
;
1878 if (offset
!= old_offset
) {
1879 l2_table
[j
] = cpu_to_be64(offset
);
1885 if (bdrv_pwrite(s
->hd
,
1886 l2_offset
, l2_table
, l2_size
) != l2_size
)
1891 refcount
= update_cluster_refcount(bs
, l2_offset
>> s
->cluster_bits
, addend
);
1893 refcount
= get_refcount(bs
, l2_offset
>> s
->cluster_bits
);
1895 if (refcount
== 1) {
1896 l2_offset
|= QCOW_OFLAG_COPIED
;
1898 if (l2_offset
!= old_l2_offset
) {
1899 l1_table
[i
] = l2_offset
;
1905 for(i
= 0; i
< l1_size
; i
++)
1906 cpu_to_be64s(&l1_table
[i
]);
1907 if (bdrv_pwrite(s
->hd
, l1_table_offset
, l1_table
,
1908 l1_size2
) != l1_size2
)
1910 for(i
= 0; i
< l1_size
; i
++)
1911 be64_to_cpus(&l1_table
[i
]);
1914 qemu_free(l1_table
);
1915 qemu_free(l2_table
);
1919 qemu_free(l1_table
);
1920 qemu_free(l2_table
);
1924 static void qcow_free_snapshots(BlockDriverState
*bs
)
1926 BDRVQcowState
*s
= bs
->opaque
;
1929 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1930 qemu_free(s
->snapshots
[i
].name
);
1931 qemu_free(s
->snapshots
[i
].id_str
);
1933 qemu_free(s
->snapshots
);
1934 s
->snapshots
= NULL
;
1935 s
->nb_snapshots
= 0;
1938 static int qcow_read_snapshots(BlockDriverState
*bs
)
1940 BDRVQcowState
*s
= bs
->opaque
;
1941 QCowSnapshotHeader h
;
1943 int i
, id_str_size
, name_size
;
1945 uint32_t extra_data_size
;
1947 if (!s
->nb_snapshots
) {
1948 s
->snapshots
= NULL
;
1949 s
->snapshots_size
= 0;
1953 offset
= s
->snapshots_offset
;
1954 s
->snapshots
= qemu_mallocz(s
->nb_snapshots
* sizeof(QCowSnapshot
));
1955 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1956 offset
= align_offset(offset
, 8);
1957 if (bdrv_pread(s
->hd
, offset
, &h
, sizeof(h
)) != sizeof(h
))
1959 offset
+= sizeof(h
);
1960 sn
= s
->snapshots
+ i
;
1961 sn
->l1_table_offset
= be64_to_cpu(h
.l1_table_offset
);
1962 sn
->l1_size
= be32_to_cpu(h
.l1_size
);
1963 sn
->vm_state_size
= be32_to_cpu(h
.vm_state_size
);
1964 sn
->date_sec
= be32_to_cpu(h
.date_sec
);
1965 sn
->date_nsec
= be32_to_cpu(h
.date_nsec
);
1966 sn
->vm_clock_nsec
= be64_to_cpu(h
.vm_clock_nsec
);
1967 extra_data_size
= be32_to_cpu(h
.extra_data_size
);
1969 id_str_size
= be16_to_cpu(h
.id_str_size
);
1970 name_size
= be16_to_cpu(h
.name_size
);
1972 offset
+= extra_data_size
;
1974 sn
->id_str
= qemu_malloc(id_str_size
+ 1);
1975 if (bdrv_pread(s
->hd
, offset
, sn
->id_str
, id_str_size
) != id_str_size
)
1977 offset
+= id_str_size
;
1978 sn
->id_str
[id_str_size
] = '\0';
1980 sn
->name
= qemu_malloc(name_size
+ 1);
1981 if (bdrv_pread(s
->hd
, offset
, sn
->name
, name_size
) != name_size
)
1983 offset
+= name_size
;
1984 sn
->name
[name_size
] = '\0';
1986 s
->snapshots_size
= offset
- s
->snapshots_offset
;
1989 qcow_free_snapshots(bs
);
1993 /* add at the end of the file a new list of snapshots */
1994 static int qcow_write_snapshots(BlockDriverState
*bs
)
1996 BDRVQcowState
*s
= bs
->opaque
;
1998 QCowSnapshotHeader h
;
1999 int i
, name_size
, id_str_size
, snapshots_size
;
2002 int64_t offset
, snapshots_offset
;
2004 /* compute the size of the snapshots */
2006 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
2007 sn
= s
->snapshots
+ i
;
2008 offset
= align_offset(offset
, 8);
2009 offset
+= sizeof(h
);
2010 offset
+= strlen(sn
->id_str
);
2011 offset
+= strlen(sn
->name
);
2013 snapshots_size
= offset
;
2015 snapshots_offset
= alloc_clusters(bs
, snapshots_size
);
2016 offset
= snapshots_offset
;
2018 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
2019 sn
= s
->snapshots
+ i
;
2020 memset(&h
, 0, sizeof(h
));
2021 h
.l1_table_offset
= cpu_to_be64(sn
->l1_table_offset
);
2022 h
.l1_size
= cpu_to_be32(sn
->l1_size
);
2023 h
.vm_state_size
= cpu_to_be32(sn
->vm_state_size
);
2024 h
.date_sec
= cpu_to_be32(sn
->date_sec
);
2025 h
.date_nsec
= cpu_to_be32(sn
->date_nsec
);
2026 h
.vm_clock_nsec
= cpu_to_be64(sn
->vm_clock_nsec
);
2028 id_str_size
= strlen(sn
->id_str
);
2029 name_size
= strlen(sn
->name
);
2030 h
.id_str_size
= cpu_to_be16(id_str_size
);
2031 h
.name_size
= cpu_to_be16(name_size
);
2032 offset
= align_offset(offset
, 8);
2033 if (bdrv_pwrite(s
->hd
, offset
, &h
, sizeof(h
)) != sizeof(h
))
2035 offset
+= sizeof(h
);
2036 if (bdrv_pwrite(s
->hd
, offset
, sn
->id_str
, id_str_size
) != id_str_size
)
2038 offset
+= id_str_size
;
2039 if (bdrv_pwrite(s
->hd
, offset
, sn
->name
, name_size
) != name_size
)
2041 offset
+= name_size
;
2044 /* update the various header fields */
2045 data64
= cpu_to_be64(snapshots_offset
);
2046 if (bdrv_pwrite(s
->hd
, offsetof(QCowHeader
, snapshots_offset
),
2047 &data64
, sizeof(data64
)) != sizeof(data64
))
2049 data32
= cpu_to_be32(s
->nb_snapshots
);
2050 if (bdrv_pwrite(s
->hd
, offsetof(QCowHeader
, nb_snapshots
),
2051 &data32
, sizeof(data32
)) != sizeof(data32
))
2054 /* free the old snapshot table */
2055 free_clusters(bs
, s
->snapshots_offset
, s
->snapshots_size
);
2056 s
->snapshots_offset
= snapshots_offset
;
2057 s
->snapshots_size
= snapshots_size
;
2063 static void find_new_snapshot_id(BlockDriverState
*bs
,
2064 char *id_str
, int id_str_size
)
2066 BDRVQcowState
*s
= bs
->opaque
;
2068 int i
, id
, id_max
= 0;
2070 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
2071 sn
= s
->snapshots
+ i
;
2072 id
= strtoul(sn
->id_str
, NULL
, 10);
2076 snprintf(id_str
, id_str_size
, "%d", id_max
+ 1);
2079 static int find_snapshot_by_id(BlockDriverState
*bs
, const char *id_str
)
2081 BDRVQcowState
*s
= bs
->opaque
;
2084 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
2085 if (!strcmp(s
->snapshots
[i
].id_str
, id_str
))
2091 static int find_snapshot_by_id_or_name(BlockDriverState
*bs
, const char *name
)
2093 BDRVQcowState
*s
= bs
->opaque
;
2096 ret
= find_snapshot_by_id(bs
, name
);
2099 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
2100 if (!strcmp(s
->snapshots
[i
].name
, name
))
2106 /* if no id is provided, a new one is constructed */
2107 static int qcow_snapshot_create(BlockDriverState
*bs
,
2108 QEMUSnapshotInfo
*sn_info
)
2110 BDRVQcowState
*s
= bs
->opaque
;
2111 QCowSnapshot
*snapshots1
, sn1
, *sn
= &sn1
;
2113 uint64_t *l1_table
= NULL
;
2115 memset(sn
, 0, sizeof(*sn
));
2117 if (sn_info
->id_str
[0] == '\0') {
2118 /* compute a new id */
2119 find_new_snapshot_id(bs
, sn_info
->id_str
, sizeof(sn_info
->id_str
));
2122 /* check that the ID is unique */
2123 if (find_snapshot_by_id(bs
, sn_info
->id_str
) >= 0)
2126 sn
->id_str
= qemu_strdup(sn_info
->id_str
);
2129 sn
->name
= qemu_strdup(sn_info
->name
);
2132 sn
->vm_state_size
= sn_info
->vm_state_size
;
2133 sn
->date_sec
= sn_info
->date_sec
;
2134 sn
->date_nsec
= sn_info
->date_nsec
;
2135 sn
->vm_clock_nsec
= sn_info
->vm_clock_nsec
;
2137 ret
= update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 1);
2141 /* create the L1 table of the snapshot */
2142 sn
->l1_table_offset
= alloc_clusters(bs
, s
->l1_size
* sizeof(uint64_t));
2143 sn
->l1_size
= s
->l1_size
;
2145 l1_table
= qemu_malloc(s
->l1_size
* sizeof(uint64_t));
2146 for(i
= 0; i
< s
->l1_size
; i
++) {
2147 l1_table
[i
] = cpu_to_be64(s
->l1_table
[i
]);
2149 if (bdrv_pwrite(s
->hd
, sn
->l1_table_offset
,
2150 l1_table
, s
->l1_size
* sizeof(uint64_t)) !=
2151 (s
->l1_size
* sizeof(uint64_t)))
2153 qemu_free(l1_table
);
2156 snapshots1
= qemu_malloc((s
->nb_snapshots
+ 1) * sizeof(QCowSnapshot
));
2158 memcpy(snapshots1
, s
->snapshots
, s
->nb_snapshots
* sizeof(QCowSnapshot
));
2159 qemu_free(s
->snapshots
);
2161 s
->snapshots
= snapshots1
;
2162 s
->snapshots
[s
->nb_snapshots
++] = *sn
;
2164 if (qcow_write_snapshots(bs
) < 0)
2167 check_refcounts(bs
);
2171 qemu_free(sn
->name
);
2172 qemu_free(l1_table
);
2176 /* copy the snapshot 'snapshot_name' into the current disk image */
2177 static int qcow_snapshot_goto(BlockDriverState
*bs
,
2178 const char *snapshot_id
)
2180 BDRVQcowState
*s
= bs
->opaque
;
2182 int i
, snapshot_index
, l1_size2
;
2184 snapshot_index
= find_snapshot_by_id_or_name(bs
, snapshot_id
);
2185 if (snapshot_index
< 0)
2187 sn
= &s
->snapshots
[snapshot_index
];
2189 if (update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, -1) < 0)
2192 if (grow_l1_table(bs
, sn
->l1_size
) < 0)
2195 s
->l1_size
= sn
->l1_size
;
2196 l1_size2
= s
->l1_size
* sizeof(uint64_t);
2197 /* copy the snapshot l1 table to the current l1 table */
2198 if (bdrv_pread(s
->hd
, sn
->l1_table_offset
,
2199 s
->l1_table
, l1_size2
) != l1_size2
)
2201 if (bdrv_pwrite(s
->hd
, s
->l1_table_offset
,
2202 s
->l1_table
, l1_size2
) != l1_size2
)
2204 for(i
= 0;i
< s
->l1_size
; i
++) {
2205 be64_to_cpus(&s
->l1_table
[i
]);
2208 if (update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 1) < 0)
2212 check_refcounts(bs
);
2219 static int qcow_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
2221 BDRVQcowState
*s
= bs
->opaque
;
2223 int snapshot_index
, ret
;
2225 snapshot_index
= find_snapshot_by_id_or_name(bs
, snapshot_id
);
2226 if (snapshot_index
< 0)
2228 sn
= &s
->snapshots
[snapshot_index
];
2230 ret
= update_snapshot_refcount(bs
, sn
->l1_table_offset
, sn
->l1_size
, -1);
2233 /* must update the copied flag on the current cluster offsets */
2234 ret
= update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 0);
2237 free_clusters(bs
, sn
->l1_table_offset
, sn
->l1_size
* sizeof(uint64_t));
2239 qemu_free(sn
->id_str
);
2240 qemu_free(sn
->name
);
2241 memmove(sn
, sn
+ 1, (s
->nb_snapshots
- snapshot_index
- 1) * sizeof(*sn
));
2243 ret
= qcow_write_snapshots(bs
);
2245 /* XXX: restore snapshot if error ? */
2249 check_refcounts(bs
);
2254 static int qcow_snapshot_list(BlockDriverState
*bs
,
2255 QEMUSnapshotInfo
**psn_tab
)
2257 BDRVQcowState
*s
= bs
->opaque
;
2258 QEMUSnapshotInfo
*sn_tab
, *sn_info
;
2262 sn_tab
= qemu_mallocz(s
->nb_snapshots
* sizeof(QEMUSnapshotInfo
));
2263 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
2264 sn_info
= sn_tab
+ i
;
2265 sn
= s
->snapshots
+ i
;
2266 pstrcpy(sn_info
->id_str
, sizeof(sn_info
->id_str
),
2268 pstrcpy(sn_info
->name
, sizeof(sn_info
->name
),
2270 sn_info
->vm_state_size
= sn
->vm_state_size
;
2271 sn_info
->date_sec
= sn
->date_sec
;
2272 sn_info
->date_nsec
= sn
->date_nsec
;
2273 sn_info
->vm_clock_nsec
= sn
->vm_clock_nsec
;
2276 return s
->nb_snapshots
;
2279 /*********************************************************/
2280 /* refcount handling */
2282 static int refcount_init(BlockDriverState
*bs
)
2284 BDRVQcowState
*s
= bs
->opaque
;
2285 int ret
, refcount_table_size2
, i
;
2287 s
->refcount_block_cache
= qemu_malloc(s
->cluster_size
);
2288 refcount_table_size2
= s
->refcount_table_size
* sizeof(uint64_t);
2289 s
->refcount_table
= qemu_malloc(refcount_table_size2
);
2290 if (s
->refcount_table_size
> 0) {
2291 ret
= bdrv_pread(s
->hd
, s
->refcount_table_offset
,
2292 s
->refcount_table
, refcount_table_size2
);
2293 if (ret
!= refcount_table_size2
)
2295 for(i
= 0; i
< s
->refcount_table_size
; i
++)
2296 be64_to_cpus(&s
->refcount_table
[i
]);
2303 static void refcount_close(BlockDriverState
*bs
)
2305 BDRVQcowState
*s
= bs
->opaque
;
2306 qemu_free(s
->refcount_block_cache
);
2307 qemu_free(s
->refcount_table
);
2311 static int load_refcount_block(BlockDriverState
*bs
,
2312 int64_t refcount_block_offset
)
2314 BDRVQcowState
*s
= bs
->opaque
;
2316 ret
= bdrv_pread(s
->hd
, refcount_block_offset
, s
->refcount_block_cache
,
2318 if (ret
!= s
->cluster_size
)
2320 s
->refcount_block_cache_offset
= refcount_block_offset
;
2324 static int get_refcount(BlockDriverState
*bs
, int64_t cluster_index
)
2326 BDRVQcowState
*s
= bs
->opaque
;
2327 int refcount_table_index
, block_index
;
2328 int64_t refcount_block_offset
;
2330 refcount_table_index
= cluster_index
>> (s
->cluster_bits
- REFCOUNT_SHIFT
);
2331 if (refcount_table_index
>= s
->refcount_table_size
)
2333 refcount_block_offset
= s
->refcount_table
[refcount_table_index
];
2334 if (!refcount_block_offset
)
2336 if (refcount_block_offset
!= s
->refcount_block_cache_offset
) {
2337 /* better than nothing: return allocated if read error */
2338 if (load_refcount_block(bs
, refcount_block_offset
) < 0)
2341 block_index
= cluster_index
&
2342 ((1 << (s
->cluster_bits
- REFCOUNT_SHIFT
)) - 1);
2343 return be16_to_cpu(s
->refcount_block_cache
[block_index
]);
2346 /* return < 0 if error */
2347 static int64_t alloc_clusters_noref(BlockDriverState
*bs
, int64_t size
)
2349 BDRVQcowState
*s
= bs
->opaque
;
2352 nb_clusters
= size_to_clusters(s
, size
);
2354 for(i
= 0; i
< nb_clusters
; i
++) {
2355 int64_t i
= s
->free_cluster_index
++;
2356 if (get_refcount(bs
, i
) != 0)
2360 printf("alloc_clusters: size=%lld -> %lld\n",
2362 (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
);
2364 return (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
;
2367 static int64_t alloc_clusters(BlockDriverState
*bs
, int64_t size
)
2371 offset
= alloc_clusters_noref(bs
, size
);
2372 update_refcount(bs
, offset
, size
, 1);
2376 /* only used to allocate compressed sectors. We try to allocate
2377 contiguous sectors. size must be <= cluster_size */
2378 static int64_t alloc_bytes(BlockDriverState
*bs
, int size
)
2380 BDRVQcowState
*s
= bs
->opaque
;
2381 int64_t offset
, cluster_offset
;
2382 int free_in_cluster
;
2384 assert(size
> 0 && size
<= s
->cluster_size
);
2385 if (s
->free_byte_offset
== 0) {
2386 s
->free_byte_offset
= alloc_clusters(bs
, s
->cluster_size
);
2389 free_in_cluster
= s
->cluster_size
-
2390 (s
->free_byte_offset
& (s
->cluster_size
- 1));
2391 if (size
<= free_in_cluster
) {
2392 /* enough space in current cluster */
2393 offset
= s
->free_byte_offset
;
2394 s
->free_byte_offset
+= size
;
2395 free_in_cluster
-= size
;
2396 if (free_in_cluster
== 0)
2397 s
->free_byte_offset
= 0;
2398 if ((offset
& (s
->cluster_size
- 1)) != 0)
2399 update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, 1);
2401 offset
= alloc_clusters(bs
, s
->cluster_size
);
2402 cluster_offset
= s
->free_byte_offset
& ~(s
->cluster_size
- 1);
2403 if ((cluster_offset
+ s
->cluster_size
) == offset
) {
2404 /* we are lucky: contiguous data */
2405 offset
= s
->free_byte_offset
;
2406 update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, 1);
2407 s
->free_byte_offset
+= size
;
2409 s
->free_byte_offset
= offset
;
2416 static void free_clusters(BlockDriverState
*bs
,
2417 int64_t offset
, int64_t size
)
2419 update_refcount(bs
, offset
, size
, -1);
2422 static int grow_refcount_table(BlockDriverState
*bs
, int min_size
)
2424 BDRVQcowState
*s
= bs
->opaque
;
2425 int new_table_size
, new_table_size2
, refcount_table_clusters
, i
, ret
;
2426 uint64_t *new_table
;
2427 int64_t table_offset
;
2430 int64_t old_table_offset
;
2432 if (min_size
<= s
->refcount_table_size
)
2434 /* compute new table size */
2435 refcount_table_clusters
= s
->refcount_table_size
>> (s
->cluster_bits
- 3);
2437 if (refcount_table_clusters
== 0) {
2438 refcount_table_clusters
= 1;
2440 refcount_table_clusters
= (refcount_table_clusters
* 3 + 1) / 2;
2442 new_table_size
= refcount_table_clusters
<< (s
->cluster_bits
- 3);
2443 if (min_size
<= new_table_size
)
2447 printf("grow_refcount_table from %d to %d\n",
2448 s
->refcount_table_size
,
2451 new_table_size2
= new_table_size
* sizeof(uint64_t);
2452 new_table
= qemu_mallocz(new_table_size2
);
2453 memcpy(new_table
, s
->refcount_table
,
2454 s
->refcount_table_size
* sizeof(uint64_t));
2455 for(i
= 0; i
< s
->refcount_table_size
; i
++)
2456 cpu_to_be64s(&new_table
[i
]);
2457 /* Note: we cannot update the refcount now to avoid recursion */
2458 table_offset
= alloc_clusters_noref(bs
, new_table_size2
);
2459 ret
= bdrv_pwrite(s
->hd
, table_offset
, new_table
, new_table_size2
);
2460 if (ret
!= new_table_size2
)
2462 for(i
= 0; i
< s
->refcount_table_size
; i
++)
2463 be64_to_cpus(&new_table
[i
]);
2465 cpu_to_be64w((uint64_t*)data
, table_offset
);
2466 cpu_to_be32w((uint32_t*)(data
+ 8), refcount_table_clusters
);
2467 if (bdrv_pwrite(s
->hd
, offsetof(QCowHeader
, refcount_table_offset
),
2468 data
, sizeof(data
)) != sizeof(data
))
2470 qemu_free(s
->refcount_table
);
2471 old_table_offset
= s
->refcount_table_offset
;
2472 old_table_size
= s
->refcount_table_size
;
2473 s
->refcount_table
= new_table
;
2474 s
->refcount_table_size
= new_table_size
;
2475 s
->refcount_table_offset
= table_offset
;
2477 update_refcount(bs
, table_offset
, new_table_size2
, 1);
2478 free_clusters(bs
, old_table_offset
, old_table_size
* sizeof(uint64_t));
2481 free_clusters(bs
, table_offset
, new_table_size2
);
2482 qemu_free(new_table
);
2486 /* addend must be 1 or -1 */
2487 /* XXX: cache several refcount block clusters ? */
2488 static int update_cluster_refcount(BlockDriverState
*bs
,
2489 int64_t cluster_index
,
2492 BDRVQcowState
*s
= bs
->opaque
;
2493 int64_t offset
, refcount_block_offset
;
2494 int ret
, refcount_table_index
, block_index
, refcount
;
2497 refcount_table_index
= cluster_index
>> (s
->cluster_bits
- REFCOUNT_SHIFT
);
2498 if (refcount_table_index
>= s
->refcount_table_size
) {
2501 ret
= grow_refcount_table(bs
, refcount_table_index
+ 1);
2505 refcount_block_offset
= s
->refcount_table
[refcount_table_index
];
2506 if (!refcount_block_offset
) {
2509 /* create a new refcount block */
2510 /* Note: we cannot update the refcount now to avoid recursion */
2511 offset
= alloc_clusters_noref(bs
, s
->cluster_size
);
2512 memset(s
->refcount_block_cache
, 0, s
->cluster_size
);
2513 ret
= bdrv_pwrite(s
->hd
, offset
, s
->refcount_block_cache
, s
->cluster_size
);
2514 if (ret
!= s
->cluster_size
)
2516 s
->refcount_table
[refcount_table_index
] = offset
;
2517 data64
= cpu_to_be64(offset
);
2518 ret
= bdrv_pwrite(s
->hd
, s
->refcount_table_offset
+
2519 refcount_table_index
* sizeof(uint64_t),
2520 &data64
, sizeof(data64
));
2521 if (ret
!= sizeof(data64
))
2524 refcount_block_offset
= offset
;
2525 s
->refcount_block_cache_offset
= offset
;
2526 update_refcount(bs
, offset
, s
->cluster_size
, 1);
2528 if (refcount_block_offset
!= s
->refcount_block_cache_offset
) {
2529 if (load_refcount_block(bs
, refcount_block_offset
) < 0)
2533 /* we can update the count and save it */
2534 block_index
= cluster_index
&
2535 ((1 << (s
->cluster_bits
- REFCOUNT_SHIFT
)) - 1);
2536 refcount
= be16_to_cpu(s
->refcount_block_cache
[block_index
]);
2538 if (refcount
< 0 || refcount
> 0xffff)
2540 if (refcount
== 0 && cluster_index
< s
->free_cluster_index
) {
2541 s
->free_cluster_index
= cluster_index
;
2543 s
->refcount_block_cache
[block_index
] = cpu_to_be16(refcount
);
2544 if (bdrv_pwrite(s
->hd
,
2545 refcount_block_offset
+ (block_index
<< REFCOUNT_SHIFT
),
2546 &s
->refcount_block_cache
[block_index
], 2) != 2)
2551 static void update_refcount(BlockDriverState
*bs
,
2552 int64_t offset
, int64_t length
,
2555 BDRVQcowState
*s
= bs
->opaque
;
2556 int64_t start
, last
, cluster_offset
;
2559 printf("update_refcount: offset=%lld size=%lld addend=%d\n",
2560 offset
, length
, addend
);
2564 start
= offset
& ~(s
->cluster_size
- 1);
2565 last
= (offset
+ length
- 1) & ~(s
->cluster_size
- 1);
2566 for(cluster_offset
= start
; cluster_offset
<= last
;
2567 cluster_offset
+= s
->cluster_size
) {
2568 update_cluster_refcount(bs
, cluster_offset
>> s
->cluster_bits
, addend
);
2573 * Increases the refcount for a range of clusters in a given refcount table.
2574 * This is used to construct a temporary refcount table out of L1 and L2 tables
2575 * which can be compared the the refcount table saved in the image.
2577 * Returns the number of errors in the image that were found
2579 static int inc_refcounts(BlockDriverState
*bs
,
2580 uint16_t *refcount_table
,
2581 int refcount_table_size
,
2582 int64_t offset
, int64_t size
)
2584 BDRVQcowState
*s
= bs
->opaque
;
2585 int64_t start
, last
, cluster_offset
;
2592 start
= offset
& ~(s
->cluster_size
- 1);
2593 last
= (offset
+ size
- 1) & ~(s
->cluster_size
- 1);
2594 for(cluster_offset
= start
; cluster_offset
<= last
;
2595 cluster_offset
+= s
->cluster_size
) {
2596 k
= cluster_offset
>> s
->cluster_bits
;
2597 if (k
< 0 || k
>= refcount_table_size
) {
2598 fprintf(stderr
, "ERROR: invalid cluster offset=0x%" PRIx64
"\n",
2602 if (++refcount_table
[k
] == 0) {
2603 fprintf(stderr
, "ERROR: overflow cluster offset=0x%" PRIx64
2604 "\n", cluster_offset
);
2614 * Increases the refcount in the given refcount table for the all clusters
2615 * referenced in the L2 table. While doing so, performs some checks on L2
2618 * Returns the number of errors found by the checks or -errno if an internal
2621 static int check_refcounts_l2(BlockDriverState
*bs
,
2622 uint16_t *refcount_table
, int refcount_table_size
, int64_t l2_offset
,
2625 BDRVQcowState
*s
= bs
->opaque
;
2626 uint64_t *l2_table
, offset
;
2627 int i
, l2_size
, nb_csectors
, refcount
;
2630 /* Read L2 table from disk */
2631 l2_size
= s
->l2_size
* sizeof(uint64_t);
2632 l2_table
= qemu_malloc(l2_size
);
2634 if (bdrv_pread(s
->hd
, l2_offset
, l2_table
, l2_size
) != l2_size
)
2637 /* Do the actual checks */
2638 for(i
= 0; i
< s
->l2_size
; i
++) {
2639 offset
= be64_to_cpu(l2_table
[i
]);
2641 if (offset
& QCOW_OFLAG_COMPRESSED
) {
2642 /* Compressed clusters don't have QCOW_OFLAG_COPIED */
2643 if (offset
& QCOW_OFLAG_COPIED
) {
2644 fprintf(stderr
, "ERROR: cluster %" PRId64
": "
2645 "copied flag must never be set for compressed "
2646 "clusters\n", offset
>> s
->cluster_bits
);
2647 offset
&= ~QCOW_OFLAG_COPIED
;
2651 /* Mark cluster as used */
2652 nb_csectors
= ((offset
>> s
->csize_shift
) &
2654 offset
&= s
->cluster_offset_mask
;
2655 errors
+= inc_refcounts(bs
, refcount_table
,
2656 refcount_table_size
,
2657 offset
& ~511, nb_csectors
* 512);
2659 /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
2661 uint64_t entry
= offset
;
2662 offset
&= ~QCOW_OFLAG_COPIED
;
2663 refcount
= get_refcount(bs
, offset
>> s
->cluster_bits
);
2664 if ((refcount
== 1) != ((entry
& QCOW_OFLAG_COPIED
) != 0)) {
2665 fprintf(stderr
, "ERROR OFLAG_COPIED: offset=%"
2666 PRIx64
" refcount=%d\n", entry
, refcount
);
2671 /* Mark cluster as used */
2672 offset
&= ~QCOW_OFLAG_COPIED
;
2673 errors
+= inc_refcounts(bs
, refcount_table
,
2674 refcount_table_size
,
2675 offset
, s
->cluster_size
);
2677 /* Correct offsets are cluster aligned */
2678 if (offset
& (s
->cluster_size
- 1)) {
2679 fprintf(stderr
, "ERROR offset=%" PRIx64
": Cluster is not "
2680 "properly aligned; L2 entry corrupted.\n", offset
);
2687 qemu_free(l2_table
);
2691 fprintf(stderr
, "ERROR: I/O error in check_refcounts_l1\n");
2692 qemu_free(l2_table
);
2697 * Increases the refcount for the L1 table, its L2 tables and all referenced
2698 * clusters in the given refcount table. While doing so, performs some checks
2699 * on L1 and L2 entries.
2701 * Returns the number of errors found by the checks or -errno if an internal
2704 static int check_refcounts_l1(BlockDriverState
*bs
,
2705 uint16_t *refcount_table
,
2706 int refcount_table_size
,
2707 int64_t l1_table_offset
, int l1_size
,
2710 BDRVQcowState
*s
= bs
->opaque
;
2711 uint64_t *l1_table
, l2_offset
, l1_size2
;
2712 int i
, refcount
, ret
;
2715 l1_size2
= l1_size
* sizeof(uint64_t);
2717 /* Mark L1 table as used */
2718 errors
+= inc_refcounts(bs
, refcount_table
, refcount_table_size
,
2719 l1_table_offset
, l1_size2
);
2721 /* Read L1 table entries from disk */
2722 l1_table
= qemu_malloc(l1_size2
);
2723 if (bdrv_pread(s
->hd
, l1_table_offset
,
2724 l1_table
, l1_size2
) != l1_size2
)
2726 for(i
= 0;i
< l1_size
; i
++)
2727 be64_to_cpus(&l1_table
[i
]);
2729 /* Do the actual checks */
2730 for(i
= 0; i
< l1_size
; i
++) {
2731 l2_offset
= l1_table
[i
];
2733 /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
2735 refcount
= get_refcount(bs
, (l2_offset
& ~QCOW_OFLAG_COPIED
)
2736 >> s
->cluster_bits
);
2737 if ((refcount
== 1) != ((l2_offset
& QCOW_OFLAG_COPIED
) != 0)) {
2738 fprintf(stderr
, "ERROR OFLAG_COPIED: l2_offset=%" PRIx64
2739 " refcount=%d\n", l2_offset
, refcount
);
2744 /* Mark L2 table as used */
2745 l2_offset
&= ~QCOW_OFLAG_COPIED
;
2746 errors
+= inc_refcounts(bs
, refcount_table
,
2747 refcount_table_size
,
2751 /* L2 tables are cluster aligned */
2752 if (l2_offset
& (s
->cluster_size
- 1)) {
2753 fprintf(stderr
, "ERROR l2_offset=%" PRIx64
": Table is not "
2754 "cluster aligned; L1 entry corrupted\n", l2_offset
);
2758 /* Process and check L2 entries */
2759 ret
= check_refcounts_l2(bs
, refcount_table
, refcount_table_size
,
2760 l2_offset
, check_copied
);
2767 qemu_free(l1_table
);
2771 fprintf(stderr
, "ERROR: I/O error in check_refcounts_l1\n");
2772 qemu_free(l1_table
);
2777 * Checks an image for refcount consistency.
2779 * Returns 0 if no errors are found, the number of errors in case the image is
2780 * detected as corrupted, and -errno when an internal error occured.
2782 static int check_refcounts(BlockDriverState
*bs
)
2784 BDRVQcowState
*s
= bs
->opaque
;
2786 int nb_clusters
, refcount1
, refcount2
, i
;
2788 uint16_t *refcount_table
;
2789 int ret
, errors
= 0;
2791 size
= bdrv_getlength(s
->hd
);
2792 nb_clusters
= size_to_clusters(s
, size
);
2793 refcount_table
= qemu_mallocz(nb_clusters
* sizeof(uint16_t));
2796 errors
+= inc_refcounts(bs
, refcount_table
, nb_clusters
,
2797 0, s
->cluster_size
);
2799 /* current L1 table */
2800 ret
= check_refcounts_l1(bs
, refcount_table
, nb_clusters
,
2801 s
->l1_table_offset
, s
->l1_size
, 1);
2808 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
2809 sn
= s
->snapshots
+ i
;
2810 check_refcounts_l1(bs
, refcount_table
, nb_clusters
,
2811 sn
->l1_table_offset
, sn
->l1_size
, 0);
2813 errors
+= inc_refcounts(bs
, refcount_table
, nb_clusters
,
2814 s
->snapshots_offset
, s
->snapshots_size
);
2817 errors
+= inc_refcounts(bs
, refcount_table
, nb_clusters
,
2818 s
->refcount_table_offset
,
2819 s
->refcount_table_size
* sizeof(uint64_t));
2820 for(i
= 0; i
< s
->refcount_table_size
; i
++) {
2822 offset
= s
->refcount_table
[i
];
2824 errors
+= inc_refcounts(bs
, refcount_table
, nb_clusters
,
2825 offset
, s
->cluster_size
);
2829 /* compare ref counts */
2830 for(i
= 0; i
< nb_clusters
; i
++) {
2831 refcount1
= get_refcount(bs
, i
);
2832 refcount2
= refcount_table
[i
];
2833 if (refcount1
!= refcount2
) {
2834 fprintf(stderr
, "ERROR cluster %d refcount=%d reference=%d\n",
2835 i
, refcount1
, refcount2
);
2840 qemu_free(refcount_table
);
2845 static int qcow_check(BlockDriverState
*bs
)
2847 return check_refcounts(bs
);
2851 static void dump_refcounts(BlockDriverState
*bs
)
2853 BDRVQcowState
*s
= bs
->opaque
;
2854 int64_t nb_clusters
, k
, k1
, size
;
2857 size
= bdrv_getlength(s
->hd
);
2858 nb_clusters
= size_to_clusters(s
, size
);
2859 for(k
= 0; k
< nb_clusters
;) {
2861 refcount
= get_refcount(bs
, k
);
2863 while (k
< nb_clusters
&& get_refcount(bs
, k
) == refcount
)
2865 printf("%lld: refcount=%d nb=%lld\n", k
, refcount
, k
- k1
);
2870 static int qcow_put_buffer(BlockDriverState
*bs
, const uint8_t *buf
,
2871 int64_t pos
, int size
)
2873 int growable
= bs
->growable
;
2876 bdrv_pwrite(bs
, pos
, buf
, size
);
2877 bs
->growable
= growable
;
2882 static int qcow_get_buffer(BlockDriverState
*bs
, uint8_t *buf
,
2883 int64_t pos
, int size
)
2885 int growable
= bs
->growable
;
2889 ret
= bdrv_pread(bs
, pos
, buf
, size
);
2890 bs
->growable
= growable
;
2895 BlockDriver bdrv_qcow2
= {
2896 .format_name
= "qcow2",
2897 .instance_size
= sizeof(BDRVQcowState
),
2898 .bdrv_probe
= qcow_probe
,
2899 .bdrv_open
= qcow_open
,
2900 .bdrv_close
= qcow_close
,
2901 .bdrv_create
= qcow_create
,
2902 .bdrv_flush
= qcow_flush
,
2903 .bdrv_is_allocated
= qcow_is_allocated
,
2904 .bdrv_set_key
= qcow_set_key
,
2905 .bdrv_make_empty
= qcow_make_empty
,
2907 .bdrv_aio_readv
= qcow_aio_readv
,
2908 .bdrv_aio_writev
= qcow_aio_writev
,
2909 .bdrv_aio_cancel
= qcow_aio_cancel
,
2910 .aiocb_size
= sizeof(QCowAIOCB
),
2911 .bdrv_write_compressed
= qcow_write_compressed
,
2913 .bdrv_snapshot_create
= qcow_snapshot_create
,
2914 .bdrv_snapshot_goto
= qcow_snapshot_goto
,
2915 .bdrv_snapshot_delete
= qcow_snapshot_delete
,
2916 .bdrv_snapshot_list
= qcow_snapshot_list
,
2917 .bdrv_get_info
= qcow_get_info
,
2919 .bdrv_put_buffer
= qcow_put_buffer
,
2920 .bdrv_get_buffer
= qcow_get_buffer
,
2922 .bdrv_create2
= qcow_create2
,
2923 .bdrv_check
= qcow_check
,