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"
29 #include "migration/migration.h"
31 /**************************************************************/
32 /* QEMU COW block driver with compression and encryption support */
34 #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
35 #define QCOW_VERSION 1
37 #define QCOW_CRYPT_NONE 0
38 #define QCOW_CRYPT_AES 1
40 #define QCOW_OFLAG_COMPRESSED (1LL << 63)
42 typedef struct QCowHeader
{
45 uint64_t backing_file_offset
;
46 uint32_t backing_file_size
;
48 uint64_t size
; /* in bytes */
52 uint32_t crypt_method
;
53 uint64_t l1_table_offset
;
54 } QEMU_PACKED QCowHeader
;
56 #define L2_CACHE_SIZE 16
58 typedef struct BDRVQcowState
{
65 uint64_t cluster_offset_mask
;
66 uint64_t l1_table_offset
;
69 uint64_t l2_cache_offsets
[L2_CACHE_SIZE
];
70 uint32_t l2_cache_counts
[L2_CACHE_SIZE
];
71 uint8_t *cluster_cache
;
72 uint8_t *cluster_data
;
73 uint64_t cluster_cache_offset
;
74 uint32_t crypt_method
; /* current crypt method, 0 if no key yet */
75 uint32_t crypt_method_header
;
76 AES_KEY aes_encrypt_key
;
77 AES_KEY aes_decrypt_key
;
79 Error
*migration_blocker
;
82 static int decompress_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
);
84 static int qcow_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
86 const QCowHeader
*cow_header
= (const void *)buf
;
88 if (buf_size
>= sizeof(QCowHeader
) &&
89 be32_to_cpu(cow_header
->magic
) == QCOW_MAGIC
&&
90 be32_to_cpu(cow_header
->version
) == QCOW_VERSION
)
96 static int qcow_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
99 BDRVQcowState
*s
= bs
->opaque
;
100 unsigned int len
, i
, shift
;
104 ret
= bdrv_pread(bs
->file
, 0, &header
, sizeof(header
));
108 be32_to_cpus(&header
.magic
);
109 be32_to_cpus(&header
.version
);
110 be64_to_cpus(&header
.backing_file_offset
);
111 be32_to_cpus(&header
.backing_file_size
);
112 be32_to_cpus(&header
.mtime
);
113 be64_to_cpus(&header
.size
);
114 be32_to_cpus(&header
.crypt_method
);
115 be64_to_cpus(&header
.l1_table_offset
);
117 if (header
.magic
!= QCOW_MAGIC
) {
118 error_setg(errp
, "Image not in qcow format");
122 if (header
.version
!= QCOW_VERSION
) {
124 snprintf(version
, sizeof(version
), "QCOW version %" PRIu32
,
126 error_set(errp
, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE
,
127 bs
->device_name
, "qcow", 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 s
->crypt_method_header
= header
.crypt_method
;
157 if (s
->crypt_method_header
) {
160 s
->cluster_bits
= header
.cluster_bits
;
161 s
->cluster_size
= 1 << s
->cluster_bits
;
162 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
163 s
->l2_bits
= header
.l2_bits
;
164 s
->l2_size
= 1 << s
->l2_bits
;
165 bs
->total_sectors
= header
.size
/ 512;
166 s
->cluster_offset_mask
= (1LL << (63 - s
->cluster_bits
)) - 1;
168 /* read the level 1 table */
169 shift
= s
->cluster_bits
+ s
->l2_bits
;
170 if (header
.size
> UINT64_MAX
- (1LL << shift
)) {
171 error_setg(errp
, "Image too large");
175 uint64_t l1_size
= (header
.size
+ (1LL << shift
) - 1) >> shift
;
176 if (l1_size
> INT_MAX
/ sizeof(uint64_t)) {
177 error_setg(errp
, "Image too large");
181 s
->l1_size
= l1_size
;
184 s
->l1_table_offset
= header
.l1_table_offset
;
185 s
->l1_table
= g_try_new(uint64_t, s
->l1_size
);
186 if (s
->l1_table
== NULL
) {
187 error_setg(errp
, "Could not allocate memory for L1 table");
192 ret
= bdrv_pread(bs
->file
, s
->l1_table_offset
, s
->l1_table
,
193 s
->l1_size
* sizeof(uint64_t));
198 for(i
= 0;i
< s
->l1_size
; i
++) {
199 be64_to_cpus(&s
->l1_table
[i
]);
202 /* alloc L2 cache (max. 64k * 16 * 8 = 8 MB) */
204 qemu_try_blockalign(bs
->file
,
205 s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
206 if (s
->l2_cache
== NULL
) {
207 error_setg(errp
, "Could not allocate L2 table cache");
211 s
->cluster_cache
= g_malloc(s
->cluster_size
);
212 s
->cluster_data
= g_malloc(s
->cluster_size
);
213 s
->cluster_cache_offset
= -1;
215 /* read the backing file name */
216 if (header
.backing_file_offset
!= 0) {
217 len
= header
.backing_file_size
;
219 error_setg(errp
, "Backing file name too long");
223 ret
= bdrv_pread(bs
->file
, header
.backing_file_offset
,
224 bs
->backing_file
, len
);
228 bs
->backing_file
[len
] = '\0';
231 /* Disable migration when qcow images are used */
232 error_set(&s
->migration_blocker
,
233 QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED
,
234 "qcow", bs
->device_name
, "live migration");
235 migrate_add_blocker(s
->migration_blocker
);
237 qemu_co_mutex_init(&s
->lock
);
242 qemu_vfree(s
->l2_cache
);
243 g_free(s
->cluster_cache
);
244 g_free(s
->cluster_data
);
249 /* We have nothing to do for QCOW reopen, stubs just return
251 static int qcow_reopen_prepare(BDRVReopenState
*state
,
252 BlockReopenQueue
*queue
, Error
**errp
)
257 static int qcow_set_key(BlockDriverState
*bs
, const char *key
)
259 BDRVQcowState
*s
= bs
->opaque
;
263 memset(keybuf
, 0, 16);
267 /* XXX: we could compress the chars to 7 bits to increase
269 for(i
= 0;i
< len
;i
++) {
272 s
->crypt_method
= s
->crypt_method_header
;
274 if (AES_set_encrypt_key(keybuf
, 128, &s
->aes_encrypt_key
) != 0)
276 if (AES_set_decrypt_key(keybuf
, 128, &s
->aes_decrypt_key
) != 0)
281 /* The crypt function is compatible with the linux cryptoloop
282 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
284 static void encrypt_sectors(BDRVQcowState
*s
, int64_t sector_num
,
285 uint8_t *out_buf
, const uint8_t *in_buf
,
286 int nb_sectors
, int enc
,
295 for(i
= 0; i
< nb_sectors
; i
++) {
296 ivec
.ll
[0] = cpu_to_le64(sector_num
);
298 AES_cbc_encrypt(in_buf
, out_buf
, 512, key
,
310 * 1 to allocate a normal cluster (for sector indexes 'n_start' to
313 * 2 to allocate a compressed cluster of size
314 * 'compressed_size'. 'compressed_size' must be > 0 and <
317 * return 0 if not allocated.
319 static uint64_t get_cluster_offset(BlockDriverState
*bs
,
320 uint64_t offset
, int allocate
,
322 int n_start
, int n_end
)
324 BDRVQcowState
*s
= bs
->opaque
;
325 int min_index
, i
, j
, l1_index
, l2_index
;
326 uint64_t l2_offset
, *l2_table
, cluster_offset
, tmp
;
330 l1_index
= offset
>> (s
->l2_bits
+ s
->cluster_bits
);
331 l2_offset
= s
->l1_table
[l1_index
];
336 /* allocate a new l2 entry */
337 l2_offset
= bdrv_getlength(bs
->file
);
338 /* round to cluster size */
339 l2_offset
= (l2_offset
+ s
->cluster_size
- 1) & ~(s
->cluster_size
- 1);
340 /* update the L1 entry */
341 s
->l1_table
[l1_index
] = l2_offset
;
342 tmp
= cpu_to_be64(l2_offset
);
343 if (bdrv_pwrite_sync(bs
->file
,
344 s
->l1_table_offset
+ l1_index
* sizeof(tmp
),
345 &tmp
, sizeof(tmp
)) < 0)
349 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
350 if (l2_offset
== s
->l2_cache_offsets
[i
]) {
351 /* increment the hit count */
352 if (++s
->l2_cache_counts
[i
] == 0xffffffff) {
353 for(j
= 0; j
< L2_CACHE_SIZE
; j
++) {
354 s
->l2_cache_counts
[j
] >>= 1;
357 l2_table
= s
->l2_cache
+ (i
<< s
->l2_bits
);
361 /* not found: load a new entry in the least used one */
363 min_count
= 0xffffffff;
364 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
365 if (s
->l2_cache_counts
[i
] < min_count
) {
366 min_count
= s
->l2_cache_counts
[i
];
370 l2_table
= s
->l2_cache
+ (min_index
<< s
->l2_bits
);
372 memset(l2_table
, 0, s
->l2_size
* sizeof(uint64_t));
373 if (bdrv_pwrite_sync(bs
->file
, l2_offset
, l2_table
,
374 s
->l2_size
* sizeof(uint64_t)) < 0)
377 if (bdrv_pread(bs
->file
, l2_offset
, l2_table
, s
->l2_size
* sizeof(uint64_t)) !=
378 s
->l2_size
* sizeof(uint64_t))
381 s
->l2_cache_offsets
[min_index
] = l2_offset
;
382 s
->l2_cache_counts
[min_index
] = 1;
384 l2_index
= (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
385 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
386 if (!cluster_offset
||
387 ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) && allocate
== 1)) {
390 /* allocate a new cluster */
391 if ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) &&
392 (n_end
- n_start
) < s
->cluster_sectors
) {
393 /* if the cluster is already compressed, we must
394 decompress it in the case it is not completely
396 if (decompress_cluster(bs
, cluster_offset
) < 0)
398 cluster_offset
= bdrv_getlength(bs
->file
);
399 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
400 ~(s
->cluster_size
- 1);
401 /* write the cluster content */
402 if (bdrv_pwrite(bs
->file
, cluster_offset
, s
->cluster_cache
, s
->cluster_size
) !=
406 cluster_offset
= bdrv_getlength(bs
->file
);
408 /* round to cluster size */
409 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
410 ~(s
->cluster_size
- 1);
411 bdrv_truncate(bs
->file
, cluster_offset
+ s
->cluster_size
);
412 /* if encrypted, we must initialize the cluster
413 content which won't be written */
414 if (s
->crypt_method
&&
415 (n_end
- n_start
) < s
->cluster_sectors
) {
417 start_sect
= (offset
& ~(s
->cluster_size
- 1)) >> 9;
418 memset(s
->cluster_data
+ 512, 0x00, 512);
419 for(i
= 0; i
< s
->cluster_sectors
; i
++) {
420 if (i
< n_start
|| i
>= n_end
) {
421 encrypt_sectors(s
, start_sect
+ i
,
423 s
->cluster_data
+ 512, 1, 1,
424 &s
->aes_encrypt_key
);
425 if (bdrv_pwrite(bs
->file
, cluster_offset
+ i
* 512,
426 s
->cluster_data
, 512) != 512)
431 } else if (allocate
== 2) {
432 cluster_offset
|= QCOW_OFLAG_COMPRESSED
|
433 (uint64_t)compressed_size
<< (63 - s
->cluster_bits
);
436 /* update L2 table */
437 tmp
= cpu_to_be64(cluster_offset
);
438 l2_table
[l2_index
] = tmp
;
439 if (bdrv_pwrite_sync(bs
->file
, l2_offset
+ l2_index
* sizeof(tmp
),
440 &tmp
, sizeof(tmp
)) < 0)
443 return cluster_offset
;
446 static int64_t coroutine_fn
qcow_co_get_block_status(BlockDriverState
*bs
,
447 int64_t sector_num
, int nb_sectors
, int *pnum
)
449 BDRVQcowState
*s
= bs
->opaque
;
450 int index_in_cluster
, n
;
451 uint64_t cluster_offset
;
453 qemu_co_mutex_lock(&s
->lock
);
454 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 0, 0, 0, 0);
455 qemu_co_mutex_unlock(&s
->lock
);
456 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
457 n
= s
->cluster_sectors
- index_in_cluster
;
461 if (!cluster_offset
) {
464 if ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) || s
->crypt_method
) {
465 return BDRV_BLOCK_DATA
;
467 cluster_offset
|= (index_in_cluster
<< BDRV_SECTOR_BITS
);
468 return BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
| cluster_offset
;
471 static int decompress_buffer(uint8_t *out_buf
, int out_buf_size
,
472 const uint8_t *buf
, int buf_size
)
474 z_stream strm1
, *strm
= &strm1
;
477 memset(strm
, 0, sizeof(*strm
));
479 strm
->next_in
= (uint8_t *)buf
;
480 strm
->avail_in
= buf_size
;
481 strm
->next_out
= out_buf
;
482 strm
->avail_out
= out_buf_size
;
484 ret
= inflateInit2(strm
, -12);
487 ret
= inflate(strm
, Z_FINISH
);
488 out_len
= strm
->next_out
- out_buf
;
489 if ((ret
!= Z_STREAM_END
&& ret
!= Z_BUF_ERROR
) ||
490 out_len
!= out_buf_size
) {
498 static int decompress_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
)
500 BDRVQcowState
*s
= bs
->opaque
;
504 coffset
= cluster_offset
& s
->cluster_offset_mask
;
505 if (s
->cluster_cache_offset
!= coffset
) {
506 csize
= cluster_offset
>> (63 - s
->cluster_bits
);
507 csize
&= (s
->cluster_size
- 1);
508 ret
= bdrv_pread(bs
->file
, coffset
, s
->cluster_data
, csize
);
511 if (decompress_buffer(s
->cluster_cache
, s
->cluster_size
,
512 s
->cluster_data
, csize
) < 0) {
515 s
->cluster_cache_offset
= coffset
;
520 static coroutine_fn
int qcow_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
521 int nb_sectors
, QEMUIOVector
*qiov
)
523 BDRVQcowState
*s
= bs
->opaque
;
524 int index_in_cluster
;
526 uint64_t cluster_offset
;
528 QEMUIOVector hd_qiov
;
532 if (qiov
->niov
> 1) {
533 buf
= orig_buf
= qemu_try_blockalign(bs
, qiov
->size
);
539 buf
= (uint8_t *)qiov
->iov
->iov_base
;
542 qemu_co_mutex_lock(&s
->lock
);
544 while (nb_sectors
!= 0) {
545 /* prepare next request */
546 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9,
548 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
549 n
= s
->cluster_sectors
- index_in_cluster
;
550 if (n
> nb_sectors
) {
554 if (!cluster_offset
) {
555 if (bs
->backing_hd
) {
556 /* read from the base image */
557 hd_iov
.iov_base
= (void *)buf
;
558 hd_iov
.iov_len
= n
* 512;
559 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
560 qemu_co_mutex_unlock(&s
->lock
);
561 ret
= bdrv_co_readv(bs
->backing_hd
, sector_num
,
563 qemu_co_mutex_lock(&s
->lock
);
568 /* Note: in this case, no need to wait */
569 memset(buf
, 0, 512 * n
);
571 } else if (cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
572 /* add AIO support for compressed blocks ? */
573 if (decompress_cluster(bs
, cluster_offset
) < 0) {
577 s
->cluster_cache
+ index_in_cluster
* 512, 512 * n
);
579 if ((cluster_offset
& 511) != 0) {
582 hd_iov
.iov_base
= (void *)buf
;
583 hd_iov
.iov_len
= n
* 512;
584 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
585 qemu_co_mutex_unlock(&s
->lock
);
586 ret
= bdrv_co_readv(bs
->file
,
587 (cluster_offset
>> 9) + index_in_cluster
,
589 qemu_co_mutex_lock(&s
->lock
);
593 if (s
->crypt_method
) {
594 encrypt_sectors(s
, sector_num
, buf
, buf
,
596 &s
->aes_decrypt_key
);
607 qemu_co_mutex_unlock(&s
->lock
);
609 if (qiov
->niov
> 1) {
610 qemu_iovec_from_buf(qiov
, 0, orig_buf
, qiov
->size
);
611 qemu_vfree(orig_buf
);
621 static coroutine_fn
int qcow_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
622 int nb_sectors
, QEMUIOVector
*qiov
)
624 BDRVQcowState
*s
= bs
->opaque
;
625 int index_in_cluster
;
626 uint64_t cluster_offset
;
627 const uint8_t *src_buf
;
629 uint8_t *cluster_data
= NULL
;
631 QEMUIOVector hd_qiov
;
635 s
->cluster_cache_offset
= -1; /* disable compressed cache */
637 if (qiov
->niov
> 1) {
638 buf
= orig_buf
= qemu_try_blockalign(bs
, qiov
->size
);
642 qemu_iovec_to_buf(qiov
, 0, buf
, qiov
->size
);
645 buf
= (uint8_t *)qiov
->iov
->iov_base
;
648 qemu_co_mutex_lock(&s
->lock
);
650 while (nb_sectors
!= 0) {
652 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
653 n
= s
->cluster_sectors
- index_in_cluster
;
654 if (n
> nb_sectors
) {
657 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 1, 0,
659 index_in_cluster
+ n
);
660 if (!cluster_offset
|| (cluster_offset
& 511) != 0) {
664 if (s
->crypt_method
) {
666 cluster_data
= g_malloc0(s
->cluster_size
);
668 encrypt_sectors(s
, sector_num
, cluster_data
, buf
,
669 n
, 1, &s
->aes_encrypt_key
);
670 src_buf
= cluster_data
;
675 hd_iov
.iov_base
= (void *)src_buf
;
676 hd_iov
.iov_len
= n
* 512;
677 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
678 qemu_co_mutex_unlock(&s
->lock
);
679 ret
= bdrv_co_writev(bs
->file
,
680 (cluster_offset
>> 9) + index_in_cluster
,
682 qemu_co_mutex_lock(&s
->lock
);
692 qemu_co_mutex_unlock(&s
->lock
);
694 if (qiov
->niov
> 1) {
695 qemu_vfree(orig_buf
);
697 g_free(cluster_data
);
702 static void qcow_close(BlockDriverState
*bs
)
704 BDRVQcowState
*s
= bs
->opaque
;
707 qemu_vfree(s
->l2_cache
);
708 g_free(s
->cluster_cache
);
709 g_free(s
->cluster_data
);
711 migrate_del_blocker(s
->migration_blocker
);
712 error_free(s
->migration_blocker
);
715 static int qcow_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
717 int header_size
, backing_filename_len
, l1_size
, shift
, i
;
720 int64_t total_size
= 0;
721 char *backing_file
= NULL
;
723 Error
*local_err
= NULL
;
725 BlockDriverState
*qcow_bs
;
727 /* Read out options */
728 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
730 backing_file
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FILE
);
731 if (qemu_opt_get_bool_del(opts
, BLOCK_OPT_ENCRYPT
, false)) {
732 flags
|= BLOCK_FLAG_ENCRYPT
;
735 ret
= bdrv_create_file(filename
, opts
, &local_err
);
737 error_propagate(errp
, local_err
);
742 ret
= bdrv_open(&qcow_bs
, filename
, NULL
, NULL
,
743 BDRV_O_RDWR
| BDRV_O_PROTOCOL
, NULL
, &local_err
);
745 error_propagate(errp
, local_err
);
749 ret
= bdrv_truncate(qcow_bs
, 0);
754 memset(&header
, 0, sizeof(header
));
755 header
.magic
= cpu_to_be32(QCOW_MAGIC
);
756 header
.version
= cpu_to_be32(QCOW_VERSION
);
757 header
.size
= cpu_to_be64(total_size
);
758 header_size
= sizeof(header
);
759 backing_filename_len
= 0;
761 if (strcmp(backing_file
, "fat:")) {
762 header
.backing_file_offset
= cpu_to_be64(header_size
);
763 backing_filename_len
= strlen(backing_file
);
764 header
.backing_file_size
= cpu_to_be32(backing_filename_len
);
765 header_size
+= backing_filename_len
;
767 /* special backing file for vvfat */
770 header
.cluster_bits
= 9; /* 512 byte cluster to avoid copying
771 unmodified sectors */
772 header
.l2_bits
= 12; /* 32 KB L2 tables */
774 header
.cluster_bits
= 12; /* 4 KB clusters */
775 header
.l2_bits
= 9; /* 4 KB L2 tables */
777 header_size
= (header_size
+ 7) & ~7;
778 shift
= header
.cluster_bits
+ header
.l2_bits
;
779 l1_size
= (total_size
+ (1LL << shift
) - 1) >> shift
;
781 header
.l1_table_offset
= cpu_to_be64(header_size
);
782 if (flags
& BLOCK_FLAG_ENCRYPT
) {
783 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
785 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
788 /* write all the data */
789 ret
= bdrv_pwrite(qcow_bs
, 0, &header
, sizeof(header
));
790 if (ret
!= sizeof(header
)) {
795 ret
= bdrv_pwrite(qcow_bs
, sizeof(header
),
796 backing_file
, backing_filename_len
);
797 if (ret
!= backing_filename_len
) {
802 tmp
= g_malloc0(BDRV_SECTOR_SIZE
);
803 for (i
= 0; i
< ((sizeof(uint64_t)*l1_size
+ BDRV_SECTOR_SIZE
- 1)/
804 BDRV_SECTOR_SIZE
); i
++) {
805 ret
= bdrv_pwrite(qcow_bs
, header_size
+
806 BDRV_SECTOR_SIZE
*i
, tmp
, BDRV_SECTOR_SIZE
);
807 if (ret
!= BDRV_SECTOR_SIZE
) {
818 g_free(backing_file
);
822 static int qcow_make_empty(BlockDriverState
*bs
)
824 BDRVQcowState
*s
= bs
->opaque
;
825 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
828 memset(s
->l1_table
, 0, l1_length
);
829 if (bdrv_pwrite_sync(bs
->file
, s
->l1_table_offset
, s
->l1_table
,
832 ret
= bdrv_truncate(bs
->file
, s
->l1_table_offset
+ l1_length
);
836 memset(s
->l2_cache
, 0, s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
837 memset(s
->l2_cache_offsets
, 0, L2_CACHE_SIZE
* sizeof(uint64_t));
838 memset(s
->l2_cache_counts
, 0, L2_CACHE_SIZE
* sizeof(uint32_t));
843 /* XXX: put compressed sectors first, then all the cluster aligned
844 tables to avoid losing bytes in alignment */
845 static int qcow_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
846 const uint8_t *buf
, int nb_sectors
)
848 BDRVQcowState
*s
= bs
->opaque
;
852 uint64_t cluster_offset
;
854 if (nb_sectors
!= s
->cluster_sectors
) {
857 /* Zero-pad last write if image size is not cluster aligned */
858 if (sector_num
+ nb_sectors
== bs
->total_sectors
&&
859 nb_sectors
< s
->cluster_sectors
) {
860 uint8_t *pad_buf
= qemu_blockalign(bs
, s
->cluster_size
);
861 memset(pad_buf
, 0, s
->cluster_size
);
862 memcpy(pad_buf
, buf
, nb_sectors
* BDRV_SECTOR_SIZE
);
863 ret
= qcow_write_compressed(bs
, sector_num
,
864 pad_buf
, s
->cluster_sectors
);
870 out_buf
= g_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
872 /* best compression, small window, no zlib header */
873 memset(&strm
, 0, sizeof(strm
));
874 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
876 9, Z_DEFAULT_STRATEGY
);
882 strm
.avail_in
= s
->cluster_size
;
883 strm
.next_in
= (uint8_t *)buf
;
884 strm
.avail_out
= s
->cluster_size
;
885 strm
.next_out
= out_buf
;
887 ret
= deflate(&strm
, Z_FINISH
);
888 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
893 out_len
= strm
.next_out
- out_buf
;
897 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
898 /* could not compress: write normal cluster */
899 ret
= bdrv_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
904 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 2,
906 if (cluster_offset
== 0) {
911 cluster_offset
&= s
->cluster_offset_mask
;
912 ret
= bdrv_pwrite(bs
->file
, cluster_offset
, out_buf
, out_len
);
924 static int qcow_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
926 BDRVQcowState
*s
= bs
->opaque
;
927 bdi
->cluster_size
= s
->cluster_size
;
931 static QemuOptsList qcow_create_opts
= {
932 .name
= "qcow-create-opts",
933 .head
= QTAILQ_HEAD_INITIALIZER(qcow_create_opts
.head
),
936 .name
= BLOCK_OPT_SIZE
,
937 .type
= QEMU_OPT_SIZE
,
938 .help
= "Virtual disk size"
941 .name
= BLOCK_OPT_BACKING_FILE
,
942 .type
= QEMU_OPT_STRING
,
943 .help
= "File name of a base image"
946 .name
= BLOCK_OPT_ENCRYPT
,
947 .type
= QEMU_OPT_BOOL
,
948 .help
= "Encrypt the image",
949 .def_value_str
= "off"
951 { /* end of list */ }
955 static BlockDriver bdrv_qcow
= {
956 .format_name
= "qcow",
957 .instance_size
= sizeof(BDRVQcowState
),
958 .bdrv_probe
= qcow_probe
,
959 .bdrv_open
= qcow_open
,
960 .bdrv_close
= qcow_close
,
961 .bdrv_reopen_prepare
= qcow_reopen_prepare
,
962 .bdrv_create
= qcow_create
,
963 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
964 .supports_backing
= true,
966 .bdrv_co_readv
= qcow_co_readv
,
967 .bdrv_co_writev
= qcow_co_writev
,
968 .bdrv_co_get_block_status
= qcow_co_get_block_status
,
970 .bdrv_set_key
= qcow_set_key
,
971 .bdrv_make_empty
= qcow_make_empty
,
972 .bdrv_write_compressed
= qcow_write_compressed
,
973 .bdrv_get_info
= qcow_get_info
,
975 .create_opts
= &qcow_create_opts
,
978 static void bdrv_qcow_init(void)
980 bdrv_register(&bdrv_qcow
);
983 block_init(bdrv_qcow_init
);