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
61 static int qcow_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
63 const QCowHeader
*cow_header
= (const void *)buf
;
65 if (buf_size
>= sizeof(QCowHeader
) &&
66 be32_to_cpu(cow_header
->magic
) == QCOW_MAGIC
&&
67 be32_to_cpu(cow_header
->version
) == QCOW_VERSION
)
75 * read qcow2 extension and fill bs
76 * start reading from start_offset
77 * finish reading upon magic of value 0 or when end_offset reached
78 * unknown magic is skipped (future extension this version knows nothing about)
79 * return 0 upon success, non-0 otherwise
81 static int qcow_read_extensions(BlockDriverState
*bs
, uint64_t start_offset
,
84 BDRVQcowState
*s
= bs
->opaque
;
89 printf("qcow_read_extensions: start=%ld end=%ld\n", start_offset
, end_offset
);
91 offset
= start_offset
;
92 while (offset
< end_offset
) {
96 if (offset
> s
->cluster_size
)
97 printf("qcow_handle_extension: suspicious offset %lu\n", offset
);
99 printf("attemting to read extended header in offset %lu\n", offset
);
102 if (bdrv_pread(s
->hd
, offset
, &ext
, sizeof(ext
)) != sizeof(ext
)) {
103 fprintf(stderr
, "qcow_handle_extension: ERROR: pread fail from offset %llu\n",
104 (unsigned long long)offset
);
107 be32_to_cpus(&ext
.magic
);
108 be32_to_cpus(&ext
.len
);
109 offset
+= sizeof(ext
);
111 printf("ext.magic = 0x%x\n", ext
.magic
);
114 case QCOW_EXT_MAGIC_END
:
117 case QCOW_EXT_MAGIC_BACKING_FORMAT
:
118 if (ext
.len
>= sizeof(bs
->backing_format
)) {
119 fprintf(stderr
, "ERROR: ext_backing_format: len=%u too large"
121 ext
.len
, sizeof(bs
->backing_format
));
124 if (bdrv_pread(s
->hd
, offset
, bs
->backing_format
,
127 bs
->backing_format
[ext
.len
] = '\0';
129 printf("Qcow2: Got format extension %s\n", bs
->backing_format
);
131 offset
+= ((ext
.len
+ 7) & ~7);
135 /* unknown magic -- just skip it */
136 offset
+= ((ext
.len
+ 7) & ~7);
145 static int qcow_open(BlockDriverState
*bs
, const char *filename
, int flags
)
147 BDRVQcowState
*s
= bs
->opaque
;
148 int len
, i
, shift
, ret
;
152 /* Performance is terrible right now with cache=writethrough due mainly
153 * to reference count updates. If the user does not explicitly specify
154 * a caching type, force to writeback caching.
156 if ((flags
& BDRV_O_CACHE_DEF
)) {
157 flags
|= BDRV_O_CACHE_WB
;
158 flags
&= ~BDRV_O_CACHE_DEF
;
160 ret
= bdrv_file_open(&s
->hd
, filename
, flags
);
163 if (bdrv_pread(s
->hd
, 0, &header
, sizeof(header
)) != sizeof(header
))
165 be32_to_cpus(&header
.magic
);
166 be32_to_cpus(&header
.version
);
167 be64_to_cpus(&header
.backing_file_offset
);
168 be32_to_cpus(&header
.backing_file_size
);
169 be64_to_cpus(&header
.size
);
170 be32_to_cpus(&header
.cluster_bits
);
171 be32_to_cpus(&header
.crypt_method
);
172 be64_to_cpus(&header
.l1_table_offset
);
173 be32_to_cpus(&header
.l1_size
);
174 be64_to_cpus(&header
.refcount_table_offset
);
175 be32_to_cpus(&header
.refcount_table_clusters
);
176 be64_to_cpus(&header
.snapshots_offset
);
177 be32_to_cpus(&header
.nb_snapshots
);
179 if (header
.magic
!= QCOW_MAGIC
|| header
.version
!= QCOW_VERSION
)
181 if (header
.size
<= 1 ||
182 header
.cluster_bits
< MIN_CLUSTER_BITS
||
183 header
.cluster_bits
> MAX_CLUSTER_BITS
)
185 if (header
.crypt_method
> QCOW_CRYPT_AES
)
187 s
->crypt_method_header
= header
.crypt_method
;
188 if (s
->crypt_method_header
)
190 s
->cluster_bits
= header
.cluster_bits
;
191 s
->cluster_size
= 1 << s
->cluster_bits
;
192 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
193 s
->l2_bits
= s
->cluster_bits
- 3; /* L2 is always one cluster */
194 s
->l2_size
= 1 << s
->l2_bits
;
195 bs
->total_sectors
= header
.size
/ 512;
196 s
->csize_shift
= (62 - (s
->cluster_bits
- 8));
197 s
->csize_mask
= (1 << (s
->cluster_bits
- 8)) - 1;
198 s
->cluster_offset_mask
= (1LL << s
->csize_shift
) - 1;
199 s
->refcount_table_offset
= header
.refcount_table_offset
;
200 s
->refcount_table_size
=
201 header
.refcount_table_clusters
<< (s
->cluster_bits
- 3);
203 s
->snapshots_offset
= header
.snapshots_offset
;
204 s
->nb_snapshots
= header
.nb_snapshots
;
206 /* read the level 1 table */
207 s
->l1_size
= header
.l1_size
;
208 shift
= s
->cluster_bits
+ s
->l2_bits
;
209 s
->l1_vm_state_index
= (header
.size
+ (1LL << shift
) - 1) >> shift
;
210 /* the L1 table must contain at least enough entries to put
212 if (s
->l1_size
< s
->l1_vm_state_index
)
214 s
->l1_table_offset
= header
.l1_table_offset
;
215 s
->l1_table
= qemu_malloc(s
->l1_size
* sizeof(uint64_t));
216 if (bdrv_pread(s
->hd
, s
->l1_table_offset
, s
->l1_table
, s
->l1_size
* sizeof(uint64_t)) !=
217 s
->l1_size
* sizeof(uint64_t))
219 for(i
= 0;i
< s
->l1_size
; i
++) {
220 be64_to_cpus(&s
->l1_table
[i
]);
223 s
->l2_cache
= qemu_malloc(s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
224 s
->cluster_cache
= qemu_malloc(s
->cluster_size
);
225 /* one more sector for decompressed data alignment */
226 s
->cluster_data
= qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
228 s
->cluster_cache_offset
= -1;
230 if (refcount_init(bs
) < 0)
233 /* read qcow2 extensions */
234 if (header
.backing_file_offset
)
235 ext_end
= header
.backing_file_offset
;
237 ext_end
= s
->cluster_size
;
238 if (qcow_read_extensions(bs
, sizeof(header
), ext_end
))
241 /* read the backing file name */
242 if (header
.backing_file_offset
!= 0) {
243 len
= header
.backing_file_size
;
246 if (bdrv_pread(s
->hd
, header
.backing_file_offset
, bs
->backing_file
, len
) != len
)
248 bs
->backing_file
[len
] = '\0';
250 if (qcow_read_snapshots(bs
) < 0)
259 qcow_free_snapshots(bs
);
261 qemu_free(s
->l1_table
);
262 qemu_free(s
->l2_cache
);
263 qemu_free(s
->cluster_cache
);
264 qemu_free(s
->cluster_data
);
269 static int qcow_set_key(BlockDriverState
*bs
, const char *key
)
271 BDRVQcowState
*s
= bs
->opaque
;
275 memset(keybuf
, 0, 16);
279 /* XXX: we could compress the chars to 7 bits to increase
281 for(i
= 0;i
< len
;i
++) {
284 s
->crypt_method
= s
->crypt_method_header
;
286 if (AES_set_encrypt_key(keybuf
, 128, &s
->aes_encrypt_key
) != 0)
288 if (AES_set_decrypt_key(keybuf
, 128, &s
->aes_decrypt_key
) != 0)
298 AES_encrypt(in
, tmp
, &s
->aes_encrypt_key
);
299 AES_decrypt(tmp
, out
, &s
->aes_decrypt_key
);
300 for(i
= 0; i
< 16; i
++)
301 printf(" %02x", tmp
[i
]);
303 for(i
= 0; i
< 16; i
++)
304 printf(" %02x", out
[i
]);
311 static int qcow_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
312 int nb_sectors
, int *pnum
)
314 uint64_t cluster_offset
;
317 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, pnum
);
319 return (cluster_offset
!= 0);
322 /* handle reading after the end of the backing file */
323 int backing_read1(BlockDriverState
*bs
,
324 int64_t sector_num
, uint8_t *buf
, int nb_sectors
)
327 if ((sector_num
+ nb_sectors
) <= bs
->total_sectors
)
329 if (sector_num
>= bs
->total_sectors
)
332 n1
= bs
->total_sectors
- sector_num
;
333 memset(buf
+ n1
* 512, 0, 512 * (nb_sectors
- n1
));
337 typedef struct QCowAIOCB
{
338 BlockDriverAIOCB common
;
345 uint64_t cluster_offset
;
346 uint8_t *cluster_data
;
347 BlockDriverAIOCB
*hd_aiocb
;
349 QEMUIOVector hd_qiov
;
354 static void qcow_aio_cancel(BlockDriverAIOCB
*blockacb
)
356 QCowAIOCB
*acb
= (QCowAIOCB
*)blockacb
;
358 bdrv_aio_cancel(acb
->hd_aiocb
);
359 qemu_aio_release(acb
);
362 static AIOPool qcow_aio_pool
= {
363 .aiocb_size
= sizeof(QCowAIOCB
),
364 .cancel
= qcow_aio_cancel
,
367 static void qcow_aio_read_cb(void *opaque
, int ret
);
368 static void qcow_aio_read_bh(void *opaque
)
370 QCowAIOCB
*acb
= opaque
;
371 qemu_bh_delete(acb
->bh
);
373 qcow_aio_read_cb(opaque
, 0);
376 static int qcow_schedule_bh(QEMUBHFunc
*cb
, QCowAIOCB
*acb
)
381 acb
->bh
= qemu_bh_new(cb
, acb
);
385 qemu_bh_schedule(acb
->bh
);
390 static void qcow_aio_read_cb(void *opaque
, int ret
)
392 QCowAIOCB
*acb
= opaque
;
393 BlockDriverState
*bs
= acb
->common
.bs
;
394 BDRVQcowState
*s
= bs
->opaque
;
395 int index_in_cluster
, n1
;
397 acb
->hd_aiocb
= NULL
;
401 /* post process the read buffer */
402 if (!acb
->cluster_offset
) {
404 } else if (acb
->cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
407 if (s
->crypt_method
) {
408 encrypt_sectors(s
, acb
->sector_num
, acb
->buf
, acb
->buf
,
410 &s
->aes_decrypt_key
);
414 acb
->nb_sectors
-= acb
->n
;
415 acb
->sector_num
+= acb
->n
;
416 acb
->buf
+= acb
->n
* 512;
418 if (acb
->nb_sectors
== 0) {
419 /* request completed */
424 /* prepare next AIO request */
425 acb
->n
= acb
->nb_sectors
;
426 acb
->cluster_offset
= get_cluster_offset(bs
, acb
->sector_num
<< 9, &acb
->n
);
427 index_in_cluster
= acb
->sector_num
& (s
->cluster_sectors
- 1);
429 if (!acb
->cluster_offset
) {
430 if (bs
->backing_hd
) {
431 /* read from the base image */
432 n1
= backing_read1(bs
->backing_hd
, acb
->sector_num
,
435 acb
->hd_iov
.iov_base
= (void *)acb
->buf
;
436 acb
->hd_iov
.iov_len
= acb
->n
* 512;
437 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
438 acb
->hd_aiocb
= bdrv_aio_readv(bs
->backing_hd
, acb
->sector_num
,
439 &acb
->hd_qiov
, acb
->n
,
440 qcow_aio_read_cb
, acb
);
441 if (acb
->hd_aiocb
== NULL
)
444 ret
= qcow_schedule_bh(qcow_aio_read_bh
, acb
);
449 /* Note: in this case, no need to wait */
450 memset(acb
->buf
, 0, 512 * acb
->n
);
451 ret
= qcow_schedule_bh(qcow_aio_read_bh
, acb
);
455 } else if (acb
->cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
456 /* add AIO support for compressed blocks ? */
457 if (decompress_cluster(s
, acb
->cluster_offset
) < 0)
460 s
->cluster_cache
+ index_in_cluster
* 512, 512 * acb
->n
);
461 ret
= qcow_schedule_bh(qcow_aio_read_bh
, acb
);
465 if ((acb
->cluster_offset
& 511) != 0) {
470 acb
->hd_iov
.iov_base
= (void *)acb
->buf
;
471 acb
->hd_iov
.iov_len
= acb
->n
* 512;
472 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
473 acb
->hd_aiocb
= bdrv_aio_readv(s
->hd
,
474 (acb
->cluster_offset
>> 9) + index_in_cluster
,
475 &acb
->hd_qiov
, acb
->n
, qcow_aio_read_cb
, acb
);
476 if (acb
->hd_aiocb
== NULL
)
482 if (acb
->qiov
->niov
> 1) {
483 qemu_iovec_from_buffer(acb
->qiov
, acb
->orig_buf
, acb
->qiov
->size
);
484 qemu_vfree(acb
->orig_buf
);
486 acb
->common
.cb(acb
->common
.opaque
, ret
);
487 qemu_aio_release(acb
);
490 static QCowAIOCB
*qcow_aio_setup(BlockDriverState
*bs
,
491 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
492 BlockDriverCompletionFunc
*cb
, void *opaque
, int is_write
)
496 acb
= qemu_aio_get(&qcow_aio_pool
, bs
, cb
, opaque
);
499 acb
->hd_aiocb
= NULL
;
500 acb
->sector_num
= sector_num
;
502 if (qiov
->niov
> 1) {
503 acb
->buf
= acb
->orig_buf
= qemu_blockalign(bs
, qiov
->size
);
505 qemu_iovec_to_buffer(qiov
, acb
->buf
);
507 acb
->buf
= (uint8_t *)qiov
->iov
->iov_base
;
509 acb
->nb_sectors
= nb_sectors
;
511 acb
->cluster_offset
= 0;
512 acb
->l2meta
.nb_clusters
= 0;
516 static BlockDriverAIOCB
*qcow_aio_readv(BlockDriverState
*bs
,
517 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
518 BlockDriverCompletionFunc
*cb
, void *opaque
)
522 acb
= qcow_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
526 qcow_aio_read_cb(acb
, 0);
530 static void qcow_aio_write_cb(void *opaque
, int ret
)
532 QCowAIOCB
*acb
= opaque
;
533 BlockDriverState
*bs
= acb
->common
.bs
;
534 BDRVQcowState
*s
= bs
->opaque
;
535 int index_in_cluster
;
536 const uint8_t *src_buf
;
539 acb
->hd_aiocb
= NULL
;
544 if (alloc_cluster_link_l2(bs
, acb
->cluster_offset
, &acb
->l2meta
) < 0) {
545 free_any_clusters(bs
, acb
->cluster_offset
, acb
->l2meta
.nb_clusters
);
549 acb
->nb_sectors
-= acb
->n
;
550 acb
->sector_num
+= acb
->n
;
551 acb
->buf
+= acb
->n
* 512;
553 if (acb
->nb_sectors
== 0) {
554 /* request completed */
559 index_in_cluster
= acb
->sector_num
& (s
->cluster_sectors
- 1);
560 n_end
= index_in_cluster
+ acb
->nb_sectors
;
561 if (s
->crypt_method
&&
562 n_end
> QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
)
563 n_end
= QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
;
565 acb
->cluster_offset
= alloc_cluster_offset(bs
, acb
->sector_num
<< 9,
567 n_end
, &acb
->n
, &acb
->l2meta
);
568 if (!acb
->cluster_offset
|| (acb
->cluster_offset
& 511) != 0) {
572 if (s
->crypt_method
) {
573 if (!acb
->cluster_data
) {
574 acb
->cluster_data
= qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS
*
577 encrypt_sectors(s
, acb
->sector_num
, acb
->cluster_data
, acb
->buf
,
578 acb
->n
, 1, &s
->aes_encrypt_key
);
579 src_buf
= acb
->cluster_data
;
583 acb
->hd_iov
.iov_base
= (void *)src_buf
;
584 acb
->hd_iov
.iov_len
= acb
->n
* 512;
585 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
586 acb
->hd_aiocb
= bdrv_aio_writev(s
->hd
,
587 (acb
->cluster_offset
>> 9) + index_in_cluster
,
588 &acb
->hd_qiov
, acb
->n
,
589 qcow_aio_write_cb
, acb
);
590 if (acb
->hd_aiocb
== NULL
)
596 if (acb
->qiov
->niov
> 1)
597 qemu_vfree(acb
->orig_buf
);
598 acb
->common
.cb(acb
->common
.opaque
, ret
);
599 qemu_aio_release(acb
);
602 static BlockDriverAIOCB
*qcow_aio_writev(BlockDriverState
*bs
,
603 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
604 BlockDriverCompletionFunc
*cb
, void *opaque
)
606 BDRVQcowState
*s
= bs
->opaque
;
609 s
->cluster_cache_offset
= -1; /* disable compressed cache */
611 acb
= qcow_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
615 qcow_aio_write_cb(acb
, 0);
619 static void qcow_close(BlockDriverState
*bs
)
621 BDRVQcowState
*s
= bs
->opaque
;
622 qemu_free(s
->l1_table
);
623 qemu_free(s
->l2_cache
);
624 qemu_free(s
->cluster_cache
);
625 qemu_free(s
->cluster_data
);
630 static int get_bits_from_size(size_t size
)
639 /* Not a power of two */
651 static int qcow_create2(const char *filename
, int64_t total_size
,
652 const char *backing_file
, const char *backing_format
,
653 int flags
, size_t cluster_size
)
656 int fd
, header_size
, backing_filename_len
, l1_size
, i
, shift
, l2_bits
;
657 int ref_clusters
, backing_format_len
= 0;
659 uint64_t tmp
, offset
;
660 QCowCreateState s1
, *s
= &s1
;
661 QCowExtension ext_bf
= {0, 0};
664 memset(s
, 0, sizeof(*s
));
666 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0644);
669 memset(&header
, 0, sizeof(header
));
670 header
.magic
= cpu_to_be32(QCOW_MAGIC
);
671 header
.version
= cpu_to_be32(QCOW_VERSION
);
672 header
.size
= cpu_to_be64(total_size
* 512);
673 header_size
= sizeof(header
);
674 backing_filename_len
= 0;
676 if (backing_format
) {
677 ext_bf
.magic
= QCOW_EXT_MAGIC_BACKING_FORMAT
;
678 backing_format_len
= strlen(backing_format
);
679 ext_bf
.len
= (backing_format_len
+ 7) & ~7;
680 header_size
+= ((sizeof(ext_bf
) + ext_bf
.len
+ 7) & ~7);
682 header
.backing_file_offset
= cpu_to_be64(header_size
);
683 backing_filename_len
= strlen(backing_file
);
684 header
.backing_file_size
= cpu_to_be32(backing_filename_len
);
685 header_size
+= backing_filename_len
;
689 s
->cluster_bits
= get_bits_from_size(cluster_size
);
690 if (s
->cluster_bits
< MIN_CLUSTER_BITS
||
691 s
->cluster_bits
> MAX_CLUSTER_BITS
)
693 fprintf(stderr
, "Cluster size must be a power of two between "
695 1 << MIN_CLUSTER_BITS
,
696 1 << (MAX_CLUSTER_BITS
- 10));
699 s
->cluster_size
= 1 << s
->cluster_bits
;
701 header
.cluster_bits
= cpu_to_be32(s
->cluster_bits
);
702 header_size
= (header_size
+ 7) & ~7;
703 if (flags
& BLOCK_FLAG_ENCRYPT
) {
704 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
706 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
708 l2_bits
= s
->cluster_bits
- 3;
709 shift
= s
->cluster_bits
+ l2_bits
;
710 l1_size
= (((total_size
* 512) + (1LL << shift
) - 1) >> shift
);
711 offset
= align_offset(header_size
, s
->cluster_size
);
712 s
->l1_table_offset
= offset
;
713 header
.l1_table_offset
= cpu_to_be64(s
->l1_table_offset
);
714 header
.l1_size
= cpu_to_be32(l1_size
);
715 offset
+= align_offset(l1_size
* sizeof(uint64_t), s
->cluster_size
);
717 s
->refcount_table
= qemu_mallocz(s
->cluster_size
);
719 s
->refcount_table_offset
= offset
;
720 header
.refcount_table_offset
= cpu_to_be64(offset
);
721 header
.refcount_table_clusters
= cpu_to_be32(1);
722 offset
+= s
->cluster_size
;
723 s
->refcount_block_offset
= offset
;
725 /* count how many refcount blocks needed */
726 tmp
= offset
>> s
->cluster_bits
;
727 ref_clusters
= (tmp
>> (s
->cluster_bits
- REFCOUNT_SHIFT
)) + 1;
728 for (i
=0; i
< ref_clusters
; i
++) {
729 s
->refcount_table
[i
] = cpu_to_be64(offset
);
730 offset
+= s
->cluster_size
;
733 s
->refcount_block
= qemu_mallocz(ref_clusters
* s
->cluster_size
);
735 /* update refcounts */
736 create_refcount_update(s
, 0, header_size
);
737 create_refcount_update(s
, s
->l1_table_offset
, l1_size
* sizeof(uint64_t));
738 create_refcount_update(s
, s
->refcount_table_offset
, s
->cluster_size
);
739 create_refcount_update(s
, s
->refcount_block_offset
, ref_clusters
* s
->cluster_size
);
741 /* write all the data */
742 write(fd
, &header
, sizeof(header
));
744 if (backing_format_len
) {
746 int d
= ext_bf
.len
- backing_format_len
;
748 memset(zero
, 0, sizeof(zero
));
749 cpu_to_be32s(&ext_bf
.magic
);
750 cpu_to_be32s(&ext_bf
.len
);
751 write(fd
, &ext_bf
, sizeof(ext_bf
));
752 write(fd
, backing_format
, backing_format_len
);
757 write(fd
, backing_file
, backing_filename_len
);
759 lseek(fd
, s
->l1_table_offset
, SEEK_SET
);
761 for(i
= 0;i
< l1_size
; i
++) {
762 write(fd
, &tmp
, sizeof(tmp
));
764 lseek(fd
, s
->refcount_table_offset
, SEEK_SET
);
765 write(fd
, s
->refcount_table
, s
->cluster_size
);
767 lseek(fd
, s
->refcount_block_offset
, SEEK_SET
);
768 write(fd
, s
->refcount_block
, ref_clusters
* s
->cluster_size
);
770 qemu_free(s
->refcount_table
);
771 qemu_free(s
->refcount_block
);
776 static int qcow_create(const char *filename
, QEMUOptionParameter
*options
)
778 const char *backing_file
= NULL
;
779 const char *backing_fmt
= NULL
;
780 uint64_t sectors
= 0;
782 size_t cluster_size
= 65536;
784 /* Read out options */
785 while (options
&& options
->name
) {
786 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
787 sectors
= options
->value
.n
/ 512;
788 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
789 backing_file
= options
->value
.s
;
790 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FMT
)) {
791 backing_fmt
= options
->value
.s
;
792 } else if (!strcmp(options
->name
, BLOCK_OPT_ENCRYPT
)) {
793 flags
|= options
->value
.n
? BLOCK_FLAG_ENCRYPT
: 0;
794 } else if (!strcmp(options
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
795 if (options
->value
.n
) {
796 cluster_size
= options
->value
.n
;
802 return qcow_create2(filename
, sectors
, backing_file
, backing_fmt
, flags
,
806 static int qcow_make_empty(BlockDriverState
*bs
)
809 /* XXX: not correct */
810 BDRVQcowState
*s
= bs
->opaque
;
811 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
814 memset(s
->l1_table
, 0, l1_length
);
815 if (bdrv_pwrite(s
->hd
, s
->l1_table_offset
, s
->l1_table
, l1_length
) < 0)
817 ret
= bdrv_truncate(s
->hd
, s
->l1_table_offset
+ l1_length
);
826 /* XXX: put compressed sectors first, then all the cluster aligned
827 tables to avoid losing bytes in alignment */
828 static int qcow_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
829 const uint8_t *buf
, int nb_sectors
)
831 BDRVQcowState
*s
= bs
->opaque
;
835 uint64_t cluster_offset
;
837 if (nb_sectors
== 0) {
838 /* align end of file to a sector boundary to ease reading with
840 cluster_offset
= bdrv_getlength(s
->hd
);
841 cluster_offset
= (cluster_offset
+ 511) & ~511;
842 bdrv_truncate(s
->hd
, cluster_offset
);
846 if (nb_sectors
!= s
->cluster_sectors
)
849 out_buf
= qemu_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
851 /* best compression, small window, no zlib header */
852 memset(&strm
, 0, sizeof(strm
));
853 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
855 9, Z_DEFAULT_STRATEGY
);
861 strm
.avail_in
= s
->cluster_size
;
862 strm
.next_in
= (uint8_t *)buf
;
863 strm
.avail_out
= s
->cluster_size
;
864 strm
.next_out
= out_buf
;
866 ret
= deflate(&strm
, Z_FINISH
);
867 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
872 out_len
= strm
.next_out
- out_buf
;
876 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
877 /* could not compress: write normal cluster */
878 bdrv_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
880 cluster_offset
= alloc_compressed_cluster_offset(bs
, sector_num
<< 9,
884 cluster_offset
&= s
->cluster_offset_mask
;
885 if (bdrv_pwrite(s
->hd
, cluster_offset
, out_buf
, out_len
) != out_len
) {
895 static void qcow_flush(BlockDriverState
*bs
)
897 BDRVQcowState
*s
= bs
->opaque
;
901 static int qcow_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
903 BDRVQcowState
*s
= bs
->opaque
;
904 bdi
->cluster_size
= s
->cluster_size
;
905 bdi
->vm_state_offset
= (int64_t)s
->l1_vm_state_index
<<
906 (s
->cluster_bits
+ s
->l2_bits
);
911 static int qcow_check(BlockDriverState
*bs
)
913 return check_refcounts(bs
);
917 static void dump_refcounts(BlockDriverState
*bs
)
919 BDRVQcowState
*s
= bs
->opaque
;
920 int64_t nb_clusters
, k
, k1
, size
;
923 size
= bdrv_getlength(s
->hd
);
924 nb_clusters
= size_to_clusters(s
, size
);
925 for(k
= 0; k
< nb_clusters
;) {
927 refcount
= get_refcount(bs
, k
);
929 while (k
< nb_clusters
&& get_refcount(bs
, k
) == refcount
)
931 printf("%lld: refcount=%d nb=%lld\n", k
, refcount
, k
- k1
);
936 static int qcow_put_buffer(BlockDriverState
*bs
, const uint8_t *buf
,
937 int64_t pos
, int size
)
939 int growable
= bs
->growable
;
942 bdrv_pwrite(bs
, pos
, buf
, size
);
943 bs
->growable
= growable
;
948 static int qcow_get_buffer(BlockDriverState
*bs
, uint8_t *buf
,
949 int64_t pos
, int size
)
951 int growable
= bs
->growable
;
955 ret
= bdrv_pread(bs
, pos
, buf
, size
);
956 bs
->growable
= growable
;
961 static QEMUOptionParameter qcow_create_options
[] = {
963 .name
= BLOCK_OPT_SIZE
,
965 .help
= "Virtual disk size"
968 .name
= BLOCK_OPT_BACKING_FILE
,
970 .help
= "File name of a base image"
973 .name
= BLOCK_OPT_BACKING_FMT
,
975 .help
= "Image format of the base image"
978 .name
= BLOCK_OPT_ENCRYPT
,
980 .help
= "Encrypt the image"
983 .name
= BLOCK_OPT_CLUSTER_SIZE
,
985 .help
= "qcow2 cluster size"
990 static BlockDriver bdrv_qcow2
= {
991 .format_name
= "qcow2",
992 .instance_size
= sizeof(BDRVQcowState
),
993 .bdrv_probe
= qcow_probe
,
994 .bdrv_open
= qcow_open
,
995 .bdrv_close
= qcow_close
,
996 .bdrv_create
= qcow_create
,
997 .bdrv_flush
= qcow_flush
,
998 .bdrv_is_allocated
= qcow_is_allocated
,
999 .bdrv_set_key
= qcow_set_key
,
1000 .bdrv_make_empty
= qcow_make_empty
,
1002 .bdrv_aio_readv
= qcow_aio_readv
,
1003 .bdrv_aio_writev
= qcow_aio_writev
,
1004 .bdrv_write_compressed
= qcow_write_compressed
,
1006 .bdrv_snapshot_create
= qcow_snapshot_create
,
1007 .bdrv_snapshot_goto
= qcow_snapshot_goto
,
1008 .bdrv_snapshot_delete
= qcow_snapshot_delete
,
1009 .bdrv_snapshot_list
= qcow_snapshot_list
,
1010 .bdrv_get_info
= qcow_get_info
,
1012 .bdrv_put_buffer
= qcow_put_buffer
,
1013 .bdrv_get_buffer
= qcow_get_buffer
,
1015 .create_options
= qcow_create_options
,
1016 .bdrv_check
= qcow_check
,
1019 static void bdrv_qcow2_init(void)
1021 bdrv_register(&bdrv_qcow2
);
1024 block_init(bdrv_qcow2_init
);