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"
29 #include "block/qcow2.h"
32 Differences with QCOW:
34 - Support for multiple incremental snapshots.
35 - Memory management by reference counts.
36 - Clusters which have a reference count of one have the bit
37 QCOW_OFLAG_COPIED to optimize write performance.
38 - Size of compressed clusters is stored in sectors to reduce bit usage
39 in the cluster offsets.
40 - Support for storing additional data (such as the VM state) in the
42 - If a backing store is used, the cluster size is not constrained
43 (could be backported to QCOW).
44 - L2 tables have always a size of one cluster.
48 //#define DEBUG_ALLOC2
56 #define QCOW_EXT_MAGIC_END 0
57 #define QCOW_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
60 typedef struct __attribute__((packed
)) QCowSnapshotHeader
{
61 /* header is 8 byte aligned */
62 uint64_t l1_table_offset
;
71 uint64_t vm_clock_nsec
;
73 uint32_t vm_state_size
;
74 uint32_t extra_data_size
; /* for extension */
75 /* extra data follows */
81 static int decompress_cluster(BDRVQcowState
*s
, uint64_t cluster_offset
);
82 static int qcow_read(BlockDriverState
*bs
, int64_t sector_num
,
83 uint8_t *buf
, int nb_sectors
);
84 static int qcow_read_snapshots(BlockDriverState
*bs
);
85 static void qcow_free_snapshots(BlockDriverState
*bs
);
87 static int qcow_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
89 const QCowHeader
*cow_header
= (const void *)buf
;
91 if (buf_size
>= sizeof(QCowHeader
) &&
92 be32_to_cpu(cow_header
->magic
) == QCOW_MAGIC
&&
93 be32_to_cpu(cow_header
->version
) == QCOW_VERSION
)
101 * read qcow2 extension and fill bs
102 * start reading from start_offset
103 * finish reading upon magic of value 0 or when end_offset reached
104 * unknown magic is skipped (future extension this version knows nothing about)
105 * return 0 upon success, non-0 otherwise
107 static int qcow_read_extensions(BlockDriverState
*bs
, uint64_t start_offset
,
110 BDRVQcowState
*s
= bs
->opaque
;
115 printf("qcow_read_extensions: start=%ld end=%ld\n", start_offset
, end_offset
);
117 offset
= start_offset
;
118 while (offset
< end_offset
) {
122 if (offset
> s
->cluster_size
)
123 printf("qcow_handle_extension: suspicious offset %lu\n", offset
);
125 printf("attemting to read extended header in offset %lu\n", offset
);
128 if (bdrv_pread(s
->hd
, offset
, &ext
, sizeof(ext
)) != sizeof(ext
)) {
129 fprintf(stderr
, "qcow_handle_extension: ERROR: pread fail from offset %llu\n",
130 (unsigned long long)offset
);
133 be32_to_cpus(&ext
.magic
);
134 be32_to_cpus(&ext
.len
);
135 offset
+= sizeof(ext
);
137 printf("ext.magic = 0x%x\n", ext
.magic
);
140 case QCOW_EXT_MAGIC_END
:
143 case QCOW_EXT_MAGIC_BACKING_FORMAT
:
144 if (ext
.len
>= sizeof(bs
->backing_format
)) {
145 fprintf(stderr
, "ERROR: ext_backing_format: len=%u too large"
147 ext
.len
, sizeof(bs
->backing_format
));
150 if (bdrv_pread(s
->hd
, offset
, bs
->backing_format
,
153 bs
->backing_format
[ext
.len
] = '\0';
155 printf("Qcow2: Got format extension %s\n", bs
->backing_format
);
157 offset
+= ((ext
.len
+ 7) & ~7);
161 /* unknown magic -- just skip it */
162 offset
+= ((ext
.len
+ 7) & ~7);
171 static int qcow_open(BlockDriverState
*bs
, const char *filename
, int flags
)
173 BDRVQcowState
*s
= bs
->opaque
;
174 int len
, i
, shift
, ret
;
178 /* Performance is terrible right now with cache=writethrough due mainly
179 * to reference count updates. If the user does not explicitly specify
180 * a caching type, force to writeback caching.
182 if ((flags
& BDRV_O_CACHE_DEF
)) {
183 flags
|= BDRV_O_CACHE_WB
;
184 flags
&= ~BDRV_O_CACHE_DEF
;
186 ret
= bdrv_file_open(&s
->hd
, filename
, flags
);
189 if (bdrv_pread(s
->hd
, 0, &header
, sizeof(header
)) != sizeof(header
))
191 be32_to_cpus(&header
.magic
);
192 be32_to_cpus(&header
.version
);
193 be64_to_cpus(&header
.backing_file_offset
);
194 be32_to_cpus(&header
.backing_file_size
);
195 be64_to_cpus(&header
.size
);
196 be32_to_cpus(&header
.cluster_bits
);
197 be32_to_cpus(&header
.crypt_method
);
198 be64_to_cpus(&header
.l1_table_offset
);
199 be32_to_cpus(&header
.l1_size
);
200 be64_to_cpus(&header
.refcount_table_offset
);
201 be32_to_cpus(&header
.refcount_table_clusters
);
202 be64_to_cpus(&header
.snapshots_offset
);
203 be32_to_cpus(&header
.nb_snapshots
);
205 if (header
.magic
!= QCOW_MAGIC
|| header
.version
!= QCOW_VERSION
)
207 if (header
.size
<= 1 ||
208 header
.cluster_bits
< MIN_CLUSTER_BITS
||
209 header
.cluster_bits
> MAX_CLUSTER_BITS
)
211 if (header
.crypt_method
> QCOW_CRYPT_AES
)
213 s
->crypt_method_header
= header
.crypt_method
;
214 if (s
->crypt_method_header
)
216 s
->cluster_bits
= header
.cluster_bits
;
217 s
->cluster_size
= 1 << s
->cluster_bits
;
218 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
219 s
->l2_bits
= s
->cluster_bits
- 3; /* L2 is always one cluster */
220 s
->l2_size
= 1 << s
->l2_bits
;
221 bs
->total_sectors
= header
.size
/ 512;
222 s
->csize_shift
= (62 - (s
->cluster_bits
- 8));
223 s
->csize_mask
= (1 << (s
->cluster_bits
- 8)) - 1;
224 s
->cluster_offset_mask
= (1LL << s
->csize_shift
) - 1;
225 s
->refcount_table_offset
= header
.refcount_table_offset
;
226 s
->refcount_table_size
=
227 header
.refcount_table_clusters
<< (s
->cluster_bits
- 3);
229 s
->snapshots_offset
= header
.snapshots_offset
;
230 s
->nb_snapshots
= header
.nb_snapshots
;
232 /* read the level 1 table */
233 s
->l1_size
= header
.l1_size
;
234 shift
= s
->cluster_bits
+ s
->l2_bits
;
235 s
->l1_vm_state_index
= (header
.size
+ (1LL << shift
) - 1) >> shift
;
236 /* the L1 table must contain at least enough entries to put
238 if (s
->l1_size
< s
->l1_vm_state_index
)
240 s
->l1_table_offset
= header
.l1_table_offset
;
241 s
->l1_table
= qemu_malloc(s
->l1_size
* sizeof(uint64_t));
242 if (bdrv_pread(s
->hd
, s
->l1_table_offset
, s
->l1_table
, s
->l1_size
* sizeof(uint64_t)) !=
243 s
->l1_size
* sizeof(uint64_t))
245 for(i
= 0;i
< s
->l1_size
; i
++) {
246 be64_to_cpus(&s
->l1_table
[i
]);
249 s
->l2_cache
= qemu_malloc(s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
250 s
->cluster_cache
= qemu_malloc(s
->cluster_size
);
251 /* one more sector for decompressed data alignment */
252 s
->cluster_data
= qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
254 s
->cluster_cache_offset
= -1;
256 if (refcount_init(bs
) < 0)
259 /* read qcow2 extensions */
260 if (header
.backing_file_offset
)
261 ext_end
= header
.backing_file_offset
;
263 ext_end
= s
->cluster_size
;
264 if (qcow_read_extensions(bs
, sizeof(header
), ext_end
))
267 /* read the backing file name */
268 if (header
.backing_file_offset
!= 0) {
269 len
= header
.backing_file_size
;
272 if (bdrv_pread(s
->hd
, header
.backing_file_offset
, bs
->backing_file
, len
) != len
)
274 bs
->backing_file
[len
] = '\0';
276 if (qcow_read_snapshots(bs
) < 0)
285 qcow_free_snapshots(bs
);
287 qemu_free(s
->l1_table
);
288 qemu_free(s
->l2_cache
);
289 qemu_free(s
->cluster_cache
);
290 qemu_free(s
->cluster_data
);
295 static int qcow_set_key(BlockDriverState
*bs
, const char *key
)
297 BDRVQcowState
*s
= bs
->opaque
;
301 memset(keybuf
, 0, 16);
305 /* XXX: we could compress the chars to 7 bits to increase
307 for(i
= 0;i
< len
;i
++) {
310 s
->crypt_method
= s
->crypt_method_header
;
312 if (AES_set_encrypt_key(keybuf
, 128, &s
->aes_encrypt_key
) != 0)
314 if (AES_set_decrypt_key(keybuf
, 128, &s
->aes_decrypt_key
) != 0)
324 AES_encrypt(in
, tmp
, &s
->aes_encrypt_key
);
325 AES_decrypt(tmp
, out
, &s
->aes_decrypt_key
);
326 for(i
= 0; i
< 16; i
++)
327 printf(" %02x", tmp
[i
]);
329 for(i
= 0; i
< 16; i
++)
330 printf(" %02x", out
[i
]);
337 /* The crypt function is compatible with the linux cryptoloop
338 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
340 static void encrypt_sectors(BDRVQcowState
*s
, int64_t sector_num
,
341 uint8_t *out_buf
, const uint8_t *in_buf
,
342 int nb_sectors
, int enc
,
351 for(i
= 0; i
< nb_sectors
; i
++) {
352 ivec
.ll
[0] = cpu_to_le64(sector_num
);
354 AES_cbc_encrypt(in_buf
, out_buf
, 512, key
,
362 static int copy_sectors(BlockDriverState
*bs
, uint64_t start_sect
,
363 uint64_t cluster_offset
, int n_start
, int n_end
)
365 BDRVQcowState
*s
= bs
->opaque
;
371 ret
= qcow_read(bs
, start_sect
+ n_start
, s
->cluster_data
, n
);
374 if (s
->crypt_method
) {
375 encrypt_sectors(s
, start_sect
+ n_start
,
377 s
->cluster_data
, n
, 1,
378 &s
->aes_encrypt_key
);
380 ret
= bdrv_write(s
->hd
, (cluster_offset
>> 9) + n_start
,
387 void l2_cache_reset(BlockDriverState
*bs
)
389 BDRVQcowState
*s
= bs
->opaque
;
391 memset(s
->l2_cache
, 0, s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
392 memset(s
->l2_cache_offsets
, 0, L2_CACHE_SIZE
* sizeof(uint64_t));
393 memset(s
->l2_cache_counts
, 0, L2_CACHE_SIZE
* sizeof(uint32_t));
396 static inline int l2_cache_new_entry(BlockDriverState
*bs
)
398 BDRVQcowState
*s
= bs
->opaque
;
402 /* find a new entry in the least used one */
404 min_count
= 0xffffffff;
405 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
406 if (s
->l2_cache_counts
[i
] < min_count
) {
407 min_count
= s
->l2_cache_counts
[i
];
414 static int64_t align_offset(int64_t offset
, int n
)
416 offset
= (offset
+ n
- 1) & ~(n
- 1);
420 static int grow_l1_table(BlockDriverState
*bs
, int min_size
)
422 BDRVQcowState
*s
= bs
->opaque
;
423 int new_l1_size
, new_l1_size2
, ret
, i
;
424 uint64_t *new_l1_table
;
425 uint64_t new_l1_table_offset
;
428 new_l1_size
= s
->l1_size
;
429 if (min_size
<= new_l1_size
)
431 while (min_size
> new_l1_size
) {
432 new_l1_size
= (new_l1_size
* 3 + 1) / 2;
435 printf("grow l1_table from %d to %d\n", s
->l1_size
, new_l1_size
);
438 new_l1_size2
= sizeof(uint64_t) * new_l1_size
;
439 new_l1_table
= qemu_mallocz(new_l1_size2
);
440 memcpy(new_l1_table
, s
->l1_table
, s
->l1_size
* sizeof(uint64_t));
442 /* write new table (align to cluster) */
443 new_l1_table_offset
= alloc_clusters(bs
, new_l1_size2
);
445 for(i
= 0; i
< s
->l1_size
; i
++)
446 new_l1_table
[i
] = cpu_to_be64(new_l1_table
[i
]);
447 ret
= bdrv_pwrite(s
->hd
, new_l1_table_offset
, new_l1_table
, new_l1_size2
);
448 if (ret
!= new_l1_size2
)
450 for(i
= 0; i
< s
->l1_size
; i
++)
451 new_l1_table
[i
] = be64_to_cpu(new_l1_table
[i
]);
454 cpu_to_be32w((uint32_t*)data
, new_l1_size
);
455 cpu_to_be64w((uint64_t*)(data
+ 4), new_l1_table_offset
);
456 if (bdrv_pwrite(s
->hd
, offsetof(QCowHeader
, l1_size
), data
,
457 sizeof(data
)) != sizeof(data
))
459 qemu_free(s
->l1_table
);
460 free_clusters(bs
, s
->l1_table_offset
, s
->l1_size
* sizeof(uint64_t));
461 s
->l1_table_offset
= new_l1_table_offset
;
462 s
->l1_table
= new_l1_table
;
463 s
->l1_size
= new_l1_size
;
466 qemu_free(s
->l1_table
);
473 * seek l2_offset in the l2_cache table
474 * if not found, return NULL,
476 * increments the l2 cache hit count of the entry,
477 * if counter overflow, divide by two all counters
478 * return the pointer to the l2 cache entry
482 static uint64_t *seek_l2_table(BDRVQcowState
*s
, uint64_t l2_offset
)
486 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
487 if (l2_offset
== s
->l2_cache_offsets
[i
]) {
488 /* increment the hit count */
489 if (++s
->l2_cache_counts
[i
] == 0xffffffff) {
490 for(j
= 0; j
< L2_CACHE_SIZE
; j
++) {
491 s
->l2_cache_counts
[j
] >>= 1;
494 return s
->l2_cache
+ (i
<< s
->l2_bits
);
503 * Loads a L2 table into memory. If the table is in the cache, the cache
504 * is used; otherwise the L2 table is loaded from the image file.
506 * Returns a pointer to the L2 table on success, or NULL if the read from
507 * the image file failed.
510 static uint64_t *l2_load(BlockDriverState
*bs
, uint64_t l2_offset
)
512 BDRVQcowState
*s
= bs
->opaque
;
516 /* seek if the table for the given offset is in the cache */
518 l2_table
= seek_l2_table(s
, l2_offset
);
519 if (l2_table
!= NULL
)
522 /* not found: load a new entry in the least used one */
524 min_index
= l2_cache_new_entry(bs
);
525 l2_table
= s
->l2_cache
+ (min_index
<< s
->l2_bits
);
526 if (bdrv_pread(s
->hd
, l2_offset
, l2_table
, s
->l2_size
* sizeof(uint64_t)) !=
527 s
->l2_size
* sizeof(uint64_t))
529 s
->l2_cache_offsets
[min_index
] = l2_offset
;
530 s
->l2_cache_counts
[min_index
] = 1;
538 * Allocate a new l2 entry in the file. If l1_index points to an already
539 * used entry in the L2 table (i.e. we are doing a copy on write for the L2
540 * table) copy the contents of the old L2 table into the newly allocated one.
541 * Otherwise the new table is initialized with zeros.
545 static uint64_t *l2_allocate(BlockDriverState
*bs
, int l1_index
)
547 BDRVQcowState
*s
= bs
->opaque
;
549 uint64_t old_l2_offset
, tmp
;
550 uint64_t *l2_table
, l2_offset
;
552 old_l2_offset
= s
->l1_table
[l1_index
];
554 /* allocate a new l2 entry */
556 l2_offset
= alloc_clusters(bs
, s
->l2_size
* sizeof(uint64_t));
558 /* update the L1 entry */
560 s
->l1_table
[l1_index
] = l2_offset
| QCOW_OFLAG_COPIED
;
562 tmp
= cpu_to_be64(l2_offset
| QCOW_OFLAG_COPIED
);
563 if (bdrv_pwrite(s
->hd
, s
->l1_table_offset
+ l1_index
* sizeof(tmp
),
564 &tmp
, sizeof(tmp
)) != sizeof(tmp
))
567 /* allocate a new entry in the l2 cache */
569 min_index
= l2_cache_new_entry(bs
);
570 l2_table
= s
->l2_cache
+ (min_index
<< s
->l2_bits
);
572 if (old_l2_offset
== 0) {
573 /* if there was no old l2 table, clear the new table */
574 memset(l2_table
, 0, s
->l2_size
* sizeof(uint64_t));
576 /* if there was an old l2 table, read it from the disk */
577 if (bdrv_pread(s
->hd
, old_l2_offset
,
578 l2_table
, s
->l2_size
* sizeof(uint64_t)) !=
579 s
->l2_size
* sizeof(uint64_t))
582 /* write the l2 table to the file */
583 if (bdrv_pwrite(s
->hd
, l2_offset
,
584 l2_table
, s
->l2_size
* sizeof(uint64_t)) !=
585 s
->l2_size
* sizeof(uint64_t))
588 /* update the l2 cache entry */
590 s
->l2_cache_offsets
[min_index
] = l2_offset
;
591 s
->l2_cache_counts
[min_index
] = 1;
596 static int count_contiguous_clusters(uint64_t nb_clusters
, int cluster_size
,
597 uint64_t *l2_table
, uint64_t start
, uint64_t mask
)
600 uint64_t offset
= be64_to_cpu(l2_table
[0]) & ~mask
;
605 for (i
= start
; i
< start
+ nb_clusters
; i
++)
606 if (offset
+ i
* cluster_size
!= (be64_to_cpu(l2_table
[i
]) & ~mask
))
612 static int count_contiguous_free_clusters(uint64_t nb_clusters
, uint64_t *l2_table
)
616 while(nb_clusters
-- && l2_table
[i
] == 0)
625 * For a given offset of the disk image, return cluster offset in
628 * on entry, *num is the number of contiguous clusters we'd like to
629 * access following offset.
631 * on exit, *num is the number of contiguous clusters we can read.
633 * Return 1, if the offset is found
634 * Return 0, otherwise.
638 static uint64_t get_cluster_offset(BlockDriverState
*bs
,
639 uint64_t offset
, int *num
)
641 BDRVQcowState
*s
= bs
->opaque
;
642 int l1_index
, l2_index
;
643 uint64_t l2_offset
, *l2_table
, cluster_offset
;
645 int index_in_cluster
, nb_available
, nb_needed
, nb_clusters
;
647 index_in_cluster
= (offset
>> 9) & (s
->cluster_sectors
- 1);
648 nb_needed
= *num
+ index_in_cluster
;
650 l1_bits
= s
->l2_bits
+ s
->cluster_bits
;
652 /* compute how many bytes there are between the offset and
653 * the end of the l1 entry
656 nb_available
= (1 << l1_bits
) - (offset
& ((1 << l1_bits
) - 1));
658 /* compute the number of available sectors */
660 nb_available
= (nb_available
>> 9) + index_in_cluster
;
662 if (nb_needed
> nb_available
) {
663 nb_needed
= nb_available
;
668 /* seek the the l2 offset in the l1 table */
670 l1_index
= offset
>> l1_bits
;
671 if (l1_index
>= s
->l1_size
)
674 l2_offset
= s
->l1_table
[l1_index
];
676 /* seek the l2 table of the given l2 offset */
681 /* load the l2 table in memory */
683 l2_offset
&= ~QCOW_OFLAG_COPIED
;
684 l2_table
= l2_load(bs
, l2_offset
);
685 if (l2_table
== NULL
)
688 /* find the cluster offset for the given disk offset */
690 l2_index
= (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
691 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
692 nb_clusters
= size_to_clusters(s
, nb_needed
<< 9);
694 if (!cluster_offset
) {
695 /* how many empty clusters ? */
696 c
= count_contiguous_free_clusters(nb_clusters
, &l2_table
[l2_index
]);
698 /* how many allocated clusters ? */
699 c
= count_contiguous_clusters(nb_clusters
, s
->cluster_size
,
700 &l2_table
[l2_index
], 0, QCOW_OFLAG_COPIED
);
703 nb_available
= (c
* s
->cluster_sectors
);
705 if (nb_available
> nb_needed
)
706 nb_available
= nb_needed
;
708 *num
= nb_available
- index_in_cluster
;
710 return cluster_offset
& ~QCOW_OFLAG_COPIED
;
716 * free clusters according to its type: compressed or not
720 static void free_any_clusters(BlockDriverState
*bs
,
721 uint64_t cluster_offset
, int nb_clusters
)
723 BDRVQcowState
*s
= bs
->opaque
;
725 /* free the cluster */
727 if (cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
729 nb_csectors
= ((cluster_offset
>> s
->csize_shift
) &
731 free_clusters(bs
, (cluster_offset
& s
->cluster_offset_mask
) & ~511,
736 free_clusters(bs
, cluster_offset
, nb_clusters
<< s
->cluster_bits
);
744 * for a given disk offset, load (and allocate if needed)
747 * the l2 table offset in the qcow2 file and the cluster index
748 * in the l2 table are given to the caller.
752 static int get_cluster_table(BlockDriverState
*bs
, uint64_t offset
,
753 uint64_t **new_l2_table
,
754 uint64_t *new_l2_offset
,
757 BDRVQcowState
*s
= bs
->opaque
;
758 int l1_index
, l2_index
, ret
;
759 uint64_t l2_offset
, *l2_table
;
761 /* seek the the l2 offset in the l1 table */
763 l1_index
= offset
>> (s
->l2_bits
+ s
->cluster_bits
);
764 if (l1_index
>= s
->l1_size
) {
765 ret
= grow_l1_table(bs
, l1_index
+ 1);
769 l2_offset
= s
->l1_table
[l1_index
];
771 /* seek the l2 table of the given l2 offset */
773 if (l2_offset
& QCOW_OFLAG_COPIED
) {
774 /* load the l2 table in memory */
775 l2_offset
&= ~QCOW_OFLAG_COPIED
;
776 l2_table
= l2_load(bs
, l2_offset
);
777 if (l2_table
== NULL
)
781 free_clusters(bs
, l2_offset
, s
->l2_size
* sizeof(uint64_t));
782 l2_table
= l2_allocate(bs
, l1_index
);
783 if (l2_table
== NULL
)
785 l2_offset
= s
->l1_table
[l1_index
] & ~QCOW_OFLAG_COPIED
;
788 /* find the cluster offset for the given disk offset */
790 l2_index
= (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
792 *new_l2_table
= l2_table
;
793 *new_l2_offset
= l2_offset
;
794 *new_l2_index
= l2_index
;
800 * alloc_compressed_cluster_offset
802 * For a given offset of the disk image, return cluster offset in
805 * If the offset is not found, allocate a new compressed cluster.
807 * Return the cluster offset if successful,
808 * Return 0, otherwise.
812 static uint64_t alloc_compressed_cluster_offset(BlockDriverState
*bs
,
816 BDRVQcowState
*s
= bs
->opaque
;
818 uint64_t l2_offset
, *l2_table
, cluster_offset
;
821 ret
= get_cluster_table(bs
, offset
, &l2_table
, &l2_offset
, &l2_index
);
825 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
826 if (cluster_offset
& QCOW_OFLAG_COPIED
)
827 return cluster_offset
& ~QCOW_OFLAG_COPIED
;
830 free_any_clusters(bs
, cluster_offset
, 1);
832 cluster_offset
= alloc_bytes(bs
, compressed_size
);
833 nb_csectors
= ((cluster_offset
+ compressed_size
- 1) >> 9) -
834 (cluster_offset
>> 9);
836 cluster_offset
|= QCOW_OFLAG_COMPRESSED
|
837 ((uint64_t)nb_csectors
<< s
->csize_shift
);
839 /* update L2 table */
841 /* compressed clusters never have the copied flag */
843 l2_table
[l2_index
] = cpu_to_be64(cluster_offset
);
844 if (bdrv_pwrite(s
->hd
,
845 l2_offset
+ l2_index
* sizeof(uint64_t),
847 sizeof(uint64_t)) != sizeof(uint64_t))
850 return cluster_offset
;
853 typedef struct QCowL2Meta
861 static int alloc_cluster_link_l2(BlockDriverState
*bs
, uint64_t cluster_offset
,
864 BDRVQcowState
*s
= bs
->opaque
;
865 int i
, j
= 0, l2_index
, ret
;
866 uint64_t *old_cluster
, start_sect
, l2_offset
, *l2_table
;
868 if (m
->nb_clusters
== 0)
871 old_cluster
= qemu_malloc(m
->nb_clusters
* sizeof(uint64_t));
873 /* copy content of unmodified sectors */
874 start_sect
= (m
->offset
& ~(s
->cluster_size
- 1)) >> 9;
876 ret
= copy_sectors(bs
, start_sect
, cluster_offset
, 0, m
->n_start
);
881 if (m
->nb_available
& (s
->cluster_sectors
- 1)) {
882 uint64_t end
= m
->nb_available
& ~(uint64_t)(s
->cluster_sectors
- 1);
883 ret
= copy_sectors(bs
, start_sect
+ end
, cluster_offset
+ (end
<< 9),
884 m
->nb_available
- end
, s
->cluster_sectors
);
890 /* update L2 table */
891 if (!get_cluster_table(bs
, m
->offset
, &l2_table
, &l2_offset
, &l2_index
))
894 for (i
= 0; i
< m
->nb_clusters
; i
++) {
895 /* if two concurrent writes happen to the same unallocated cluster
896 * each write allocates separate cluster and writes data concurrently.
897 * The first one to complete updates l2 table with pointer to its
898 * cluster the second one has to do RMW (which is done above by
899 * copy_sectors()), update l2 table with its cluster pointer and free
900 * old cluster. This is what this loop does */
901 if(l2_table
[l2_index
+ i
] != 0)
902 old_cluster
[j
++] = l2_table
[l2_index
+ i
];
904 l2_table
[l2_index
+ i
] = cpu_to_be64((cluster_offset
+
905 (i
<< s
->cluster_bits
)) | QCOW_OFLAG_COPIED
);
908 if (bdrv_pwrite(s
->hd
, l2_offset
+ l2_index
* sizeof(uint64_t),
909 l2_table
+ l2_index
, m
->nb_clusters
* sizeof(uint64_t)) !=
910 m
->nb_clusters
* sizeof(uint64_t))
913 for (i
= 0; i
< j
; i
++)
914 free_any_clusters(bs
, be64_to_cpu(old_cluster
[i
]) & ~QCOW_OFLAG_COPIED
,
919 qemu_free(old_cluster
);
924 * alloc_cluster_offset
926 * For a given offset of the disk image, return cluster offset in
929 * If the offset is not found, allocate a new cluster.
931 * Return the cluster offset if successful,
932 * Return 0, otherwise.
936 static uint64_t alloc_cluster_offset(BlockDriverState
*bs
,
938 int n_start
, int n_end
,
939 int *num
, QCowL2Meta
*m
)
941 BDRVQcowState
*s
= bs
->opaque
;
943 uint64_t l2_offset
, *l2_table
, cluster_offset
;
944 int nb_clusters
, i
= 0;
946 ret
= get_cluster_table(bs
, offset
, &l2_table
, &l2_offset
, &l2_index
);
950 nb_clusters
= size_to_clusters(s
, n_end
<< 9);
952 nb_clusters
= MIN(nb_clusters
, s
->l2_size
- l2_index
);
954 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
956 /* We keep all QCOW_OFLAG_COPIED clusters */
958 if (cluster_offset
& QCOW_OFLAG_COPIED
) {
959 nb_clusters
= count_contiguous_clusters(nb_clusters
, s
->cluster_size
,
960 &l2_table
[l2_index
], 0, 0);
962 cluster_offset
&= ~QCOW_OFLAG_COPIED
;
968 /* for the moment, multiple compressed clusters are not managed */
970 if (cluster_offset
& QCOW_OFLAG_COMPRESSED
)
973 /* how many available clusters ? */
975 while (i
< nb_clusters
) {
976 i
+= count_contiguous_clusters(nb_clusters
- i
, s
->cluster_size
,
977 &l2_table
[l2_index
], i
, 0);
979 if(be64_to_cpu(l2_table
[l2_index
+ i
]))
982 i
+= count_contiguous_free_clusters(nb_clusters
- i
,
983 &l2_table
[l2_index
+ i
]);
985 cluster_offset
= be64_to_cpu(l2_table
[l2_index
+ i
]);
987 if ((cluster_offset
& QCOW_OFLAG_COPIED
) ||
988 (cluster_offset
& QCOW_OFLAG_COMPRESSED
))
993 /* allocate a new cluster */
995 cluster_offset
= alloc_clusters(bs
, nb_clusters
* s
->cluster_size
);
997 /* save info needed for meta data update */
999 m
->n_start
= n_start
;
1000 m
->nb_clusters
= nb_clusters
;
1003 m
->nb_available
= MIN(nb_clusters
<< (s
->cluster_bits
- 9), n_end
);
1005 *num
= m
->nb_available
- n_start
;
1007 return cluster_offset
;
1010 static int qcow_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
1011 int nb_sectors
, int *pnum
)
1013 uint64_t cluster_offset
;
1016 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, pnum
);
1018 return (cluster_offset
!= 0);
1021 static int decompress_buffer(uint8_t *out_buf
, int out_buf_size
,
1022 const uint8_t *buf
, int buf_size
)
1024 z_stream strm1
, *strm
= &strm1
;
1027 memset(strm
, 0, sizeof(*strm
));
1029 strm
->next_in
= (uint8_t *)buf
;
1030 strm
->avail_in
= buf_size
;
1031 strm
->next_out
= out_buf
;
1032 strm
->avail_out
= out_buf_size
;
1034 ret
= inflateInit2(strm
, -12);
1037 ret
= inflate(strm
, Z_FINISH
);
1038 out_len
= strm
->next_out
- out_buf
;
1039 if ((ret
!= Z_STREAM_END
&& ret
!= Z_BUF_ERROR
) ||
1040 out_len
!= out_buf_size
) {
1048 static int decompress_cluster(BDRVQcowState
*s
, uint64_t cluster_offset
)
1050 int ret
, csize
, nb_csectors
, sector_offset
;
1053 coffset
= cluster_offset
& s
->cluster_offset_mask
;
1054 if (s
->cluster_cache_offset
!= coffset
) {
1055 nb_csectors
= ((cluster_offset
>> s
->csize_shift
) & s
->csize_mask
) + 1;
1056 sector_offset
= coffset
& 511;
1057 csize
= nb_csectors
* 512 - sector_offset
;
1058 ret
= bdrv_read(s
->hd
, coffset
>> 9, s
->cluster_data
, nb_csectors
);
1062 if (decompress_buffer(s
->cluster_cache
, s
->cluster_size
,
1063 s
->cluster_data
+ sector_offset
, csize
) < 0) {
1066 s
->cluster_cache_offset
= coffset
;
1071 /* handle reading after the end of the backing file */
1072 static int backing_read1(BlockDriverState
*bs
,
1073 int64_t sector_num
, uint8_t *buf
, int nb_sectors
)
1076 if ((sector_num
+ nb_sectors
) <= bs
->total_sectors
)
1078 if (sector_num
>= bs
->total_sectors
)
1081 n1
= bs
->total_sectors
- sector_num
;
1082 memset(buf
+ n1
* 512, 0, 512 * (nb_sectors
- n1
));
1086 static int qcow_read(BlockDriverState
*bs
, int64_t sector_num
,
1087 uint8_t *buf
, int nb_sectors
)
1089 BDRVQcowState
*s
= bs
->opaque
;
1090 int ret
, index_in_cluster
, n
, n1
;
1091 uint64_t cluster_offset
;
1093 while (nb_sectors
> 0) {
1095 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, &n
);
1096 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
1097 if (!cluster_offset
) {
1098 if (bs
->backing_hd
) {
1099 /* read from the base image */
1100 n1
= backing_read1(bs
->backing_hd
, sector_num
, buf
, n
);
1102 ret
= bdrv_read(bs
->backing_hd
, sector_num
, buf
, n1
);
1107 memset(buf
, 0, 512 * n
);
1109 } else if (cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
1110 if (decompress_cluster(s
, cluster_offset
) < 0)
1112 memcpy(buf
, s
->cluster_cache
+ index_in_cluster
* 512, 512 * n
);
1114 ret
= bdrv_pread(s
->hd
, cluster_offset
+ index_in_cluster
* 512, buf
, n
* 512);
1117 if (s
->crypt_method
) {
1118 encrypt_sectors(s
, sector_num
, buf
, buf
, n
, 0,
1119 &s
->aes_decrypt_key
);
1129 typedef struct QCowAIOCB
{
1130 BlockDriverAIOCB common
;
1137 uint64_t cluster_offset
;
1138 uint8_t *cluster_data
;
1139 BlockDriverAIOCB
*hd_aiocb
;
1140 struct iovec hd_iov
;
1141 QEMUIOVector hd_qiov
;
1146 static void qcow_aio_cancel(BlockDriverAIOCB
*blockacb
)
1148 QCowAIOCB
*acb
= (QCowAIOCB
*)blockacb
;
1150 bdrv_aio_cancel(acb
->hd_aiocb
);
1151 qemu_aio_release(acb
);
1154 static AIOPool qcow_aio_pool
= {
1155 .aiocb_size
= sizeof(QCowAIOCB
),
1156 .cancel
= qcow_aio_cancel
,
1159 static void qcow_aio_read_cb(void *opaque
, int ret
);
1160 static void qcow_aio_read_bh(void *opaque
)
1162 QCowAIOCB
*acb
= opaque
;
1163 qemu_bh_delete(acb
->bh
);
1165 qcow_aio_read_cb(opaque
, 0);
1168 static int qcow_schedule_bh(QEMUBHFunc
*cb
, QCowAIOCB
*acb
)
1173 acb
->bh
= qemu_bh_new(cb
, acb
);
1177 qemu_bh_schedule(acb
->bh
);
1182 static void qcow_aio_read_cb(void *opaque
, int ret
)
1184 QCowAIOCB
*acb
= opaque
;
1185 BlockDriverState
*bs
= acb
->common
.bs
;
1186 BDRVQcowState
*s
= bs
->opaque
;
1187 int index_in_cluster
, n1
;
1189 acb
->hd_aiocb
= NULL
;
1193 /* post process the read buffer */
1194 if (!acb
->cluster_offset
) {
1196 } else if (acb
->cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
1199 if (s
->crypt_method
) {
1200 encrypt_sectors(s
, acb
->sector_num
, acb
->buf
, acb
->buf
,
1202 &s
->aes_decrypt_key
);
1206 acb
->nb_sectors
-= acb
->n
;
1207 acb
->sector_num
+= acb
->n
;
1208 acb
->buf
+= acb
->n
* 512;
1210 if (acb
->nb_sectors
== 0) {
1211 /* request completed */
1216 /* prepare next AIO request */
1217 acb
->n
= acb
->nb_sectors
;
1218 acb
->cluster_offset
= get_cluster_offset(bs
, acb
->sector_num
<< 9, &acb
->n
);
1219 index_in_cluster
= acb
->sector_num
& (s
->cluster_sectors
- 1);
1221 if (!acb
->cluster_offset
) {
1222 if (bs
->backing_hd
) {
1223 /* read from the base image */
1224 n1
= backing_read1(bs
->backing_hd
, acb
->sector_num
,
1227 acb
->hd_iov
.iov_base
= (void *)acb
->buf
;
1228 acb
->hd_iov
.iov_len
= acb
->n
* 512;
1229 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
1230 acb
->hd_aiocb
= bdrv_aio_readv(bs
->backing_hd
, acb
->sector_num
,
1231 &acb
->hd_qiov
, acb
->n
,
1232 qcow_aio_read_cb
, acb
);
1233 if (acb
->hd_aiocb
== NULL
)
1236 ret
= qcow_schedule_bh(qcow_aio_read_bh
, acb
);
1241 /* Note: in this case, no need to wait */
1242 memset(acb
->buf
, 0, 512 * acb
->n
);
1243 ret
= qcow_schedule_bh(qcow_aio_read_bh
, acb
);
1247 } else if (acb
->cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
1248 /* add AIO support for compressed blocks ? */
1249 if (decompress_cluster(s
, acb
->cluster_offset
) < 0)
1252 s
->cluster_cache
+ index_in_cluster
* 512, 512 * acb
->n
);
1253 ret
= qcow_schedule_bh(qcow_aio_read_bh
, acb
);
1257 if ((acb
->cluster_offset
& 511) != 0) {
1262 acb
->hd_iov
.iov_base
= (void *)acb
->buf
;
1263 acb
->hd_iov
.iov_len
= acb
->n
* 512;
1264 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
1265 acb
->hd_aiocb
= bdrv_aio_readv(s
->hd
,
1266 (acb
->cluster_offset
>> 9) + index_in_cluster
,
1267 &acb
->hd_qiov
, acb
->n
, qcow_aio_read_cb
, acb
);
1268 if (acb
->hd_aiocb
== NULL
)
1274 if (acb
->qiov
->niov
> 1) {
1275 qemu_iovec_from_buffer(acb
->qiov
, acb
->orig_buf
, acb
->qiov
->size
);
1276 qemu_vfree(acb
->orig_buf
);
1278 acb
->common
.cb(acb
->common
.opaque
, ret
);
1279 qemu_aio_release(acb
);
1282 static QCowAIOCB
*qcow_aio_setup(BlockDriverState
*bs
,
1283 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1284 BlockDriverCompletionFunc
*cb
, void *opaque
, int is_write
)
1288 acb
= qemu_aio_get(&qcow_aio_pool
, bs
, cb
, opaque
);
1291 acb
->hd_aiocb
= NULL
;
1292 acb
->sector_num
= sector_num
;
1294 if (qiov
->niov
> 1) {
1295 acb
->buf
= acb
->orig_buf
= qemu_blockalign(bs
, qiov
->size
);
1297 qemu_iovec_to_buffer(qiov
, acb
->buf
);
1299 acb
->buf
= (uint8_t *)qiov
->iov
->iov_base
;
1301 acb
->nb_sectors
= nb_sectors
;
1303 acb
->cluster_offset
= 0;
1304 acb
->l2meta
.nb_clusters
= 0;
1308 static BlockDriverAIOCB
*qcow_aio_readv(BlockDriverState
*bs
,
1309 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1310 BlockDriverCompletionFunc
*cb
, void *opaque
)
1314 acb
= qcow_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
1318 qcow_aio_read_cb(acb
, 0);
1319 return &acb
->common
;
1322 static void qcow_aio_write_cb(void *opaque
, int ret
)
1324 QCowAIOCB
*acb
= opaque
;
1325 BlockDriverState
*bs
= acb
->common
.bs
;
1326 BDRVQcowState
*s
= bs
->opaque
;
1327 int index_in_cluster
;
1328 const uint8_t *src_buf
;
1331 acb
->hd_aiocb
= NULL
;
1336 if (alloc_cluster_link_l2(bs
, acb
->cluster_offset
, &acb
->l2meta
) < 0) {
1337 free_any_clusters(bs
, acb
->cluster_offset
, acb
->l2meta
.nb_clusters
);
1341 acb
->nb_sectors
-= acb
->n
;
1342 acb
->sector_num
+= acb
->n
;
1343 acb
->buf
+= acb
->n
* 512;
1345 if (acb
->nb_sectors
== 0) {
1346 /* request completed */
1351 index_in_cluster
= acb
->sector_num
& (s
->cluster_sectors
- 1);
1352 n_end
= index_in_cluster
+ acb
->nb_sectors
;
1353 if (s
->crypt_method
&&
1354 n_end
> QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
)
1355 n_end
= QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
;
1357 acb
->cluster_offset
= alloc_cluster_offset(bs
, acb
->sector_num
<< 9,
1359 n_end
, &acb
->n
, &acb
->l2meta
);
1360 if (!acb
->cluster_offset
|| (acb
->cluster_offset
& 511) != 0) {
1364 if (s
->crypt_method
) {
1365 if (!acb
->cluster_data
) {
1366 acb
->cluster_data
= qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS
*
1369 encrypt_sectors(s
, acb
->sector_num
, acb
->cluster_data
, acb
->buf
,
1370 acb
->n
, 1, &s
->aes_encrypt_key
);
1371 src_buf
= acb
->cluster_data
;
1375 acb
->hd_iov
.iov_base
= (void *)src_buf
;
1376 acb
->hd_iov
.iov_len
= acb
->n
* 512;
1377 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
1378 acb
->hd_aiocb
= bdrv_aio_writev(s
->hd
,
1379 (acb
->cluster_offset
>> 9) + index_in_cluster
,
1380 &acb
->hd_qiov
, acb
->n
,
1381 qcow_aio_write_cb
, acb
);
1382 if (acb
->hd_aiocb
== NULL
)
1388 if (acb
->qiov
->niov
> 1)
1389 qemu_vfree(acb
->orig_buf
);
1390 acb
->common
.cb(acb
->common
.opaque
, ret
);
1391 qemu_aio_release(acb
);
1394 static BlockDriverAIOCB
*qcow_aio_writev(BlockDriverState
*bs
,
1395 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1396 BlockDriverCompletionFunc
*cb
, void *opaque
)
1398 BDRVQcowState
*s
= bs
->opaque
;
1401 s
->cluster_cache_offset
= -1; /* disable compressed cache */
1403 acb
= qcow_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
1407 qcow_aio_write_cb(acb
, 0);
1408 return &acb
->common
;
1411 static void qcow_close(BlockDriverState
*bs
)
1413 BDRVQcowState
*s
= bs
->opaque
;
1414 qemu_free(s
->l1_table
);
1415 qemu_free(s
->l2_cache
);
1416 qemu_free(s
->cluster_cache
);
1417 qemu_free(s
->cluster_data
);
1422 static int get_bits_from_size(size_t size
)
1431 /* Not a power of two */
1443 static int qcow_create2(const char *filename
, int64_t total_size
,
1444 const char *backing_file
, const char *backing_format
,
1445 int flags
, size_t cluster_size
)
1448 int fd
, header_size
, backing_filename_len
, l1_size
, i
, shift
, l2_bits
;
1449 int ref_clusters
, backing_format_len
= 0;
1451 uint64_t tmp
, offset
;
1452 QCowCreateState s1
, *s
= &s1
;
1453 QCowExtension ext_bf
= {0, 0};
1456 memset(s
, 0, sizeof(*s
));
1458 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0644);
1461 memset(&header
, 0, sizeof(header
));
1462 header
.magic
= cpu_to_be32(QCOW_MAGIC
);
1463 header
.version
= cpu_to_be32(QCOW_VERSION
);
1464 header
.size
= cpu_to_be64(total_size
* 512);
1465 header_size
= sizeof(header
);
1466 backing_filename_len
= 0;
1468 if (backing_format
) {
1469 ext_bf
.magic
= QCOW_EXT_MAGIC_BACKING_FORMAT
;
1470 backing_format_len
= strlen(backing_format
);
1471 ext_bf
.len
= (backing_format_len
+ 7) & ~7;
1472 header_size
+= ((sizeof(ext_bf
) + ext_bf
.len
+ 7) & ~7);
1474 header
.backing_file_offset
= cpu_to_be64(header_size
);
1475 backing_filename_len
= strlen(backing_file
);
1476 header
.backing_file_size
= cpu_to_be32(backing_filename_len
);
1477 header_size
+= backing_filename_len
;
1481 s
->cluster_bits
= get_bits_from_size(cluster_size
);
1482 if (s
->cluster_bits
< MIN_CLUSTER_BITS
||
1483 s
->cluster_bits
> MAX_CLUSTER_BITS
)
1485 fprintf(stderr
, "Cluster size must be a power of two between "
1487 1 << MIN_CLUSTER_BITS
,
1488 1 << (MAX_CLUSTER_BITS
- 10));
1491 s
->cluster_size
= 1 << s
->cluster_bits
;
1493 header
.cluster_bits
= cpu_to_be32(s
->cluster_bits
);
1494 header_size
= (header_size
+ 7) & ~7;
1495 if (flags
& BLOCK_FLAG_ENCRYPT
) {
1496 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
1498 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
1500 l2_bits
= s
->cluster_bits
- 3;
1501 shift
= s
->cluster_bits
+ l2_bits
;
1502 l1_size
= (((total_size
* 512) + (1LL << shift
) - 1) >> shift
);
1503 offset
= align_offset(header_size
, s
->cluster_size
);
1504 s
->l1_table_offset
= offset
;
1505 header
.l1_table_offset
= cpu_to_be64(s
->l1_table_offset
);
1506 header
.l1_size
= cpu_to_be32(l1_size
);
1507 offset
+= align_offset(l1_size
* sizeof(uint64_t), s
->cluster_size
);
1509 s
->refcount_table
= qemu_mallocz(s
->cluster_size
);
1511 s
->refcount_table_offset
= offset
;
1512 header
.refcount_table_offset
= cpu_to_be64(offset
);
1513 header
.refcount_table_clusters
= cpu_to_be32(1);
1514 offset
+= s
->cluster_size
;
1515 s
->refcount_block_offset
= offset
;
1517 /* count how many refcount blocks needed */
1518 tmp
= offset
>> s
->cluster_bits
;
1519 ref_clusters
= (tmp
>> (s
->cluster_bits
- REFCOUNT_SHIFT
)) + 1;
1520 for (i
=0; i
< ref_clusters
; i
++) {
1521 s
->refcount_table
[i
] = cpu_to_be64(offset
);
1522 offset
+= s
->cluster_size
;
1525 s
->refcount_block
= qemu_mallocz(ref_clusters
* s
->cluster_size
);
1527 /* update refcounts */
1528 create_refcount_update(s
, 0, header_size
);
1529 create_refcount_update(s
, s
->l1_table_offset
, l1_size
* sizeof(uint64_t));
1530 create_refcount_update(s
, s
->refcount_table_offset
, s
->cluster_size
);
1531 create_refcount_update(s
, s
->refcount_block_offset
, ref_clusters
* s
->cluster_size
);
1533 /* write all the data */
1534 write(fd
, &header
, sizeof(header
));
1536 if (backing_format_len
) {
1538 int d
= ext_bf
.len
- backing_format_len
;
1540 memset(zero
, 0, sizeof(zero
));
1541 cpu_to_be32s(&ext_bf
.magic
);
1542 cpu_to_be32s(&ext_bf
.len
);
1543 write(fd
, &ext_bf
, sizeof(ext_bf
));
1544 write(fd
, backing_format
, backing_format_len
);
1549 write(fd
, backing_file
, backing_filename_len
);
1551 lseek(fd
, s
->l1_table_offset
, SEEK_SET
);
1553 for(i
= 0;i
< l1_size
; i
++) {
1554 write(fd
, &tmp
, sizeof(tmp
));
1556 lseek(fd
, s
->refcount_table_offset
, SEEK_SET
);
1557 write(fd
, s
->refcount_table
, s
->cluster_size
);
1559 lseek(fd
, s
->refcount_block_offset
, SEEK_SET
);
1560 write(fd
, s
->refcount_block
, ref_clusters
* s
->cluster_size
);
1562 qemu_free(s
->refcount_table
);
1563 qemu_free(s
->refcount_block
);
1568 static int qcow_create(const char *filename
, QEMUOptionParameter
*options
)
1570 const char *backing_file
= NULL
;
1571 const char *backing_fmt
= NULL
;
1572 uint64_t sectors
= 0;
1574 size_t cluster_size
= 65536;
1576 /* Read out options */
1577 while (options
&& options
->name
) {
1578 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
1579 sectors
= options
->value
.n
/ 512;
1580 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
1581 backing_file
= options
->value
.s
;
1582 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FMT
)) {
1583 backing_fmt
= options
->value
.s
;
1584 } else if (!strcmp(options
->name
, BLOCK_OPT_ENCRYPT
)) {
1585 flags
|= options
->value
.n
? BLOCK_FLAG_ENCRYPT
: 0;
1586 } else if (!strcmp(options
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
1587 if (options
->value
.n
) {
1588 cluster_size
= options
->value
.n
;
1594 return qcow_create2(filename
, sectors
, backing_file
, backing_fmt
, flags
,
1598 static int qcow_make_empty(BlockDriverState
*bs
)
1601 /* XXX: not correct */
1602 BDRVQcowState
*s
= bs
->opaque
;
1603 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
1606 memset(s
->l1_table
, 0, l1_length
);
1607 if (bdrv_pwrite(s
->hd
, s
->l1_table_offset
, s
->l1_table
, l1_length
) < 0)
1609 ret
= bdrv_truncate(s
->hd
, s
->l1_table_offset
+ l1_length
);
1618 /* XXX: put compressed sectors first, then all the cluster aligned
1619 tables to avoid losing bytes in alignment */
1620 static int qcow_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1621 const uint8_t *buf
, int nb_sectors
)
1623 BDRVQcowState
*s
= bs
->opaque
;
1627 uint64_t cluster_offset
;
1629 if (nb_sectors
== 0) {
1630 /* align end of file to a sector boundary to ease reading with
1631 sector based I/Os */
1632 cluster_offset
= bdrv_getlength(s
->hd
);
1633 cluster_offset
= (cluster_offset
+ 511) & ~511;
1634 bdrv_truncate(s
->hd
, cluster_offset
);
1638 if (nb_sectors
!= s
->cluster_sectors
)
1641 out_buf
= qemu_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
1643 /* best compression, small window, no zlib header */
1644 memset(&strm
, 0, sizeof(strm
));
1645 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
1647 9, Z_DEFAULT_STRATEGY
);
1653 strm
.avail_in
= s
->cluster_size
;
1654 strm
.next_in
= (uint8_t *)buf
;
1655 strm
.avail_out
= s
->cluster_size
;
1656 strm
.next_out
= out_buf
;
1658 ret
= deflate(&strm
, Z_FINISH
);
1659 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
1664 out_len
= strm
.next_out
- out_buf
;
1668 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
1669 /* could not compress: write normal cluster */
1670 bdrv_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
1672 cluster_offset
= alloc_compressed_cluster_offset(bs
, sector_num
<< 9,
1674 if (!cluster_offset
)
1676 cluster_offset
&= s
->cluster_offset_mask
;
1677 if (bdrv_pwrite(s
->hd
, cluster_offset
, out_buf
, out_len
) != out_len
) {
1687 static void qcow_flush(BlockDriverState
*bs
)
1689 BDRVQcowState
*s
= bs
->opaque
;
1693 static int qcow_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1695 BDRVQcowState
*s
= bs
->opaque
;
1696 bdi
->cluster_size
= s
->cluster_size
;
1697 bdi
->vm_state_offset
= (int64_t)s
->l1_vm_state_index
<<
1698 (s
->cluster_bits
+ s
->l2_bits
);
1702 /*********************************************************/
1703 /* snapshot support */
1706 static void qcow_free_snapshots(BlockDriverState
*bs
)
1708 BDRVQcowState
*s
= bs
->opaque
;
1711 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1712 qemu_free(s
->snapshots
[i
].name
);
1713 qemu_free(s
->snapshots
[i
].id_str
);
1715 qemu_free(s
->snapshots
);
1716 s
->snapshots
= NULL
;
1717 s
->nb_snapshots
= 0;
1720 static int qcow_read_snapshots(BlockDriverState
*bs
)
1722 BDRVQcowState
*s
= bs
->opaque
;
1723 QCowSnapshotHeader h
;
1725 int i
, id_str_size
, name_size
;
1727 uint32_t extra_data_size
;
1729 if (!s
->nb_snapshots
) {
1730 s
->snapshots
= NULL
;
1731 s
->snapshots_size
= 0;
1735 offset
= s
->snapshots_offset
;
1736 s
->snapshots
= qemu_mallocz(s
->nb_snapshots
* sizeof(QCowSnapshot
));
1737 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1738 offset
= align_offset(offset
, 8);
1739 if (bdrv_pread(s
->hd
, offset
, &h
, sizeof(h
)) != sizeof(h
))
1741 offset
+= sizeof(h
);
1742 sn
= s
->snapshots
+ i
;
1743 sn
->l1_table_offset
= be64_to_cpu(h
.l1_table_offset
);
1744 sn
->l1_size
= be32_to_cpu(h
.l1_size
);
1745 sn
->vm_state_size
= be32_to_cpu(h
.vm_state_size
);
1746 sn
->date_sec
= be32_to_cpu(h
.date_sec
);
1747 sn
->date_nsec
= be32_to_cpu(h
.date_nsec
);
1748 sn
->vm_clock_nsec
= be64_to_cpu(h
.vm_clock_nsec
);
1749 extra_data_size
= be32_to_cpu(h
.extra_data_size
);
1751 id_str_size
= be16_to_cpu(h
.id_str_size
);
1752 name_size
= be16_to_cpu(h
.name_size
);
1754 offset
+= extra_data_size
;
1756 sn
->id_str
= qemu_malloc(id_str_size
+ 1);
1757 if (bdrv_pread(s
->hd
, offset
, sn
->id_str
, id_str_size
) != id_str_size
)
1759 offset
+= id_str_size
;
1760 sn
->id_str
[id_str_size
] = '\0';
1762 sn
->name
= qemu_malloc(name_size
+ 1);
1763 if (bdrv_pread(s
->hd
, offset
, sn
->name
, name_size
) != name_size
)
1765 offset
+= name_size
;
1766 sn
->name
[name_size
] = '\0';
1768 s
->snapshots_size
= offset
- s
->snapshots_offset
;
1771 qcow_free_snapshots(bs
);
1775 /* add at the end of the file a new list of snapshots */
1776 static int qcow_write_snapshots(BlockDriverState
*bs
)
1778 BDRVQcowState
*s
= bs
->opaque
;
1780 QCowSnapshotHeader h
;
1781 int i
, name_size
, id_str_size
, snapshots_size
;
1784 int64_t offset
, snapshots_offset
;
1786 /* compute the size of the snapshots */
1788 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1789 sn
= s
->snapshots
+ i
;
1790 offset
= align_offset(offset
, 8);
1791 offset
+= sizeof(h
);
1792 offset
+= strlen(sn
->id_str
);
1793 offset
+= strlen(sn
->name
);
1795 snapshots_size
= offset
;
1797 snapshots_offset
= alloc_clusters(bs
, snapshots_size
);
1798 offset
= snapshots_offset
;
1800 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1801 sn
= s
->snapshots
+ i
;
1802 memset(&h
, 0, sizeof(h
));
1803 h
.l1_table_offset
= cpu_to_be64(sn
->l1_table_offset
);
1804 h
.l1_size
= cpu_to_be32(sn
->l1_size
);
1805 h
.vm_state_size
= cpu_to_be32(sn
->vm_state_size
);
1806 h
.date_sec
= cpu_to_be32(sn
->date_sec
);
1807 h
.date_nsec
= cpu_to_be32(sn
->date_nsec
);
1808 h
.vm_clock_nsec
= cpu_to_be64(sn
->vm_clock_nsec
);
1810 id_str_size
= strlen(sn
->id_str
);
1811 name_size
= strlen(sn
->name
);
1812 h
.id_str_size
= cpu_to_be16(id_str_size
);
1813 h
.name_size
= cpu_to_be16(name_size
);
1814 offset
= align_offset(offset
, 8);
1815 if (bdrv_pwrite(s
->hd
, offset
, &h
, sizeof(h
)) != sizeof(h
))
1817 offset
+= sizeof(h
);
1818 if (bdrv_pwrite(s
->hd
, offset
, sn
->id_str
, id_str_size
) != id_str_size
)
1820 offset
+= id_str_size
;
1821 if (bdrv_pwrite(s
->hd
, offset
, sn
->name
, name_size
) != name_size
)
1823 offset
+= name_size
;
1826 /* update the various header fields */
1827 data64
= cpu_to_be64(snapshots_offset
);
1828 if (bdrv_pwrite(s
->hd
, offsetof(QCowHeader
, snapshots_offset
),
1829 &data64
, sizeof(data64
)) != sizeof(data64
))
1831 data32
= cpu_to_be32(s
->nb_snapshots
);
1832 if (bdrv_pwrite(s
->hd
, offsetof(QCowHeader
, nb_snapshots
),
1833 &data32
, sizeof(data32
)) != sizeof(data32
))
1836 /* free the old snapshot table */
1837 free_clusters(bs
, s
->snapshots_offset
, s
->snapshots_size
);
1838 s
->snapshots_offset
= snapshots_offset
;
1839 s
->snapshots_size
= snapshots_size
;
1845 static void find_new_snapshot_id(BlockDriverState
*bs
,
1846 char *id_str
, int id_str_size
)
1848 BDRVQcowState
*s
= bs
->opaque
;
1850 int i
, id
, id_max
= 0;
1852 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1853 sn
= s
->snapshots
+ i
;
1854 id
= strtoul(sn
->id_str
, NULL
, 10);
1858 snprintf(id_str
, id_str_size
, "%d", id_max
+ 1);
1861 static int find_snapshot_by_id(BlockDriverState
*bs
, const char *id_str
)
1863 BDRVQcowState
*s
= bs
->opaque
;
1866 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1867 if (!strcmp(s
->snapshots
[i
].id_str
, id_str
))
1873 static int find_snapshot_by_id_or_name(BlockDriverState
*bs
, const char *name
)
1875 BDRVQcowState
*s
= bs
->opaque
;
1878 ret
= find_snapshot_by_id(bs
, name
);
1881 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1882 if (!strcmp(s
->snapshots
[i
].name
, name
))
1888 /* if no id is provided, a new one is constructed */
1889 static int qcow_snapshot_create(BlockDriverState
*bs
,
1890 QEMUSnapshotInfo
*sn_info
)
1892 BDRVQcowState
*s
= bs
->opaque
;
1893 QCowSnapshot
*snapshots1
, sn1
, *sn
= &sn1
;
1895 uint64_t *l1_table
= NULL
;
1897 memset(sn
, 0, sizeof(*sn
));
1899 if (sn_info
->id_str
[0] == '\0') {
1900 /* compute a new id */
1901 find_new_snapshot_id(bs
, sn_info
->id_str
, sizeof(sn_info
->id_str
));
1904 /* check that the ID is unique */
1905 if (find_snapshot_by_id(bs
, sn_info
->id_str
) >= 0)
1908 sn
->id_str
= qemu_strdup(sn_info
->id_str
);
1911 sn
->name
= qemu_strdup(sn_info
->name
);
1914 sn
->vm_state_size
= sn_info
->vm_state_size
;
1915 sn
->date_sec
= sn_info
->date_sec
;
1916 sn
->date_nsec
= sn_info
->date_nsec
;
1917 sn
->vm_clock_nsec
= sn_info
->vm_clock_nsec
;
1919 ret
= update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 1);
1923 /* create the L1 table of the snapshot */
1924 sn
->l1_table_offset
= alloc_clusters(bs
, s
->l1_size
* sizeof(uint64_t));
1925 sn
->l1_size
= s
->l1_size
;
1927 l1_table
= qemu_malloc(s
->l1_size
* sizeof(uint64_t));
1928 for(i
= 0; i
< s
->l1_size
; i
++) {
1929 l1_table
[i
] = cpu_to_be64(s
->l1_table
[i
]);
1931 if (bdrv_pwrite(s
->hd
, sn
->l1_table_offset
,
1932 l1_table
, s
->l1_size
* sizeof(uint64_t)) !=
1933 (s
->l1_size
* sizeof(uint64_t)))
1935 qemu_free(l1_table
);
1938 snapshots1
= qemu_malloc((s
->nb_snapshots
+ 1) * sizeof(QCowSnapshot
));
1940 memcpy(snapshots1
, s
->snapshots
, s
->nb_snapshots
* sizeof(QCowSnapshot
));
1941 qemu_free(s
->snapshots
);
1943 s
->snapshots
= snapshots1
;
1944 s
->snapshots
[s
->nb_snapshots
++] = *sn
;
1946 if (qcow_write_snapshots(bs
) < 0)
1949 check_refcounts(bs
);
1953 qemu_free(sn
->name
);
1954 qemu_free(l1_table
);
1958 /* copy the snapshot 'snapshot_name' into the current disk image */
1959 static int qcow_snapshot_goto(BlockDriverState
*bs
,
1960 const char *snapshot_id
)
1962 BDRVQcowState
*s
= bs
->opaque
;
1964 int i
, snapshot_index
, l1_size2
;
1966 snapshot_index
= find_snapshot_by_id_or_name(bs
, snapshot_id
);
1967 if (snapshot_index
< 0)
1969 sn
= &s
->snapshots
[snapshot_index
];
1971 if (update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, -1) < 0)
1974 if (grow_l1_table(bs
, sn
->l1_size
) < 0)
1977 s
->l1_size
= sn
->l1_size
;
1978 l1_size2
= s
->l1_size
* sizeof(uint64_t);
1979 /* copy the snapshot l1 table to the current l1 table */
1980 if (bdrv_pread(s
->hd
, sn
->l1_table_offset
,
1981 s
->l1_table
, l1_size2
) != l1_size2
)
1983 if (bdrv_pwrite(s
->hd
, s
->l1_table_offset
,
1984 s
->l1_table
, l1_size2
) != l1_size2
)
1986 for(i
= 0;i
< s
->l1_size
; i
++) {
1987 be64_to_cpus(&s
->l1_table
[i
]);
1990 if (update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 1) < 0)
1994 check_refcounts(bs
);
2001 static int qcow_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
2003 BDRVQcowState
*s
= bs
->opaque
;
2005 int snapshot_index
, ret
;
2007 snapshot_index
= find_snapshot_by_id_or_name(bs
, snapshot_id
);
2008 if (snapshot_index
< 0)
2010 sn
= &s
->snapshots
[snapshot_index
];
2012 ret
= update_snapshot_refcount(bs
, sn
->l1_table_offset
, sn
->l1_size
, -1);
2015 /* must update the copied flag on the current cluster offsets */
2016 ret
= update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 0);
2019 free_clusters(bs
, sn
->l1_table_offset
, sn
->l1_size
* sizeof(uint64_t));
2021 qemu_free(sn
->id_str
);
2022 qemu_free(sn
->name
);
2023 memmove(sn
, sn
+ 1, (s
->nb_snapshots
- snapshot_index
- 1) * sizeof(*sn
));
2025 ret
= qcow_write_snapshots(bs
);
2027 /* XXX: restore snapshot if error ? */
2031 check_refcounts(bs
);
2036 static int qcow_snapshot_list(BlockDriverState
*bs
,
2037 QEMUSnapshotInfo
**psn_tab
)
2039 BDRVQcowState
*s
= bs
->opaque
;
2040 QEMUSnapshotInfo
*sn_tab
, *sn_info
;
2044 if (!s
->nb_snapshots
) {
2046 return s
->nb_snapshots
;
2049 sn_tab
= qemu_mallocz(s
->nb_snapshots
* sizeof(QEMUSnapshotInfo
));
2050 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
2051 sn_info
= sn_tab
+ i
;
2052 sn
= s
->snapshots
+ i
;
2053 pstrcpy(sn_info
->id_str
, sizeof(sn_info
->id_str
),
2055 pstrcpy(sn_info
->name
, sizeof(sn_info
->name
),
2057 sn_info
->vm_state_size
= sn
->vm_state_size
;
2058 sn_info
->date_sec
= sn
->date_sec
;
2059 sn_info
->date_nsec
= sn
->date_nsec
;
2060 sn_info
->vm_clock_nsec
= sn
->vm_clock_nsec
;
2063 return s
->nb_snapshots
;
2066 static int qcow_check(BlockDriverState
*bs
)
2068 return check_refcounts(bs
);
2072 static void dump_refcounts(BlockDriverState
*bs
)
2074 BDRVQcowState
*s
= bs
->opaque
;
2075 int64_t nb_clusters
, k
, k1
, size
;
2078 size
= bdrv_getlength(s
->hd
);
2079 nb_clusters
= size_to_clusters(s
, size
);
2080 for(k
= 0; k
< nb_clusters
;) {
2082 refcount
= get_refcount(bs
, k
);
2084 while (k
< nb_clusters
&& get_refcount(bs
, k
) == refcount
)
2086 printf("%lld: refcount=%d nb=%lld\n", k
, refcount
, k
- k1
);
2091 static int qcow_put_buffer(BlockDriverState
*bs
, const uint8_t *buf
,
2092 int64_t pos
, int size
)
2094 int growable
= bs
->growable
;
2097 bdrv_pwrite(bs
, pos
, buf
, size
);
2098 bs
->growable
= growable
;
2103 static int qcow_get_buffer(BlockDriverState
*bs
, uint8_t *buf
,
2104 int64_t pos
, int size
)
2106 int growable
= bs
->growable
;
2110 ret
= bdrv_pread(bs
, pos
, buf
, size
);
2111 bs
->growable
= growable
;
2116 static QEMUOptionParameter qcow_create_options
[] = {
2118 .name
= BLOCK_OPT_SIZE
,
2120 .help
= "Virtual disk size"
2123 .name
= BLOCK_OPT_BACKING_FILE
,
2125 .help
= "File name of a base image"
2128 .name
= BLOCK_OPT_BACKING_FMT
,
2130 .help
= "Image format of the base image"
2133 .name
= BLOCK_OPT_ENCRYPT
,
2135 .help
= "Encrypt the image"
2138 .name
= BLOCK_OPT_CLUSTER_SIZE
,
2140 .help
= "qcow2 cluster size"
2145 static BlockDriver bdrv_qcow2
= {
2146 .format_name
= "qcow2",
2147 .instance_size
= sizeof(BDRVQcowState
),
2148 .bdrv_probe
= qcow_probe
,
2149 .bdrv_open
= qcow_open
,
2150 .bdrv_close
= qcow_close
,
2151 .bdrv_create
= qcow_create
,
2152 .bdrv_flush
= qcow_flush
,
2153 .bdrv_is_allocated
= qcow_is_allocated
,
2154 .bdrv_set_key
= qcow_set_key
,
2155 .bdrv_make_empty
= qcow_make_empty
,
2157 .bdrv_aio_readv
= qcow_aio_readv
,
2158 .bdrv_aio_writev
= qcow_aio_writev
,
2159 .bdrv_write_compressed
= qcow_write_compressed
,
2161 .bdrv_snapshot_create
= qcow_snapshot_create
,
2162 .bdrv_snapshot_goto
= qcow_snapshot_goto
,
2163 .bdrv_snapshot_delete
= qcow_snapshot_delete
,
2164 .bdrv_snapshot_list
= qcow_snapshot_list
,
2165 .bdrv_get_info
= qcow_get_info
,
2167 .bdrv_put_buffer
= qcow_put_buffer
,
2168 .bdrv_get_buffer
= qcow_get_buffer
,
2170 .create_options
= qcow_create_options
,
2171 .bdrv_check
= qcow_check
,
2174 static void bdrv_qcow2_init(void)
2176 bdrv_register(&bdrv_qcow2
);
2179 block_init(bdrv_qcow2_init
);