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 */
51 uint32_t crypt_method
;
52 uint64_t l1_table_offset
;
55 #define L2_CACHE_SIZE 16
57 typedef struct BDRVQcowState
{
64 uint64_t cluster_offset_mask
;
65 uint64_t l1_table_offset
;
68 uint64_t l2_cache_offsets
[L2_CACHE_SIZE
];
69 uint32_t l2_cache_counts
[L2_CACHE_SIZE
];
70 uint8_t *cluster_cache
;
71 uint8_t *cluster_data
;
72 uint64_t cluster_cache_offset
;
73 uint32_t crypt_method
; /* current crypt method, 0 if no key yet */
74 uint32_t crypt_method_header
;
75 AES_KEY aes_encrypt_key
;
76 AES_KEY aes_decrypt_key
;
78 Error
*migration_blocker
;
81 static int decompress_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
);
83 static int qcow_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
85 const QCowHeader
*cow_header
= (const void *)buf
;
87 if (buf_size
>= sizeof(QCowHeader
) &&
88 be32_to_cpu(cow_header
->magic
) == QCOW_MAGIC
&&
89 be32_to_cpu(cow_header
->version
) == QCOW_VERSION
)
95 static int qcow_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
98 BDRVQcowState
*s
= bs
->opaque
;
99 int len
, i
, shift
, ret
;
102 ret
= bdrv_pread(bs
->file
, 0, &header
, sizeof(header
));
106 be32_to_cpus(&header
.magic
);
107 be32_to_cpus(&header
.version
);
108 be64_to_cpus(&header
.backing_file_offset
);
109 be32_to_cpus(&header
.backing_file_size
);
110 be32_to_cpus(&header
.mtime
);
111 be64_to_cpus(&header
.size
);
112 be32_to_cpus(&header
.crypt_method
);
113 be64_to_cpus(&header
.l1_table_offset
);
115 if (header
.magic
!= QCOW_MAGIC
) {
119 if (header
.version
!= QCOW_VERSION
) {
121 snprintf(version
, sizeof(version
), "QCOW version %d", header
.version
);
122 qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE
,
123 bs
->device_name
, "qcow", version
);
128 if (header
.size
<= 1 || header
.cluster_bits
< 9) {
132 if (header
.crypt_method
> QCOW_CRYPT_AES
) {
136 s
->crypt_method_header
= header
.crypt_method
;
137 if (s
->crypt_method_header
) {
140 s
->cluster_bits
= header
.cluster_bits
;
141 s
->cluster_size
= 1 << s
->cluster_bits
;
142 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
143 s
->l2_bits
= header
.l2_bits
;
144 s
->l2_size
= 1 << s
->l2_bits
;
145 bs
->total_sectors
= header
.size
/ 512;
146 s
->cluster_offset_mask
= (1LL << (63 - s
->cluster_bits
)) - 1;
148 /* read the level 1 table */
149 shift
= s
->cluster_bits
+ s
->l2_bits
;
150 s
->l1_size
= (header
.size
+ (1LL << shift
) - 1) >> shift
;
152 s
->l1_table_offset
= header
.l1_table_offset
;
153 s
->l1_table
= g_malloc(s
->l1_size
* sizeof(uint64_t));
155 ret
= bdrv_pread(bs
->file
, s
->l1_table_offset
, s
->l1_table
,
156 s
->l1_size
* sizeof(uint64_t));
161 for(i
= 0;i
< s
->l1_size
; i
++) {
162 be64_to_cpus(&s
->l1_table
[i
]);
165 s
->l2_cache
= g_malloc(s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
166 s
->cluster_cache
= g_malloc(s
->cluster_size
);
167 s
->cluster_data
= g_malloc(s
->cluster_size
);
168 s
->cluster_cache_offset
= -1;
170 /* read the backing file name */
171 if (header
.backing_file_offset
!= 0) {
172 len
= header
.backing_file_size
;
176 ret
= bdrv_pread(bs
->file
, header
.backing_file_offset
,
177 bs
->backing_file
, len
);
181 bs
->backing_file
[len
] = '\0';
184 /* Disable migration when qcow images are used */
185 error_set(&s
->migration_blocker
,
186 QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED
,
187 "qcow", bs
->device_name
, "live migration");
188 migrate_add_blocker(s
->migration_blocker
);
190 qemu_co_mutex_init(&s
->lock
);
196 g_free(s
->cluster_cache
);
197 g_free(s
->cluster_data
);
202 /* We have nothing to do for QCOW reopen, stubs just return
204 static int qcow_reopen_prepare(BDRVReopenState
*state
,
205 BlockReopenQueue
*queue
, Error
**errp
)
210 static int qcow_set_key(BlockDriverState
*bs
, const char *key
)
212 BDRVQcowState
*s
= bs
->opaque
;
216 memset(keybuf
, 0, 16);
220 /* XXX: we could compress the chars to 7 bits to increase
222 for(i
= 0;i
< len
;i
++) {
225 s
->crypt_method
= s
->crypt_method_header
;
227 if (AES_set_encrypt_key(keybuf
, 128, &s
->aes_encrypt_key
) != 0)
229 if (AES_set_decrypt_key(keybuf
, 128, &s
->aes_decrypt_key
) != 0)
234 /* The crypt function is compatible with the linux cryptoloop
235 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
237 static void encrypt_sectors(BDRVQcowState
*s
, int64_t sector_num
,
238 uint8_t *out_buf
, const uint8_t *in_buf
,
239 int nb_sectors
, int enc
,
248 for(i
= 0; i
< nb_sectors
; i
++) {
249 ivec
.ll
[0] = cpu_to_le64(sector_num
);
251 AES_cbc_encrypt(in_buf
, out_buf
, 512, key
,
263 * 1 to allocate a normal cluster (for sector indexes 'n_start' to
266 * 2 to allocate a compressed cluster of size
267 * 'compressed_size'. 'compressed_size' must be > 0 and <
270 * return 0 if not allocated.
272 static uint64_t get_cluster_offset(BlockDriverState
*bs
,
273 uint64_t offset
, int allocate
,
275 int n_start
, int n_end
)
277 BDRVQcowState
*s
= bs
->opaque
;
278 int min_index
, i
, j
, l1_index
, l2_index
;
279 uint64_t l2_offset
, *l2_table
, cluster_offset
, tmp
;
283 l1_index
= offset
>> (s
->l2_bits
+ s
->cluster_bits
);
284 l2_offset
= s
->l1_table
[l1_index
];
289 /* allocate a new l2 entry */
290 l2_offset
= bdrv_getlength(bs
->file
);
291 /* round to cluster size */
292 l2_offset
= (l2_offset
+ s
->cluster_size
- 1) & ~(s
->cluster_size
- 1);
293 /* update the L1 entry */
294 s
->l1_table
[l1_index
] = l2_offset
;
295 tmp
= cpu_to_be64(l2_offset
);
296 if (bdrv_pwrite_sync(bs
->file
,
297 s
->l1_table_offset
+ l1_index
* sizeof(tmp
),
298 &tmp
, sizeof(tmp
)) < 0)
302 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
303 if (l2_offset
== s
->l2_cache_offsets
[i
]) {
304 /* increment the hit count */
305 if (++s
->l2_cache_counts
[i
] == 0xffffffff) {
306 for(j
= 0; j
< L2_CACHE_SIZE
; j
++) {
307 s
->l2_cache_counts
[j
] >>= 1;
310 l2_table
= s
->l2_cache
+ (i
<< s
->l2_bits
);
314 /* not found: load a new entry in the least used one */
316 min_count
= 0xffffffff;
317 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
318 if (s
->l2_cache_counts
[i
] < min_count
) {
319 min_count
= s
->l2_cache_counts
[i
];
323 l2_table
= s
->l2_cache
+ (min_index
<< s
->l2_bits
);
325 memset(l2_table
, 0, s
->l2_size
* sizeof(uint64_t));
326 if (bdrv_pwrite_sync(bs
->file
, l2_offset
, l2_table
,
327 s
->l2_size
* sizeof(uint64_t)) < 0)
330 if (bdrv_pread(bs
->file
, l2_offset
, l2_table
, s
->l2_size
* sizeof(uint64_t)) !=
331 s
->l2_size
* sizeof(uint64_t))
334 s
->l2_cache_offsets
[min_index
] = l2_offset
;
335 s
->l2_cache_counts
[min_index
] = 1;
337 l2_index
= (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
338 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
339 if (!cluster_offset
||
340 ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) && allocate
== 1)) {
343 /* allocate a new cluster */
344 if ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) &&
345 (n_end
- n_start
) < s
->cluster_sectors
) {
346 /* if the cluster is already compressed, we must
347 decompress it in the case it is not completely
349 if (decompress_cluster(bs
, cluster_offset
) < 0)
351 cluster_offset
= bdrv_getlength(bs
->file
);
352 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
353 ~(s
->cluster_size
- 1);
354 /* write the cluster content */
355 if (bdrv_pwrite(bs
->file
, cluster_offset
, s
->cluster_cache
, s
->cluster_size
) !=
359 cluster_offset
= bdrv_getlength(bs
->file
);
361 /* round to cluster size */
362 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
363 ~(s
->cluster_size
- 1);
364 bdrv_truncate(bs
->file
, cluster_offset
+ s
->cluster_size
);
365 /* if encrypted, we must initialize the cluster
366 content which won't be written */
367 if (s
->crypt_method
&&
368 (n_end
- n_start
) < s
->cluster_sectors
) {
370 start_sect
= (offset
& ~(s
->cluster_size
- 1)) >> 9;
371 memset(s
->cluster_data
+ 512, 0x00, 512);
372 for(i
= 0; i
< s
->cluster_sectors
; i
++) {
373 if (i
< n_start
|| i
>= n_end
) {
374 encrypt_sectors(s
, start_sect
+ i
,
376 s
->cluster_data
+ 512, 1, 1,
377 &s
->aes_encrypt_key
);
378 if (bdrv_pwrite(bs
->file
, cluster_offset
+ i
* 512,
379 s
->cluster_data
, 512) != 512)
384 } else if (allocate
== 2) {
385 cluster_offset
|= QCOW_OFLAG_COMPRESSED
|
386 (uint64_t)compressed_size
<< (63 - s
->cluster_bits
);
389 /* update L2 table */
390 tmp
= cpu_to_be64(cluster_offset
);
391 l2_table
[l2_index
] = tmp
;
392 if (bdrv_pwrite_sync(bs
->file
, l2_offset
+ l2_index
* sizeof(tmp
),
393 &tmp
, sizeof(tmp
)) < 0)
396 return cluster_offset
;
399 static int64_t coroutine_fn
qcow_co_get_block_status(BlockDriverState
*bs
,
400 int64_t sector_num
, int nb_sectors
, int *pnum
)
402 BDRVQcowState
*s
= bs
->opaque
;
403 int index_in_cluster
, n
;
404 uint64_t cluster_offset
;
406 qemu_co_mutex_lock(&s
->lock
);
407 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 0, 0, 0, 0);
408 qemu_co_mutex_unlock(&s
->lock
);
409 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
410 n
= s
->cluster_sectors
- index_in_cluster
;
414 if (!cluster_offset
) {
417 if ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) || s
->crypt_method
) {
418 return BDRV_BLOCK_DATA
;
420 cluster_offset
|= (index_in_cluster
<< BDRV_SECTOR_BITS
);
421 return BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
| cluster_offset
;
424 static int decompress_buffer(uint8_t *out_buf
, int out_buf_size
,
425 const uint8_t *buf
, int buf_size
)
427 z_stream strm1
, *strm
= &strm1
;
430 memset(strm
, 0, sizeof(*strm
));
432 strm
->next_in
= (uint8_t *)buf
;
433 strm
->avail_in
= buf_size
;
434 strm
->next_out
= out_buf
;
435 strm
->avail_out
= out_buf_size
;
437 ret
= inflateInit2(strm
, -12);
440 ret
= inflate(strm
, Z_FINISH
);
441 out_len
= strm
->next_out
- out_buf
;
442 if ((ret
!= Z_STREAM_END
&& ret
!= Z_BUF_ERROR
) ||
443 out_len
!= out_buf_size
) {
451 static int decompress_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
)
453 BDRVQcowState
*s
= bs
->opaque
;
457 coffset
= cluster_offset
& s
->cluster_offset_mask
;
458 if (s
->cluster_cache_offset
!= coffset
) {
459 csize
= cluster_offset
>> (63 - s
->cluster_bits
);
460 csize
&= (s
->cluster_size
- 1);
461 ret
= bdrv_pread(bs
->file
, coffset
, s
->cluster_data
, csize
);
464 if (decompress_buffer(s
->cluster_cache
, s
->cluster_size
,
465 s
->cluster_data
, csize
) < 0) {
468 s
->cluster_cache_offset
= coffset
;
473 static coroutine_fn
int qcow_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
474 int nb_sectors
, QEMUIOVector
*qiov
)
476 BDRVQcowState
*s
= bs
->opaque
;
477 int index_in_cluster
;
479 uint64_t cluster_offset
;
481 QEMUIOVector hd_qiov
;
485 if (qiov
->niov
> 1) {
486 buf
= orig_buf
= qemu_blockalign(bs
, qiov
->size
);
489 buf
= (uint8_t *)qiov
->iov
->iov_base
;
492 qemu_co_mutex_lock(&s
->lock
);
494 while (nb_sectors
!= 0) {
495 /* prepare next request */
496 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9,
498 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
499 n
= s
->cluster_sectors
- index_in_cluster
;
500 if (n
> nb_sectors
) {
504 if (!cluster_offset
) {
505 if (bs
->backing_hd
) {
506 /* read from the base image */
507 hd_iov
.iov_base
= (void *)buf
;
508 hd_iov
.iov_len
= n
* 512;
509 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
510 qemu_co_mutex_unlock(&s
->lock
);
511 ret
= bdrv_co_readv(bs
->backing_hd
, sector_num
,
513 qemu_co_mutex_lock(&s
->lock
);
518 /* Note: in this case, no need to wait */
519 memset(buf
, 0, 512 * n
);
521 } else if (cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
522 /* add AIO support for compressed blocks ? */
523 if (decompress_cluster(bs
, cluster_offset
) < 0) {
527 s
->cluster_cache
+ index_in_cluster
* 512, 512 * n
);
529 if ((cluster_offset
& 511) != 0) {
532 hd_iov
.iov_base
= (void *)buf
;
533 hd_iov
.iov_len
= n
* 512;
534 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
535 qemu_co_mutex_unlock(&s
->lock
);
536 ret
= bdrv_co_readv(bs
->file
,
537 (cluster_offset
>> 9) + index_in_cluster
,
539 qemu_co_mutex_lock(&s
->lock
);
543 if (s
->crypt_method
) {
544 encrypt_sectors(s
, sector_num
, buf
, buf
,
546 &s
->aes_decrypt_key
);
557 qemu_co_mutex_unlock(&s
->lock
);
559 if (qiov
->niov
> 1) {
560 qemu_iovec_from_buf(qiov
, 0, orig_buf
, qiov
->size
);
561 qemu_vfree(orig_buf
);
571 static coroutine_fn
int qcow_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
572 int nb_sectors
, QEMUIOVector
*qiov
)
574 BDRVQcowState
*s
= bs
->opaque
;
575 int index_in_cluster
;
576 uint64_t cluster_offset
;
577 const uint8_t *src_buf
;
579 uint8_t *cluster_data
= NULL
;
581 QEMUIOVector hd_qiov
;
585 s
->cluster_cache_offset
= -1; /* disable compressed cache */
587 if (qiov
->niov
> 1) {
588 buf
= orig_buf
= qemu_blockalign(bs
, qiov
->size
);
589 qemu_iovec_to_buf(qiov
, 0, buf
, qiov
->size
);
592 buf
= (uint8_t *)qiov
->iov
->iov_base
;
595 qemu_co_mutex_lock(&s
->lock
);
597 while (nb_sectors
!= 0) {
599 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
600 n
= s
->cluster_sectors
- index_in_cluster
;
601 if (n
> nb_sectors
) {
604 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 1, 0,
606 index_in_cluster
+ n
);
607 if (!cluster_offset
|| (cluster_offset
& 511) != 0) {
611 if (s
->crypt_method
) {
613 cluster_data
= g_malloc0(s
->cluster_size
);
615 encrypt_sectors(s
, sector_num
, cluster_data
, buf
,
616 n
, 1, &s
->aes_encrypt_key
);
617 src_buf
= cluster_data
;
622 hd_iov
.iov_base
= (void *)src_buf
;
623 hd_iov
.iov_len
= n
* 512;
624 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
625 qemu_co_mutex_unlock(&s
->lock
);
626 ret
= bdrv_co_writev(bs
->file
,
627 (cluster_offset
>> 9) + index_in_cluster
,
629 qemu_co_mutex_lock(&s
->lock
);
639 qemu_co_mutex_unlock(&s
->lock
);
641 if (qiov
->niov
> 1) {
642 qemu_vfree(orig_buf
);
644 g_free(cluster_data
);
649 static void qcow_close(BlockDriverState
*bs
)
651 BDRVQcowState
*s
= bs
->opaque
;
655 g_free(s
->cluster_cache
);
656 g_free(s
->cluster_data
);
658 migrate_del_blocker(s
->migration_blocker
);
659 error_free(s
->migration_blocker
);
662 static int qcow_create(const char *filename
, QEMUOptionParameter
*options
,
665 int header_size
, backing_filename_len
, l1_size
, shift
, i
;
668 int64_t total_size
= 0;
669 const char *backing_file
= NULL
;
671 Error
*local_err
= NULL
;
673 BlockDriverState
*qcow_bs
;
675 /* Read out options */
676 while (options
&& options
->name
) {
677 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
678 total_size
= options
->value
.n
/ 512;
679 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
680 backing_file
= options
->value
.s
;
681 } else if (!strcmp(options
->name
, BLOCK_OPT_ENCRYPT
)) {
682 flags
|= options
->value
.n
? BLOCK_FLAG_ENCRYPT
: 0;
687 ret
= bdrv_create_file(filename
, options
, &local_err
);
689 qerror_report_err(local_err
);
690 error_free(local_err
);
694 ret
= bdrv_file_open(&qcow_bs
, filename
, NULL
, BDRV_O_RDWR
, &local_err
);
696 qerror_report_err(local_err
);
697 error_free(local_err
);
701 ret
= bdrv_truncate(qcow_bs
, 0);
706 memset(&header
, 0, sizeof(header
));
707 header
.magic
= cpu_to_be32(QCOW_MAGIC
);
708 header
.version
= cpu_to_be32(QCOW_VERSION
);
709 header
.size
= cpu_to_be64(total_size
* 512);
710 header_size
= sizeof(header
);
711 backing_filename_len
= 0;
713 if (strcmp(backing_file
, "fat:")) {
714 header
.backing_file_offset
= cpu_to_be64(header_size
);
715 backing_filename_len
= strlen(backing_file
);
716 header
.backing_file_size
= cpu_to_be32(backing_filename_len
);
717 header_size
+= backing_filename_len
;
719 /* special backing file for vvfat */
722 header
.cluster_bits
= 9; /* 512 byte cluster to avoid copying
723 unmodifyed sectors */
724 header
.l2_bits
= 12; /* 32 KB L2 tables */
726 header
.cluster_bits
= 12; /* 4 KB clusters */
727 header
.l2_bits
= 9; /* 4 KB L2 tables */
729 header_size
= (header_size
+ 7) & ~7;
730 shift
= header
.cluster_bits
+ header
.l2_bits
;
731 l1_size
= ((total_size
* 512) + (1LL << shift
) - 1) >> shift
;
733 header
.l1_table_offset
= cpu_to_be64(header_size
);
734 if (flags
& BLOCK_FLAG_ENCRYPT
) {
735 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
737 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
740 /* write all the data */
741 ret
= bdrv_pwrite(qcow_bs
, 0, &header
, sizeof(header
));
742 if (ret
!= sizeof(header
)) {
747 ret
= bdrv_pwrite(qcow_bs
, sizeof(header
),
748 backing_file
, backing_filename_len
);
749 if (ret
!= backing_filename_len
) {
754 tmp
= g_malloc0(BDRV_SECTOR_SIZE
);
755 for (i
= 0; i
< ((sizeof(uint64_t)*l1_size
+ BDRV_SECTOR_SIZE
- 1)/
756 BDRV_SECTOR_SIZE
); i
++) {
757 ret
= bdrv_pwrite(qcow_bs
, header_size
+
758 BDRV_SECTOR_SIZE
*i
, tmp
, BDRV_SECTOR_SIZE
);
759 if (ret
!= BDRV_SECTOR_SIZE
) {
772 static int qcow_make_empty(BlockDriverState
*bs
)
774 BDRVQcowState
*s
= bs
->opaque
;
775 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
778 memset(s
->l1_table
, 0, l1_length
);
779 if (bdrv_pwrite_sync(bs
->file
, s
->l1_table_offset
, s
->l1_table
,
782 ret
= bdrv_truncate(bs
->file
, s
->l1_table_offset
+ l1_length
);
786 memset(s
->l2_cache
, 0, s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
787 memset(s
->l2_cache_offsets
, 0, L2_CACHE_SIZE
* sizeof(uint64_t));
788 memset(s
->l2_cache_counts
, 0, L2_CACHE_SIZE
* sizeof(uint32_t));
793 /* XXX: put compressed sectors first, then all the cluster aligned
794 tables to avoid losing bytes in alignment */
795 static int qcow_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
796 const uint8_t *buf
, int nb_sectors
)
798 BDRVQcowState
*s
= bs
->opaque
;
802 uint64_t cluster_offset
;
804 if (nb_sectors
!= s
->cluster_sectors
) {
807 /* Zero-pad last write if image size is not cluster aligned */
808 if (sector_num
+ nb_sectors
== bs
->total_sectors
&&
809 nb_sectors
< s
->cluster_sectors
) {
810 uint8_t *pad_buf
= qemu_blockalign(bs
, s
->cluster_size
);
811 memset(pad_buf
, 0, s
->cluster_size
);
812 memcpy(pad_buf
, buf
, nb_sectors
* BDRV_SECTOR_SIZE
);
813 ret
= qcow_write_compressed(bs
, sector_num
,
814 pad_buf
, s
->cluster_sectors
);
820 out_buf
= g_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
822 /* best compression, small window, no zlib header */
823 memset(&strm
, 0, sizeof(strm
));
824 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
826 9, Z_DEFAULT_STRATEGY
);
832 strm
.avail_in
= s
->cluster_size
;
833 strm
.next_in
= (uint8_t *)buf
;
834 strm
.avail_out
= s
->cluster_size
;
835 strm
.next_out
= out_buf
;
837 ret
= deflate(&strm
, Z_FINISH
);
838 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
843 out_len
= strm
.next_out
- out_buf
;
847 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
848 /* could not compress: write normal cluster */
849 ret
= bdrv_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
854 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 2,
856 if (cluster_offset
== 0) {
861 cluster_offset
&= s
->cluster_offset_mask
;
862 ret
= bdrv_pwrite(bs
->file
, cluster_offset
, out_buf
, out_len
);
874 static int qcow_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
876 BDRVQcowState
*s
= bs
->opaque
;
877 bdi
->cluster_size
= s
->cluster_size
;
882 static QEMUOptionParameter qcow_create_options
[] = {
884 .name
= BLOCK_OPT_SIZE
,
886 .help
= "Virtual disk size"
889 .name
= BLOCK_OPT_BACKING_FILE
,
891 .help
= "File name of a base image"
894 .name
= BLOCK_OPT_ENCRYPT
,
896 .help
= "Encrypt the image"
901 static BlockDriver bdrv_qcow
= {
902 .format_name
= "qcow",
903 .instance_size
= sizeof(BDRVQcowState
),
904 .bdrv_probe
= qcow_probe
,
905 .bdrv_open
= qcow_open
,
906 .bdrv_close
= qcow_close
,
907 .bdrv_reopen_prepare
= qcow_reopen_prepare
,
908 .bdrv_create
= qcow_create
,
909 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
911 .bdrv_co_readv
= qcow_co_readv
,
912 .bdrv_co_writev
= qcow_co_writev
,
913 .bdrv_co_get_block_status
= qcow_co_get_block_status
,
915 .bdrv_set_key
= qcow_set_key
,
916 .bdrv_make_empty
= qcow_make_empty
,
917 .bdrv_write_compressed
= qcow_write_compressed
,
918 .bdrv_get_info
= qcow_get_info
,
920 .create_options
= qcow_create_options
,
923 static void bdrv_qcow_init(void)
925 bdrv_register(&bdrv_qcow
);
928 block_init(bdrv_qcow_init
);