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/migration.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
;
108 ret
= bdrv_pread(bs
->file
->bs
, 0, &header
, sizeof(header
));
112 be32_to_cpus(&header
.magic
);
113 be32_to_cpus(&header
.version
);
114 be64_to_cpus(&header
.backing_file_offset
);
115 be32_to_cpus(&header
.backing_file_size
);
116 be32_to_cpus(&header
.mtime
);
117 be64_to_cpus(&header
.size
);
118 be32_to_cpus(&header
.crypt_method
);
119 be64_to_cpus(&header
.l1_table_offset
);
121 if (header
.magic
!= QCOW_MAGIC
) {
122 error_setg(errp
, "Image not in qcow format");
126 if (header
.version
!= QCOW_VERSION
) {
127 error_setg(errp
, "Unsupported qcow version %" PRIu32
, header
.version
);
132 if (header
.size
<= 1) {
133 error_setg(errp
, "Image size is too small (must be at least 2 bytes)");
137 if (header
.cluster_bits
< 9 || header
.cluster_bits
> 16) {
138 error_setg(errp
, "Cluster size must be between 512 and 64k");
143 /* l2_bits specifies number of entries; storing a uint64_t in each entry,
144 * so bytes = num_entries << 3. */
145 if (header
.l2_bits
< 9 - 3 || header
.l2_bits
> 16 - 3) {
146 error_setg(errp
, "L2 table size must be between 512 and 64k");
151 if (header
.crypt_method
> QCOW_CRYPT_AES
) {
152 error_setg(errp
, "invalid encryption method in qcow header");
156 if (!qcrypto_cipher_supports(QCRYPTO_CIPHER_ALG_AES_128
)) {
157 error_setg(errp
, "AES cipher not available");
161 s
->crypt_method_header
= header
.crypt_method
;
162 if (s
->crypt_method_header
) {
163 if (bdrv_uses_whitelist() &&
164 s
->crypt_method_header
== QCOW_CRYPT_AES
) {
165 error_report("qcow built-in AES encryption is deprecated");
166 error_printf("Support for it will be removed in a future release.\n"
167 "You can use 'qemu-img convert' to switch to an\n"
168 "unencrypted qcow image, or a LUKS raw image.\n");
173 s
->cluster_bits
= header
.cluster_bits
;
174 s
->cluster_size
= 1 << s
->cluster_bits
;
175 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
176 s
->l2_bits
= header
.l2_bits
;
177 s
->l2_size
= 1 << s
->l2_bits
;
178 bs
->total_sectors
= header
.size
/ 512;
179 s
->cluster_offset_mask
= (1LL << (63 - s
->cluster_bits
)) - 1;
181 /* read the level 1 table */
182 shift
= s
->cluster_bits
+ s
->l2_bits
;
183 if (header
.size
> UINT64_MAX
- (1LL << shift
)) {
184 error_setg(errp
, "Image too large");
188 uint64_t l1_size
= (header
.size
+ (1LL << shift
) - 1) >> shift
;
189 if (l1_size
> INT_MAX
/ sizeof(uint64_t)) {
190 error_setg(errp
, "Image too large");
194 s
->l1_size
= l1_size
;
197 s
->l1_table_offset
= header
.l1_table_offset
;
198 s
->l1_table
= g_try_new(uint64_t, s
->l1_size
);
199 if (s
->l1_table
== NULL
) {
200 error_setg(errp
, "Could not allocate memory for L1 table");
205 ret
= bdrv_pread(bs
->file
->bs
, s
->l1_table_offset
, s
->l1_table
,
206 s
->l1_size
* sizeof(uint64_t));
211 for(i
= 0;i
< s
->l1_size
; i
++) {
212 be64_to_cpus(&s
->l1_table
[i
]);
215 /* alloc L2 cache (max. 64k * 16 * 8 = 8 MB) */
217 qemu_try_blockalign(bs
->file
->bs
,
218 s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
219 if (s
->l2_cache
== NULL
) {
220 error_setg(errp
, "Could not allocate L2 table cache");
224 s
->cluster_cache
= g_malloc(s
->cluster_size
);
225 s
->cluster_data
= g_malloc(s
->cluster_size
);
226 s
->cluster_cache_offset
= -1;
228 /* read the backing file name */
229 if (header
.backing_file_offset
!= 0) {
230 len
= header
.backing_file_size
;
231 if (len
> 1023 || len
>= sizeof(bs
->backing_file
)) {
232 error_setg(errp
, "Backing file name too long");
236 ret
= bdrv_pread(bs
->file
->bs
, header
.backing_file_offset
,
237 bs
->backing_file
, len
);
241 bs
->backing_file
[len
] = '\0';
244 /* Disable migration when qcow images are used */
245 error_setg(&s
->migration_blocker
, "The qcow format used by node '%s' "
246 "does not support live migration",
247 bdrv_get_device_or_node_name(bs
));
248 migrate_add_blocker(s
->migration_blocker
);
250 qemu_co_mutex_init(&s
->lock
);
255 qemu_vfree(s
->l2_cache
);
256 g_free(s
->cluster_cache
);
257 g_free(s
->cluster_data
);
262 /* We have nothing to do for QCOW reopen, stubs just return
264 static int qcow_reopen_prepare(BDRVReopenState
*state
,
265 BlockReopenQueue
*queue
, Error
**errp
)
270 static int qcow_set_key(BlockDriverState
*bs
, const char *key
)
272 BDRVQcowState
*s
= bs
->opaque
;
277 memset(keybuf
, 0, 16);
281 /* XXX: we could compress the chars to 7 bits to increase
283 for(i
= 0;i
< len
;i
++) {
286 assert(bs
->encrypted
);
288 qcrypto_cipher_free(s
->cipher
);
289 s
->cipher
= qcrypto_cipher_new(
290 QCRYPTO_CIPHER_ALG_AES_128
,
291 QCRYPTO_CIPHER_MODE_CBC
,
292 keybuf
, G_N_ELEMENTS(keybuf
),
296 /* XXX would be nice if errors in this method could
297 * be properly propagate to the caller. Would need
298 * the bdrv_set_key() API signature to be fixed. */
305 /* The crypt function is compatible with the linux cryptoloop
306 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
308 static int encrypt_sectors(BDRVQcowState
*s
, int64_t sector_num
,
309 uint8_t *out_buf
, const uint8_t *in_buf
,
310 int nb_sectors
, bool enc
, Error
**errp
)
319 for(i
= 0; i
< nb_sectors
; i
++) {
320 ivec
.ll
[0] = cpu_to_le64(sector_num
);
322 if (qcrypto_cipher_setiv(s
->cipher
,
323 ivec
.b
, G_N_ELEMENTS(ivec
.b
),
328 ret
= qcrypto_cipher_encrypt(s
->cipher
,
334 ret
= qcrypto_cipher_decrypt(s
->cipher
,
354 * 1 to allocate a normal cluster (for sector indexes 'n_start' to
357 * 2 to allocate a compressed cluster of size
358 * 'compressed_size'. 'compressed_size' must be > 0 and <
361 * return 0 if not allocated.
363 static uint64_t get_cluster_offset(BlockDriverState
*bs
,
364 uint64_t offset
, int allocate
,
366 int n_start
, int n_end
)
368 BDRVQcowState
*s
= bs
->opaque
;
369 int min_index
, i
, j
, l1_index
, l2_index
;
370 uint64_t l2_offset
, *l2_table
, cluster_offset
, tmp
;
374 l1_index
= offset
>> (s
->l2_bits
+ s
->cluster_bits
);
375 l2_offset
= s
->l1_table
[l1_index
];
380 /* allocate a new l2 entry */
381 l2_offset
= bdrv_getlength(bs
->file
->bs
);
382 /* round to cluster size */
383 l2_offset
= (l2_offset
+ s
->cluster_size
- 1) & ~(s
->cluster_size
- 1);
384 /* update the L1 entry */
385 s
->l1_table
[l1_index
] = l2_offset
;
386 tmp
= cpu_to_be64(l2_offset
);
387 if (bdrv_pwrite_sync(bs
->file
->bs
,
388 s
->l1_table_offset
+ l1_index
* sizeof(tmp
),
389 &tmp
, sizeof(tmp
)) < 0)
393 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
394 if (l2_offset
== s
->l2_cache_offsets
[i
]) {
395 /* increment the hit count */
396 if (++s
->l2_cache_counts
[i
] == 0xffffffff) {
397 for(j
= 0; j
< L2_CACHE_SIZE
; j
++) {
398 s
->l2_cache_counts
[j
] >>= 1;
401 l2_table
= s
->l2_cache
+ (i
<< s
->l2_bits
);
405 /* not found: load a new entry in the least used one */
407 min_count
= 0xffffffff;
408 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
409 if (s
->l2_cache_counts
[i
] < min_count
) {
410 min_count
= s
->l2_cache_counts
[i
];
414 l2_table
= s
->l2_cache
+ (min_index
<< s
->l2_bits
);
416 memset(l2_table
, 0, s
->l2_size
* sizeof(uint64_t));
417 if (bdrv_pwrite_sync(bs
->file
->bs
, l2_offset
, l2_table
,
418 s
->l2_size
* sizeof(uint64_t)) < 0)
421 if (bdrv_pread(bs
->file
->bs
, l2_offset
, l2_table
,
422 s
->l2_size
* sizeof(uint64_t)) !=
423 s
->l2_size
* sizeof(uint64_t))
426 s
->l2_cache_offsets
[min_index
] = l2_offset
;
427 s
->l2_cache_counts
[min_index
] = 1;
429 l2_index
= (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
430 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
431 if (!cluster_offset
||
432 ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) && allocate
== 1)) {
435 /* allocate a new cluster */
436 if ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) &&
437 (n_end
- n_start
) < s
->cluster_sectors
) {
438 /* if the cluster is already compressed, we must
439 decompress it in the case it is not completely
441 if (decompress_cluster(bs
, cluster_offset
) < 0)
443 cluster_offset
= bdrv_getlength(bs
->file
->bs
);
444 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
445 ~(s
->cluster_size
- 1);
446 /* write the cluster content */
447 if (bdrv_pwrite(bs
->file
->bs
, cluster_offset
, s
->cluster_cache
,
452 cluster_offset
= bdrv_getlength(bs
->file
->bs
);
454 /* round to cluster size */
455 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
456 ~(s
->cluster_size
- 1);
457 bdrv_truncate(bs
->file
->bs
, cluster_offset
+ s
->cluster_size
);
458 /* if encrypted, we must initialize the cluster
459 content which won't be written */
461 (n_end
- n_start
) < s
->cluster_sectors
) {
464 start_sect
= (offset
& ~(s
->cluster_size
- 1)) >> 9;
465 memset(s
->cluster_data
+ 512, 0x00, 512);
466 for(i
= 0; i
< s
->cluster_sectors
; i
++) {
467 if (i
< n_start
|| i
>= n_end
) {
469 if (encrypt_sectors(s
, start_sect
+ i
,
471 s
->cluster_data
+ 512, 1,
477 if (bdrv_pwrite(bs
->file
->bs
,
478 cluster_offset
+ i
* 512,
479 s
->cluster_data
, 512) != 512)
484 } else if (allocate
== 2) {
485 cluster_offset
|= QCOW_OFLAG_COMPRESSED
|
486 (uint64_t)compressed_size
<< (63 - s
->cluster_bits
);
489 /* update L2 table */
490 tmp
= cpu_to_be64(cluster_offset
);
491 l2_table
[l2_index
] = tmp
;
492 if (bdrv_pwrite_sync(bs
->file
->bs
, l2_offset
+ l2_index
* sizeof(tmp
),
493 &tmp
, sizeof(tmp
)) < 0)
496 return cluster_offset
;
499 static int64_t coroutine_fn
qcow_co_get_block_status(BlockDriverState
*bs
,
500 int64_t sector_num
, int nb_sectors
, int *pnum
, BlockDriverState
**file
)
502 BDRVQcowState
*s
= bs
->opaque
;
503 int index_in_cluster
, n
;
504 uint64_t cluster_offset
;
506 qemu_co_mutex_lock(&s
->lock
);
507 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 0, 0, 0, 0);
508 qemu_co_mutex_unlock(&s
->lock
);
509 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
510 n
= s
->cluster_sectors
- index_in_cluster
;
514 if (!cluster_offset
) {
517 if ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) || s
->cipher
) {
518 return BDRV_BLOCK_DATA
;
520 cluster_offset
|= (index_in_cluster
<< BDRV_SECTOR_BITS
);
521 *file
= bs
->file
->bs
;
522 return BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
| cluster_offset
;
525 static int decompress_buffer(uint8_t *out_buf
, int out_buf_size
,
526 const uint8_t *buf
, int buf_size
)
528 z_stream strm1
, *strm
= &strm1
;
531 memset(strm
, 0, sizeof(*strm
));
533 strm
->next_in
= (uint8_t *)buf
;
534 strm
->avail_in
= buf_size
;
535 strm
->next_out
= out_buf
;
536 strm
->avail_out
= out_buf_size
;
538 ret
= inflateInit2(strm
, -12);
541 ret
= inflate(strm
, Z_FINISH
);
542 out_len
= strm
->next_out
- out_buf
;
543 if ((ret
!= Z_STREAM_END
&& ret
!= Z_BUF_ERROR
) ||
544 out_len
!= out_buf_size
) {
552 static int decompress_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
)
554 BDRVQcowState
*s
= bs
->opaque
;
558 coffset
= cluster_offset
& s
->cluster_offset_mask
;
559 if (s
->cluster_cache_offset
!= coffset
) {
560 csize
= cluster_offset
>> (63 - s
->cluster_bits
);
561 csize
&= (s
->cluster_size
- 1);
562 ret
= bdrv_pread(bs
->file
->bs
, coffset
, s
->cluster_data
, csize
);
565 if (decompress_buffer(s
->cluster_cache
, s
->cluster_size
,
566 s
->cluster_data
, csize
) < 0) {
569 s
->cluster_cache_offset
= coffset
;
574 static coroutine_fn
int qcow_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
575 int nb_sectors
, QEMUIOVector
*qiov
)
577 BDRVQcowState
*s
= bs
->opaque
;
578 int index_in_cluster
;
580 uint64_t cluster_offset
;
582 QEMUIOVector hd_qiov
;
587 if (qiov
->niov
> 1) {
588 buf
= orig_buf
= qemu_try_blockalign(bs
, qiov
->size
);
594 buf
= (uint8_t *)qiov
->iov
->iov_base
;
597 qemu_co_mutex_lock(&s
->lock
);
599 while (nb_sectors
!= 0) {
600 /* prepare next request */
601 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9,
603 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
604 n
= s
->cluster_sectors
- index_in_cluster
;
605 if (n
> nb_sectors
) {
609 if (!cluster_offset
) {
611 /* read from the base image */
612 hd_iov
.iov_base
= (void *)buf
;
613 hd_iov
.iov_len
= n
* 512;
614 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
615 qemu_co_mutex_unlock(&s
->lock
);
616 ret
= bdrv_co_readv(bs
->backing
->bs
, sector_num
,
618 qemu_co_mutex_lock(&s
->lock
);
623 /* Note: in this case, no need to wait */
624 memset(buf
, 0, 512 * n
);
626 } else if (cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
627 /* add AIO support for compressed blocks ? */
628 if (decompress_cluster(bs
, cluster_offset
) < 0) {
632 s
->cluster_cache
+ index_in_cluster
* 512, 512 * n
);
634 if ((cluster_offset
& 511) != 0) {
637 hd_iov
.iov_base
= (void *)buf
;
638 hd_iov
.iov_len
= n
* 512;
639 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
640 qemu_co_mutex_unlock(&s
->lock
);
641 ret
= bdrv_co_readv(bs
->file
->bs
,
642 (cluster_offset
>> 9) + index_in_cluster
,
644 qemu_co_mutex_lock(&s
->lock
);
650 if (encrypt_sectors(s
, sector_num
, buf
, buf
,
651 n
, false, &err
) < 0) {
664 qemu_co_mutex_unlock(&s
->lock
);
666 if (qiov
->niov
> 1) {
667 qemu_iovec_from_buf(qiov
, 0, orig_buf
, qiov
->size
);
668 qemu_vfree(orig_buf
);
679 static coroutine_fn
int qcow_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
680 int nb_sectors
, QEMUIOVector
*qiov
)
682 BDRVQcowState
*s
= bs
->opaque
;
683 int index_in_cluster
;
684 uint64_t cluster_offset
;
685 const uint8_t *src_buf
;
687 uint8_t *cluster_data
= NULL
;
689 QEMUIOVector hd_qiov
;
693 s
->cluster_cache_offset
= -1; /* disable compressed cache */
695 if (qiov
->niov
> 1) {
696 buf
= orig_buf
= qemu_try_blockalign(bs
, qiov
->size
);
700 qemu_iovec_to_buf(qiov
, 0, buf
, qiov
->size
);
703 buf
= (uint8_t *)qiov
->iov
->iov_base
;
706 qemu_co_mutex_lock(&s
->lock
);
708 while (nb_sectors
!= 0) {
710 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
711 n
= s
->cluster_sectors
- index_in_cluster
;
712 if (n
> nb_sectors
) {
715 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 1, 0,
717 index_in_cluster
+ n
);
718 if (!cluster_offset
|| (cluster_offset
& 511) != 0) {
726 cluster_data
= g_malloc0(s
->cluster_size
);
728 if (encrypt_sectors(s
, sector_num
, cluster_data
, buf
,
729 n
, true, &err
) < 0) {
734 src_buf
= cluster_data
;
739 hd_iov
.iov_base
= (void *)src_buf
;
740 hd_iov
.iov_len
= n
* 512;
741 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
742 qemu_co_mutex_unlock(&s
->lock
);
743 ret
= bdrv_co_writev(bs
->file
->bs
,
744 (cluster_offset
>> 9) + index_in_cluster
,
746 qemu_co_mutex_lock(&s
->lock
);
756 qemu_co_mutex_unlock(&s
->lock
);
758 if (qiov
->niov
> 1) {
759 qemu_vfree(orig_buf
);
761 g_free(cluster_data
);
766 static void qcow_close(BlockDriverState
*bs
)
768 BDRVQcowState
*s
= bs
->opaque
;
770 qcrypto_cipher_free(s
->cipher
);
773 qemu_vfree(s
->l2_cache
);
774 g_free(s
->cluster_cache
);
775 g_free(s
->cluster_data
);
777 migrate_del_blocker(s
->migration_blocker
);
778 error_free(s
->migration_blocker
);
781 static int qcow_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
783 int header_size
, backing_filename_len
, l1_size
, shift
, i
;
786 int64_t total_size
= 0;
787 char *backing_file
= NULL
;
789 Error
*local_err
= NULL
;
791 BlockBackend
*qcow_blk
;
793 /* Read out options */
794 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
796 backing_file
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FILE
);
797 if (qemu_opt_get_bool_del(opts
, BLOCK_OPT_ENCRYPT
, false)) {
798 flags
|= BLOCK_FLAG_ENCRYPT
;
801 ret
= bdrv_create_file(filename
, opts
, &local_err
);
803 error_propagate(errp
, local_err
);
807 qcow_blk
= blk_new_open(filename
, NULL
, NULL
,
808 BDRV_O_RDWR
| BDRV_O_PROTOCOL
, &local_err
);
809 if (qcow_blk
== NULL
) {
810 error_propagate(errp
, local_err
);
815 blk_set_allow_write_beyond_eof(qcow_blk
, true);
817 ret
= blk_truncate(qcow_blk
, 0);
822 memset(&header
, 0, sizeof(header
));
823 header
.magic
= cpu_to_be32(QCOW_MAGIC
);
824 header
.version
= cpu_to_be32(QCOW_VERSION
);
825 header
.size
= cpu_to_be64(total_size
);
826 header_size
= sizeof(header
);
827 backing_filename_len
= 0;
829 if (strcmp(backing_file
, "fat:")) {
830 header
.backing_file_offset
= cpu_to_be64(header_size
);
831 backing_filename_len
= strlen(backing_file
);
832 header
.backing_file_size
= cpu_to_be32(backing_filename_len
);
833 header_size
+= backing_filename_len
;
835 /* special backing file for vvfat */
838 header
.cluster_bits
= 9; /* 512 byte cluster to avoid copying
839 unmodified sectors */
840 header
.l2_bits
= 12; /* 32 KB L2 tables */
842 header
.cluster_bits
= 12; /* 4 KB clusters */
843 header
.l2_bits
= 9; /* 4 KB L2 tables */
845 header_size
= (header_size
+ 7) & ~7;
846 shift
= header
.cluster_bits
+ header
.l2_bits
;
847 l1_size
= (total_size
+ (1LL << shift
) - 1) >> shift
;
849 header
.l1_table_offset
= cpu_to_be64(header_size
);
850 if (flags
& BLOCK_FLAG_ENCRYPT
) {
851 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
853 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
856 /* write all the data */
857 ret
= blk_pwrite(qcow_blk
, 0, &header
, sizeof(header
), 0);
858 if (ret
!= sizeof(header
)) {
863 ret
= blk_pwrite(qcow_blk
, sizeof(header
),
864 backing_file
, backing_filename_len
, 0);
865 if (ret
!= backing_filename_len
) {
870 tmp
= g_malloc0(BDRV_SECTOR_SIZE
);
871 for (i
= 0; i
< ((sizeof(uint64_t)*l1_size
+ BDRV_SECTOR_SIZE
- 1)/
872 BDRV_SECTOR_SIZE
); i
++) {
873 ret
= blk_pwrite(qcow_blk
, header_size
+ BDRV_SECTOR_SIZE
* i
,
874 tmp
, BDRV_SECTOR_SIZE
, 0);
875 if (ret
!= BDRV_SECTOR_SIZE
) {
886 g_free(backing_file
);
890 static int qcow_make_empty(BlockDriverState
*bs
)
892 BDRVQcowState
*s
= bs
->opaque
;
893 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
896 memset(s
->l1_table
, 0, l1_length
);
897 if (bdrv_pwrite_sync(bs
->file
->bs
, s
->l1_table_offset
, s
->l1_table
,
900 ret
= bdrv_truncate(bs
->file
->bs
, s
->l1_table_offset
+ l1_length
);
904 memset(s
->l2_cache
, 0, s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
905 memset(s
->l2_cache_offsets
, 0, L2_CACHE_SIZE
* sizeof(uint64_t));
906 memset(s
->l2_cache_counts
, 0, L2_CACHE_SIZE
* sizeof(uint32_t));
911 /* XXX: put compressed sectors first, then all the cluster aligned
912 tables to avoid losing bytes in alignment */
913 static int qcow_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
914 const uint8_t *buf
, int nb_sectors
)
916 BDRVQcowState
*s
= bs
->opaque
;
920 uint64_t cluster_offset
;
922 if (nb_sectors
!= s
->cluster_sectors
) {
925 /* Zero-pad last write if image size is not cluster aligned */
926 if (sector_num
+ nb_sectors
== bs
->total_sectors
&&
927 nb_sectors
< s
->cluster_sectors
) {
928 uint8_t *pad_buf
= qemu_blockalign(bs
, s
->cluster_size
);
929 memset(pad_buf
, 0, s
->cluster_size
);
930 memcpy(pad_buf
, buf
, nb_sectors
* BDRV_SECTOR_SIZE
);
931 ret
= qcow_write_compressed(bs
, sector_num
,
932 pad_buf
, s
->cluster_sectors
);
938 out_buf
= g_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
940 /* best compression, small window, no zlib header */
941 memset(&strm
, 0, sizeof(strm
));
942 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
944 9, Z_DEFAULT_STRATEGY
);
950 strm
.avail_in
= s
->cluster_size
;
951 strm
.next_in
= (uint8_t *)buf
;
952 strm
.avail_out
= s
->cluster_size
;
953 strm
.next_out
= out_buf
;
955 ret
= deflate(&strm
, Z_FINISH
);
956 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
961 out_len
= strm
.next_out
- out_buf
;
965 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
966 /* could not compress: write normal cluster */
967 ret
= bdrv_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
972 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 2,
974 if (cluster_offset
== 0) {
979 cluster_offset
&= s
->cluster_offset_mask
;
980 ret
= bdrv_pwrite(bs
->file
->bs
, cluster_offset
, out_buf
, out_len
);
992 static int qcow_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
994 BDRVQcowState
*s
= bs
->opaque
;
995 bdi
->cluster_size
= s
->cluster_size
;
999 static QemuOptsList qcow_create_opts
= {
1000 .name
= "qcow-create-opts",
1001 .head
= QTAILQ_HEAD_INITIALIZER(qcow_create_opts
.head
),
1004 .name
= BLOCK_OPT_SIZE
,
1005 .type
= QEMU_OPT_SIZE
,
1006 .help
= "Virtual disk size"
1009 .name
= BLOCK_OPT_BACKING_FILE
,
1010 .type
= QEMU_OPT_STRING
,
1011 .help
= "File name of a base image"
1014 .name
= BLOCK_OPT_ENCRYPT
,
1015 .type
= QEMU_OPT_BOOL
,
1016 .help
= "Encrypt the image",
1017 .def_value_str
= "off"
1019 { /* end of list */ }
1023 static BlockDriver bdrv_qcow
= {
1024 .format_name
= "qcow",
1025 .instance_size
= sizeof(BDRVQcowState
),
1026 .bdrv_probe
= qcow_probe
,
1027 .bdrv_open
= qcow_open
,
1028 .bdrv_close
= qcow_close
,
1029 .bdrv_reopen_prepare
= qcow_reopen_prepare
,
1030 .bdrv_create
= qcow_create
,
1031 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
1032 .supports_backing
= true,
1034 .bdrv_co_readv
= qcow_co_readv
,
1035 .bdrv_co_writev
= qcow_co_writev
,
1036 .bdrv_co_get_block_status
= qcow_co_get_block_status
,
1038 .bdrv_set_key
= qcow_set_key
,
1039 .bdrv_make_empty
= qcow_make_empty
,
1040 .bdrv_write_compressed
= qcow_write_compressed
,
1041 .bdrv_get_info
= qcow_get_info
,
1043 .create_opts
= &qcow_create_opts
,
1046 static void bdrv_qcow_init(void)
1048 bdrv_register(&bdrv_qcow
);
1051 block_init(bdrv_qcow_init
);