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"
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 uint32_t crypt_method
; /* current crypt method, 0 if no key yet */
76 uint32_t crypt_method_header
;
77 AES_KEY aes_encrypt_key
;
78 AES_KEY aes_decrypt_key
;
80 Error
*migration_blocker
;
83 static int decompress_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
);
85 static int qcow_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
87 const QCowHeader
*cow_header
= (const void *)buf
;
89 if (buf_size
>= sizeof(QCowHeader
) &&
90 be32_to_cpu(cow_header
->magic
) == QCOW_MAGIC
&&
91 be32_to_cpu(cow_header
->version
) == QCOW_VERSION
)
97 static int qcow_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
100 BDRVQcowState
*s
= bs
->opaque
;
101 unsigned int len
, i
, shift
;
105 ret
= bdrv_pread(bs
->file
, 0, &header
, sizeof(header
));
109 be32_to_cpus(&header
.magic
);
110 be32_to_cpus(&header
.version
);
111 be64_to_cpus(&header
.backing_file_offset
);
112 be32_to_cpus(&header
.backing_file_size
);
113 be32_to_cpus(&header
.mtime
);
114 be64_to_cpus(&header
.size
);
115 be32_to_cpus(&header
.crypt_method
);
116 be64_to_cpus(&header
.l1_table_offset
);
118 if (header
.magic
!= QCOW_MAGIC
) {
119 error_setg(errp
, "Image not in qcow format");
123 if (header
.version
!= QCOW_VERSION
) {
125 snprintf(version
, sizeof(version
), "QCOW version %" PRIu32
,
127 error_setg(errp
, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE
,
128 bdrv_get_device_or_node_name(bs
), "qcow", version
);
133 if (header
.size
<= 1) {
134 error_setg(errp
, "Image size is too small (must be at least 2 bytes)");
138 if (header
.cluster_bits
< 9 || header
.cluster_bits
> 16) {
139 error_setg(errp
, "Cluster size must be between 512 and 64k");
144 /* l2_bits specifies number of entries; storing a uint64_t in each entry,
145 * so bytes = num_entries << 3. */
146 if (header
.l2_bits
< 9 - 3 || header
.l2_bits
> 16 - 3) {
147 error_setg(errp
, "L2 table size must be between 512 and 64k");
152 if (header
.crypt_method
> QCOW_CRYPT_AES
) {
153 error_setg(errp
, "invalid encryption method in qcow header");
157 s
->crypt_method_header
= header
.crypt_method
;
158 if (s
->crypt_method_header
) {
161 s
->cluster_bits
= header
.cluster_bits
;
162 s
->cluster_size
= 1 << s
->cluster_bits
;
163 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
164 s
->l2_bits
= header
.l2_bits
;
165 s
->l2_size
= 1 << s
->l2_bits
;
166 bs
->total_sectors
= header
.size
/ 512;
167 s
->cluster_offset_mask
= (1LL << (63 - s
->cluster_bits
)) - 1;
169 /* read the level 1 table */
170 shift
= s
->cluster_bits
+ s
->l2_bits
;
171 if (header
.size
> UINT64_MAX
- (1LL << shift
)) {
172 error_setg(errp
, "Image too large");
176 uint64_t l1_size
= (header
.size
+ (1LL << shift
) - 1) >> shift
;
177 if (l1_size
> INT_MAX
/ sizeof(uint64_t)) {
178 error_setg(errp
, "Image too large");
182 s
->l1_size
= l1_size
;
185 s
->l1_table_offset
= header
.l1_table_offset
;
186 s
->l1_table
= g_try_new(uint64_t, s
->l1_size
);
187 if (s
->l1_table
== NULL
) {
188 error_setg(errp
, "Could not allocate memory for L1 table");
193 ret
= bdrv_pread(bs
->file
, s
->l1_table_offset
, s
->l1_table
,
194 s
->l1_size
* sizeof(uint64_t));
199 for(i
= 0;i
< s
->l1_size
; i
++) {
200 be64_to_cpus(&s
->l1_table
[i
]);
203 /* alloc L2 cache (max. 64k * 16 * 8 = 8 MB) */
205 qemu_try_blockalign(bs
->file
,
206 s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
207 if (s
->l2_cache
== NULL
) {
208 error_setg(errp
, "Could not allocate L2 table cache");
212 s
->cluster_cache
= g_malloc(s
->cluster_size
);
213 s
->cluster_data
= g_malloc(s
->cluster_size
);
214 s
->cluster_cache_offset
= -1;
216 /* read the backing file name */
217 if (header
.backing_file_offset
!= 0) {
218 len
= header
.backing_file_size
;
219 if (len
> 1023 || len
>= sizeof(bs
->backing_file
)) {
220 error_setg(errp
, "Backing file name too long");
224 ret
= bdrv_pread(bs
->file
, header
.backing_file_offset
,
225 bs
->backing_file
, len
);
229 bs
->backing_file
[len
] = '\0';
232 /* Disable migration when qcow images are used */
233 error_setg(&s
->migration_blocker
, "The qcow format used by node '%s' "
234 "does not support live migration",
235 bdrv_get_device_or_node_name(bs
));
236 migrate_add_blocker(s
->migration_blocker
);
238 qemu_co_mutex_init(&s
->lock
);
243 qemu_vfree(s
->l2_cache
);
244 g_free(s
->cluster_cache
);
245 g_free(s
->cluster_data
);
250 /* We have nothing to do for QCOW reopen, stubs just return
252 static int qcow_reopen_prepare(BDRVReopenState
*state
,
253 BlockReopenQueue
*queue
, Error
**errp
)
258 static int qcow_set_key(BlockDriverState
*bs
, const char *key
)
260 BDRVQcowState
*s
= bs
->opaque
;
264 memset(keybuf
, 0, 16);
268 /* XXX: we could compress the chars to 7 bits to increase
270 for(i
= 0;i
< len
;i
++) {
273 assert(bs
->encrypted
);
274 s
->crypt_method
= s
->crypt_method_header
;
276 if (AES_set_encrypt_key(keybuf
, 128, &s
->aes_encrypt_key
) != 0)
278 if (AES_set_decrypt_key(keybuf
, 128, &s
->aes_decrypt_key
) != 0)
283 /* The crypt function is compatible with the linux cryptoloop
284 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
286 static void encrypt_sectors(BDRVQcowState
*s
, int64_t sector_num
,
287 uint8_t *out_buf
, const uint8_t *in_buf
,
288 int nb_sectors
, int enc
,
297 for(i
= 0; i
< nb_sectors
; i
++) {
298 ivec
.ll
[0] = cpu_to_le64(sector_num
);
300 AES_cbc_encrypt(in_buf
, out_buf
, 512, key
,
312 * 1 to allocate a normal cluster (for sector indexes 'n_start' to
315 * 2 to allocate a compressed cluster of size
316 * 'compressed_size'. 'compressed_size' must be > 0 and <
319 * return 0 if not allocated.
321 static uint64_t get_cluster_offset(BlockDriverState
*bs
,
322 uint64_t offset
, int allocate
,
324 int n_start
, int n_end
)
326 BDRVQcowState
*s
= bs
->opaque
;
327 int min_index
, i
, j
, l1_index
, l2_index
;
328 uint64_t l2_offset
, *l2_table
, cluster_offset
, tmp
;
332 l1_index
= offset
>> (s
->l2_bits
+ s
->cluster_bits
);
333 l2_offset
= s
->l1_table
[l1_index
];
338 /* allocate a new l2 entry */
339 l2_offset
= bdrv_getlength(bs
->file
);
340 /* round to cluster size */
341 l2_offset
= (l2_offset
+ s
->cluster_size
- 1) & ~(s
->cluster_size
- 1);
342 /* update the L1 entry */
343 s
->l1_table
[l1_index
] = l2_offset
;
344 tmp
= cpu_to_be64(l2_offset
);
345 if (bdrv_pwrite_sync(bs
->file
,
346 s
->l1_table_offset
+ l1_index
* sizeof(tmp
),
347 &tmp
, sizeof(tmp
)) < 0)
351 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
352 if (l2_offset
== s
->l2_cache_offsets
[i
]) {
353 /* increment the hit count */
354 if (++s
->l2_cache_counts
[i
] == 0xffffffff) {
355 for(j
= 0; j
< L2_CACHE_SIZE
; j
++) {
356 s
->l2_cache_counts
[j
] >>= 1;
359 l2_table
= s
->l2_cache
+ (i
<< s
->l2_bits
);
363 /* not found: load a new entry in the least used one */
365 min_count
= 0xffffffff;
366 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
367 if (s
->l2_cache_counts
[i
] < min_count
) {
368 min_count
= s
->l2_cache_counts
[i
];
372 l2_table
= s
->l2_cache
+ (min_index
<< s
->l2_bits
);
374 memset(l2_table
, 0, s
->l2_size
* sizeof(uint64_t));
375 if (bdrv_pwrite_sync(bs
->file
, l2_offset
, l2_table
,
376 s
->l2_size
* sizeof(uint64_t)) < 0)
379 if (bdrv_pread(bs
->file
, l2_offset
, l2_table
, s
->l2_size
* sizeof(uint64_t)) !=
380 s
->l2_size
* sizeof(uint64_t))
383 s
->l2_cache_offsets
[min_index
] = l2_offset
;
384 s
->l2_cache_counts
[min_index
] = 1;
386 l2_index
= (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
387 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
388 if (!cluster_offset
||
389 ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) && allocate
== 1)) {
392 /* allocate a new cluster */
393 if ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) &&
394 (n_end
- n_start
) < s
->cluster_sectors
) {
395 /* if the cluster is already compressed, we must
396 decompress it in the case it is not completely
398 if (decompress_cluster(bs
, cluster_offset
) < 0)
400 cluster_offset
= bdrv_getlength(bs
->file
);
401 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
402 ~(s
->cluster_size
- 1);
403 /* write the cluster content */
404 if (bdrv_pwrite(bs
->file
, cluster_offset
, s
->cluster_cache
, s
->cluster_size
) !=
408 cluster_offset
= bdrv_getlength(bs
->file
);
410 /* round to cluster size */
411 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
412 ~(s
->cluster_size
- 1);
413 bdrv_truncate(bs
->file
, cluster_offset
+ s
->cluster_size
);
414 /* if encrypted, we must initialize the cluster
415 content which won't be written */
417 (n_end
- n_start
) < s
->cluster_sectors
) {
419 assert(s
->crypt_method
);
420 start_sect
= (offset
& ~(s
->cluster_size
- 1)) >> 9;
421 memset(s
->cluster_data
+ 512, 0x00, 512);
422 for(i
= 0; i
< s
->cluster_sectors
; i
++) {
423 if (i
< n_start
|| i
>= n_end
) {
424 encrypt_sectors(s
, start_sect
+ i
,
426 s
->cluster_data
+ 512, 1, 1,
427 &s
->aes_encrypt_key
);
428 if (bdrv_pwrite(bs
->file
, cluster_offset
+ i
* 512,
429 s
->cluster_data
, 512) != 512)
434 } else if (allocate
== 2) {
435 cluster_offset
|= QCOW_OFLAG_COMPRESSED
|
436 (uint64_t)compressed_size
<< (63 - s
->cluster_bits
);
439 /* update L2 table */
440 tmp
= cpu_to_be64(cluster_offset
);
441 l2_table
[l2_index
] = tmp
;
442 if (bdrv_pwrite_sync(bs
->file
, l2_offset
+ l2_index
* sizeof(tmp
),
443 &tmp
, sizeof(tmp
)) < 0)
446 return cluster_offset
;
449 static int64_t coroutine_fn
qcow_co_get_block_status(BlockDriverState
*bs
,
450 int64_t sector_num
, int nb_sectors
, int *pnum
)
452 BDRVQcowState
*s
= bs
->opaque
;
453 int index_in_cluster
, n
;
454 uint64_t cluster_offset
;
456 qemu_co_mutex_lock(&s
->lock
);
457 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 0, 0, 0, 0);
458 qemu_co_mutex_unlock(&s
->lock
);
459 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
460 n
= s
->cluster_sectors
- index_in_cluster
;
464 if (!cluster_offset
) {
467 if ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) || s
->crypt_method
) {
468 return BDRV_BLOCK_DATA
;
470 cluster_offset
|= (index_in_cluster
<< BDRV_SECTOR_BITS
);
471 return BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
| cluster_offset
;
474 static int decompress_buffer(uint8_t *out_buf
, int out_buf_size
,
475 const uint8_t *buf
, int buf_size
)
477 z_stream strm1
, *strm
= &strm1
;
480 memset(strm
, 0, sizeof(*strm
));
482 strm
->next_in
= (uint8_t *)buf
;
483 strm
->avail_in
= buf_size
;
484 strm
->next_out
= out_buf
;
485 strm
->avail_out
= out_buf_size
;
487 ret
= inflateInit2(strm
, -12);
490 ret
= inflate(strm
, Z_FINISH
);
491 out_len
= strm
->next_out
- out_buf
;
492 if ((ret
!= Z_STREAM_END
&& ret
!= Z_BUF_ERROR
) ||
493 out_len
!= out_buf_size
) {
501 static int decompress_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
)
503 BDRVQcowState
*s
= bs
->opaque
;
507 coffset
= cluster_offset
& s
->cluster_offset_mask
;
508 if (s
->cluster_cache_offset
!= coffset
) {
509 csize
= cluster_offset
>> (63 - s
->cluster_bits
);
510 csize
&= (s
->cluster_size
- 1);
511 ret
= bdrv_pread(bs
->file
, coffset
, s
->cluster_data
, csize
);
514 if (decompress_buffer(s
->cluster_cache
, s
->cluster_size
,
515 s
->cluster_data
, csize
) < 0) {
518 s
->cluster_cache_offset
= coffset
;
523 static coroutine_fn
int qcow_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
524 int nb_sectors
, QEMUIOVector
*qiov
)
526 BDRVQcowState
*s
= bs
->opaque
;
527 int index_in_cluster
;
529 uint64_t cluster_offset
;
531 QEMUIOVector hd_qiov
;
535 if (qiov
->niov
> 1) {
536 buf
= orig_buf
= qemu_try_blockalign(bs
, qiov
->size
);
542 buf
= (uint8_t *)qiov
->iov
->iov_base
;
545 qemu_co_mutex_lock(&s
->lock
);
547 while (nb_sectors
!= 0) {
548 /* prepare next request */
549 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9,
551 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
552 n
= s
->cluster_sectors
- index_in_cluster
;
553 if (n
> nb_sectors
) {
557 if (!cluster_offset
) {
558 if (bs
->backing_hd
) {
559 /* read from the base image */
560 hd_iov
.iov_base
= (void *)buf
;
561 hd_iov
.iov_len
= n
* 512;
562 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
563 qemu_co_mutex_unlock(&s
->lock
);
564 ret
= bdrv_co_readv(bs
->backing_hd
, sector_num
,
566 qemu_co_mutex_lock(&s
->lock
);
571 /* Note: in this case, no need to wait */
572 memset(buf
, 0, 512 * n
);
574 } else if (cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
575 /* add AIO support for compressed blocks ? */
576 if (decompress_cluster(bs
, cluster_offset
) < 0) {
580 s
->cluster_cache
+ index_in_cluster
* 512, 512 * n
);
582 if ((cluster_offset
& 511) != 0) {
585 hd_iov
.iov_base
= (void *)buf
;
586 hd_iov
.iov_len
= n
* 512;
587 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
588 qemu_co_mutex_unlock(&s
->lock
);
589 ret
= bdrv_co_readv(bs
->file
,
590 (cluster_offset
>> 9) + index_in_cluster
,
592 qemu_co_mutex_lock(&s
->lock
);
597 assert(s
->crypt_method
);
598 encrypt_sectors(s
, sector_num
, buf
, buf
,
600 &s
->aes_decrypt_key
);
611 qemu_co_mutex_unlock(&s
->lock
);
613 if (qiov
->niov
> 1) {
614 qemu_iovec_from_buf(qiov
, 0, orig_buf
, qiov
->size
);
615 qemu_vfree(orig_buf
);
625 static coroutine_fn
int qcow_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
626 int nb_sectors
, QEMUIOVector
*qiov
)
628 BDRVQcowState
*s
= bs
->opaque
;
629 int index_in_cluster
;
630 uint64_t cluster_offset
;
631 const uint8_t *src_buf
;
633 uint8_t *cluster_data
= NULL
;
635 QEMUIOVector hd_qiov
;
639 s
->cluster_cache_offset
= -1; /* disable compressed cache */
641 if (qiov
->niov
> 1) {
642 buf
= orig_buf
= qemu_try_blockalign(bs
, qiov
->size
);
646 qemu_iovec_to_buf(qiov
, 0, buf
, qiov
->size
);
649 buf
= (uint8_t *)qiov
->iov
->iov_base
;
652 qemu_co_mutex_lock(&s
->lock
);
654 while (nb_sectors
!= 0) {
656 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
657 n
= s
->cluster_sectors
- index_in_cluster
;
658 if (n
> nb_sectors
) {
661 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 1, 0,
663 index_in_cluster
+ n
);
664 if (!cluster_offset
|| (cluster_offset
& 511) != 0) {
669 assert(s
->crypt_method
);
671 cluster_data
= g_malloc0(s
->cluster_size
);
673 encrypt_sectors(s
, sector_num
, cluster_data
, buf
,
674 n
, 1, &s
->aes_encrypt_key
);
675 src_buf
= cluster_data
;
680 hd_iov
.iov_base
= (void *)src_buf
;
681 hd_iov
.iov_len
= n
* 512;
682 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
683 qemu_co_mutex_unlock(&s
->lock
);
684 ret
= bdrv_co_writev(bs
->file
,
685 (cluster_offset
>> 9) + index_in_cluster
,
687 qemu_co_mutex_lock(&s
->lock
);
697 qemu_co_mutex_unlock(&s
->lock
);
699 if (qiov
->niov
> 1) {
700 qemu_vfree(orig_buf
);
702 g_free(cluster_data
);
707 static void qcow_close(BlockDriverState
*bs
)
709 BDRVQcowState
*s
= bs
->opaque
;
712 qemu_vfree(s
->l2_cache
);
713 g_free(s
->cluster_cache
);
714 g_free(s
->cluster_data
);
716 migrate_del_blocker(s
->migration_blocker
);
717 error_free(s
->migration_blocker
);
720 static int qcow_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
722 int header_size
, backing_filename_len
, l1_size
, shift
, i
;
725 int64_t total_size
= 0;
726 char *backing_file
= NULL
;
728 Error
*local_err
= NULL
;
730 BlockDriverState
*qcow_bs
;
732 /* Read out options */
733 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
735 backing_file
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FILE
);
736 if (qemu_opt_get_bool_del(opts
, BLOCK_OPT_ENCRYPT
, false)) {
737 flags
|= BLOCK_FLAG_ENCRYPT
;
740 ret
= bdrv_create_file(filename
, opts
, &local_err
);
742 error_propagate(errp
, local_err
);
747 ret
= bdrv_open(&qcow_bs
, filename
, NULL
, NULL
,
748 BDRV_O_RDWR
| BDRV_O_PROTOCOL
, NULL
, &local_err
);
750 error_propagate(errp
, local_err
);
754 ret
= bdrv_truncate(qcow_bs
, 0);
759 memset(&header
, 0, sizeof(header
));
760 header
.magic
= cpu_to_be32(QCOW_MAGIC
);
761 header
.version
= cpu_to_be32(QCOW_VERSION
);
762 header
.size
= cpu_to_be64(total_size
);
763 header_size
= sizeof(header
);
764 backing_filename_len
= 0;
766 if (strcmp(backing_file
, "fat:")) {
767 header
.backing_file_offset
= cpu_to_be64(header_size
);
768 backing_filename_len
= strlen(backing_file
);
769 header
.backing_file_size
= cpu_to_be32(backing_filename_len
);
770 header_size
+= backing_filename_len
;
772 /* special backing file for vvfat */
775 header
.cluster_bits
= 9; /* 512 byte cluster to avoid copying
776 unmodified sectors */
777 header
.l2_bits
= 12; /* 32 KB L2 tables */
779 header
.cluster_bits
= 12; /* 4 KB clusters */
780 header
.l2_bits
= 9; /* 4 KB L2 tables */
782 header_size
= (header_size
+ 7) & ~7;
783 shift
= header
.cluster_bits
+ header
.l2_bits
;
784 l1_size
= (total_size
+ (1LL << shift
) - 1) >> shift
;
786 header
.l1_table_offset
= cpu_to_be64(header_size
);
787 if (flags
& BLOCK_FLAG_ENCRYPT
) {
788 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
790 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
793 /* write all the data */
794 ret
= bdrv_pwrite(qcow_bs
, 0, &header
, sizeof(header
));
795 if (ret
!= sizeof(header
)) {
800 ret
= bdrv_pwrite(qcow_bs
, sizeof(header
),
801 backing_file
, backing_filename_len
);
802 if (ret
!= backing_filename_len
) {
807 tmp
= g_malloc0(BDRV_SECTOR_SIZE
);
808 for (i
= 0; i
< ((sizeof(uint64_t)*l1_size
+ BDRV_SECTOR_SIZE
- 1)/
809 BDRV_SECTOR_SIZE
); i
++) {
810 ret
= bdrv_pwrite(qcow_bs
, header_size
+
811 BDRV_SECTOR_SIZE
*i
, tmp
, BDRV_SECTOR_SIZE
);
812 if (ret
!= BDRV_SECTOR_SIZE
) {
823 g_free(backing_file
);
827 static int qcow_make_empty(BlockDriverState
*bs
)
829 BDRVQcowState
*s
= bs
->opaque
;
830 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
833 memset(s
->l1_table
, 0, l1_length
);
834 if (bdrv_pwrite_sync(bs
->file
, s
->l1_table_offset
, s
->l1_table
,
837 ret
= bdrv_truncate(bs
->file
, s
->l1_table_offset
+ l1_length
);
841 memset(s
->l2_cache
, 0, s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
842 memset(s
->l2_cache_offsets
, 0, L2_CACHE_SIZE
* sizeof(uint64_t));
843 memset(s
->l2_cache_counts
, 0, L2_CACHE_SIZE
* sizeof(uint32_t));
848 /* XXX: put compressed sectors first, then all the cluster aligned
849 tables to avoid losing bytes in alignment */
850 static int qcow_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
851 const uint8_t *buf
, int nb_sectors
)
853 BDRVQcowState
*s
= bs
->opaque
;
857 uint64_t cluster_offset
;
859 if (nb_sectors
!= s
->cluster_sectors
) {
862 /* Zero-pad last write if image size is not cluster aligned */
863 if (sector_num
+ nb_sectors
== bs
->total_sectors
&&
864 nb_sectors
< s
->cluster_sectors
) {
865 uint8_t *pad_buf
= qemu_blockalign(bs
, s
->cluster_size
);
866 memset(pad_buf
, 0, s
->cluster_size
);
867 memcpy(pad_buf
, buf
, nb_sectors
* BDRV_SECTOR_SIZE
);
868 ret
= qcow_write_compressed(bs
, sector_num
,
869 pad_buf
, s
->cluster_sectors
);
875 out_buf
= g_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
877 /* best compression, small window, no zlib header */
878 memset(&strm
, 0, sizeof(strm
));
879 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
881 9, Z_DEFAULT_STRATEGY
);
887 strm
.avail_in
= s
->cluster_size
;
888 strm
.next_in
= (uint8_t *)buf
;
889 strm
.avail_out
= s
->cluster_size
;
890 strm
.next_out
= out_buf
;
892 ret
= deflate(&strm
, Z_FINISH
);
893 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
898 out_len
= strm
.next_out
- out_buf
;
902 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
903 /* could not compress: write normal cluster */
904 ret
= bdrv_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
909 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 2,
911 if (cluster_offset
== 0) {
916 cluster_offset
&= s
->cluster_offset_mask
;
917 ret
= bdrv_pwrite(bs
->file
, cluster_offset
, out_buf
, out_len
);
929 static int qcow_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
931 BDRVQcowState
*s
= bs
->opaque
;
932 bdi
->cluster_size
= s
->cluster_size
;
936 static QemuOptsList qcow_create_opts
= {
937 .name
= "qcow-create-opts",
938 .head
= QTAILQ_HEAD_INITIALIZER(qcow_create_opts
.head
),
941 .name
= BLOCK_OPT_SIZE
,
942 .type
= QEMU_OPT_SIZE
,
943 .help
= "Virtual disk size"
946 .name
= BLOCK_OPT_BACKING_FILE
,
947 .type
= QEMU_OPT_STRING
,
948 .help
= "File name of a base image"
951 .name
= BLOCK_OPT_ENCRYPT
,
952 .type
= QEMU_OPT_BOOL
,
953 .help
= "Encrypt the image",
954 .def_value_str
= "off"
956 { /* end of list */ }
960 static BlockDriver bdrv_qcow
= {
961 .format_name
= "qcow",
962 .instance_size
= sizeof(BDRVQcowState
),
963 .bdrv_probe
= qcow_probe
,
964 .bdrv_open
= qcow_open
,
965 .bdrv_close
= qcow_close
,
966 .bdrv_reopen_prepare
= qcow_reopen_prepare
,
967 .bdrv_create
= qcow_create
,
968 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
969 .supports_backing
= true,
971 .bdrv_co_readv
= qcow_co_readv
,
972 .bdrv_co_writev
= qcow_co_writev
,
973 .bdrv_co_get_block_status
= qcow_co_get_block_status
,
975 .bdrv_set_key
= qcow_set_key
,
976 .bdrv_make_empty
= qcow_make_empty
,
977 .bdrv_write_compressed
= qcow_write_compressed
,
978 .bdrv_get_info
= qcow_get_info
,
980 .create_opts
= &qcow_create_opts
,
983 static void bdrv_qcow_init(void)
985 bdrv_register(&bdrv_qcow
);
988 block_init(bdrv_qcow_init
);