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/osdep.h"
25 #include "qapi/error.h"
26 #include "qemu-common.h"
27 #include "qemu/error-report.h"
28 #include "block/block_int.h"
29 #include "sysemu/block-backend.h"
30 #include "qemu/module.h"
31 #include "qemu/bswap.h"
33 #include "qapi/qmp/qerror.h"
34 #include "crypto/cipher.h"
35 #include "migration/blocker.h"
37 /**************************************************************/
38 /* QEMU COW block driver with compression and encryption support */
40 #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
41 #define QCOW_VERSION 1
43 #define QCOW_CRYPT_NONE 0
44 #define QCOW_CRYPT_AES 1
46 #define QCOW_OFLAG_COMPRESSED (1LL << 63)
48 typedef struct QCowHeader
{
51 uint64_t backing_file_offset
;
52 uint32_t backing_file_size
;
54 uint64_t size
; /* in bytes */
58 uint32_t crypt_method
;
59 uint64_t l1_table_offset
;
60 } QEMU_PACKED QCowHeader
;
62 #define L2_CACHE_SIZE 16
64 typedef struct BDRVQcowState
{
71 uint64_t cluster_offset_mask
;
72 uint64_t l1_table_offset
;
75 uint64_t l2_cache_offsets
[L2_CACHE_SIZE
];
76 uint32_t l2_cache_counts
[L2_CACHE_SIZE
];
77 uint8_t *cluster_cache
;
78 uint8_t *cluster_data
;
79 uint64_t cluster_cache_offset
;
80 QCryptoCipher
*cipher
; /* NULL if no key yet */
81 uint32_t crypt_method_header
;
83 Error
*migration_blocker
;
86 static int decompress_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
);
88 static int qcow_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
90 const QCowHeader
*cow_header
= (const void *)buf
;
92 if (buf_size
>= sizeof(QCowHeader
) &&
93 be32_to_cpu(cow_header
->magic
) == QCOW_MAGIC
&&
94 be32_to_cpu(cow_header
->version
) == QCOW_VERSION
)
100 static int qcow_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
103 BDRVQcowState
*s
= bs
->opaque
;
104 unsigned int len
, i
, shift
;
107 Error
*local_err
= NULL
;
109 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_file
,
115 ret
= bdrv_pread(bs
->file
, 0, &header
, sizeof(header
));
119 be32_to_cpus(&header
.magic
);
120 be32_to_cpus(&header
.version
);
121 be64_to_cpus(&header
.backing_file_offset
);
122 be32_to_cpus(&header
.backing_file_size
);
123 be32_to_cpus(&header
.mtime
);
124 be64_to_cpus(&header
.size
);
125 be32_to_cpus(&header
.crypt_method
);
126 be64_to_cpus(&header
.l1_table_offset
);
128 if (header
.magic
!= QCOW_MAGIC
) {
129 error_setg(errp
, "Image not in qcow format");
133 if (header
.version
!= QCOW_VERSION
) {
134 error_setg(errp
, "Unsupported qcow version %" PRIu32
, header
.version
);
139 if (header
.size
<= 1) {
140 error_setg(errp
, "Image size is too small (must be at least 2 bytes)");
144 if (header
.cluster_bits
< 9 || header
.cluster_bits
> 16) {
145 error_setg(errp
, "Cluster size must be between 512 and 64k");
150 /* l2_bits specifies number of entries; storing a uint64_t in each entry,
151 * so bytes = num_entries << 3. */
152 if (header
.l2_bits
< 9 - 3 || header
.l2_bits
> 16 - 3) {
153 error_setg(errp
, "L2 table size must be between 512 and 64k");
158 if (header
.crypt_method
> QCOW_CRYPT_AES
) {
159 error_setg(errp
, "invalid encryption method in qcow header");
163 if (!qcrypto_cipher_supports(QCRYPTO_CIPHER_ALG_AES_128
,
164 QCRYPTO_CIPHER_MODE_CBC
)) {
165 error_setg(errp
, "AES cipher not available");
169 s
->crypt_method_header
= header
.crypt_method
;
170 if (s
->crypt_method_header
) {
171 if (bdrv_uses_whitelist() &&
172 s
->crypt_method_header
== QCOW_CRYPT_AES
) {
174 "Use of AES-CBC encrypted qcow images is no longer "
175 "supported in system emulators");
176 error_append_hint(errp
,
177 "You can use 'qemu-img convert' to convert your "
178 "image to an alternative supported format, such "
179 "as unencrypted qcow, or raw with the LUKS "
180 "format instead.\n");
185 bs
->encrypted
= true;
187 s
->cluster_bits
= header
.cluster_bits
;
188 s
->cluster_size
= 1 << s
->cluster_bits
;
189 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
190 s
->l2_bits
= header
.l2_bits
;
191 s
->l2_size
= 1 << s
->l2_bits
;
192 bs
->total_sectors
= header
.size
/ 512;
193 s
->cluster_offset_mask
= (1LL << (63 - s
->cluster_bits
)) - 1;
195 /* read the level 1 table */
196 shift
= s
->cluster_bits
+ s
->l2_bits
;
197 if (header
.size
> UINT64_MAX
- (1LL << shift
)) {
198 error_setg(errp
, "Image too large");
202 uint64_t l1_size
= (header
.size
+ (1LL << shift
) - 1) >> shift
;
203 if (l1_size
> INT_MAX
/ sizeof(uint64_t)) {
204 error_setg(errp
, "Image too large");
208 s
->l1_size
= l1_size
;
211 s
->l1_table_offset
= header
.l1_table_offset
;
212 s
->l1_table
= g_try_new(uint64_t, s
->l1_size
);
213 if (s
->l1_table
== NULL
) {
214 error_setg(errp
, "Could not allocate memory for L1 table");
219 ret
= bdrv_pread(bs
->file
, s
->l1_table_offset
, s
->l1_table
,
220 s
->l1_size
* sizeof(uint64_t));
225 for(i
= 0;i
< s
->l1_size
; i
++) {
226 be64_to_cpus(&s
->l1_table
[i
]);
229 /* alloc L2 cache (max. 64k * 16 * 8 = 8 MB) */
231 qemu_try_blockalign(bs
->file
->bs
,
232 s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
233 if (s
->l2_cache
== NULL
) {
234 error_setg(errp
, "Could not allocate L2 table cache");
238 s
->cluster_cache
= g_malloc(s
->cluster_size
);
239 s
->cluster_data
= g_malloc(s
->cluster_size
);
240 s
->cluster_cache_offset
= -1;
242 /* read the backing file name */
243 if (header
.backing_file_offset
!= 0) {
244 len
= header
.backing_file_size
;
245 if (len
> 1023 || len
>= sizeof(bs
->backing_file
)) {
246 error_setg(errp
, "Backing file name too long");
250 ret
= bdrv_pread(bs
->file
, header
.backing_file_offset
,
251 bs
->backing_file
, len
);
255 bs
->backing_file
[len
] = '\0';
258 /* Disable migration when qcow images are used */
259 error_setg(&s
->migration_blocker
, "The qcow format used by node '%s' "
260 "does not support live migration",
261 bdrv_get_device_or_node_name(bs
));
262 ret
= migrate_add_blocker(s
->migration_blocker
, &local_err
);
264 error_propagate(errp
, local_err
);
265 error_free(s
->migration_blocker
);
269 qemu_co_mutex_init(&s
->lock
);
274 qemu_vfree(s
->l2_cache
);
275 g_free(s
->cluster_cache
);
276 g_free(s
->cluster_data
);
281 /* We have nothing to do for QCOW reopen, stubs just return
283 static int qcow_reopen_prepare(BDRVReopenState
*state
,
284 BlockReopenQueue
*queue
, Error
**errp
)
289 static int qcow_set_key(BlockDriverState
*bs
, const char *key
)
291 BDRVQcowState
*s
= bs
->opaque
;
296 memset(keybuf
, 0, 16);
300 /* XXX: we could compress the chars to 7 bits to increase
302 for(i
= 0;i
< len
;i
++) {
305 assert(bs
->encrypted
);
307 qcrypto_cipher_free(s
->cipher
);
308 s
->cipher
= qcrypto_cipher_new(
309 QCRYPTO_CIPHER_ALG_AES_128
,
310 QCRYPTO_CIPHER_MODE_CBC
,
311 keybuf
, G_N_ELEMENTS(keybuf
),
315 /* XXX would be nice if errors in this method could
316 * be properly propagate to the caller. Would need
317 * the bdrv_set_key() API signature to be fixed. */
324 /* The crypt function is compatible with the linux cryptoloop
325 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
327 static int encrypt_sectors(BDRVQcowState
*s
, int64_t sector_num
,
328 uint8_t *out_buf
, const uint8_t *in_buf
,
329 int nb_sectors
, bool enc
, Error
**errp
)
338 for(i
= 0; i
< nb_sectors
; i
++) {
339 ivec
.ll
[0] = cpu_to_le64(sector_num
);
341 if (qcrypto_cipher_setiv(s
->cipher
,
342 ivec
.b
, G_N_ELEMENTS(ivec
.b
),
347 ret
= qcrypto_cipher_encrypt(s
->cipher
,
353 ret
= qcrypto_cipher_decrypt(s
->cipher
,
373 * 1 to allocate a normal cluster (for sector indexes 'n_start' to
376 * 2 to allocate a compressed cluster of size
377 * 'compressed_size'. 'compressed_size' must be > 0 and <
380 * return 0 if not allocated.
382 static uint64_t get_cluster_offset(BlockDriverState
*bs
,
383 uint64_t offset
, int allocate
,
385 int n_start
, int n_end
)
387 BDRVQcowState
*s
= bs
->opaque
;
388 int min_index
, i
, j
, l1_index
, l2_index
;
389 uint64_t l2_offset
, *l2_table
, cluster_offset
, tmp
;
393 l1_index
= offset
>> (s
->l2_bits
+ s
->cluster_bits
);
394 l2_offset
= s
->l1_table
[l1_index
];
399 /* allocate a new l2 entry */
400 l2_offset
= bdrv_getlength(bs
->file
->bs
);
401 /* round to cluster size */
402 l2_offset
= (l2_offset
+ s
->cluster_size
- 1) & ~(s
->cluster_size
- 1);
403 /* update the L1 entry */
404 s
->l1_table
[l1_index
] = l2_offset
;
405 tmp
= cpu_to_be64(l2_offset
);
406 if (bdrv_pwrite_sync(bs
->file
,
407 s
->l1_table_offset
+ l1_index
* sizeof(tmp
),
408 &tmp
, sizeof(tmp
)) < 0)
412 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
413 if (l2_offset
== s
->l2_cache_offsets
[i
]) {
414 /* increment the hit count */
415 if (++s
->l2_cache_counts
[i
] == 0xffffffff) {
416 for(j
= 0; j
< L2_CACHE_SIZE
; j
++) {
417 s
->l2_cache_counts
[j
] >>= 1;
420 l2_table
= s
->l2_cache
+ (i
<< s
->l2_bits
);
424 /* not found: load a new entry in the least used one */
426 min_count
= 0xffffffff;
427 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
428 if (s
->l2_cache_counts
[i
] < min_count
) {
429 min_count
= s
->l2_cache_counts
[i
];
433 l2_table
= s
->l2_cache
+ (min_index
<< s
->l2_bits
);
435 memset(l2_table
, 0, s
->l2_size
* sizeof(uint64_t));
436 if (bdrv_pwrite_sync(bs
->file
, l2_offset
, l2_table
,
437 s
->l2_size
* sizeof(uint64_t)) < 0)
440 if (bdrv_pread(bs
->file
, l2_offset
, l2_table
,
441 s
->l2_size
* sizeof(uint64_t)) !=
442 s
->l2_size
* sizeof(uint64_t))
445 s
->l2_cache_offsets
[min_index
] = l2_offset
;
446 s
->l2_cache_counts
[min_index
] = 1;
448 l2_index
= (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
449 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
450 if (!cluster_offset
||
451 ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) && allocate
== 1)) {
454 /* allocate a new cluster */
455 if ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) &&
456 (n_end
- n_start
) < s
->cluster_sectors
) {
457 /* if the cluster is already compressed, we must
458 decompress it in the case it is not completely
460 if (decompress_cluster(bs
, cluster_offset
) < 0)
462 cluster_offset
= bdrv_getlength(bs
->file
->bs
);
463 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
464 ~(s
->cluster_size
- 1);
465 /* write the cluster content */
466 if (bdrv_pwrite(bs
->file
, cluster_offset
, s
->cluster_cache
,
471 cluster_offset
= bdrv_getlength(bs
->file
->bs
);
473 /* round to cluster size */
474 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
475 ~(s
->cluster_size
- 1);
476 bdrv_truncate(bs
->file
, cluster_offset
+ s
->cluster_size
, NULL
);
477 /* if encrypted, we must initialize the cluster
478 content which won't be written */
480 (n_end
- n_start
) < s
->cluster_sectors
) {
483 start_sect
= (offset
& ~(s
->cluster_size
- 1)) >> 9;
484 memset(s
->cluster_data
+ 512, 0x00, 512);
485 for(i
= 0; i
< s
->cluster_sectors
; i
++) {
486 if (i
< n_start
|| i
>= n_end
) {
488 if (encrypt_sectors(s
, start_sect
+ i
,
490 s
->cluster_data
+ 512, 1,
496 if (bdrv_pwrite(bs
->file
,
497 cluster_offset
+ i
* 512,
498 s
->cluster_data
, 512) != 512)
503 } else if (allocate
== 2) {
504 cluster_offset
|= QCOW_OFLAG_COMPRESSED
|
505 (uint64_t)compressed_size
<< (63 - s
->cluster_bits
);
508 /* update L2 table */
509 tmp
= cpu_to_be64(cluster_offset
);
510 l2_table
[l2_index
] = tmp
;
511 if (bdrv_pwrite_sync(bs
->file
, l2_offset
+ l2_index
* sizeof(tmp
),
512 &tmp
, sizeof(tmp
)) < 0)
515 return cluster_offset
;
518 static int64_t coroutine_fn
qcow_co_get_block_status(BlockDriverState
*bs
,
519 int64_t sector_num
, int nb_sectors
, int *pnum
, BlockDriverState
**file
)
521 BDRVQcowState
*s
= bs
->opaque
;
522 int index_in_cluster
, n
;
523 uint64_t cluster_offset
;
525 qemu_co_mutex_lock(&s
->lock
);
526 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 0, 0, 0, 0);
527 qemu_co_mutex_unlock(&s
->lock
);
528 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
529 n
= s
->cluster_sectors
- index_in_cluster
;
533 if (!cluster_offset
) {
536 if ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) || s
->cipher
) {
537 return BDRV_BLOCK_DATA
;
539 cluster_offset
|= (index_in_cluster
<< BDRV_SECTOR_BITS
);
540 *file
= bs
->file
->bs
;
541 return BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
| cluster_offset
;
544 static int decompress_buffer(uint8_t *out_buf
, int out_buf_size
,
545 const uint8_t *buf
, int buf_size
)
547 z_stream strm1
, *strm
= &strm1
;
550 memset(strm
, 0, sizeof(*strm
));
552 strm
->next_in
= (uint8_t *)buf
;
553 strm
->avail_in
= buf_size
;
554 strm
->next_out
= out_buf
;
555 strm
->avail_out
= out_buf_size
;
557 ret
= inflateInit2(strm
, -12);
560 ret
= inflate(strm
, Z_FINISH
);
561 out_len
= strm
->next_out
- out_buf
;
562 if ((ret
!= Z_STREAM_END
&& ret
!= Z_BUF_ERROR
) ||
563 out_len
!= out_buf_size
) {
571 static int decompress_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
)
573 BDRVQcowState
*s
= bs
->opaque
;
577 coffset
= cluster_offset
& s
->cluster_offset_mask
;
578 if (s
->cluster_cache_offset
!= coffset
) {
579 csize
= cluster_offset
>> (63 - s
->cluster_bits
);
580 csize
&= (s
->cluster_size
- 1);
581 ret
= bdrv_pread(bs
->file
, coffset
, s
->cluster_data
, csize
);
584 if (decompress_buffer(s
->cluster_cache
, s
->cluster_size
,
585 s
->cluster_data
, csize
) < 0) {
588 s
->cluster_cache_offset
= coffset
;
593 static coroutine_fn
int qcow_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
594 int nb_sectors
, QEMUIOVector
*qiov
)
596 BDRVQcowState
*s
= bs
->opaque
;
597 int index_in_cluster
;
599 uint64_t cluster_offset
;
601 QEMUIOVector hd_qiov
;
606 if (qiov
->niov
> 1) {
607 buf
= orig_buf
= qemu_try_blockalign(bs
, qiov
->size
);
613 buf
= (uint8_t *)qiov
->iov
->iov_base
;
616 qemu_co_mutex_lock(&s
->lock
);
618 while (nb_sectors
!= 0) {
619 /* prepare next request */
620 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9,
622 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
623 n
= s
->cluster_sectors
- index_in_cluster
;
624 if (n
> nb_sectors
) {
628 if (!cluster_offset
) {
630 /* read from the base image */
631 hd_iov
.iov_base
= (void *)buf
;
632 hd_iov
.iov_len
= n
* 512;
633 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
634 qemu_co_mutex_unlock(&s
->lock
);
635 ret
= bdrv_co_readv(bs
->backing
, sector_num
, n
, &hd_qiov
);
636 qemu_co_mutex_lock(&s
->lock
);
641 /* Note: in this case, no need to wait */
642 memset(buf
, 0, 512 * n
);
644 } else if (cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
645 /* add AIO support for compressed blocks ? */
646 if (decompress_cluster(bs
, cluster_offset
) < 0) {
650 s
->cluster_cache
+ index_in_cluster
* 512, 512 * n
);
652 if ((cluster_offset
& 511) != 0) {
655 hd_iov
.iov_base
= (void *)buf
;
656 hd_iov
.iov_len
= n
* 512;
657 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
658 qemu_co_mutex_unlock(&s
->lock
);
659 ret
= bdrv_co_readv(bs
->file
,
660 (cluster_offset
>> 9) + index_in_cluster
,
662 qemu_co_mutex_lock(&s
->lock
);
668 if (encrypt_sectors(s
, sector_num
, buf
, buf
,
669 n
, false, &err
) < 0) {
682 qemu_co_mutex_unlock(&s
->lock
);
684 if (qiov
->niov
> 1) {
685 qemu_iovec_from_buf(qiov
, 0, orig_buf
, qiov
->size
);
686 qemu_vfree(orig_buf
);
697 static coroutine_fn
int qcow_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
698 int nb_sectors
, QEMUIOVector
*qiov
)
700 BDRVQcowState
*s
= bs
->opaque
;
701 int index_in_cluster
;
702 uint64_t cluster_offset
;
703 const uint8_t *src_buf
;
705 uint8_t *cluster_data
= NULL
;
707 QEMUIOVector hd_qiov
;
711 s
->cluster_cache_offset
= -1; /* disable compressed cache */
713 if (qiov
->niov
> 1) {
714 buf
= orig_buf
= qemu_try_blockalign(bs
, qiov
->size
);
718 qemu_iovec_to_buf(qiov
, 0, buf
, qiov
->size
);
721 buf
= (uint8_t *)qiov
->iov
->iov_base
;
724 qemu_co_mutex_lock(&s
->lock
);
726 while (nb_sectors
!= 0) {
728 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
729 n
= s
->cluster_sectors
- index_in_cluster
;
730 if (n
> nb_sectors
) {
733 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 1, 0,
735 index_in_cluster
+ n
);
736 if (!cluster_offset
|| (cluster_offset
& 511) != 0) {
744 cluster_data
= g_malloc0(s
->cluster_size
);
746 if (encrypt_sectors(s
, sector_num
, cluster_data
, buf
,
747 n
, true, &err
) < 0) {
752 src_buf
= cluster_data
;
757 hd_iov
.iov_base
= (void *)src_buf
;
758 hd_iov
.iov_len
= n
* 512;
759 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
760 qemu_co_mutex_unlock(&s
->lock
);
761 ret
= bdrv_co_writev(bs
->file
,
762 (cluster_offset
>> 9) + index_in_cluster
,
764 qemu_co_mutex_lock(&s
->lock
);
774 qemu_co_mutex_unlock(&s
->lock
);
776 if (qiov
->niov
> 1) {
777 qemu_vfree(orig_buf
);
779 g_free(cluster_data
);
784 static void qcow_close(BlockDriverState
*bs
)
786 BDRVQcowState
*s
= bs
->opaque
;
788 qcrypto_cipher_free(s
->cipher
);
791 qemu_vfree(s
->l2_cache
);
792 g_free(s
->cluster_cache
);
793 g_free(s
->cluster_data
);
795 migrate_del_blocker(s
->migration_blocker
);
796 error_free(s
->migration_blocker
);
799 static int qcow_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
801 int header_size
, backing_filename_len
, l1_size
, shift
, i
;
804 int64_t total_size
= 0;
805 char *backing_file
= NULL
;
807 Error
*local_err
= NULL
;
809 BlockBackend
*qcow_blk
;
811 /* Read out options */
812 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
814 backing_file
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FILE
);
815 if (qemu_opt_get_bool_del(opts
, BLOCK_OPT_ENCRYPT
, false)) {
816 flags
|= BLOCK_FLAG_ENCRYPT
;
819 ret
= bdrv_create_file(filename
, opts
, &local_err
);
821 error_propagate(errp
, local_err
);
825 qcow_blk
= blk_new_open(filename
, NULL
, NULL
,
826 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
,
828 if (qcow_blk
== NULL
) {
829 error_propagate(errp
, local_err
);
834 blk_set_allow_write_beyond_eof(qcow_blk
, true);
836 ret
= blk_truncate(qcow_blk
, 0, errp
);
841 memset(&header
, 0, sizeof(header
));
842 header
.magic
= cpu_to_be32(QCOW_MAGIC
);
843 header
.version
= cpu_to_be32(QCOW_VERSION
);
844 header
.size
= cpu_to_be64(total_size
);
845 header_size
= sizeof(header
);
846 backing_filename_len
= 0;
848 if (strcmp(backing_file
, "fat:")) {
849 header
.backing_file_offset
= cpu_to_be64(header_size
);
850 backing_filename_len
= strlen(backing_file
);
851 header
.backing_file_size
= cpu_to_be32(backing_filename_len
);
852 header_size
+= backing_filename_len
;
854 /* special backing file for vvfat */
855 g_free(backing_file
);
858 header
.cluster_bits
= 9; /* 512 byte cluster to avoid copying
859 unmodified sectors */
860 header
.l2_bits
= 12; /* 32 KB L2 tables */
862 header
.cluster_bits
= 12; /* 4 KB clusters */
863 header
.l2_bits
= 9; /* 4 KB L2 tables */
865 header_size
= (header_size
+ 7) & ~7;
866 shift
= header
.cluster_bits
+ header
.l2_bits
;
867 l1_size
= (total_size
+ (1LL << shift
) - 1) >> shift
;
869 header
.l1_table_offset
= cpu_to_be64(header_size
);
870 if (flags
& BLOCK_FLAG_ENCRYPT
) {
871 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
873 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
876 /* write all the data */
877 ret
= blk_pwrite(qcow_blk
, 0, &header
, sizeof(header
), 0);
878 if (ret
!= sizeof(header
)) {
883 ret
= blk_pwrite(qcow_blk
, sizeof(header
),
884 backing_file
, backing_filename_len
, 0);
885 if (ret
!= backing_filename_len
) {
890 tmp
= g_malloc0(BDRV_SECTOR_SIZE
);
891 for (i
= 0; i
< DIV_ROUND_UP(sizeof(uint64_t) * l1_size
, BDRV_SECTOR_SIZE
);
893 ret
= blk_pwrite(qcow_blk
, header_size
+ BDRV_SECTOR_SIZE
* i
,
894 tmp
, BDRV_SECTOR_SIZE
, 0);
895 if (ret
!= BDRV_SECTOR_SIZE
) {
906 g_free(backing_file
);
910 static int qcow_make_empty(BlockDriverState
*bs
)
912 BDRVQcowState
*s
= bs
->opaque
;
913 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
916 memset(s
->l1_table
, 0, l1_length
);
917 if (bdrv_pwrite_sync(bs
->file
, s
->l1_table_offset
, s
->l1_table
,
920 ret
= bdrv_truncate(bs
->file
, s
->l1_table_offset
+ l1_length
, NULL
);
924 memset(s
->l2_cache
, 0, s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
925 memset(s
->l2_cache_offsets
, 0, L2_CACHE_SIZE
* sizeof(uint64_t));
926 memset(s
->l2_cache_counts
, 0, L2_CACHE_SIZE
* sizeof(uint32_t));
931 /* XXX: put compressed sectors first, then all the cluster aligned
932 tables to avoid losing bytes in alignment */
933 static coroutine_fn
int
934 qcow_co_pwritev_compressed(BlockDriverState
*bs
, uint64_t offset
,
935 uint64_t bytes
, QEMUIOVector
*qiov
)
937 BDRVQcowState
*s
= bs
->opaque
;
938 QEMUIOVector hd_qiov
;
942 uint8_t *buf
, *out_buf
;
943 uint64_t cluster_offset
;
945 buf
= qemu_blockalign(bs
, s
->cluster_size
);
946 if (bytes
!= s
->cluster_size
) {
947 if (bytes
> s
->cluster_size
||
948 offset
+ bytes
!= bs
->total_sectors
<< BDRV_SECTOR_BITS
)
953 /* Zero-pad last write if image size is not cluster aligned */
954 memset(buf
+ bytes
, 0, s
->cluster_size
- bytes
);
956 qemu_iovec_to_buf(qiov
, 0, buf
, qiov
->size
);
958 out_buf
= g_malloc(s
->cluster_size
);
960 /* best compression, small window, no zlib header */
961 memset(&strm
, 0, sizeof(strm
));
962 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
964 9, Z_DEFAULT_STRATEGY
);
970 strm
.avail_in
= s
->cluster_size
;
971 strm
.next_in
= (uint8_t *)buf
;
972 strm
.avail_out
= s
->cluster_size
;
973 strm
.next_out
= out_buf
;
975 ret
= deflate(&strm
, Z_FINISH
);
976 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
981 out_len
= strm
.next_out
- out_buf
;
985 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
986 /* could not compress: write normal cluster */
987 ret
= qcow_co_writev(bs
, offset
>> BDRV_SECTOR_BITS
,
988 bytes
>> BDRV_SECTOR_BITS
, qiov
);
994 qemu_co_mutex_lock(&s
->lock
);
995 cluster_offset
= get_cluster_offset(bs
, offset
, 2, out_len
, 0, 0);
996 qemu_co_mutex_unlock(&s
->lock
);
997 if (cluster_offset
== 0) {
1001 cluster_offset
&= s
->cluster_offset_mask
;
1003 iov
= (struct iovec
) {
1004 .iov_base
= out_buf
,
1007 qemu_iovec_init_external(&hd_qiov
, &iov
, 1);
1008 ret
= bdrv_co_pwritev(bs
->file
, cluster_offset
, out_len
, &hd_qiov
, 0);
1020 static int qcow_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1022 BDRVQcowState
*s
= bs
->opaque
;
1023 bdi
->cluster_size
= s
->cluster_size
;
1027 static QemuOptsList qcow_create_opts
= {
1028 .name
= "qcow-create-opts",
1029 .head
= QTAILQ_HEAD_INITIALIZER(qcow_create_opts
.head
),
1032 .name
= BLOCK_OPT_SIZE
,
1033 .type
= QEMU_OPT_SIZE
,
1034 .help
= "Virtual disk size"
1037 .name
= BLOCK_OPT_BACKING_FILE
,
1038 .type
= QEMU_OPT_STRING
,
1039 .help
= "File name of a base image"
1042 .name
= BLOCK_OPT_ENCRYPT
,
1043 .type
= QEMU_OPT_BOOL
,
1044 .help
= "Encrypt the image",
1045 .def_value_str
= "off"
1047 { /* end of list */ }
1051 static BlockDriver bdrv_qcow
= {
1052 .format_name
= "qcow",
1053 .instance_size
= sizeof(BDRVQcowState
),
1054 .bdrv_probe
= qcow_probe
,
1055 .bdrv_open
= qcow_open
,
1056 .bdrv_close
= qcow_close
,
1057 .bdrv_child_perm
= bdrv_format_default_perms
,
1058 .bdrv_reopen_prepare
= qcow_reopen_prepare
,
1059 .bdrv_create
= qcow_create
,
1060 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
1061 .supports_backing
= true,
1063 .bdrv_co_readv
= qcow_co_readv
,
1064 .bdrv_co_writev
= qcow_co_writev
,
1065 .bdrv_co_get_block_status
= qcow_co_get_block_status
,
1067 .bdrv_set_key
= qcow_set_key
,
1068 .bdrv_make_empty
= qcow_make_empty
,
1069 .bdrv_co_pwritev_compressed
= qcow_co_pwritev_compressed
,
1070 .bdrv_get_info
= qcow_get_info
,
1072 .create_opts
= &qcow_create_opts
,
1075 static void bdrv_qcow_init(void)
1077 bdrv_register(&bdrv_qcow
);
1080 block_init(bdrv_qcow_init
);