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"
32 #include "qapi/qmp/qerror.h"
33 #include "crypto/cipher.h"
34 #include "migration/migration.h"
36 /**************************************************************/
37 /* QEMU COW block driver with compression and encryption support */
39 #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
40 #define QCOW_VERSION 1
42 #define QCOW_CRYPT_NONE 0
43 #define QCOW_CRYPT_AES 1
45 #define QCOW_OFLAG_COMPRESSED (1LL << 63)
47 typedef struct QCowHeader
{
50 uint64_t backing_file_offset
;
51 uint32_t backing_file_size
;
53 uint64_t size
; /* in bytes */
57 uint32_t crypt_method
;
58 uint64_t l1_table_offset
;
59 } QEMU_PACKED QCowHeader
;
61 #define L2_CACHE_SIZE 16
63 typedef struct BDRVQcowState
{
70 uint64_t cluster_offset_mask
;
71 uint64_t l1_table_offset
;
74 uint64_t l2_cache_offsets
[L2_CACHE_SIZE
];
75 uint32_t l2_cache_counts
[L2_CACHE_SIZE
];
76 uint8_t *cluster_cache
;
77 uint8_t *cluster_data
;
78 uint64_t cluster_cache_offset
;
79 QCryptoCipher
*cipher
; /* NULL if no key yet */
80 uint32_t crypt_method_header
;
82 Error
*migration_blocker
;
85 static int decompress_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
);
87 static int qcow_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
89 const QCowHeader
*cow_header
= (const void *)buf
;
91 if (buf_size
>= sizeof(QCowHeader
) &&
92 be32_to_cpu(cow_header
->magic
) == QCOW_MAGIC
&&
93 be32_to_cpu(cow_header
->version
) == QCOW_VERSION
)
99 static int qcow_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
102 BDRVQcowState
*s
= bs
->opaque
;
103 unsigned int len
, i
, shift
;
107 ret
= bdrv_pread(bs
->file
->bs
, 0, &header
, sizeof(header
));
111 be32_to_cpus(&header
.magic
);
112 be32_to_cpus(&header
.version
);
113 be64_to_cpus(&header
.backing_file_offset
);
114 be32_to_cpus(&header
.backing_file_size
);
115 be32_to_cpus(&header
.mtime
);
116 be64_to_cpus(&header
.size
);
117 be32_to_cpus(&header
.crypt_method
);
118 be64_to_cpus(&header
.l1_table_offset
);
120 if (header
.magic
!= QCOW_MAGIC
) {
121 error_setg(errp
, "Image not in qcow format");
125 if (header
.version
!= QCOW_VERSION
) {
126 error_setg(errp
, "Unsupported qcow version %" PRIu32
, header
.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
) {
162 if (bdrv_uses_whitelist() &&
163 s
->crypt_method_header
== QCOW_CRYPT_AES
) {
164 error_report("qcow built-in AES encryption is deprecated");
165 error_printf("Support for it will be removed in a future release.\n"
166 "You can use 'qemu-img convert' to switch to an\n"
167 "unencrypted qcow image, or a LUKS raw image.\n");
172 s
->cluster_bits
= header
.cluster_bits
;
173 s
->cluster_size
= 1 << s
->cluster_bits
;
174 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
175 s
->l2_bits
= header
.l2_bits
;
176 s
->l2_size
= 1 << s
->l2_bits
;
177 bs
->total_sectors
= header
.size
/ 512;
178 s
->cluster_offset_mask
= (1LL << (63 - s
->cluster_bits
)) - 1;
180 /* read the level 1 table */
181 shift
= s
->cluster_bits
+ s
->l2_bits
;
182 if (header
.size
> UINT64_MAX
- (1LL << shift
)) {
183 error_setg(errp
, "Image too large");
187 uint64_t l1_size
= (header
.size
+ (1LL << shift
) - 1) >> shift
;
188 if (l1_size
> INT_MAX
/ sizeof(uint64_t)) {
189 error_setg(errp
, "Image too large");
193 s
->l1_size
= l1_size
;
196 s
->l1_table_offset
= header
.l1_table_offset
;
197 s
->l1_table
= g_try_new(uint64_t, s
->l1_size
);
198 if (s
->l1_table
== NULL
) {
199 error_setg(errp
, "Could not allocate memory for L1 table");
204 ret
= bdrv_pread(bs
->file
->bs
, s
->l1_table_offset
, s
->l1_table
,
205 s
->l1_size
* sizeof(uint64_t));
210 for(i
= 0;i
< s
->l1_size
; i
++) {
211 be64_to_cpus(&s
->l1_table
[i
]);
214 /* alloc L2 cache (max. 64k * 16 * 8 = 8 MB) */
216 qemu_try_blockalign(bs
->file
->bs
,
217 s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
218 if (s
->l2_cache
== NULL
) {
219 error_setg(errp
, "Could not allocate L2 table cache");
223 s
->cluster_cache
= g_malloc(s
->cluster_size
);
224 s
->cluster_data
= g_malloc(s
->cluster_size
);
225 s
->cluster_cache_offset
= -1;
227 /* read the backing file name */
228 if (header
.backing_file_offset
!= 0) {
229 len
= header
.backing_file_size
;
230 if (len
> 1023 || len
>= sizeof(bs
->backing_file
)) {
231 error_setg(errp
, "Backing file name too long");
235 ret
= bdrv_pread(bs
->file
->bs
, header
.backing_file_offset
,
236 bs
->backing_file
, len
);
240 bs
->backing_file
[len
] = '\0';
243 /* Disable migration when qcow images are used */
244 error_setg(&s
->migration_blocker
, "The qcow format used by node '%s' "
245 "does not support live migration",
246 bdrv_get_device_or_node_name(bs
));
247 migrate_add_blocker(s
->migration_blocker
);
249 qemu_co_mutex_init(&s
->lock
);
254 qemu_vfree(s
->l2_cache
);
255 g_free(s
->cluster_cache
);
256 g_free(s
->cluster_data
);
261 /* We have nothing to do for QCOW reopen, stubs just return
263 static int qcow_reopen_prepare(BDRVReopenState
*state
,
264 BlockReopenQueue
*queue
, Error
**errp
)
269 static int qcow_set_key(BlockDriverState
*bs
, const char *key
)
271 BDRVQcowState
*s
= bs
->opaque
;
276 memset(keybuf
, 0, 16);
280 /* XXX: we could compress the chars to 7 bits to increase
282 for(i
= 0;i
< len
;i
++) {
285 assert(bs
->encrypted
);
287 qcrypto_cipher_free(s
->cipher
);
288 s
->cipher
= qcrypto_cipher_new(
289 QCRYPTO_CIPHER_ALG_AES_128
,
290 QCRYPTO_CIPHER_MODE_CBC
,
291 keybuf
, G_N_ELEMENTS(keybuf
),
295 /* XXX would be nice if errors in this method could
296 * be properly propagate to the caller. Would need
297 * the bdrv_set_key() API signature to be fixed. */
304 /* The crypt function is compatible with the linux cryptoloop
305 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
307 static int encrypt_sectors(BDRVQcowState
*s
, int64_t sector_num
,
308 uint8_t *out_buf
, const uint8_t *in_buf
,
309 int nb_sectors
, bool enc
, Error
**errp
)
318 for(i
= 0; i
< nb_sectors
; i
++) {
319 ivec
.ll
[0] = cpu_to_le64(sector_num
);
321 if (qcrypto_cipher_setiv(s
->cipher
,
322 ivec
.b
, G_N_ELEMENTS(ivec
.b
),
327 ret
= qcrypto_cipher_encrypt(s
->cipher
,
333 ret
= qcrypto_cipher_decrypt(s
->cipher
,
353 * 1 to allocate a normal cluster (for sector indexes 'n_start' to
356 * 2 to allocate a compressed cluster of size
357 * 'compressed_size'. 'compressed_size' must be > 0 and <
360 * return 0 if not allocated.
362 static uint64_t get_cluster_offset(BlockDriverState
*bs
,
363 uint64_t offset
, int allocate
,
365 int n_start
, int n_end
)
367 BDRVQcowState
*s
= bs
->opaque
;
368 int min_index
, i
, j
, l1_index
, l2_index
;
369 uint64_t l2_offset
, *l2_table
, cluster_offset
, tmp
;
373 l1_index
= offset
>> (s
->l2_bits
+ s
->cluster_bits
);
374 l2_offset
= s
->l1_table
[l1_index
];
379 /* allocate a new l2 entry */
380 l2_offset
= bdrv_getlength(bs
->file
->bs
);
381 /* round to cluster size */
382 l2_offset
= (l2_offset
+ s
->cluster_size
- 1) & ~(s
->cluster_size
- 1);
383 /* update the L1 entry */
384 s
->l1_table
[l1_index
] = l2_offset
;
385 tmp
= cpu_to_be64(l2_offset
);
386 if (bdrv_pwrite_sync(bs
->file
->bs
,
387 s
->l1_table_offset
+ l1_index
* sizeof(tmp
),
388 &tmp
, sizeof(tmp
)) < 0)
392 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
393 if (l2_offset
== s
->l2_cache_offsets
[i
]) {
394 /* increment the hit count */
395 if (++s
->l2_cache_counts
[i
] == 0xffffffff) {
396 for(j
= 0; j
< L2_CACHE_SIZE
; j
++) {
397 s
->l2_cache_counts
[j
] >>= 1;
400 l2_table
= s
->l2_cache
+ (i
<< s
->l2_bits
);
404 /* not found: load a new entry in the least used one */
406 min_count
= 0xffffffff;
407 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
408 if (s
->l2_cache_counts
[i
] < min_count
) {
409 min_count
= s
->l2_cache_counts
[i
];
413 l2_table
= s
->l2_cache
+ (min_index
<< s
->l2_bits
);
415 memset(l2_table
, 0, s
->l2_size
* sizeof(uint64_t));
416 if (bdrv_pwrite_sync(bs
->file
->bs
, l2_offset
, l2_table
,
417 s
->l2_size
* sizeof(uint64_t)) < 0)
420 if (bdrv_pread(bs
->file
->bs
, l2_offset
, l2_table
,
421 s
->l2_size
* sizeof(uint64_t)) !=
422 s
->l2_size
* sizeof(uint64_t))
425 s
->l2_cache_offsets
[min_index
] = l2_offset
;
426 s
->l2_cache_counts
[min_index
] = 1;
428 l2_index
= (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
429 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
430 if (!cluster_offset
||
431 ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) && allocate
== 1)) {
434 /* allocate a new cluster */
435 if ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) &&
436 (n_end
- n_start
) < s
->cluster_sectors
) {
437 /* if the cluster is already compressed, we must
438 decompress it in the case it is not completely
440 if (decompress_cluster(bs
, cluster_offset
) < 0)
442 cluster_offset
= bdrv_getlength(bs
->file
->bs
);
443 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
444 ~(s
->cluster_size
- 1);
445 /* write the cluster content */
446 if (bdrv_pwrite(bs
->file
->bs
, cluster_offset
, s
->cluster_cache
,
451 cluster_offset
= bdrv_getlength(bs
->file
->bs
);
453 /* round to cluster size */
454 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
455 ~(s
->cluster_size
- 1);
456 bdrv_truncate(bs
->file
->bs
, cluster_offset
+ s
->cluster_size
);
457 /* if encrypted, we must initialize the cluster
458 content which won't be written */
460 (n_end
- n_start
) < s
->cluster_sectors
) {
463 start_sect
= (offset
& ~(s
->cluster_size
- 1)) >> 9;
464 memset(s
->cluster_data
+ 512, 0x00, 512);
465 for(i
= 0; i
< s
->cluster_sectors
; i
++) {
466 if (i
< n_start
|| i
>= n_end
) {
468 if (encrypt_sectors(s
, start_sect
+ i
,
470 s
->cluster_data
+ 512, 1,
476 if (bdrv_pwrite(bs
->file
->bs
,
477 cluster_offset
+ i
* 512,
478 s
->cluster_data
, 512) != 512)
483 } else if (allocate
== 2) {
484 cluster_offset
|= QCOW_OFLAG_COMPRESSED
|
485 (uint64_t)compressed_size
<< (63 - s
->cluster_bits
);
488 /* update L2 table */
489 tmp
= cpu_to_be64(cluster_offset
);
490 l2_table
[l2_index
] = tmp
;
491 if (bdrv_pwrite_sync(bs
->file
->bs
, l2_offset
+ l2_index
* sizeof(tmp
),
492 &tmp
, sizeof(tmp
)) < 0)
495 return cluster_offset
;
498 static int64_t coroutine_fn
qcow_co_get_block_status(BlockDriverState
*bs
,
499 int64_t sector_num
, int nb_sectors
, int *pnum
, BlockDriverState
**file
)
501 BDRVQcowState
*s
= bs
->opaque
;
502 int index_in_cluster
, n
;
503 uint64_t cluster_offset
;
505 qemu_co_mutex_lock(&s
->lock
);
506 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 0, 0, 0, 0);
507 qemu_co_mutex_unlock(&s
->lock
);
508 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
509 n
= s
->cluster_sectors
- index_in_cluster
;
513 if (!cluster_offset
) {
516 if ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) || s
->cipher
) {
517 return BDRV_BLOCK_DATA
;
519 cluster_offset
|= (index_in_cluster
<< BDRV_SECTOR_BITS
);
520 *file
= bs
->file
->bs
;
521 return BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
| cluster_offset
;
524 static int decompress_buffer(uint8_t *out_buf
, int out_buf_size
,
525 const uint8_t *buf
, int buf_size
)
527 z_stream strm1
, *strm
= &strm1
;
530 memset(strm
, 0, sizeof(*strm
));
532 strm
->next_in
= (uint8_t *)buf
;
533 strm
->avail_in
= buf_size
;
534 strm
->next_out
= out_buf
;
535 strm
->avail_out
= out_buf_size
;
537 ret
= inflateInit2(strm
, -12);
540 ret
= inflate(strm
, Z_FINISH
);
541 out_len
= strm
->next_out
- out_buf
;
542 if ((ret
!= Z_STREAM_END
&& ret
!= Z_BUF_ERROR
) ||
543 out_len
!= out_buf_size
) {
551 static int decompress_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
)
553 BDRVQcowState
*s
= bs
->opaque
;
557 coffset
= cluster_offset
& s
->cluster_offset_mask
;
558 if (s
->cluster_cache_offset
!= coffset
) {
559 csize
= cluster_offset
>> (63 - s
->cluster_bits
);
560 csize
&= (s
->cluster_size
- 1);
561 ret
= bdrv_pread(bs
->file
->bs
, coffset
, s
->cluster_data
, csize
);
564 if (decompress_buffer(s
->cluster_cache
, s
->cluster_size
,
565 s
->cluster_data
, csize
) < 0) {
568 s
->cluster_cache_offset
= coffset
;
573 static coroutine_fn
int qcow_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
574 int nb_sectors
, QEMUIOVector
*qiov
)
576 BDRVQcowState
*s
= bs
->opaque
;
577 int index_in_cluster
;
579 uint64_t cluster_offset
;
581 QEMUIOVector hd_qiov
;
586 if (qiov
->niov
> 1) {
587 buf
= orig_buf
= qemu_try_blockalign(bs
, qiov
->size
);
593 buf
= (uint8_t *)qiov
->iov
->iov_base
;
596 qemu_co_mutex_lock(&s
->lock
);
598 while (nb_sectors
!= 0) {
599 /* prepare next request */
600 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9,
602 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
603 n
= s
->cluster_sectors
- index_in_cluster
;
604 if (n
> nb_sectors
) {
608 if (!cluster_offset
) {
610 /* read from the base image */
611 hd_iov
.iov_base
= (void *)buf
;
612 hd_iov
.iov_len
= n
* 512;
613 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
614 qemu_co_mutex_unlock(&s
->lock
);
615 ret
= bdrv_co_readv(bs
->backing
->bs
, sector_num
,
617 qemu_co_mutex_lock(&s
->lock
);
622 /* Note: in this case, no need to wait */
623 memset(buf
, 0, 512 * n
);
625 } else if (cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
626 /* add AIO support for compressed blocks ? */
627 if (decompress_cluster(bs
, cluster_offset
) < 0) {
631 s
->cluster_cache
+ index_in_cluster
* 512, 512 * n
);
633 if ((cluster_offset
& 511) != 0) {
636 hd_iov
.iov_base
= (void *)buf
;
637 hd_iov
.iov_len
= n
* 512;
638 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
639 qemu_co_mutex_unlock(&s
->lock
);
640 ret
= bdrv_co_readv(bs
->file
->bs
,
641 (cluster_offset
>> 9) + index_in_cluster
,
643 qemu_co_mutex_lock(&s
->lock
);
649 if (encrypt_sectors(s
, sector_num
, buf
, buf
,
650 n
, false, &err
) < 0) {
663 qemu_co_mutex_unlock(&s
->lock
);
665 if (qiov
->niov
> 1) {
666 qemu_iovec_from_buf(qiov
, 0, orig_buf
, qiov
->size
);
667 qemu_vfree(orig_buf
);
678 static coroutine_fn
int qcow_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
679 int nb_sectors
, QEMUIOVector
*qiov
)
681 BDRVQcowState
*s
= bs
->opaque
;
682 int index_in_cluster
;
683 uint64_t cluster_offset
;
684 const uint8_t *src_buf
;
686 uint8_t *cluster_data
= NULL
;
688 QEMUIOVector hd_qiov
;
692 s
->cluster_cache_offset
= -1; /* disable compressed cache */
694 if (qiov
->niov
> 1) {
695 buf
= orig_buf
= qemu_try_blockalign(bs
, qiov
->size
);
699 qemu_iovec_to_buf(qiov
, 0, buf
, qiov
->size
);
702 buf
= (uint8_t *)qiov
->iov
->iov_base
;
705 qemu_co_mutex_lock(&s
->lock
);
707 while (nb_sectors
!= 0) {
709 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
710 n
= s
->cluster_sectors
- index_in_cluster
;
711 if (n
> nb_sectors
) {
714 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 1, 0,
716 index_in_cluster
+ n
);
717 if (!cluster_offset
|| (cluster_offset
& 511) != 0) {
725 cluster_data
= g_malloc0(s
->cluster_size
);
727 if (encrypt_sectors(s
, sector_num
, cluster_data
, buf
,
728 n
, true, &err
) < 0) {
733 src_buf
= cluster_data
;
738 hd_iov
.iov_base
= (void *)src_buf
;
739 hd_iov
.iov_len
= n
* 512;
740 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
741 qemu_co_mutex_unlock(&s
->lock
);
742 ret
= bdrv_co_writev(bs
->file
->bs
,
743 (cluster_offset
>> 9) + index_in_cluster
,
745 qemu_co_mutex_lock(&s
->lock
);
755 qemu_co_mutex_unlock(&s
->lock
);
757 if (qiov
->niov
> 1) {
758 qemu_vfree(orig_buf
);
760 g_free(cluster_data
);
765 static void qcow_close(BlockDriverState
*bs
)
767 BDRVQcowState
*s
= bs
->opaque
;
769 qcrypto_cipher_free(s
->cipher
);
772 qemu_vfree(s
->l2_cache
);
773 g_free(s
->cluster_cache
);
774 g_free(s
->cluster_data
);
776 migrate_del_blocker(s
->migration_blocker
);
777 error_free(s
->migration_blocker
);
780 static int qcow_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
782 int header_size
, backing_filename_len
, l1_size
, shift
, i
;
785 int64_t total_size
= 0;
786 char *backing_file
= NULL
;
788 Error
*local_err
= NULL
;
790 BlockBackend
*qcow_blk
;
792 /* Read out options */
793 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
795 backing_file
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FILE
);
796 if (qemu_opt_get_bool_del(opts
, BLOCK_OPT_ENCRYPT
, false)) {
797 flags
|= BLOCK_FLAG_ENCRYPT
;
800 ret
= bdrv_create_file(filename
, opts
, &local_err
);
802 error_propagate(errp
, local_err
);
806 qcow_blk
= blk_new_open(filename
, NULL
, NULL
,
807 BDRV_O_RDWR
| BDRV_O_PROTOCOL
, &local_err
);
808 if (qcow_blk
== NULL
) {
809 error_propagate(errp
, local_err
);
814 blk_set_allow_write_beyond_eof(qcow_blk
, true);
816 ret
= blk_truncate(qcow_blk
, 0);
821 memset(&header
, 0, sizeof(header
));
822 header
.magic
= cpu_to_be32(QCOW_MAGIC
);
823 header
.version
= cpu_to_be32(QCOW_VERSION
);
824 header
.size
= cpu_to_be64(total_size
);
825 header_size
= sizeof(header
);
826 backing_filename_len
= 0;
828 if (strcmp(backing_file
, "fat:")) {
829 header
.backing_file_offset
= cpu_to_be64(header_size
);
830 backing_filename_len
= strlen(backing_file
);
831 header
.backing_file_size
= cpu_to_be32(backing_filename_len
);
832 header_size
+= backing_filename_len
;
834 /* special backing file for vvfat */
837 header
.cluster_bits
= 9; /* 512 byte cluster to avoid copying
838 unmodified sectors */
839 header
.l2_bits
= 12; /* 32 KB L2 tables */
841 header
.cluster_bits
= 12; /* 4 KB clusters */
842 header
.l2_bits
= 9; /* 4 KB L2 tables */
844 header_size
= (header_size
+ 7) & ~7;
845 shift
= header
.cluster_bits
+ header
.l2_bits
;
846 l1_size
= (total_size
+ (1LL << shift
) - 1) >> shift
;
848 header
.l1_table_offset
= cpu_to_be64(header_size
);
849 if (flags
& BLOCK_FLAG_ENCRYPT
) {
850 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
852 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
855 /* write all the data */
856 ret
= blk_pwrite(qcow_blk
, 0, &header
, sizeof(header
), 0);
857 if (ret
!= sizeof(header
)) {
862 ret
= blk_pwrite(qcow_blk
, sizeof(header
),
863 backing_file
, backing_filename_len
, 0);
864 if (ret
!= backing_filename_len
) {
869 tmp
= g_malloc0(BDRV_SECTOR_SIZE
);
870 for (i
= 0; i
< ((sizeof(uint64_t)*l1_size
+ BDRV_SECTOR_SIZE
- 1)/
871 BDRV_SECTOR_SIZE
); i
++) {
872 ret
= blk_pwrite(qcow_blk
, header_size
+ BDRV_SECTOR_SIZE
* i
,
873 tmp
, BDRV_SECTOR_SIZE
, 0);
874 if (ret
!= BDRV_SECTOR_SIZE
) {
885 g_free(backing_file
);
889 static int qcow_make_empty(BlockDriverState
*bs
)
891 BDRVQcowState
*s
= bs
->opaque
;
892 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
895 memset(s
->l1_table
, 0, l1_length
);
896 if (bdrv_pwrite_sync(bs
->file
->bs
, s
->l1_table_offset
, s
->l1_table
,
899 ret
= bdrv_truncate(bs
->file
->bs
, s
->l1_table_offset
+ l1_length
);
903 memset(s
->l2_cache
, 0, s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
904 memset(s
->l2_cache_offsets
, 0, L2_CACHE_SIZE
* sizeof(uint64_t));
905 memset(s
->l2_cache_counts
, 0, L2_CACHE_SIZE
* sizeof(uint32_t));
910 /* XXX: put compressed sectors first, then all the cluster aligned
911 tables to avoid losing bytes in alignment */
912 static int qcow_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
913 const uint8_t *buf
, int nb_sectors
)
915 BDRVQcowState
*s
= bs
->opaque
;
919 uint64_t cluster_offset
;
921 if (nb_sectors
!= s
->cluster_sectors
) {
924 /* Zero-pad last write if image size is not cluster aligned */
925 if (sector_num
+ nb_sectors
== bs
->total_sectors
&&
926 nb_sectors
< s
->cluster_sectors
) {
927 uint8_t *pad_buf
= qemu_blockalign(bs
, s
->cluster_size
);
928 memset(pad_buf
, 0, s
->cluster_size
);
929 memcpy(pad_buf
, buf
, nb_sectors
* BDRV_SECTOR_SIZE
);
930 ret
= qcow_write_compressed(bs
, sector_num
,
931 pad_buf
, s
->cluster_sectors
);
937 out_buf
= g_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
939 /* best compression, small window, no zlib header */
940 memset(&strm
, 0, sizeof(strm
));
941 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
943 9, Z_DEFAULT_STRATEGY
);
949 strm
.avail_in
= s
->cluster_size
;
950 strm
.next_in
= (uint8_t *)buf
;
951 strm
.avail_out
= s
->cluster_size
;
952 strm
.next_out
= out_buf
;
954 ret
= deflate(&strm
, Z_FINISH
);
955 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
960 out_len
= strm
.next_out
- out_buf
;
964 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
965 /* could not compress: write normal cluster */
966 ret
= bdrv_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
971 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 2,
973 if (cluster_offset
== 0) {
978 cluster_offset
&= s
->cluster_offset_mask
;
979 ret
= bdrv_pwrite(bs
->file
->bs
, cluster_offset
, out_buf
, out_len
);
991 static int qcow_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
993 BDRVQcowState
*s
= bs
->opaque
;
994 bdi
->cluster_size
= s
->cluster_size
;
998 static QemuOptsList qcow_create_opts
= {
999 .name
= "qcow-create-opts",
1000 .head
= QTAILQ_HEAD_INITIALIZER(qcow_create_opts
.head
),
1003 .name
= BLOCK_OPT_SIZE
,
1004 .type
= QEMU_OPT_SIZE
,
1005 .help
= "Virtual disk size"
1008 .name
= BLOCK_OPT_BACKING_FILE
,
1009 .type
= QEMU_OPT_STRING
,
1010 .help
= "File name of a base image"
1013 .name
= BLOCK_OPT_ENCRYPT
,
1014 .type
= QEMU_OPT_BOOL
,
1015 .help
= "Encrypt the image",
1016 .def_value_str
= "off"
1018 { /* end of list */ }
1022 static BlockDriver bdrv_qcow
= {
1023 .format_name
= "qcow",
1024 .instance_size
= sizeof(BDRVQcowState
),
1025 .bdrv_probe
= qcow_probe
,
1026 .bdrv_open
= qcow_open
,
1027 .bdrv_close
= qcow_close
,
1028 .bdrv_reopen_prepare
= qcow_reopen_prepare
,
1029 .bdrv_create
= qcow_create
,
1030 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
1031 .supports_backing
= true,
1033 .bdrv_co_readv
= qcow_co_readv
,
1034 .bdrv_co_writev
= qcow_co_writev
,
1035 .bdrv_co_get_block_status
= qcow_co_get_block_status
,
1037 .bdrv_set_key
= qcow_set_key
,
1038 .bdrv_make_empty
= qcow_make_empty
,
1039 .bdrv_write_compressed
= qcow_write_compressed
,
1040 .bdrv_get_info
= qcow_get_info
,
1042 .create_opts
= &qcow_create_opts
,
1045 static void bdrv_qcow_init(void)
1047 bdrv_register(&bdrv_qcow
);
1050 block_init(bdrv_qcow_init
);