2 * Block driver for the QCOW 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/block_int.h"
26 #include "qemu/module.h"
28 #include "qapi/qmp/qerror.h"
29 #include "crypto/cipher.h"
30 #include "migration/migration.h"
32 /**************************************************************/
33 /* QEMU COW block driver with compression and encryption support */
35 #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
36 #define QCOW_VERSION 1
38 #define QCOW_CRYPT_NONE 0
39 #define QCOW_CRYPT_AES 1
41 #define QCOW_OFLAG_COMPRESSED (1LL << 63)
43 typedef struct QCowHeader
{
46 uint64_t backing_file_offset
;
47 uint32_t backing_file_size
;
49 uint64_t size
; /* in bytes */
53 uint32_t crypt_method
;
54 uint64_t l1_table_offset
;
55 } QEMU_PACKED QCowHeader
;
57 #define L2_CACHE_SIZE 16
59 typedef struct BDRVQcowState
{
66 uint64_t cluster_offset_mask
;
67 uint64_t l1_table_offset
;
70 uint64_t l2_cache_offsets
[L2_CACHE_SIZE
];
71 uint32_t l2_cache_counts
[L2_CACHE_SIZE
];
72 uint8_t *cluster_cache
;
73 uint8_t *cluster_data
;
74 uint64_t cluster_cache_offset
;
75 QCryptoCipher
*cipher
; /* NULL if no key yet */
76 uint32_t crypt_method_header
;
78 Error
*migration_blocker
;
81 static int decompress_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
);
83 static int qcow_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
85 const QCowHeader
*cow_header
= (const void *)buf
;
87 if (buf_size
>= sizeof(QCowHeader
) &&
88 be32_to_cpu(cow_header
->magic
) == QCOW_MAGIC
&&
89 be32_to_cpu(cow_header
->version
) == QCOW_VERSION
)
95 static int qcow_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
98 BDRVQcowState
*s
= bs
->opaque
;
99 unsigned int len
, i
, shift
;
103 ret
= bdrv_pread(bs
->file
, 0, &header
, sizeof(header
));
107 be32_to_cpus(&header
.magic
);
108 be32_to_cpus(&header
.version
);
109 be64_to_cpus(&header
.backing_file_offset
);
110 be32_to_cpus(&header
.backing_file_size
);
111 be32_to_cpus(&header
.mtime
);
112 be64_to_cpus(&header
.size
);
113 be32_to_cpus(&header
.crypt_method
);
114 be64_to_cpus(&header
.l1_table_offset
);
116 if (header
.magic
!= QCOW_MAGIC
) {
117 error_setg(errp
, "Image not in qcow format");
121 if (header
.version
!= QCOW_VERSION
) {
123 snprintf(version
, sizeof(version
), "QCOW version %" PRIu32
,
125 error_setg(errp
, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE
,
126 bdrv_get_device_or_node_name(bs
), "qcow", version
);
131 if (header
.size
<= 1) {
132 error_setg(errp
, "Image size is too small (must be at least 2 bytes)");
136 if (header
.cluster_bits
< 9 || header
.cluster_bits
> 16) {
137 error_setg(errp
, "Cluster size must be between 512 and 64k");
142 /* l2_bits specifies number of entries; storing a uint64_t in each entry,
143 * so bytes = num_entries << 3. */
144 if (header
.l2_bits
< 9 - 3 || header
.l2_bits
> 16 - 3) {
145 error_setg(errp
, "L2 table size must be between 512 and 64k");
150 if (header
.crypt_method
> QCOW_CRYPT_AES
) {
151 error_setg(errp
, "invalid encryption method in qcow header");
155 if (!qcrypto_cipher_supports(QCRYPTO_CIPHER_ALG_AES_128
)) {
156 error_setg(errp
, "AES cipher not available");
160 s
->crypt_method_header
= header
.crypt_method
;
161 if (s
->crypt_method_header
) {
164 s
->cluster_bits
= header
.cluster_bits
;
165 s
->cluster_size
= 1 << s
->cluster_bits
;
166 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
167 s
->l2_bits
= header
.l2_bits
;
168 s
->l2_size
= 1 << s
->l2_bits
;
169 bs
->total_sectors
= header
.size
/ 512;
170 s
->cluster_offset_mask
= (1LL << (63 - s
->cluster_bits
)) - 1;
172 /* read the level 1 table */
173 shift
= s
->cluster_bits
+ s
->l2_bits
;
174 if (header
.size
> UINT64_MAX
- (1LL << shift
)) {
175 error_setg(errp
, "Image too large");
179 uint64_t l1_size
= (header
.size
+ (1LL << shift
) - 1) >> shift
;
180 if (l1_size
> INT_MAX
/ sizeof(uint64_t)) {
181 error_setg(errp
, "Image too large");
185 s
->l1_size
= l1_size
;
188 s
->l1_table_offset
= header
.l1_table_offset
;
189 s
->l1_table
= g_try_new(uint64_t, s
->l1_size
);
190 if (s
->l1_table
== NULL
) {
191 error_setg(errp
, "Could not allocate memory for L1 table");
196 ret
= bdrv_pread(bs
->file
, s
->l1_table_offset
, s
->l1_table
,
197 s
->l1_size
* sizeof(uint64_t));
202 for(i
= 0;i
< s
->l1_size
; i
++) {
203 be64_to_cpus(&s
->l1_table
[i
]);
206 /* alloc L2 cache (max. 64k * 16 * 8 = 8 MB) */
208 qemu_try_blockalign(bs
->file
,
209 s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
210 if (s
->l2_cache
== NULL
) {
211 error_setg(errp
, "Could not allocate L2 table cache");
215 s
->cluster_cache
= g_malloc(s
->cluster_size
);
216 s
->cluster_data
= g_malloc(s
->cluster_size
);
217 s
->cluster_cache_offset
= -1;
219 /* read the backing file name */
220 if (header
.backing_file_offset
!= 0) {
221 len
= header
.backing_file_size
;
222 if (len
> 1023 || len
>= sizeof(bs
->backing_file
)) {
223 error_setg(errp
, "Backing file name too long");
227 ret
= bdrv_pread(bs
->file
, header
.backing_file_offset
,
228 bs
->backing_file
, len
);
232 bs
->backing_file
[len
] = '\0';
235 /* Disable migration when qcow images are used */
236 error_setg(&s
->migration_blocker
, "The qcow format used by node '%s' "
237 "does not support live migration",
238 bdrv_get_device_or_node_name(bs
));
239 migrate_add_blocker(s
->migration_blocker
);
241 qemu_co_mutex_init(&s
->lock
);
246 qemu_vfree(s
->l2_cache
);
247 g_free(s
->cluster_cache
);
248 g_free(s
->cluster_data
);
253 /* We have nothing to do for QCOW reopen, stubs just return
255 static int qcow_reopen_prepare(BDRVReopenState
*state
,
256 BlockReopenQueue
*queue
, Error
**errp
)
261 static int qcow_set_key(BlockDriverState
*bs
, const char *key
)
263 BDRVQcowState
*s
= bs
->opaque
;
268 memset(keybuf
, 0, 16);
272 /* XXX: we could compress the chars to 7 bits to increase
274 for(i
= 0;i
< len
;i
++) {
277 assert(bs
->encrypted
);
279 qcrypto_cipher_free(s
->cipher
);
280 s
->cipher
= qcrypto_cipher_new(
281 QCRYPTO_CIPHER_ALG_AES_128
,
282 QCRYPTO_CIPHER_MODE_CBC
,
283 keybuf
, G_N_ELEMENTS(keybuf
),
287 /* XXX would be nice if errors in this method could
288 * be properly propagate to the caller. Would need
289 * the bdrv_set_key() API signature to be fixed. */
296 /* The crypt function is compatible with the linux cryptoloop
297 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
299 static int encrypt_sectors(BDRVQcowState
*s
, int64_t sector_num
,
300 uint8_t *out_buf
, const uint8_t *in_buf
,
301 int nb_sectors
, bool enc
, Error
**errp
)
310 for(i
= 0; i
< nb_sectors
; i
++) {
311 ivec
.ll
[0] = cpu_to_le64(sector_num
);
313 if (qcrypto_cipher_setiv(s
->cipher
,
314 ivec
.b
, G_N_ELEMENTS(ivec
.b
),
319 ret
= qcrypto_cipher_encrypt(s
->cipher
,
325 ret
= qcrypto_cipher_decrypt(s
->cipher
,
345 * 1 to allocate a normal cluster (for sector indexes 'n_start' to
348 * 2 to allocate a compressed cluster of size
349 * 'compressed_size'. 'compressed_size' must be > 0 and <
352 * return 0 if not allocated.
354 static uint64_t get_cluster_offset(BlockDriverState
*bs
,
355 uint64_t offset
, int allocate
,
357 int n_start
, int n_end
)
359 BDRVQcowState
*s
= bs
->opaque
;
360 int min_index
, i
, j
, l1_index
, l2_index
;
361 uint64_t l2_offset
, *l2_table
, cluster_offset
, tmp
;
365 l1_index
= offset
>> (s
->l2_bits
+ s
->cluster_bits
);
366 l2_offset
= s
->l1_table
[l1_index
];
371 /* allocate a new l2 entry */
372 l2_offset
= bdrv_getlength(bs
->file
);
373 /* round to cluster size */
374 l2_offset
= (l2_offset
+ s
->cluster_size
- 1) & ~(s
->cluster_size
- 1);
375 /* update the L1 entry */
376 s
->l1_table
[l1_index
] = l2_offset
;
377 tmp
= cpu_to_be64(l2_offset
);
378 if (bdrv_pwrite_sync(bs
->file
,
379 s
->l1_table_offset
+ l1_index
* sizeof(tmp
),
380 &tmp
, sizeof(tmp
)) < 0)
384 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
385 if (l2_offset
== s
->l2_cache_offsets
[i
]) {
386 /* increment the hit count */
387 if (++s
->l2_cache_counts
[i
] == 0xffffffff) {
388 for(j
= 0; j
< L2_CACHE_SIZE
; j
++) {
389 s
->l2_cache_counts
[j
] >>= 1;
392 l2_table
= s
->l2_cache
+ (i
<< s
->l2_bits
);
396 /* not found: load a new entry in the least used one */
398 min_count
= 0xffffffff;
399 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
400 if (s
->l2_cache_counts
[i
] < min_count
) {
401 min_count
= s
->l2_cache_counts
[i
];
405 l2_table
= s
->l2_cache
+ (min_index
<< s
->l2_bits
);
407 memset(l2_table
, 0, s
->l2_size
* sizeof(uint64_t));
408 if (bdrv_pwrite_sync(bs
->file
, l2_offset
, l2_table
,
409 s
->l2_size
* sizeof(uint64_t)) < 0)
412 if (bdrv_pread(bs
->file
, l2_offset
, l2_table
, s
->l2_size
* sizeof(uint64_t)) !=
413 s
->l2_size
* sizeof(uint64_t))
416 s
->l2_cache_offsets
[min_index
] = l2_offset
;
417 s
->l2_cache_counts
[min_index
] = 1;
419 l2_index
= (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
420 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
421 if (!cluster_offset
||
422 ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) && allocate
== 1)) {
425 /* allocate a new cluster */
426 if ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) &&
427 (n_end
- n_start
) < s
->cluster_sectors
) {
428 /* if the cluster is already compressed, we must
429 decompress it in the case it is not completely
431 if (decompress_cluster(bs
, cluster_offset
) < 0)
433 cluster_offset
= bdrv_getlength(bs
->file
);
434 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
435 ~(s
->cluster_size
- 1);
436 /* write the cluster content */
437 if (bdrv_pwrite(bs
->file
, cluster_offset
, s
->cluster_cache
, s
->cluster_size
) !=
441 cluster_offset
= bdrv_getlength(bs
->file
);
443 /* round to cluster size */
444 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
445 ~(s
->cluster_size
- 1);
446 bdrv_truncate(bs
->file
, cluster_offset
+ s
->cluster_size
);
447 /* if encrypted, we must initialize the cluster
448 content which won't be written */
450 (n_end
- n_start
) < s
->cluster_sectors
) {
453 start_sect
= (offset
& ~(s
->cluster_size
- 1)) >> 9;
454 memset(s
->cluster_data
+ 512, 0x00, 512);
455 for(i
= 0; i
< s
->cluster_sectors
; i
++) {
456 if (i
< n_start
|| i
>= n_end
) {
458 if (encrypt_sectors(s
, start_sect
+ i
,
460 s
->cluster_data
+ 512, 1,
466 if (bdrv_pwrite(bs
->file
, cluster_offset
+ i
* 512,
467 s
->cluster_data
, 512) != 512)
472 } else if (allocate
== 2) {
473 cluster_offset
|= QCOW_OFLAG_COMPRESSED
|
474 (uint64_t)compressed_size
<< (63 - s
->cluster_bits
);
477 /* update L2 table */
478 tmp
= cpu_to_be64(cluster_offset
);
479 l2_table
[l2_index
] = tmp
;
480 if (bdrv_pwrite_sync(bs
->file
, l2_offset
+ l2_index
* sizeof(tmp
),
481 &tmp
, sizeof(tmp
)) < 0)
484 return cluster_offset
;
487 static int64_t coroutine_fn
qcow_co_get_block_status(BlockDriverState
*bs
,
488 int64_t sector_num
, int nb_sectors
, int *pnum
)
490 BDRVQcowState
*s
= bs
->opaque
;
491 int index_in_cluster
, n
;
492 uint64_t cluster_offset
;
494 qemu_co_mutex_lock(&s
->lock
);
495 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 0, 0, 0, 0);
496 qemu_co_mutex_unlock(&s
->lock
);
497 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
498 n
= s
->cluster_sectors
- index_in_cluster
;
502 if (!cluster_offset
) {
505 if ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) || s
->cipher
) {
506 return BDRV_BLOCK_DATA
;
508 cluster_offset
|= (index_in_cluster
<< BDRV_SECTOR_BITS
);
509 return BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
| cluster_offset
;
512 static int decompress_buffer(uint8_t *out_buf
, int out_buf_size
,
513 const uint8_t *buf
, int buf_size
)
515 z_stream strm1
, *strm
= &strm1
;
518 memset(strm
, 0, sizeof(*strm
));
520 strm
->next_in
= (uint8_t *)buf
;
521 strm
->avail_in
= buf_size
;
522 strm
->next_out
= out_buf
;
523 strm
->avail_out
= out_buf_size
;
525 ret
= inflateInit2(strm
, -12);
528 ret
= inflate(strm
, Z_FINISH
);
529 out_len
= strm
->next_out
- out_buf
;
530 if ((ret
!= Z_STREAM_END
&& ret
!= Z_BUF_ERROR
) ||
531 out_len
!= out_buf_size
) {
539 static int decompress_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
)
541 BDRVQcowState
*s
= bs
->opaque
;
545 coffset
= cluster_offset
& s
->cluster_offset_mask
;
546 if (s
->cluster_cache_offset
!= coffset
) {
547 csize
= cluster_offset
>> (63 - s
->cluster_bits
);
548 csize
&= (s
->cluster_size
- 1);
549 ret
= bdrv_pread(bs
->file
, coffset
, s
->cluster_data
, csize
);
552 if (decompress_buffer(s
->cluster_cache
, s
->cluster_size
,
553 s
->cluster_data
, csize
) < 0) {
556 s
->cluster_cache_offset
= coffset
;
561 static coroutine_fn
int qcow_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
562 int nb_sectors
, QEMUIOVector
*qiov
)
564 BDRVQcowState
*s
= bs
->opaque
;
565 int index_in_cluster
;
567 uint64_t cluster_offset
;
569 QEMUIOVector hd_qiov
;
574 if (qiov
->niov
> 1) {
575 buf
= orig_buf
= qemu_try_blockalign(bs
, qiov
->size
);
581 buf
= (uint8_t *)qiov
->iov
->iov_base
;
584 qemu_co_mutex_lock(&s
->lock
);
586 while (nb_sectors
!= 0) {
587 /* prepare next request */
588 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9,
590 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
591 n
= s
->cluster_sectors
- index_in_cluster
;
592 if (n
> nb_sectors
) {
596 if (!cluster_offset
) {
597 if (bs
->backing_hd
) {
598 /* read from the base image */
599 hd_iov
.iov_base
= (void *)buf
;
600 hd_iov
.iov_len
= n
* 512;
601 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
602 qemu_co_mutex_unlock(&s
->lock
);
603 ret
= bdrv_co_readv(bs
->backing_hd
, sector_num
,
605 qemu_co_mutex_lock(&s
->lock
);
610 /* Note: in this case, no need to wait */
611 memset(buf
, 0, 512 * n
);
613 } else if (cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
614 /* add AIO support for compressed blocks ? */
615 if (decompress_cluster(bs
, cluster_offset
) < 0) {
619 s
->cluster_cache
+ index_in_cluster
* 512, 512 * n
);
621 if ((cluster_offset
& 511) != 0) {
624 hd_iov
.iov_base
= (void *)buf
;
625 hd_iov
.iov_len
= n
* 512;
626 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
627 qemu_co_mutex_unlock(&s
->lock
);
628 ret
= bdrv_co_readv(bs
->file
,
629 (cluster_offset
>> 9) + index_in_cluster
,
631 qemu_co_mutex_lock(&s
->lock
);
637 if (encrypt_sectors(s
, sector_num
, buf
, buf
,
638 n
, false, &err
) < 0) {
651 qemu_co_mutex_unlock(&s
->lock
);
653 if (qiov
->niov
> 1) {
654 qemu_iovec_from_buf(qiov
, 0, orig_buf
, qiov
->size
);
655 qemu_vfree(orig_buf
);
666 static coroutine_fn
int qcow_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
667 int nb_sectors
, QEMUIOVector
*qiov
)
669 BDRVQcowState
*s
= bs
->opaque
;
670 int index_in_cluster
;
671 uint64_t cluster_offset
;
672 const uint8_t *src_buf
;
674 uint8_t *cluster_data
= NULL
;
676 QEMUIOVector hd_qiov
;
680 s
->cluster_cache_offset
= -1; /* disable compressed cache */
682 if (qiov
->niov
> 1) {
683 buf
= orig_buf
= qemu_try_blockalign(bs
, qiov
->size
);
687 qemu_iovec_to_buf(qiov
, 0, buf
, qiov
->size
);
690 buf
= (uint8_t *)qiov
->iov
->iov_base
;
693 qemu_co_mutex_lock(&s
->lock
);
695 while (nb_sectors
!= 0) {
697 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
698 n
= s
->cluster_sectors
- index_in_cluster
;
699 if (n
> nb_sectors
) {
702 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 1, 0,
704 index_in_cluster
+ n
);
705 if (!cluster_offset
|| (cluster_offset
& 511) != 0) {
713 cluster_data
= g_malloc0(s
->cluster_size
);
715 if (encrypt_sectors(s
, sector_num
, cluster_data
, buf
,
716 n
, true, &err
) < 0) {
721 src_buf
= cluster_data
;
726 hd_iov
.iov_base
= (void *)src_buf
;
727 hd_iov
.iov_len
= n
* 512;
728 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
729 qemu_co_mutex_unlock(&s
->lock
);
730 ret
= bdrv_co_writev(bs
->file
,
731 (cluster_offset
>> 9) + index_in_cluster
,
733 qemu_co_mutex_lock(&s
->lock
);
743 qemu_co_mutex_unlock(&s
->lock
);
745 if (qiov
->niov
> 1) {
746 qemu_vfree(orig_buf
);
748 g_free(cluster_data
);
753 static void qcow_close(BlockDriverState
*bs
)
755 BDRVQcowState
*s
= bs
->opaque
;
757 qcrypto_cipher_free(s
->cipher
);
760 qemu_vfree(s
->l2_cache
);
761 g_free(s
->cluster_cache
);
762 g_free(s
->cluster_data
);
764 migrate_del_blocker(s
->migration_blocker
);
765 error_free(s
->migration_blocker
);
768 static int qcow_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
770 int header_size
, backing_filename_len
, l1_size
, shift
, i
;
773 int64_t total_size
= 0;
774 char *backing_file
= NULL
;
776 Error
*local_err
= NULL
;
778 BlockDriverState
*qcow_bs
;
780 /* Read out options */
781 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
783 backing_file
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FILE
);
784 if (qemu_opt_get_bool_del(opts
, BLOCK_OPT_ENCRYPT
, false)) {
785 flags
|= BLOCK_FLAG_ENCRYPT
;
788 ret
= bdrv_create_file(filename
, opts
, &local_err
);
790 error_propagate(errp
, local_err
);
795 ret
= bdrv_open(&qcow_bs
, filename
, NULL
, NULL
,
796 BDRV_O_RDWR
| BDRV_O_PROTOCOL
, NULL
, &local_err
);
798 error_propagate(errp
, local_err
);
802 ret
= bdrv_truncate(qcow_bs
, 0);
807 memset(&header
, 0, sizeof(header
));
808 header
.magic
= cpu_to_be32(QCOW_MAGIC
);
809 header
.version
= cpu_to_be32(QCOW_VERSION
);
810 header
.size
= cpu_to_be64(total_size
);
811 header_size
= sizeof(header
);
812 backing_filename_len
= 0;
814 if (strcmp(backing_file
, "fat:")) {
815 header
.backing_file_offset
= cpu_to_be64(header_size
);
816 backing_filename_len
= strlen(backing_file
);
817 header
.backing_file_size
= cpu_to_be32(backing_filename_len
);
818 header_size
+= backing_filename_len
;
820 /* special backing file for vvfat */
823 header
.cluster_bits
= 9; /* 512 byte cluster to avoid copying
824 unmodified sectors */
825 header
.l2_bits
= 12; /* 32 KB L2 tables */
827 header
.cluster_bits
= 12; /* 4 KB clusters */
828 header
.l2_bits
= 9; /* 4 KB L2 tables */
830 header_size
= (header_size
+ 7) & ~7;
831 shift
= header
.cluster_bits
+ header
.l2_bits
;
832 l1_size
= (total_size
+ (1LL << shift
) - 1) >> shift
;
834 header
.l1_table_offset
= cpu_to_be64(header_size
);
835 if (flags
& BLOCK_FLAG_ENCRYPT
) {
836 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
838 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
841 /* write all the data */
842 ret
= bdrv_pwrite(qcow_bs
, 0, &header
, sizeof(header
));
843 if (ret
!= sizeof(header
)) {
848 ret
= bdrv_pwrite(qcow_bs
, sizeof(header
),
849 backing_file
, backing_filename_len
);
850 if (ret
!= backing_filename_len
) {
855 tmp
= g_malloc0(BDRV_SECTOR_SIZE
);
856 for (i
= 0; i
< ((sizeof(uint64_t)*l1_size
+ BDRV_SECTOR_SIZE
- 1)/
857 BDRV_SECTOR_SIZE
); i
++) {
858 ret
= bdrv_pwrite(qcow_bs
, header_size
+
859 BDRV_SECTOR_SIZE
*i
, tmp
, BDRV_SECTOR_SIZE
);
860 if (ret
!= BDRV_SECTOR_SIZE
) {
871 g_free(backing_file
);
875 static int qcow_make_empty(BlockDriverState
*bs
)
877 BDRVQcowState
*s
= bs
->opaque
;
878 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
881 memset(s
->l1_table
, 0, l1_length
);
882 if (bdrv_pwrite_sync(bs
->file
, s
->l1_table_offset
, s
->l1_table
,
885 ret
= bdrv_truncate(bs
->file
, s
->l1_table_offset
+ l1_length
);
889 memset(s
->l2_cache
, 0, s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
890 memset(s
->l2_cache_offsets
, 0, L2_CACHE_SIZE
* sizeof(uint64_t));
891 memset(s
->l2_cache_counts
, 0, L2_CACHE_SIZE
* sizeof(uint32_t));
896 /* XXX: put compressed sectors first, then all the cluster aligned
897 tables to avoid losing bytes in alignment */
898 static int qcow_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
899 const uint8_t *buf
, int nb_sectors
)
901 BDRVQcowState
*s
= bs
->opaque
;
905 uint64_t cluster_offset
;
907 if (nb_sectors
!= s
->cluster_sectors
) {
910 /* Zero-pad last write if image size is not cluster aligned */
911 if (sector_num
+ nb_sectors
== bs
->total_sectors
&&
912 nb_sectors
< s
->cluster_sectors
) {
913 uint8_t *pad_buf
= qemu_blockalign(bs
, s
->cluster_size
);
914 memset(pad_buf
, 0, s
->cluster_size
);
915 memcpy(pad_buf
, buf
, nb_sectors
* BDRV_SECTOR_SIZE
);
916 ret
= qcow_write_compressed(bs
, sector_num
,
917 pad_buf
, s
->cluster_sectors
);
923 out_buf
= g_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
925 /* best compression, small window, no zlib header */
926 memset(&strm
, 0, sizeof(strm
));
927 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
929 9, Z_DEFAULT_STRATEGY
);
935 strm
.avail_in
= s
->cluster_size
;
936 strm
.next_in
= (uint8_t *)buf
;
937 strm
.avail_out
= s
->cluster_size
;
938 strm
.next_out
= out_buf
;
940 ret
= deflate(&strm
, Z_FINISH
);
941 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
946 out_len
= strm
.next_out
- out_buf
;
950 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
951 /* could not compress: write normal cluster */
952 ret
= bdrv_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
957 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 2,
959 if (cluster_offset
== 0) {
964 cluster_offset
&= s
->cluster_offset_mask
;
965 ret
= bdrv_pwrite(bs
->file
, cluster_offset
, out_buf
, out_len
);
977 static int qcow_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
979 BDRVQcowState
*s
= bs
->opaque
;
980 bdi
->cluster_size
= s
->cluster_size
;
984 static QemuOptsList qcow_create_opts
= {
985 .name
= "qcow-create-opts",
986 .head
= QTAILQ_HEAD_INITIALIZER(qcow_create_opts
.head
),
989 .name
= BLOCK_OPT_SIZE
,
990 .type
= QEMU_OPT_SIZE
,
991 .help
= "Virtual disk size"
994 .name
= BLOCK_OPT_BACKING_FILE
,
995 .type
= QEMU_OPT_STRING
,
996 .help
= "File name of a base image"
999 .name
= BLOCK_OPT_ENCRYPT
,
1000 .type
= QEMU_OPT_BOOL
,
1001 .help
= "Encrypt the image",
1002 .def_value_str
= "off"
1004 { /* end of list */ }
1008 static BlockDriver bdrv_qcow
= {
1009 .format_name
= "qcow",
1010 .instance_size
= sizeof(BDRVQcowState
),
1011 .bdrv_probe
= qcow_probe
,
1012 .bdrv_open
= qcow_open
,
1013 .bdrv_close
= qcow_close
,
1014 .bdrv_reopen_prepare
= qcow_reopen_prepare
,
1015 .bdrv_create
= qcow_create
,
1016 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
1017 .supports_backing
= true,
1019 .bdrv_co_readv
= qcow_co_readv
,
1020 .bdrv_co_writev
= qcow_co_writev
,
1021 .bdrv_co_get_block_status
= qcow_co_get_block_status
,
1023 .bdrv_set_key
= qcow_set_key
,
1024 .bdrv_make_empty
= qcow_make_empty
,
1025 .bdrv_write_compressed
= qcow_write_compressed
,
1026 .bdrv_get_info
= qcow_get_info
,
1028 .create_opts
= &qcow_create_opts
,
1031 static void bdrv_qcow_init(void)
1033 bdrv_register(&bdrv_qcow
);
1036 block_init(bdrv_qcow_init
);