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.
52 #define QCOW_EXT_MAGIC_END 0
53 #define QCOW_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
57 static int qcow_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
59 const QCowHeader
*cow_header
= (const void *)buf
;
61 if (buf_size
>= sizeof(QCowHeader
) &&
62 be32_to_cpu(cow_header
->magic
) == QCOW_MAGIC
&&
63 be32_to_cpu(cow_header
->version
) == QCOW_VERSION
)
71 * read qcow2 extension and fill bs
72 * start reading from start_offset
73 * finish reading upon magic of value 0 or when end_offset reached
74 * unknown magic is skipped (future extension this version knows nothing about)
75 * return 0 upon success, non-0 otherwise
77 static int qcow_read_extensions(BlockDriverState
*bs
, uint64_t start_offset
,
80 BDRVQcowState
*s
= bs
->opaque
;
85 printf("qcow_read_extensions: start=%ld end=%ld\n", start_offset
, end_offset
);
87 offset
= start_offset
;
88 while (offset
< end_offset
) {
92 if (offset
> s
->cluster_size
)
93 printf("qcow_handle_extension: suspicious offset %lu\n", offset
);
95 printf("attemting to read extended header in offset %lu\n", offset
);
98 if (bdrv_pread(s
->hd
, offset
, &ext
, sizeof(ext
)) != sizeof(ext
)) {
99 fprintf(stderr
, "qcow_handle_extension: ERROR: pread fail from offset %llu\n",
100 (unsigned long long)offset
);
103 be32_to_cpus(&ext
.magic
);
104 be32_to_cpus(&ext
.len
);
105 offset
+= sizeof(ext
);
107 printf("ext.magic = 0x%x\n", ext
.magic
);
110 case QCOW_EXT_MAGIC_END
:
113 case QCOW_EXT_MAGIC_BACKING_FORMAT
:
114 if (ext
.len
>= sizeof(bs
->backing_format
)) {
115 fprintf(stderr
, "ERROR: ext_backing_format: len=%u too large"
117 ext
.len
, sizeof(bs
->backing_format
));
120 if (bdrv_pread(s
->hd
, offset
, bs
->backing_format
,
123 bs
->backing_format
[ext
.len
] = '\0';
125 printf("Qcow2: Got format extension %s\n", bs
->backing_format
);
127 offset
+= ((ext
.len
+ 7) & ~7);
131 /* unknown magic -- just skip it */
132 offset
+= ((ext
.len
+ 7) & ~7);
141 static int qcow_open(BlockDriverState
*bs
, const char *filename
, int flags
)
143 BDRVQcowState
*s
= bs
->opaque
;
144 int len
, i
, shift
, ret
;
148 ret
= bdrv_file_open(&s
->hd
, filename
, flags
);
151 if (bdrv_pread(s
->hd
, 0, &header
, sizeof(header
)) != sizeof(header
))
153 be32_to_cpus(&header
.magic
);
154 be32_to_cpus(&header
.version
);
155 be64_to_cpus(&header
.backing_file_offset
);
156 be32_to_cpus(&header
.backing_file_size
);
157 be64_to_cpus(&header
.size
);
158 be32_to_cpus(&header
.cluster_bits
);
159 be32_to_cpus(&header
.crypt_method
);
160 be64_to_cpus(&header
.l1_table_offset
);
161 be32_to_cpus(&header
.l1_size
);
162 be64_to_cpus(&header
.refcount_table_offset
);
163 be32_to_cpus(&header
.refcount_table_clusters
);
164 be64_to_cpus(&header
.snapshots_offset
);
165 be32_to_cpus(&header
.nb_snapshots
);
167 if (header
.magic
!= QCOW_MAGIC
|| header
.version
!= QCOW_VERSION
)
169 if (header
.size
<= 1 ||
170 header
.cluster_bits
< MIN_CLUSTER_BITS
||
171 header
.cluster_bits
> MAX_CLUSTER_BITS
)
173 if (header
.crypt_method
> QCOW_CRYPT_AES
)
175 s
->crypt_method_header
= header
.crypt_method
;
176 if (s
->crypt_method_header
)
178 s
->cluster_bits
= header
.cluster_bits
;
179 s
->cluster_size
= 1 << s
->cluster_bits
;
180 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
181 s
->l2_bits
= s
->cluster_bits
- 3; /* L2 is always one cluster */
182 s
->l2_size
= 1 << s
->l2_bits
;
183 bs
->total_sectors
= header
.size
/ 512;
184 s
->csize_shift
= (62 - (s
->cluster_bits
- 8));
185 s
->csize_mask
= (1 << (s
->cluster_bits
- 8)) - 1;
186 s
->cluster_offset_mask
= (1LL << s
->csize_shift
) - 1;
187 s
->refcount_table_offset
= header
.refcount_table_offset
;
188 s
->refcount_table_size
=
189 header
.refcount_table_clusters
<< (s
->cluster_bits
- 3);
191 s
->snapshots_offset
= header
.snapshots_offset
;
192 s
->nb_snapshots
= header
.nb_snapshots
;
194 /* read the level 1 table */
195 s
->l1_size
= header
.l1_size
;
196 shift
= s
->cluster_bits
+ s
->l2_bits
;
197 s
->l1_vm_state_index
= (header
.size
+ (1LL << shift
) - 1) >> shift
;
198 /* the L1 table must contain at least enough entries to put
200 if (s
->l1_size
< s
->l1_vm_state_index
)
202 s
->l1_table_offset
= header
.l1_table_offset
;
203 s
->l1_table
= qemu_mallocz(
204 align_offset(s
->l1_size
* sizeof(uint64_t), 512));
205 if (bdrv_pread(s
->hd
, s
->l1_table_offset
, s
->l1_table
, s
->l1_size
* sizeof(uint64_t)) !=
206 s
->l1_size
* sizeof(uint64_t))
208 for(i
= 0;i
< s
->l1_size
; i
++) {
209 be64_to_cpus(&s
->l1_table
[i
]);
212 s
->l2_cache
= qemu_malloc(s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
213 s
->cluster_cache
= qemu_malloc(s
->cluster_size
);
214 /* one more sector for decompressed data alignment */
215 s
->cluster_data
= qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
217 s
->cluster_cache_offset
= -1;
219 if (qcow2_refcount_init(bs
) < 0)
222 /* read qcow2 extensions */
223 if (header
.backing_file_offset
)
224 ext_end
= header
.backing_file_offset
;
226 ext_end
= s
->cluster_size
;
227 if (qcow_read_extensions(bs
, sizeof(header
), ext_end
))
230 /* read the backing file name */
231 if (header
.backing_file_offset
!= 0) {
232 len
= header
.backing_file_size
;
235 if (bdrv_pread(s
->hd
, header
.backing_file_offset
, bs
->backing_file
, len
) != len
)
237 bs
->backing_file
[len
] = '\0';
239 if (qcow2_read_snapshots(bs
) < 0)
243 qcow2_check_refcounts(bs
);
248 qcow2_free_snapshots(bs
);
249 qcow2_refcount_close(bs
);
250 qemu_free(s
->l1_table
);
251 qemu_free(s
->l2_cache
);
252 qemu_free(s
->cluster_cache
);
253 qemu_free(s
->cluster_data
);
258 static int qcow_set_key(BlockDriverState
*bs
, const char *key
)
260 BDRVQcowState
*s
= bs
->opaque
;
264 memset(keybuf
, 0, 16);
268 /* XXX: we could compress the chars to 7 bits to increase
270 for(i
= 0;i
< len
;i
++) {
273 s
->crypt_method
= s
->crypt_method_header
;
275 if (AES_set_encrypt_key(keybuf
, 128, &s
->aes_encrypt_key
) != 0)
277 if (AES_set_decrypt_key(keybuf
, 128, &s
->aes_decrypt_key
) != 0)
287 AES_encrypt(in
, tmp
, &s
->aes_encrypt_key
);
288 AES_decrypt(tmp
, out
, &s
->aes_decrypt_key
);
289 for(i
= 0; i
< 16; i
++)
290 printf(" %02x", tmp
[i
]);
292 for(i
= 0; i
< 16; i
++)
293 printf(" %02x", out
[i
]);
300 static int qcow_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
301 int nb_sectors
, int *pnum
)
303 uint64_t cluster_offset
;
306 cluster_offset
= qcow2_get_cluster_offset(bs
, sector_num
<< 9, pnum
);
308 return (cluster_offset
!= 0);
311 /* handle reading after the end of the backing file */
312 int qcow2_backing_read1(BlockDriverState
*bs
,
313 int64_t sector_num
, uint8_t *buf
, int nb_sectors
)
316 if ((sector_num
+ nb_sectors
) <= bs
->total_sectors
)
318 if (sector_num
>= bs
->total_sectors
)
321 n1
= bs
->total_sectors
- sector_num
;
322 memset(buf
+ n1
* 512, 0, 512 * (nb_sectors
- n1
));
326 typedef struct QCowAIOCB
{
327 BlockDriverAIOCB common
;
334 uint64_t cluster_offset
;
335 uint8_t *cluster_data
;
336 BlockDriverAIOCB
*hd_aiocb
;
338 QEMUIOVector hd_qiov
;
343 static void qcow_aio_cancel(BlockDriverAIOCB
*blockacb
)
345 QCowAIOCB
*acb
= (QCowAIOCB
*)blockacb
;
347 bdrv_aio_cancel(acb
->hd_aiocb
);
348 qemu_aio_release(acb
);
351 static AIOPool qcow_aio_pool
= {
352 .aiocb_size
= sizeof(QCowAIOCB
),
353 .cancel
= qcow_aio_cancel
,
356 static void qcow_aio_read_cb(void *opaque
, int ret
);
357 static void qcow_aio_read_bh(void *opaque
)
359 QCowAIOCB
*acb
= opaque
;
360 qemu_bh_delete(acb
->bh
);
362 qcow_aio_read_cb(opaque
, 0);
365 static int qcow_schedule_bh(QEMUBHFunc
*cb
, QCowAIOCB
*acb
)
370 acb
->bh
= qemu_bh_new(cb
, acb
);
374 qemu_bh_schedule(acb
->bh
);
379 static void qcow_aio_read_cb(void *opaque
, int ret
)
381 QCowAIOCB
*acb
= opaque
;
382 BlockDriverState
*bs
= acb
->common
.bs
;
383 BDRVQcowState
*s
= bs
->opaque
;
384 int index_in_cluster
, n1
;
386 acb
->hd_aiocb
= NULL
;
390 /* post process the read buffer */
391 if (!acb
->cluster_offset
) {
393 } else if (acb
->cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
396 if (s
->crypt_method
) {
397 qcow2_encrypt_sectors(s
, acb
->sector_num
, acb
->buf
, acb
->buf
,
399 &s
->aes_decrypt_key
);
403 acb
->nb_sectors
-= acb
->n
;
404 acb
->sector_num
+= acb
->n
;
405 acb
->buf
+= acb
->n
* 512;
407 if (acb
->nb_sectors
== 0) {
408 /* request completed */
413 /* prepare next AIO request */
414 acb
->n
= acb
->nb_sectors
;
415 acb
->cluster_offset
=
416 qcow2_get_cluster_offset(bs
, acb
->sector_num
<< 9, &acb
->n
);
417 index_in_cluster
= acb
->sector_num
& (s
->cluster_sectors
- 1);
419 if (!acb
->cluster_offset
) {
420 if (bs
->backing_hd
) {
421 /* read from the base image */
422 n1
= qcow2_backing_read1(bs
->backing_hd
, acb
->sector_num
,
425 acb
->hd_iov
.iov_base
= (void *)acb
->buf
;
426 acb
->hd_iov
.iov_len
= acb
->n
* 512;
427 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
428 acb
->hd_aiocb
= bdrv_aio_readv(bs
->backing_hd
, acb
->sector_num
,
429 &acb
->hd_qiov
, acb
->n
,
430 qcow_aio_read_cb
, acb
);
431 if (acb
->hd_aiocb
== NULL
)
434 ret
= qcow_schedule_bh(qcow_aio_read_bh
, acb
);
439 /* Note: in this case, no need to wait */
440 memset(acb
->buf
, 0, 512 * acb
->n
);
441 ret
= qcow_schedule_bh(qcow_aio_read_bh
, acb
);
445 } else if (acb
->cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
446 /* add AIO support for compressed blocks ? */
447 if (qcow2_decompress_cluster(s
, acb
->cluster_offset
) < 0)
450 s
->cluster_cache
+ index_in_cluster
* 512, 512 * acb
->n
);
451 ret
= qcow_schedule_bh(qcow_aio_read_bh
, acb
);
455 if ((acb
->cluster_offset
& 511) != 0) {
460 acb
->hd_iov
.iov_base
= (void *)acb
->buf
;
461 acb
->hd_iov
.iov_len
= acb
->n
* 512;
462 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
463 acb
->hd_aiocb
= bdrv_aio_readv(s
->hd
,
464 (acb
->cluster_offset
>> 9) + index_in_cluster
,
465 &acb
->hd_qiov
, acb
->n
, qcow_aio_read_cb
, acb
);
466 if (acb
->hd_aiocb
== NULL
)
472 if (acb
->qiov
->niov
> 1) {
473 qemu_iovec_from_buffer(acb
->qiov
, acb
->orig_buf
, acb
->qiov
->size
);
474 qemu_vfree(acb
->orig_buf
);
476 acb
->common
.cb(acb
->common
.opaque
, ret
);
477 qemu_aio_release(acb
);
480 static QCowAIOCB
*qcow_aio_setup(BlockDriverState
*bs
,
481 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
482 BlockDriverCompletionFunc
*cb
, void *opaque
, int is_write
)
486 acb
= qemu_aio_get(&qcow_aio_pool
, bs
, cb
, opaque
);
489 acb
->hd_aiocb
= NULL
;
490 acb
->sector_num
= sector_num
;
492 if (qiov
->niov
> 1) {
493 acb
->buf
= acb
->orig_buf
= qemu_blockalign(bs
, qiov
->size
);
495 qemu_iovec_to_buffer(qiov
, acb
->buf
);
497 acb
->buf
= (uint8_t *)qiov
->iov
->iov_base
;
499 acb
->nb_sectors
= nb_sectors
;
501 acb
->cluster_offset
= 0;
502 acb
->l2meta
.nb_clusters
= 0;
506 static BlockDriverAIOCB
*qcow_aio_readv(BlockDriverState
*bs
,
507 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
508 BlockDriverCompletionFunc
*cb
, void *opaque
)
512 acb
= qcow_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
516 qcow_aio_read_cb(acb
, 0);
520 static void qcow_aio_write_cb(void *opaque
, int ret
)
522 QCowAIOCB
*acb
= opaque
;
523 BlockDriverState
*bs
= acb
->common
.bs
;
524 BDRVQcowState
*s
= bs
->opaque
;
525 int index_in_cluster
;
526 const uint8_t *src_buf
;
529 acb
->hd_aiocb
= NULL
;
534 if (qcow2_alloc_cluster_link_l2(bs
, acb
->cluster_offset
, &acb
->l2meta
) < 0) {
535 qcow2_free_any_clusters(bs
, acb
->cluster_offset
, acb
->l2meta
.nb_clusters
);
539 acb
->nb_sectors
-= acb
->n
;
540 acb
->sector_num
+= acb
->n
;
541 acb
->buf
+= acb
->n
* 512;
543 if (acb
->nb_sectors
== 0) {
544 /* request completed */
549 index_in_cluster
= acb
->sector_num
& (s
->cluster_sectors
- 1);
550 n_end
= index_in_cluster
+ acb
->nb_sectors
;
551 if (s
->crypt_method
&&
552 n_end
> QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
)
553 n_end
= QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
;
555 acb
->cluster_offset
= qcow2_alloc_cluster_offset(bs
, acb
->sector_num
<< 9,
557 n_end
, &acb
->n
, &acb
->l2meta
);
558 if (!acb
->cluster_offset
|| (acb
->cluster_offset
& 511) != 0) {
562 if (s
->crypt_method
) {
563 if (!acb
->cluster_data
) {
564 acb
->cluster_data
= qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS
*
567 qcow2_encrypt_sectors(s
, acb
->sector_num
, acb
->cluster_data
, acb
->buf
,
568 acb
->n
, 1, &s
->aes_encrypt_key
);
569 src_buf
= acb
->cluster_data
;
573 acb
->hd_iov
.iov_base
= (void *)src_buf
;
574 acb
->hd_iov
.iov_len
= acb
->n
* 512;
575 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
576 acb
->hd_aiocb
= bdrv_aio_writev(s
->hd
,
577 (acb
->cluster_offset
>> 9) + index_in_cluster
,
578 &acb
->hd_qiov
, acb
->n
,
579 qcow_aio_write_cb
, acb
);
580 if (acb
->hd_aiocb
== NULL
)
586 if (acb
->qiov
->niov
> 1)
587 qemu_vfree(acb
->orig_buf
);
588 acb
->common
.cb(acb
->common
.opaque
, ret
);
589 qemu_aio_release(acb
);
592 static BlockDriverAIOCB
*qcow_aio_writev(BlockDriverState
*bs
,
593 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
594 BlockDriverCompletionFunc
*cb
, void *opaque
)
596 BDRVQcowState
*s
= bs
->opaque
;
599 s
->cluster_cache_offset
= -1; /* disable compressed cache */
601 acb
= qcow_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
605 qcow_aio_write_cb(acb
, 0);
609 static void qcow_close(BlockDriverState
*bs
)
611 BDRVQcowState
*s
= bs
->opaque
;
612 qemu_free(s
->l1_table
);
613 qemu_free(s
->l2_cache
);
614 qemu_free(s
->cluster_cache
);
615 qemu_free(s
->cluster_data
);
616 qcow2_refcount_close(bs
);
620 static int get_bits_from_size(size_t size
)
629 /* Not a power of two */
642 static int preallocate(BlockDriverState
*bs
)
644 BDRVQcowState
*s
= bs
->opaque
;
645 uint64_t cluster_offset
= 0;
651 nb_sectors
= bdrv_getlength(bs
) >> 9;
655 num
= MIN(nb_sectors
, INT_MAX
>> 9);
656 cluster_offset
= qcow2_alloc_cluster_offset(bs
, offset
, 0, num
, &num
,
659 if (cluster_offset
== 0) {
663 if (qcow2_alloc_cluster_link_l2(bs
, cluster_offset
, &meta
) < 0) {
664 qcow2_free_any_clusters(bs
, cluster_offset
, meta
.nb_clusters
);
668 /* TODO Preallocate data if requested */
675 * It is expected that the image file is large enough to actually contain
676 * all of the allocated clusters (otherwise we get failing reads after
677 * EOF). Extend the image to the last allocated sector.
679 if (cluster_offset
!= 0) {
680 bdrv_truncate(s
->hd
, cluster_offset
+ (num
<< 9));
686 static int qcow_create2(const char *filename
, int64_t total_size
,
687 const char *backing_file
, const char *backing_format
,
688 int flags
, size_t cluster_size
, int prealloc
)
691 int fd
, header_size
, backing_filename_len
, l1_size
, i
, shift
, l2_bits
;
692 int ref_clusters
, backing_format_len
= 0;
694 uint64_t tmp
, offset
;
695 QCowCreateState s1
, *s
= &s1
;
696 QCowExtension ext_bf
= {0, 0};
699 memset(s
, 0, sizeof(*s
));
701 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0644);
704 memset(&header
, 0, sizeof(header
));
705 header
.magic
= cpu_to_be32(QCOW_MAGIC
);
706 header
.version
= cpu_to_be32(QCOW_VERSION
);
707 header
.size
= cpu_to_be64(total_size
* 512);
708 header_size
= sizeof(header
);
709 backing_filename_len
= 0;
711 if (backing_format
) {
712 ext_bf
.magic
= QCOW_EXT_MAGIC_BACKING_FORMAT
;
713 backing_format_len
= strlen(backing_format
);
714 ext_bf
.len
= (backing_format_len
+ 7) & ~7;
715 header_size
+= ((sizeof(ext_bf
) + ext_bf
.len
+ 7) & ~7);
717 header
.backing_file_offset
= cpu_to_be64(header_size
);
718 backing_filename_len
= strlen(backing_file
);
719 header
.backing_file_size
= cpu_to_be32(backing_filename_len
);
720 header_size
+= backing_filename_len
;
724 s
->cluster_bits
= get_bits_from_size(cluster_size
);
725 if (s
->cluster_bits
< MIN_CLUSTER_BITS
||
726 s
->cluster_bits
> MAX_CLUSTER_BITS
)
728 fprintf(stderr
, "Cluster size must be a power of two between "
730 1 << MIN_CLUSTER_BITS
,
731 1 << (MAX_CLUSTER_BITS
- 10));
734 s
->cluster_size
= 1 << s
->cluster_bits
;
736 header
.cluster_bits
= cpu_to_be32(s
->cluster_bits
);
737 header_size
= (header_size
+ 7) & ~7;
738 if (flags
& BLOCK_FLAG_ENCRYPT
) {
739 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
741 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
743 l2_bits
= s
->cluster_bits
- 3;
744 shift
= s
->cluster_bits
+ l2_bits
;
745 l1_size
= (((total_size
* 512) + (1LL << shift
) - 1) >> shift
);
746 offset
= align_offset(header_size
, s
->cluster_size
);
747 s
->l1_table_offset
= offset
;
748 header
.l1_table_offset
= cpu_to_be64(s
->l1_table_offset
);
749 header
.l1_size
= cpu_to_be32(l1_size
);
750 offset
+= align_offset(l1_size
* sizeof(uint64_t), s
->cluster_size
);
752 s
->refcount_table
= qemu_mallocz(s
->cluster_size
);
754 s
->refcount_table_offset
= offset
;
755 header
.refcount_table_offset
= cpu_to_be64(offset
);
756 header
.refcount_table_clusters
= cpu_to_be32(1);
757 offset
+= s
->cluster_size
;
758 s
->refcount_block_offset
= offset
;
760 /* count how many refcount blocks needed */
761 tmp
= offset
>> s
->cluster_bits
;
762 ref_clusters
= (tmp
>> (s
->cluster_bits
- REFCOUNT_SHIFT
)) + 1;
763 for (i
=0; i
< ref_clusters
; i
++) {
764 s
->refcount_table
[i
] = cpu_to_be64(offset
);
765 offset
+= s
->cluster_size
;
768 s
->refcount_block
= qemu_mallocz(ref_clusters
* s
->cluster_size
);
770 /* update refcounts */
771 qcow2_create_refcount_update(s
, 0, header_size
);
772 qcow2_create_refcount_update(s
, s
->l1_table_offset
,
773 l1_size
* sizeof(uint64_t));
774 qcow2_create_refcount_update(s
, s
->refcount_table_offset
, s
->cluster_size
);
775 qcow2_create_refcount_update(s
, s
->refcount_block_offset
,
776 ref_clusters
* s
->cluster_size
);
778 /* write all the data */
779 write(fd
, &header
, sizeof(header
));
781 if (backing_format_len
) {
783 int d
= ext_bf
.len
- backing_format_len
;
785 memset(zero
, 0, sizeof(zero
));
786 cpu_to_be32s(&ext_bf
.magic
);
787 cpu_to_be32s(&ext_bf
.len
);
788 write(fd
, &ext_bf
, sizeof(ext_bf
));
789 write(fd
, backing_format
, backing_format_len
);
794 write(fd
, backing_file
, backing_filename_len
);
796 lseek(fd
, s
->l1_table_offset
, SEEK_SET
);
798 for(i
= 0;i
< l1_size
; i
++) {
799 write(fd
, &tmp
, sizeof(tmp
));
801 lseek(fd
, s
->refcount_table_offset
, SEEK_SET
);
802 write(fd
, s
->refcount_table
, s
->cluster_size
);
804 lseek(fd
, s
->refcount_block_offset
, SEEK_SET
);
805 write(fd
, s
->refcount_block
, ref_clusters
* s
->cluster_size
);
807 qemu_free(s
->refcount_table
);
808 qemu_free(s
->refcount_block
);
811 /* Preallocate metadata */
813 BlockDriverState
*bs
;
815 bdrv_open(bs
, filename
, BDRV_O_CACHE_WB
);
823 static int qcow_create(const char *filename
, QEMUOptionParameter
*options
)
825 const char *backing_file
= NULL
;
826 const char *backing_fmt
= NULL
;
827 uint64_t sectors
= 0;
829 size_t cluster_size
= 65536;
832 /* Read out options */
833 while (options
&& options
->name
) {
834 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
835 sectors
= options
->value
.n
/ 512;
836 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
837 backing_file
= options
->value
.s
;
838 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FMT
)) {
839 backing_fmt
= options
->value
.s
;
840 } else if (!strcmp(options
->name
, BLOCK_OPT_ENCRYPT
)) {
841 flags
|= options
->value
.n
? BLOCK_FLAG_ENCRYPT
: 0;
842 } else if (!strcmp(options
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
843 if (options
->value
.n
) {
844 cluster_size
= options
->value
.n
;
846 } else if (!strcmp(options
->name
, BLOCK_OPT_PREALLOC
)) {
847 if (!options
->value
.s
|| !strcmp(options
->value
.s
, "off")) {
849 } else if (!strcmp(options
->value
.s
, "metadata")) {
852 fprintf(stderr
, "Invalid preallocation mode: '%s'\n",
860 if (backing_file
&& prealloc
) {
861 fprintf(stderr
, "Backing file and preallocation cannot be used at "
866 return qcow_create2(filename
, sectors
, backing_file
, backing_fmt
, flags
,
867 cluster_size
, prealloc
);
870 static int qcow_make_empty(BlockDriverState
*bs
)
873 /* XXX: not correct */
874 BDRVQcowState
*s
= bs
->opaque
;
875 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
878 memset(s
->l1_table
, 0, l1_length
);
879 if (bdrv_pwrite(s
->hd
, s
->l1_table_offset
, s
->l1_table
, l1_length
) < 0)
881 ret
= bdrv_truncate(s
->hd
, s
->l1_table_offset
+ l1_length
);
890 /* XXX: put compressed sectors first, then all the cluster aligned
891 tables to avoid losing bytes in alignment */
892 static int qcow_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
893 const uint8_t *buf
, int nb_sectors
)
895 BDRVQcowState
*s
= bs
->opaque
;
899 uint64_t cluster_offset
;
901 if (nb_sectors
== 0) {
902 /* align end of file to a sector boundary to ease reading with
904 cluster_offset
= bdrv_getlength(s
->hd
);
905 cluster_offset
= (cluster_offset
+ 511) & ~511;
906 bdrv_truncate(s
->hd
, cluster_offset
);
910 if (nb_sectors
!= s
->cluster_sectors
)
913 out_buf
= qemu_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
915 /* best compression, small window, no zlib header */
916 memset(&strm
, 0, sizeof(strm
));
917 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
919 9, Z_DEFAULT_STRATEGY
);
925 strm
.avail_in
= s
->cluster_size
;
926 strm
.next_in
= (uint8_t *)buf
;
927 strm
.avail_out
= s
->cluster_size
;
928 strm
.next_out
= out_buf
;
930 ret
= deflate(&strm
, Z_FINISH
);
931 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
936 out_len
= strm
.next_out
- out_buf
;
940 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
941 /* could not compress: write normal cluster */
942 bdrv_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
944 cluster_offset
= qcow2_alloc_compressed_cluster_offset(bs
,
945 sector_num
<< 9, out_len
);
948 cluster_offset
&= s
->cluster_offset_mask
;
949 if (bdrv_pwrite(s
->hd
, cluster_offset
, out_buf
, out_len
) != out_len
) {
959 static void qcow_flush(BlockDriverState
*bs
)
961 BDRVQcowState
*s
= bs
->opaque
;
965 static int64_t qcow_vm_state_offset(BDRVQcowState
*s
)
967 return (int64_t)s
->l1_vm_state_index
<< (s
->cluster_bits
+ s
->l2_bits
);
970 static int qcow_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
972 BDRVQcowState
*s
= bs
->opaque
;
973 bdi
->cluster_size
= s
->cluster_size
;
974 bdi
->vm_state_offset
= qcow_vm_state_offset(s
);
979 static int qcow_check(BlockDriverState
*bs
)
981 return qcow2_check_refcounts(bs
);
985 static void dump_refcounts(BlockDriverState
*bs
)
987 BDRVQcowState
*s
= bs
->opaque
;
988 int64_t nb_clusters
, k
, k1
, size
;
991 size
= bdrv_getlength(s
->hd
);
992 nb_clusters
= size_to_clusters(s
, size
);
993 for(k
= 0; k
< nb_clusters
;) {
995 refcount
= get_refcount(bs
, k
);
997 while (k
< nb_clusters
&& get_refcount(bs
, k
) == refcount
)
999 printf("%lld: refcount=%d nb=%lld\n", k
, refcount
, k
- k1
);
1004 static int qcow_save_vmstate(BlockDriverState
*bs
, const uint8_t *buf
,
1005 int64_t pos
, int size
)
1007 BDRVQcowState
*s
= bs
->opaque
;
1008 int growable
= bs
->growable
;
1011 bdrv_pwrite(bs
, qcow_vm_state_offset(s
) + pos
, buf
, size
);
1012 bs
->growable
= growable
;
1017 static int qcow_load_vmstate(BlockDriverState
*bs
, uint8_t *buf
,
1018 int64_t pos
, int size
)
1020 BDRVQcowState
*s
= bs
->opaque
;
1021 int growable
= bs
->growable
;
1025 ret
= bdrv_pread(bs
, qcow_vm_state_offset(s
) + pos
, buf
, size
);
1026 bs
->growable
= growable
;
1031 static QEMUOptionParameter qcow_create_options
[] = {
1033 .name
= BLOCK_OPT_SIZE
,
1035 .help
= "Virtual disk size"
1038 .name
= BLOCK_OPT_BACKING_FILE
,
1040 .help
= "File name of a base image"
1043 .name
= BLOCK_OPT_BACKING_FMT
,
1045 .help
= "Image format of the base image"
1048 .name
= BLOCK_OPT_ENCRYPT
,
1050 .help
= "Encrypt the image"
1053 .name
= BLOCK_OPT_CLUSTER_SIZE
,
1055 .help
= "qcow2 cluster size"
1058 .name
= BLOCK_OPT_PREALLOC
,
1060 .help
= "Preallocation mode (allowed values: off, metadata)"
1065 static BlockDriver bdrv_qcow2
= {
1066 .format_name
= "qcow2",
1067 .instance_size
= sizeof(BDRVQcowState
),
1068 .bdrv_probe
= qcow_probe
,
1069 .bdrv_open
= qcow_open
,
1070 .bdrv_close
= qcow_close
,
1071 .bdrv_create
= qcow_create
,
1072 .bdrv_flush
= qcow_flush
,
1073 .bdrv_is_allocated
= qcow_is_allocated
,
1074 .bdrv_set_key
= qcow_set_key
,
1075 .bdrv_make_empty
= qcow_make_empty
,
1077 .bdrv_aio_readv
= qcow_aio_readv
,
1078 .bdrv_aio_writev
= qcow_aio_writev
,
1079 .bdrv_write_compressed
= qcow_write_compressed
,
1081 .bdrv_snapshot_create
= qcow2_snapshot_create
,
1082 .bdrv_snapshot_goto
= qcow2_snapshot_goto
,
1083 .bdrv_snapshot_delete
= qcow2_snapshot_delete
,
1084 .bdrv_snapshot_list
= qcow2_snapshot_list
,
1085 .bdrv_get_info
= qcow_get_info
,
1087 .bdrv_save_vmstate
= qcow_save_vmstate
,
1088 .bdrv_load_vmstate
= qcow_load_vmstate
,
1090 .create_options
= qcow_create_options
,
1091 .bdrv_check
= qcow_check
,
1094 static void bdrv_qcow2_init(void)
1096 bdrv_register(&bdrv_qcow2
);
1099 block_init(bdrv_qcow2_init
);