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 qcow_read_snapshots(BlockDriverState
*bs
);
82 static void qcow_free_snapshots(BlockDriverState
*bs
);
84 static int qcow_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
86 const QCowHeader
*cow_header
= (const void *)buf
;
88 if (buf_size
>= sizeof(QCowHeader
) &&
89 be32_to_cpu(cow_header
->magic
) == QCOW_MAGIC
&&
90 be32_to_cpu(cow_header
->version
) == QCOW_VERSION
)
98 * read qcow2 extension and fill bs
99 * start reading from start_offset
100 * finish reading upon magic of value 0 or when end_offset reached
101 * unknown magic is skipped (future extension this version knows nothing about)
102 * return 0 upon success, non-0 otherwise
104 static int qcow_read_extensions(BlockDriverState
*bs
, uint64_t start_offset
,
107 BDRVQcowState
*s
= bs
->opaque
;
112 printf("qcow_read_extensions: start=%ld end=%ld\n", start_offset
, end_offset
);
114 offset
= start_offset
;
115 while (offset
< end_offset
) {
119 if (offset
> s
->cluster_size
)
120 printf("qcow_handle_extension: suspicious offset %lu\n", offset
);
122 printf("attemting to read extended header in offset %lu\n", offset
);
125 if (bdrv_pread(s
->hd
, offset
, &ext
, sizeof(ext
)) != sizeof(ext
)) {
126 fprintf(stderr
, "qcow_handle_extension: ERROR: pread fail from offset %llu\n",
127 (unsigned long long)offset
);
130 be32_to_cpus(&ext
.magic
);
131 be32_to_cpus(&ext
.len
);
132 offset
+= sizeof(ext
);
134 printf("ext.magic = 0x%x\n", ext
.magic
);
137 case QCOW_EXT_MAGIC_END
:
140 case QCOW_EXT_MAGIC_BACKING_FORMAT
:
141 if (ext
.len
>= sizeof(bs
->backing_format
)) {
142 fprintf(stderr
, "ERROR: ext_backing_format: len=%u too large"
144 ext
.len
, sizeof(bs
->backing_format
));
147 if (bdrv_pread(s
->hd
, offset
, bs
->backing_format
,
150 bs
->backing_format
[ext
.len
] = '\0';
152 printf("Qcow2: Got format extension %s\n", bs
->backing_format
);
154 offset
+= ((ext
.len
+ 7) & ~7);
158 /* unknown magic -- just skip it */
159 offset
+= ((ext
.len
+ 7) & ~7);
168 static int qcow_open(BlockDriverState
*bs
, const char *filename
, int flags
)
170 BDRVQcowState
*s
= bs
->opaque
;
171 int len
, i
, shift
, ret
;
175 /* Performance is terrible right now with cache=writethrough due mainly
176 * to reference count updates. If the user does not explicitly specify
177 * a caching type, force to writeback caching.
179 if ((flags
& BDRV_O_CACHE_DEF
)) {
180 flags
|= BDRV_O_CACHE_WB
;
181 flags
&= ~BDRV_O_CACHE_DEF
;
183 ret
= bdrv_file_open(&s
->hd
, filename
, flags
);
186 if (bdrv_pread(s
->hd
, 0, &header
, sizeof(header
)) != sizeof(header
))
188 be32_to_cpus(&header
.magic
);
189 be32_to_cpus(&header
.version
);
190 be64_to_cpus(&header
.backing_file_offset
);
191 be32_to_cpus(&header
.backing_file_size
);
192 be64_to_cpus(&header
.size
);
193 be32_to_cpus(&header
.cluster_bits
);
194 be32_to_cpus(&header
.crypt_method
);
195 be64_to_cpus(&header
.l1_table_offset
);
196 be32_to_cpus(&header
.l1_size
);
197 be64_to_cpus(&header
.refcount_table_offset
);
198 be32_to_cpus(&header
.refcount_table_clusters
);
199 be64_to_cpus(&header
.snapshots_offset
);
200 be32_to_cpus(&header
.nb_snapshots
);
202 if (header
.magic
!= QCOW_MAGIC
|| header
.version
!= QCOW_VERSION
)
204 if (header
.size
<= 1 ||
205 header
.cluster_bits
< MIN_CLUSTER_BITS
||
206 header
.cluster_bits
> MAX_CLUSTER_BITS
)
208 if (header
.crypt_method
> QCOW_CRYPT_AES
)
210 s
->crypt_method_header
= header
.crypt_method
;
211 if (s
->crypt_method_header
)
213 s
->cluster_bits
= header
.cluster_bits
;
214 s
->cluster_size
= 1 << s
->cluster_bits
;
215 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
216 s
->l2_bits
= s
->cluster_bits
- 3; /* L2 is always one cluster */
217 s
->l2_size
= 1 << s
->l2_bits
;
218 bs
->total_sectors
= header
.size
/ 512;
219 s
->csize_shift
= (62 - (s
->cluster_bits
- 8));
220 s
->csize_mask
= (1 << (s
->cluster_bits
- 8)) - 1;
221 s
->cluster_offset_mask
= (1LL << s
->csize_shift
) - 1;
222 s
->refcount_table_offset
= header
.refcount_table_offset
;
223 s
->refcount_table_size
=
224 header
.refcount_table_clusters
<< (s
->cluster_bits
- 3);
226 s
->snapshots_offset
= header
.snapshots_offset
;
227 s
->nb_snapshots
= header
.nb_snapshots
;
229 /* read the level 1 table */
230 s
->l1_size
= header
.l1_size
;
231 shift
= s
->cluster_bits
+ s
->l2_bits
;
232 s
->l1_vm_state_index
= (header
.size
+ (1LL << shift
) - 1) >> shift
;
233 /* the L1 table must contain at least enough entries to put
235 if (s
->l1_size
< s
->l1_vm_state_index
)
237 s
->l1_table_offset
= header
.l1_table_offset
;
238 s
->l1_table
= qemu_malloc(s
->l1_size
* sizeof(uint64_t));
239 if (bdrv_pread(s
->hd
, s
->l1_table_offset
, s
->l1_table
, s
->l1_size
* sizeof(uint64_t)) !=
240 s
->l1_size
* sizeof(uint64_t))
242 for(i
= 0;i
< s
->l1_size
; i
++) {
243 be64_to_cpus(&s
->l1_table
[i
]);
246 s
->l2_cache
= qemu_malloc(s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
247 s
->cluster_cache
= qemu_malloc(s
->cluster_size
);
248 /* one more sector for decompressed data alignment */
249 s
->cluster_data
= qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
251 s
->cluster_cache_offset
= -1;
253 if (refcount_init(bs
) < 0)
256 /* read qcow2 extensions */
257 if (header
.backing_file_offset
)
258 ext_end
= header
.backing_file_offset
;
260 ext_end
= s
->cluster_size
;
261 if (qcow_read_extensions(bs
, sizeof(header
), ext_end
))
264 /* read the backing file name */
265 if (header
.backing_file_offset
!= 0) {
266 len
= header
.backing_file_size
;
269 if (bdrv_pread(s
->hd
, header
.backing_file_offset
, bs
->backing_file
, len
) != len
)
271 bs
->backing_file
[len
] = '\0';
273 if (qcow_read_snapshots(bs
) < 0)
282 qcow_free_snapshots(bs
);
284 qemu_free(s
->l1_table
);
285 qemu_free(s
->l2_cache
);
286 qemu_free(s
->cluster_cache
);
287 qemu_free(s
->cluster_data
);
292 static int qcow_set_key(BlockDriverState
*bs
, const char *key
)
294 BDRVQcowState
*s
= bs
->opaque
;
298 memset(keybuf
, 0, 16);
302 /* XXX: we could compress the chars to 7 bits to increase
304 for(i
= 0;i
< len
;i
++) {
307 s
->crypt_method
= s
->crypt_method_header
;
309 if (AES_set_encrypt_key(keybuf
, 128, &s
->aes_encrypt_key
) != 0)
311 if (AES_set_decrypt_key(keybuf
, 128, &s
->aes_decrypt_key
) != 0)
321 AES_encrypt(in
, tmp
, &s
->aes_encrypt_key
);
322 AES_decrypt(tmp
, out
, &s
->aes_decrypt_key
);
323 for(i
= 0; i
< 16; i
++)
324 printf(" %02x", tmp
[i
]);
326 for(i
= 0; i
< 16; i
++)
327 printf(" %02x", out
[i
]);
334 static int64_t align_offset(int64_t offset
, int n
)
336 offset
= (offset
+ n
- 1) & ~(n
- 1);
340 static int qcow_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
341 int nb_sectors
, int *pnum
)
343 uint64_t cluster_offset
;
346 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, pnum
);
348 return (cluster_offset
!= 0);
351 /* handle reading after the end of the backing file */
352 int backing_read1(BlockDriverState
*bs
,
353 int64_t sector_num
, uint8_t *buf
, int nb_sectors
)
356 if ((sector_num
+ nb_sectors
) <= bs
->total_sectors
)
358 if (sector_num
>= bs
->total_sectors
)
361 n1
= bs
->total_sectors
- sector_num
;
362 memset(buf
+ n1
* 512, 0, 512 * (nb_sectors
- n1
));
366 typedef struct QCowAIOCB
{
367 BlockDriverAIOCB common
;
374 uint64_t cluster_offset
;
375 uint8_t *cluster_data
;
376 BlockDriverAIOCB
*hd_aiocb
;
378 QEMUIOVector hd_qiov
;
383 static void qcow_aio_cancel(BlockDriverAIOCB
*blockacb
)
385 QCowAIOCB
*acb
= (QCowAIOCB
*)blockacb
;
387 bdrv_aio_cancel(acb
->hd_aiocb
);
388 qemu_aio_release(acb
);
391 static AIOPool qcow_aio_pool
= {
392 .aiocb_size
= sizeof(QCowAIOCB
),
393 .cancel
= qcow_aio_cancel
,
396 static void qcow_aio_read_cb(void *opaque
, int ret
);
397 static void qcow_aio_read_bh(void *opaque
)
399 QCowAIOCB
*acb
= opaque
;
400 qemu_bh_delete(acb
->bh
);
402 qcow_aio_read_cb(opaque
, 0);
405 static int qcow_schedule_bh(QEMUBHFunc
*cb
, QCowAIOCB
*acb
)
410 acb
->bh
= qemu_bh_new(cb
, acb
);
414 qemu_bh_schedule(acb
->bh
);
419 static void qcow_aio_read_cb(void *opaque
, int ret
)
421 QCowAIOCB
*acb
= opaque
;
422 BlockDriverState
*bs
= acb
->common
.bs
;
423 BDRVQcowState
*s
= bs
->opaque
;
424 int index_in_cluster
, n1
;
426 acb
->hd_aiocb
= NULL
;
430 /* post process the read buffer */
431 if (!acb
->cluster_offset
) {
433 } else if (acb
->cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
436 if (s
->crypt_method
) {
437 encrypt_sectors(s
, acb
->sector_num
, acb
->buf
, acb
->buf
,
439 &s
->aes_decrypt_key
);
443 acb
->nb_sectors
-= acb
->n
;
444 acb
->sector_num
+= acb
->n
;
445 acb
->buf
+= acb
->n
* 512;
447 if (acb
->nb_sectors
== 0) {
448 /* request completed */
453 /* prepare next AIO request */
454 acb
->n
= acb
->nb_sectors
;
455 acb
->cluster_offset
= get_cluster_offset(bs
, acb
->sector_num
<< 9, &acb
->n
);
456 index_in_cluster
= acb
->sector_num
& (s
->cluster_sectors
- 1);
458 if (!acb
->cluster_offset
) {
459 if (bs
->backing_hd
) {
460 /* read from the base image */
461 n1
= backing_read1(bs
->backing_hd
, acb
->sector_num
,
464 acb
->hd_iov
.iov_base
= (void *)acb
->buf
;
465 acb
->hd_iov
.iov_len
= acb
->n
* 512;
466 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
467 acb
->hd_aiocb
= bdrv_aio_readv(bs
->backing_hd
, acb
->sector_num
,
468 &acb
->hd_qiov
, acb
->n
,
469 qcow_aio_read_cb
, acb
);
470 if (acb
->hd_aiocb
== NULL
)
473 ret
= qcow_schedule_bh(qcow_aio_read_bh
, acb
);
478 /* Note: in this case, no need to wait */
479 memset(acb
->buf
, 0, 512 * acb
->n
);
480 ret
= qcow_schedule_bh(qcow_aio_read_bh
, acb
);
484 } else if (acb
->cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
485 /* add AIO support for compressed blocks ? */
486 if (decompress_cluster(s
, acb
->cluster_offset
) < 0)
489 s
->cluster_cache
+ index_in_cluster
* 512, 512 * acb
->n
);
490 ret
= qcow_schedule_bh(qcow_aio_read_bh
, acb
);
494 if ((acb
->cluster_offset
& 511) != 0) {
499 acb
->hd_iov
.iov_base
= (void *)acb
->buf
;
500 acb
->hd_iov
.iov_len
= acb
->n
* 512;
501 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
502 acb
->hd_aiocb
= bdrv_aio_readv(s
->hd
,
503 (acb
->cluster_offset
>> 9) + index_in_cluster
,
504 &acb
->hd_qiov
, acb
->n
, qcow_aio_read_cb
, acb
);
505 if (acb
->hd_aiocb
== NULL
)
511 if (acb
->qiov
->niov
> 1) {
512 qemu_iovec_from_buffer(acb
->qiov
, acb
->orig_buf
, acb
->qiov
->size
);
513 qemu_vfree(acb
->orig_buf
);
515 acb
->common
.cb(acb
->common
.opaque
, ret
);
516 qemu_aio_release(acb
);
519 static QCowAIOCB
*qcow_aio_setup(BlockDriverState
*bs
,
520 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
521 BlockDriverCompletionFunc
*cb
, void *opaque
, int is_write
)
525 acb
= qemu_aio_get(&qcow_aio_pool
, bs
, cb
, opaque
);
528 acb
->hd_aiocb
= NULL
;
529 acb
->sector_num
= sector_num
;
531 if (qiov
->niov
> 1) {
532 acb
->buf
= acb
->orig_buf
= qemu_blockalign(bs
, qiov
->size
);
534 qemu_iovec_to_buffer(qiov
, acb
->buf
);
536 acb
->buf
= (uint8_t *)qiov
->iov
->iov_base
;
538 acb
->nb_sectors
= nb_sectors
;
540 acb
->cluster_offset
= 0;
541 acb
->l2meta
.nb_clusters
= 0;
545 static BlockDriverAIOCB
*qcow_aio_readv(BlockDriverState
*bs
,
546 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
547 BlockDriverCompletionFunc
*cb
, void *opaque
)
551 acb
= qcow_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
555 qcow_aio_read_cb(acb
, 0);
559 static void qcow_aio_write_cb(void *opaque
, int ret
)
561 QCowAIOCB
*acb
= opaque
;
562 BlockDriverState
*bs
= acb
->common
.bs
;
563 BDRVQcowState
*s
= bs
->opaque
;
564 int index_in_cluster
;
565 const uint8_t *src_buf
;
568 acb
->hd_aiocb
= NULL
;
573 if (alloc_cluster_link_l2(bs
, acb
->cluster_offset
, &acb
->l2meta
) < 0) {
574 free_any_clusters(bs
, acb
->cluster_offset
, acb
->l2meta
.nb_clusters
);
578 acb
->nb_sectors
-= acb
->n
;
579 acb
->sector_num
+= acb
->n
;
580 acb
->buf
+= acb
->n
* 512;
582 if (acb
->nb_sectors
== 0) {
583 /* request completed */
588 index_in_cluster
= acb
->sector_num
& (s
->cluster_sectors
- 1);
589 n_end
= index_in_cluster
+ acb
->nb_sectors
;
590 if (s
->crypt_method
&&
591 n_end
> QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
)
592 n_end
= QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
;
594 acb
->cluster_offset
= alloc_cluster_offset(bs
, acb
->sector_num
<< 9,
596 n_end
, &acb
->n
, &acb
->l2meta
);
597 if (!acb
->cluster_offset
|| (acb
->cluster_offset
& 511) != 0) {
601 if (s
->crypt_method
) {
602 if (!acb
->cluster_data
) {
603 acb
->cluster_data
= qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS
*
606 encrypt_sectors(s
, acb
->sector_num
, acb
->cluster_data
, acb
->buf
,
607 acb
->n
, 1, &s
->aes_encrypt_key
);
608 src_buf
= acb
->cluster_data
;
612 acb
->hd_iov
.iov_base
= (void *)src_buf
;
613 acb
->hd_iov
.iov_len
= acb
->n
* 512;
614 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
615 acb
->hd_aiocb
= bdrv_aio_writev(s
->hd
,
616 (acb
->cluster_offset
>> 9) + index_in_cluster
,
617 &acb
->hd_qiov
, acb
->n
,
618 qcow_aio_write_cb
, acb
);
619 if (acb
->hd_aiocb
== NULL
)
625 if (acb
->qiov
->niov
> 1)
626 qemu_vfree(acb
->orig_buf
);
627 acb
->common
.cb(acb
->common
.opaque
, ret
);
628 qemu_aio_release(acb
);
631 static BlockDriverAIOCB
*qcow_aio_writev(BlockDriverState
*bs
,
632 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
633 BlockDriverCompletionFunc
*cb
, void *opaque
)
635 BDRVQcowState
*s
= bs
->opaque
;
638 s
->cluster_cache_offset
= -1; /* disable compressed cache */
640 acb
= qcow_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
644 qcow_aio_write_cb(acb
, 0);
648 static void qcow_close(BlockDriverState
*bs
)
650 BDRVQcowState
*s
= bs
->opaque
;
651 qemu_free(s
->l1_table
);
652 qemu_free(s
->l2_cache
);
653 qemu_free(s
->cluster_cache
);
654 qemu_free(s
->cluster_data
);
659 static int get_bits_from_size(size_t size
)
668 /* Not a power of two */
680 static int qcow_create2(const char *filename
, int64_t total_size
,
681 const char *backing_file
, const char *backing_format
,
682 int flags
, size_t cluster_size
)
685 int fd
, header_size
, backing_filename_len
, l1_size
, i
, shift
, l2_bits
;
686 int ref_clusters
, backing_format_len
= 0;
688 uint64_t tmp
, offset
;
689 QCowCreateState s1
, *s
= &s1
;
690 QCowExtension ext_bf
= {0, 0};
693 memset(s
, 0, sizeof(*s
));
695 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0644);
698 memset(&header
, 0, sizeof(header
));
699 header
.magic
= cpu_to_be32(QCOW_MAGIC
);
700 header
.version
= cpu_to_be32(QCOW_VERSION
);
701 header
.size
= cpu_to_be64(total_size
* 512);
702 header_size
= sizeof(header
);
703 backing_filename_len
= 0;
705 if (backing_format
) {
706 ext_bf
.magic
= QCOW_EXT_MAGIC_BACKING_FORMAT
;
707 backing_format_len
= strlen(backing_format
);
708 ext_bf
.len
= (backing_format_len
+ 7) & ~7;
709 header_size
+= ((sizeof(ext_bf
) + ext_bf
.len
+ 7) & ~7);
711 header
.backing_file_offset
= cpu_to_be64(header_size
);
712 backing_filename_len
= strlen(backing_file
);
713 header
.backing_file_size
= cpu_to_be32(backing_filename_len
);
714 header_size
+= backing_filename_len
;
718 s
->cluster_bits
= get_bits_from_size(cluster_size
);
719 if (s
->cluster_bits
< MIN_CLUSTER_BITS
||
720 s
->cluster_bits
> MAX_CLUSTER_BITS
)
722 fprintf(stderr
, "Cluster size must be a power of two between "
724 1 << MIN_CLUSTER_BITS
,
725 1 << (MAX_CLUSTER_BITS
- 10));
728 s
->cluster_size
= 1 << s
->cluster_bits
;
730 header
.cluster_bits
= cpu_to_be32(s
->cluster_bits
);
731 header_size
= (header_size
+ 7) & ~7;
732 if (flags
& BLOCK_FLAG_ENCRYPT
) {
733 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
735 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
737 l2_bits
= s
->cluster_bits
- 3;
738 shift
= s
->cluster_bits
+ l2_bits
;
739 l1_size
= (((total_size
* 512) + (1LL << shift
) - 1) >> shift
);
740 offset
= align_offset(header_size
, s
->cluster_size
);
741 s
->l1_table_offset
= offset
;
742 header
.l1_table_offset
= cpu_to_be64(s
->l1_table_offset
);
743 header
.l1_size
= cpu_to_be32(l1_size
);
744 offset
+= align_offset(l1_size
* sizeof(uint64_t), s
->cluster_size
);
746 s
->refcount_table
= qemu_mallocz(s
->cluster_size
);
748 s
->refcount_table_offset
= offset
;
749 header
.refcount_table_offset
= cpu_to_be64(offset
);
750 header
.refcount_table_clusters
= cpu_to_be32(1);
751 offset
+= s
->cluster_size
;
752 s
->refcount_block_offset
= offset
;
754 /* count how many refcount blocks needed */
755 tmp
= offset
>> s
->cluster_bits
;
756 ref_clusters
= (tmp
>> (s
->cluster_bits
- REFCOUNT_SHIFT
)) + 1;
757 for (i
=0; i
< ref_clusters
; i
++) {
758 s
->refcount_table
[i
] = cpu_to_be64(offset
);
759 offset
+= s
->cluster_size
;
762 s
->refcount_block
= qemu_mallocz(ref_clusters
* s
->cluster_size
);
764 /* update refcounts */
765 create_refcount_update(s
, 0, header_size
);
766 create_refcount_update(s
, s
->l1_table_offset
, l1_size
* sizeof(uint64_t));
767 create_refcount_update(s
, s
->refcount_table_offset
, s
->cluster_size
);
768 create_refcount_update(s
, s
->refcount_block_offset
, ref_clusters
* s
->cluster_size
);
770 /* write all the data */
771 write(fd
, &header
, sizeof(header
));
773 if (backing_format_len
) {
775 int d
= ext_bf
.len
- backing_format_len
;
777 memset(zero
, 0, sizeof(zero
));
778 cpu_to_be32s(&ext_bf
.magic
);
779 cpu_to_be32s(&ext_bf
.len
);
780 write(fd
, &ext_bf
, sizeof(ext_bf
));
781 write(fd
, backing_format
, backing_format_len
);
786 write(fd
, backing_file
, backing_filename_len
);
788 lseek(fd
, s
->l1_table_offset
, SEEK_SET
);
790 for(i
= 0;i
< l1_size
; i
++) {
791 write(fd
, &tmp
, sizeof(tmp
));
793 lseek(fd
, s
->refcount_table_offset
, SEEK_SET
);
794 write(fd
, s
->refcount_table
, s
->cluster_size
);
796 lseek(fd
, s
->refcount_block_offset
, SEEK_SET
);
797 write(fd
, s
->refcount_block
, ref_clusters
* s
->cluster_size
);
799 qemu_free(s
->refcount_table
);
800 qemu_free(s
->refcount_block
);
805 static int qcow_create(const char *filename
, QEMUOptionParameter
*options
)
807 const char *backing_file
= NULL
;
808 const char *backing_fmt
= NULL
;
809 uint64_t sectors
= 0;
811 size_t cluster_size
= 65536;
813 /* Read out options */
814 while (options
&& options
->name
) {
815 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
816 sectors
= options
->value
.n
/ 512;
817 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
818 backing_file
= options
->value
.s
;
819 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FMT
)) {
820 backing_fmt
= options
->value
.s
;
821 } else if (!strcmp(options
->name
, BLOCK_OPT_ENCRYPT
)) {
822 flags
|= options
->value
.n
? BLOCK_FLAG_ENCRYPT
: 0;
823 } else if (!strcmp(options
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
824 if (options
->value
.n
) {
825 cluster_size
= options
->value
.n
;
831 return qcow_create2(filename
, sectors
, backing_file
, backing_fmt
, flags
,
835 static int qcow_make_empty(BlockDriverState
*bs
)
838 /* XXX: not correct */
839 BDRVQcowState
*s
= bs
->opaque
;
840 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
843 memset(s
->l1_table
, 0, l1_length
);
844 if (bdrv_pwrite(s
->hd
, s
->l1_table_offset
, s
->l1_table
, l1_length
) < 0)
846 ret
= bdrv_truncate(s
->hd
, s
->l1_table_offset
+ l1_length
);
855 /* XXX: put compressed sectors first, then all the cluster aligned
856 tables to avoid losing bytes in alignment */
857 static int qcow_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
858 const uint8_t *buf
, int nb_sectors
)
860 BDRVQcowState
*s
= bs
->opaque
;
864 uint64_t cluster_offset
;
866 if (nb_sectors
== 0) {
867 /* align end of file to a sector boundary to ease reading with
869 cluster_offset
= bdrv_getlength(s
->hd
);
870 cluster_offset
= (cluster_offset
+ 511) & ~511;
871 bdrv_truncate(s
->hd
, cluster_offset
);
875 if (nb_sectors
!= s
->cluster_sectors
)
878 out_buf
= qemu_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
880 /* best compression, small window, no zlib header */
881 memset(&strm
, 0, sizeof(strm
));
882 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
884 9, Z_DEFAULT_STRATEGY
);
890 strm
.avail_in
= s
->cluster_size
;
891 strm
.next_in
= (uint8_t *)buf
;
892 strm
.avail_out
= s
->cluster_size
;
893 strm
.next_out
= out_buf
;
895 ret
= deflate(&strm
, Z_FINISH
);
896 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
901 out_len
= strm
.next_out
- out_buf
;
905 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
906 /* could not compress: write normal cluster */
907 bdrv_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
909 cluster_offset
= alloc_compressed_cluster_offset(bs
, sector_num
<< 9,
913 cluster_offset
&= s
->cluster_offset_mask
;
914 if (bdrv_pwrite(s
->hd
, cluster_offset
, out_buf
, out_len
) != out_len
) {
924 static void qcow_flush(BlockDriverState
*bs
)
926 BDRVQcowState
*s
= bs
->opaque
;
930 static int qcow_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
932 BDRVQcowState
*s
= bs
->opaque
;
933 bdi
->cluster_size
= s
->cluster_size
;
934 bdi
->vm_state_offset
= (int64_t)s
->l1_vm_state_index
<<
935 (s
->cluster_bits
+ s
->l2_bits
);
939 /*********************************************************/
940 /* snapshot support */
943 static void qcow_free_snapshots(BlockDriverState
*bs
)
945 BDRVQcowState
*s
= bs
->opaque
;
948 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
949 qemu_free(s
->snapshots
[i
].name
);
950 qemu_free(s
->snapshots
[i
].id_str
);
952 qemu_free(s
->snapshots
);
957 static int qcow_read_snapshots(BlockDriverState
*bs
)
959 BDRVQcowState
*s
= bs
->opaque
;
960 QCowSnapshotHeader h
;
962 int i
, id_str_size
, name_size
;
964 uint32_t extra_data_size
;
966 if (!s
->nb_snapshots
) {
968 s
->snapshots_size
= 0;
972 offset
= s
->snapshots_offset
;
973 s
->snapshots
= qemu_mallocz(s
->nb_snapshots
* sizeof(QCowSnapshot
));
974 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
975 offset
= align_offset(offset
, 8);
976 if (bdrv_pread(s
->hd
, offset
, &h
, sizeof(h
)) != sizeof(h
))
979 sn
= s
->snapshots
+ i
;
980 sn
->l1_table_offset
= be64_to_cpu(h
.l1_table_offset
);
981 sn
->l1_size
= be32_to_cpu(h
.l1_size
);
982 sn
->vm_state_size
= be32_to_cpu(h
.vm_state_size
);
983 sn
->date_sec
= be32_to_cpu(h
.date_sec
);
984 sn
->date_nsec
= be32_to_cpu(h
.date_nsec
);
985 sn
->vm_clock_nsec
= be64_to_cpu(h
.vm_clock_nsec
);
986 extra_data_size
= be32_to_cpu(h
.extra_data_size
);
988 id_str_size
= be16_to_cpu(h
.id_str_size
);
989 name_size
= be16_to_cpu(h
.name_size
);
991 offset
+= extra_data_size
;
993 sn
->id_str
= qemu_malloc(id_str_size
+ 1);
994 if (bdrv_pread(s
->hd
, offset
, sn
->id_str
, id_str_size
) != id_str_size
)
996 offset
+= id_str_size
;
997 sn
->id_str
[id_str_size
] = '\0';
999 sn
->name
= qemu_malloc(name_size
+ 1);
1000 if (bdrv_pread(s
->hd
, offset
, sn
->name
, name_size
) != name_size
)
1002 offset
+= name_size
;
1003 sn
->name
[name_size
] = '\0';
1005 s
->snapshots_size
= offset
- s
->snapshots_offset
;
1008 qcow_free_snapshots(bs
);
1012 /* add at the end of the file a new list of snapshots */
1013 static int qcow_write_snapshots(BlockDriverState
*bs
)
1015 BDRVQcowState
*s
= bs
->opaque
;
1017 QCowSnapshotHeader h
;
1018 int i
, name_size
, id_str_size
, snapshots_size
;
1021 int64_t offset
, snapshots_offset
;
1023 /* compute the size of the snapshots */
1025 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1026 sn
= s
->snapshots
+ i
;
1027 offset
= align_offset(offset
, 8);
1028 offset
+= sizeof(h
);
1029 offset
+= strlen(sn
->id_str
);
1030 offset
+= strlen(sn
->name
);
1032 snapshots_size
= offset
;
1034 snapshots_offset
= alloc_clusters(bs
, snapshots_size
);
1035 offset
= snapshots_offset
;
1037 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1038 sn
= s
->snapshots
+ i
;
1039 memset(&h
, 0, sizeof(h
));
1040 h
.l1_table_offset
= cpu_to_be64(sn
->l1_table_offset
);
1041 h
.l1_size
= cpu_to_be32(sn
->l1_size
);
1042 h
.vm_state_size
= cpu_to_be32(sn
->vm_state_size
);
1043 h
.date_sec
= cpu_to_be32(sn
->date_sec
);
1044 h
.date_nsec
= cpu_to_be32(sn
->date_nsec
);
1045 h
.vm_clock_nsec
= cpu_to_be64(sn
->vm_clock_nsec
);
1047 id_str_size
= strlen(sn
->id_str
);
1048 name_size
= strlen(sn
->name
);
1049 h
.id_str_size
= cpu_to_be16(id_str_size
);
1050 h
.name_size
= cpu_to_be16(name_size
);
1051 offset
= align_offset(offset
, 8);
1052 if (bdrv_pwrite(s
->hd
, offset
, &h
, sizeof(h
)) != sizeof(h
))
1054 offset
+= sizeof(h
);
1055 if (bdrv_pwrite(s
->hd
, offset
, sn
->id_str
, id_str_size
) != id_str_size
)
1057 offset
+= id_str_size
;
1058 if (bdrv_pwrite(s
->hd
, offset
, sn
->name
, name_size
) != name_size
)
1060 offset
+= name_size
;
1063 /* update the various header fields */
1064 data64
= cpu_to_be64(snapshots_offset
);
1065 if (bdrv_pwrite(s
->hd
, offsetof(QCowHeader
, snapshots_offset
),
1066 &data64
, sizeof(data64
)) != sizeof(data64
))
1068 data32
= cpu_to_be32(s
->nb_snapshots
);
1069 if (bdrv_pwrite(s
->hd
, offsetof(QCowHeader
, nb_snapshots
),
1070 &data32
, sizeof(data32
)) != sizeof(data32
))
1073 /* free the old snapshot table */
1074 free_clusters(bs
, s
->snapshots_offset
, s
->snapshots_size
);
1075 s
->snapshots_offset
= snapshots_offset
;
1076 s
->snapshots_size
= snapshots_size
;
1082 static void find_new_snapshot_id(BlockDriverState
*bs
,
1083 char *id_str
, int id_str_size
)
1085 BDRVQcowState
*s
= bs
->opaque
;
1087 int i
, id
, id_max
= 0;
1089 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1090 sn
= s
->snapshots
+ i
;
1091 id
= strtoul(sn
->id_str
, NULL
, 10);
1095 snprintf(id_str
, id_str_size
, "%d", id_max
+ 1);
1098 static int find_snapshot_by_id(BlockDriverState
*bs
, const char *id_str
)
1100 BDRVQcowState
*s
= bs
->opaque
;
1103 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1104 if (!strcmp(s
->snapshots
[i
].id_str
, id_str
))
1110 static int find_snapshot_by_id_or_name(BlockDriverState
*bs
, const char *name
)
1112 BDRVQcowState
*s
= bs
->opaque
;
1115 ret
= find_snapshot_by_id(bs
, name
);
1118 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1119 if (!strcmp(s
->snapshots
[i
].name
, name
))
1125 /* if no id is provided, a new one is constructed */
1126 static int qcow_snapshot_create(BlockDriverState
*bs
,
1127 QEMUSnapshotInfo
*sn_info
)
1129 BDRVQcowState
*s
= bs
->opaque
;
1130 QCowSnapshot
*snapshots1
, sn1
, *sn
= &sn1
;
1132 uint64_t *l1_table
= NULL
;
1134 memset(sn
, 0, sizeof(*sn
));
1136 if (sn_info
->id_str
[0] == '\0') {
1137 /* compute a new id */
1138 find_new_snapshot_id(bs
, sn_info
->id_str
, sizeof(sn_info
->id_str
));
1141 /* check that the ID is unique */
1142 if (find_snapshot_by_id(bs
, sn_info
->id_str
) >= 0)
1145 sn
->id_str
= qemu_strdup(sn_info
->id_str
);
1148 sn
->name
= qemu_strdup(sn_info
->name
);
1151 sn
->vm_state_size
= sn_info
->vm_state_size
;
1152 sn
->date_sec
= sn_info
->date_sec
;
1153 sn
->date_nsec
= sn_info
->date_nsec
;
1154 sn
->vm_clock_nsec
= sn_info
->vm_clock_nsec
;
1156 ret
= update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 1);
1160 /* create the L1 table of the snapshot */
1161 sn
->l1_table_offset
= alloc_clusters(bs
, s
->l1_size
* sizeof(uint64_t));
1162 sn
->l1_size
= s
->l1_size
;
1164 l1_table
= qemu_malloc(s
->l1_size
* sizeof(uint64_t));
1165 for(i
= 0; i
< s
->l1_size
; i
++) {
1166 l1_table
[i
] = cpu_to_be64(s
->l1_table
[i
]);
1168 if (bdrv_pwrite(s
->hd
, sn
->l1_table_offset
,
1169 l1_table
, s
->l1_size
* sizeof(uint64_t)) !=
1170 (s
->l1_size
* sizeof(uint64_t)))
1172 qemu_free(l1_table
);
1175 snapshots1
= qemu_malloc((s
->nb_snapshots
+ 1) * sizeof(QCowSnapshot
));
1177 memcpy(snapshots1
, s
->snapshots
, s
->nb_snapshots
* sizeof(QCowSnapshot
));
1178 qemu_free(s
->snapshots
);
1180 s
->snapshots
= snapshots1
;
1181 s
->snapshots
[s
->nb_snapshots
++] = *sn
;
1183 if (qcow_write_snapshots(bs
) < 0)
1186 check_refcounts(bs
);
1190 qemu_free(sn
->name
);
1191 qemu_free(l1_table
);
1195 /* copy the snapshot 'snapshot_name' into the current disk image */
1196 static int qcow_snapshot_goto(BlockDriverState
*bs
,
1197 const char *snapshot_id
)
1199 BDRVQcowState
*s
= bs
->opaque
;
1201 int i
, snapshot_index
, l1_size2
;
1203 snapshot_index
= find_snapshot_by_id_or_name(bs
, snapshot_id
);
1204 if (snapshot_index
< 0)
1206 sn
= &s
->snapshots
[snapshot_index
];
1208 if (update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, -1) < 0)
1211 if (grow_l1_table(bs
, sn
->l1_size
) < 0)
1214 s
->l1_size
= sn
->l1_size
;
1215 l1_size2
= s
->l1_size
* sizeof(uint64_t);
1216 /* copy the snapshot l1 table to the current l1 table */
1217 if (bdrv_pread(s
->hd
, sn
->l1_table_offset
,
1218 s
->l1_table
, l1_size2
) != l1_size2
)
1220 if (bdrv_pwrite(s
->hd
, s
->l1_table_offset
,
1221 s
->l1_table
, l1_size2
) != l1_size2
)
1223 for(i
= 0;i
< s
->l1_size
; i
++) {
1224 be64_to_cpus(&s
->l1_table
[i
]);
1227 if (update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 1) < 0)
1231 check_refcounts(bs
);
1238 static int qcow_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1240 BDRVQcowState
*s
= bs
->opaque
;
1242 int snapshot_index
, ret
;
1244 snapshot_index
= find_snapshot_by_id_or_name(bs
, snapshot_id
);
1245 if (snapshot_index
< 0)
1247 sn
= &s
->snapshots
[snapshot_index
];
1249 ret
= update_snapshot_refcount(bs
, sn
->l1_table_offset
, sn
->l1_size
, -1);
1252 /* must update the copied flag on the current cluster offsets */
1253 ret
= update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 0);
1256 free_clusters(bs
, sn
->l1_table_offset
, sn
->l1_size
* sizeof(uint64_t));
1258 qemu_free(sn
->id_str
);
1259 qemu_free(sn
->name
);
1260 memmove(sn
, sn
+ 1, (s
->nb_snapshots
- snapshot_index
- 1) * sizeof(*sn
));
1262 ret
= qcow_write_snapshots(bs
);
1264 /* XXX: restore snapshot if error ? */
1268 check_refcounts(bs
);
1273 static int qcow_snapshot_list(BlockDriverState
*bs
,
1274 QEMUSnapshotInfo
**psn_tab
)
1276 BDRVQcowState
*s
= bs
->opaque
;
1277 QEMUSnapshotInfo
*sn_tab
, *sn_info
;
1281 if (!s
->nb_snapshots
) {
1283 return s
->nb_snapshots
;
1286 sn_tab
= qemu_mallocz(s
->nb_snapshots
* sizeof(QEMUSnapshotInfo
));
1287 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1288 sn_info
= sn_tab
+ i
;
1289 sn
= s
->snapshots
+ i
;
1290 pstrcpy(sn_info
->id_str
, sizeof(sn_info
->id_str
),
1292 pstrcpy(sn_info
->name
, sizeof(sn_info
->name
),
1294 sn_info
->vm_state_size
= sn
->vm_state_size
;
1295 sn_info
->date_sec
= sn
->date_sec
;
1296 sn_info
->date_nsec
= sn
->date_nsec
;
1297 sn_info
->vm_clock_nsec
= sn
->vm_clock_nsec
;
1300 return s
->nb_snapshots
;
1303 static int qcow_check(BlockDriverState
*bs
)
1305 return check_refcounts(bs
);
1309 static void dump_refcounts(BlockDriverState
*bs
)
1311 BDRVQcowState
*s
= bs
->opaque
;
1312 int64_t nb_clusters
, k
, k1
, size
;
1315 size
= bdrv_getlength(s
->hd
);
1316 nb_clusters
= size_to_clusters(s
, size
);
1317 for(k
= 0; k
< nb_clusters
;) {
1319 refcount
= get_refcount(bs
, k
);
1321 while (k
< nb_clusters
&& get_refcount(bs
, k
) == refcount
)
1323 printf("%lld: refcount=%d nb=%lld\n", k
, refcount
, k
- k1
);
1328 static int qcow_put_buffer(BlockDriverState
*bs
, const uint8_t *buf
,
1329 int64_t pos
, int size
)
1331 int growable
= bs
->growable
;
1334 bdrv_pwrite(bs
, pos
, buf
, size
);
1335 bs
->growable
= growable
;
1340 static int qcow_get_buffer(BlockDriverState
*bs
, uint8_t *buf
,
1341 int64_t pos
, int size
)
1343 int growable
= bs
->growable
;
1347 ret
= bdrv_pread(bs
, pos
, buf
, size
);
1348 bs
->growable
= growable
;
1353 static QEMUOptionParameter qcow_create_options
[] = {
1355 .name
= BLOCK_OPT_SIZE
,
1357 .help
= "Virtual disk size"
1360 .name
= BLOCK_OPT_BACKING_FILE
,
1362 .help
= "File name of a base image"
1365 .name
= BLOCK_OPT_BACKING_FMT
,
1367 .help
= "Image format of the base image"
1370 .name
= BLOCK_OPT_ENCRYPT
,
1372 .help
= "Encrypt the image"
1375 .name
= BLOCK_OPT_CLUSTER_SIZE
,
1377 .help
= "qcow2 cluster size"
1382 static BlockDriver bdrv_qcow2
= {
1383 .format_name
= "qcow2",
1384 .instance_size
= sizeof(BDRVQcowState
),
1385 .bdrv_probe
= qcow_probe
,
1386 .bdrv_open
= qcow_open
,
1387 .bdrv_close
= qcow_close
,
1388 .bdrv_create
= qcow_create
,
1389 .bdrv_flush
= qcow_flush
,
1390 .bdrv_is_allocated
= qcow_is_allocated
,
1391 .bdrv_set_key
= qcow_set_key
,
1392 .bdrv_make_empty
= qcow_make_empty
,
1394 .bdrv_aio_readv
= qcow_aio_readv
,
1395 .bdrv_aio_writev
= qcow_aio_writev
,
1396 .bdrv_write_compressed
= qcow_write_compressed
,
1398 .bdrv_snapshot_create
= qcow_snapshot_create
,
1399 .bdrv_snapshot_goto
= qcow_snapshot_goto
,
1400 .bdrv_snapshot_delete
= qcow_snapshot_delete
,
1401 .bdrv_snapshot_list
= qcow_snapshot_list
,
1402 .bdrv_get_info
= qcow_get_info
,
1404 .bdrv_put_buffer
= qcow_put_buffer
,
1405 .bdrv_get_buffer
= qcow_get_buffer
,
1407 .create_options
= qcow_create_options
,
1408 .bdrv_check
= qcow_check
,
1411 static void bdrv_qcow2_init(void)
1413 bdrv_register(&bdrv_qcow2
);
1416 block_init(bdrv_qcow2_init
);