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_int.h"
29 #include "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
, int flags
)
97 BDRVQcowState
*s
= bs
->opaque
;
101 if (bdrv_pread(bs
->file
, 0, &header
, sizeof(header
)) != sizeof(header
))
103 be32_to_cpus(&header
.magic
);
104 be32_to_cpus(&header
.version
);
105 be64_to_cpus(&header
.backing_file_offset
);
106 be32_to_cpus(&header
.backing_file_size
);
107 be32_to_cpus(&header
.mtime
);
108 be64_to_cpus(&header
.size
);
109 be32_to_cpus(&header
.crypt_method
);
110 be64_to_cpus(&header
.l1_table_offset
);
112 if (header
.magic
!= QCOW_MAGIC
|| header
.version
!= QCOW_VERSION
)
114 if (header
.size
<= 1 || header
.cluster_bits
< 9)
116 if (header
.crypt_method
> QCOW_CRYPT_AES
)
118 s
->crypt_method_header
= header
.crypt_method
;
119 if (s
->crypt_method_header
)
121 s
->cluster_bits
= header
.cluster_bits
;
122 s
->cluster_size
= 1 << s
->cluster_bits
;
123 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
124 s
->l2_bits
= header
.l2_bits
;
125 s
->l2_size
= 1 << s
->l2_bits
;
126 bs
->total_sectors
= header
.size
/ 512;
127 s
->cluster_offset_mask
= (1LL << (63 - s
->cluster_bits
)) - 1;
129 /* read the level 1 table */
130 shift
= s
->cluster_bits
+ s
->l2_bits
;
131 s
->l1_size
= (header
.size
+ (1LL << shift
) - 1) >> shift
;
133 s
->l1_table_offset
= header
.l1_table_offset
;
134 s
->l1_table
= g_malloc(s
->l1_size
* sizeof(uint64_t));
137 if (bdrv_pread(bs
->file
, s
->l1_table_offset
, s
->l1_table
, s
->l1_size
* sizeof(uint64_t)) !=
138 s
->l1_size
* sizeof(uint64_t))
140 for(i
= 0;i
< s
->l1_size
; i
++) {
141 be64_to_cpus(&s
->l1_table
[i
]);
144 s
->l2_cache
= g_malloc(s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
147 s
->cluster_cache
= g_malloc(s
->cluster_size
);
148 if (!s
->cluster_cache
)
150 s
->cluster_data
= g_malloc(s
->cluster_size
);
151 if (!s
->cluster_data
)
153 s
->cluster_cache_offset
= -1;
155 /* read the backing file name */
156 if (header
.backing_file_offset
!= 0) {
157 len
= header
.backing_file_size
;
160 if (bdrv_pread(bs
->file
, header
.backing_file_offset
, bs
->backing_file
, len
) != len
)
162 bs
->backing_file
[len
] = '\0';
165 /* Disable migration when qcow images are used */
166 error_set(&s
->migration_blocker
,
167 QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED
,
168 "qcow", bs
->device_name
, "live migration");
169 migrate_add_blocker(s
->migration_blocker
);
171 qemu_co_mutex_init(&s
->lock
);
177 g_free(s
->cluster_cache
);
178 g_free(s
->cluster_data
);
182 static int qcow_set_key(BlockDriverState
*bs
, const char *key
)
184 BDRVQcowState
*s
= bs
->opaque
;
188 memset(keybuf
, 0, 16);
192 /* XXX: we could compress the chars to 7 bits to increase
194 for(i
= 0;i
< len
;i
++) {
197 s
->crypt_method
= s
->crypt_method_header
;
199 if (AES_set_encrypt_key(keybuf
, 128, &s
->aes_encrypt_key
) != 0)
201 if (AES_set_decrypt_key(keybuf
, 128, &s
->aes_decrypt_key
) != 0)
206 /* The crypt function is compatible with the linux cryptoloop
207 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
209 static void encrypt_sectors(BDRVQcowState
*s
, int64_t sector_num
,
210 uint8_t *out_buf
, const uint8_t *in_buf
,
211 int nb_sectors
, int enc
,
220 for(i
= 0; i
< nb_sectors
; i
++) {
221 ivec
.ll
[0] = cpu_to_le64(sector_num
);
223 AES_cbc_encrypt(in_buf
, out_buf
, 512, key
,
235 * 1 to allocate a normal cluster (for sector indexes 'n_start' to
238 * 2 to allocate a compressed cluster of size
239 * 'compressed_size'. 'compressed_size' must be > 0 and <
242 * return 0 if not allocated.
244 static uint64_t get_cluster_offset(BlockDriverState
*bs
,
245 uint64_t offset
, int allocate
,
247 int n_start
, int n_end
)
249 BDRVQcowState
*s
= bs
->opaque
;
250 int min_index
, i
, j
, l1_index
, l2_index
;
251 uint64_t l2_offset
, *l2_table
, cluster_offset
, tmp
;
255 l1_index
= offset
>> (s
->l2_bits
+ s
->cluster_bits
);
256 l2_offset
= s
->l1_table
[l1_index
];
261 /* allocate a new l2 entry */
262 l2_offset
= bdrv_getlength(bs
->file
);
263 /* round to cluster size */
264 l2_offset
= (l2_offset
+ s
->cluster_size
- 1) & ~(s
->cluster_size
- 1);
265 /* update the L1 entry */
266 s
->l1_table
[l1_index
] = l2_offset
;
267 tmp
= cpu_to_be64(l2_offset
);
268 if (bdrv_pwrite_sync(bs
->file
,
269 s
->l1_table_offset
+ l1_index
* sizeof(tmp
),
270 &tmp
, sizeof(tmp
)) < 0)
274 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
275 if (l2_offset
== s
->l2_cache_offsets
[i
]) {
276 /* increment the hit count */
277 if (++s
->l2_cache_counts
[i
] == 0xffffffff) {
278 for(j
= 0; j
< L2_CACHE_SIZE
; j
++) {
279 s
->l2_cache_counts
[j
] >>= 1;
282 l2_table
= s
->l2_cache
+ (i
<< s
->l2_bits
);
286 /* not found: load a new entry in the least used one */
288 min_count
= 0xffffffff;
289 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
290 if (s
->l2_cache_counts
[i
] < min_count
) {
291 min_count
= s
->l2_cache_counts
[i
];
295 l2_table
= s
->l2_cache
+ (min_index
<< s
->l2_bits
);
297 memset(l2_table
, 0, s
->l2_size
* sizeof(uint64_t));
298 if (bdrv_pwrite_sync(bs
->file
, l2_offset
, l2_table
,
299 s
->l2_size
* sizeof(uint64_t)) < 0)
302 if (bdrv_pread(bs
->file
, l2_offset
, l2_table
, s
->l2_size
* sizeof(uint64_t)) !=
303 s
->l2_size
* sizeof(uint64_t))
306 s
->l2_cache_offsets
[min_index
] = l2_offset
;
307 s
->l2_cache_counts
[min_index
] = 1;
309 l2_index
= (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
310 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
311 if (!cluster_offset
||
312 ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) && allocate
== 1)) {
315 /* allocate a new cluster */
316 if ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) &&
317 (n_end
- n_start
) < s
->cluster_sectors
) {
318 /* if the cluster is already compressed, we must
319 decompress it in the case it is not completely
321 if (decompress_cluster(bs
, cluster_offset
) < 0)
323 cluster_offset
= bdrv_getlength(bs
->file
);
324 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
325 ~(s
->cluster_size
- 1);
326 /* write the cluster content */
327 if (bdrv_pwrite(bs
->file
, cluster_offset
, s
->cluster_cache
, s
->cluster_size
) !=
331 cluster_offset
= bdrv_getlength(bs
->file
);
333 /* round to cluster size */
334 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
335 ~(s
->cluster_size
- 1);
336 bdrv_truncate(bs
->file
, cluster_offset
+ s
->cluster_size
);
337 /* if encrypted, we must initialize the cluster
338 content which won't be written */
339 if (s
->crypt_method
&&
340 (n_end
- n_start
) < s
->cluster_sectors
) {
342 start_sect
= (offset
& ~(s
->cluster_size
- 1)) >> 9;
343 memset(s
->cluster_data
+ 512, 0x00, 512);
344 for(i
= 0; i
< s
->cluster_sectors
; i
++) {
345 if (i
< n_start
|| i
>= n_end
) {
346 encrypt_sectors(s
, start_sect
+ i
,
348 s
->cluster_data
+ 512, 1, 1,
349 &s
->aes_encrypt_key
);
350 if (bdrv_pwrite(bs
->file
, cluster_offset
+ i
* 512,
351 s
->cluster_data
, 512) != 512)
356 } else if (allocate
== 2) {
357 cluster_offset
|= QCOW_OFLAG_COMPRESSED
|
358 (uint64_t)compressed_size
<< (63 - s
->cluster_bits
);
361 /* update L2 table */
362 tmp
= cpu_to_be64(cluster_offset
);
363 l2_table
[l2_index
] = tmp
;
364 if (bdrv_pwrite_sync(bs
->file
, l2_offset
+ l2_index
* sizeof(tmp
),
365 &tmp
, sizeof(tmp
)) < 0)
368 return cluster_offset
;
371 static int qcow_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
372 int nb_sectors
, int *pnum
)
374 BDRVQcowState
*s
= bs
->opaque
;
375 int index_in_cluster
, n
;
376 uint64_t cluster_offset
;
378 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 0, 0, 0, 0);
379 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
380 n
= s
->cluster_sectors
- index_in_cluster
;
384 return (cluster_offset
!= 0);
387 static int decompress_buffer(uint8_t *out_buf
, int out_buf_size
,
388 const uint8_t *buf
, int buf_size
)
390 z_stream strm1
, *strm
= &strm1
;
393 memset(strm
, 0, sizeof(*strm
));
395 strm
->next_in
= (uint8_t *)buf
;
396 strm
->avail_in
= buf_size
;
397 strm
->next_out
= out_buf
;
398 strm
->avail_out
= out_buf_size
;
400 ret
= inflateInit2(strm
, -12);
403 ret
= inflate(strm
, Z_FINISH
);
404 out_len
= strm
->next_out
- out_buf
;
405 if ((ret
!= Z_STREAM_END
&& ret
!= Z_BUF_ERROR
) ||
406 out_len
!= out_buf_size
) {
414 static int decompress_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
)
416 BDRVQcowState
*s
= bs
->opaque
;
420 coffset
= cluster_offset
& s
->cluster_offset_mask
;
421 if (s
->cluster_cache_offset
!= coffset
) {
422 csize
= cluster_offset
>> (63 - s
->cluster_bits
);
423 csize
&= (s
->cluster_size
- 1);
424 ret
= bdrv_pread(bs
->file
, coffset
, s
->cluster_data
, csize
);
427 if (decompress_buffer(s
->cluster_cache
, s
->cluster_size
,
428 s
->cluster_data
, csize
) < 0) {
431 s
->cluster_cache_offset
= coffset
;
436 static int qcow_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
437 int nb_sectors
, QEMUIOVector
*qiov
)
439 BDRVQcowState
*s
= bs
->opaque
;
440 int index_in_cluster
;
442 uint64_t cluster_offset
;
444 QEMUIOVector hd_qiov
;
448 if (qiov
->niov
> 1) {
449 buf
= orig_buf
= qemu_blockalign(bs
, qiov
->size
);
452 buf
= (uint8_t *)qiov
->iov
->iov_base
;
455 qemu_co_mutex_lock(&s
->lock
);
457 while (nb_sectors
!= 0) {
458 /* prepare next request */
459 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9,
461 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
462 n
= s
->cluster_sectors
- index_in_cluster
;
463 if (n
> nb_sectors
) {
467 if (!cluster_offset
) {
468 if (bs
->backing_hd
) {
469 /* read from the base image */
470 hd_iov
.iov_base
= (void *)buf
;
471 hd_iov
.iov_len
= n
* 512;
472 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
473 qemu_co_mutex_unlock(&s
->lock
);
474 ret
= bdrv_co_readv(bs
->backing_hd
, sector_num
,
476 qemu_co_mutex_lock(&s
->lock
);
481 /* Note: in this case, no need to wait */
482 memset(buf
, 0, 512 * n
);
484 } else if (cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
485 /* add AIO support for compressed blocks ? */
486 if (decompress_cluster(bs
, cluster_offset
) < 0) {
490 s
->cluster_cache
+ index_in_cluster
* 512, 512 * n
);
492 if ((cluster_offset
& 511) != 0) {
495 hd_iov
.iov_base
= (void *)buf
;
496 hd_iov
.iov_len
= n
* 512;
497 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
498 qemu_co_mutex_unlock(&s
->lock
);
499 ret
= bdrv_co_readv(bs
->file
,
500 (cluster_offset
>> 9) + index_in_cluster
,
502 qemu_co_mutex_lock(&s
->lock
);
506 if (s
->crypt_method
) {
507 encrypt_sectors(s
, sector_num
, buf
, buf
,
509 &s
->aes_decrypt_key
);
520 qemu_co_mutex_unlock(&s
->lock
);
522 if (qiov
->niov
> 1) {
523 qemu_iovec_from_buffer(qiov
, orig_buf
, qiov
->size
);
524 qemu_vfree(orig_buf
);
534 static int qcow_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
535 int nb_sectors
, QEMUIOVector
*qiov
)
537 BDRVQcowState
*s
= bs
->opaque
;
538 int index_in_cluster
;
539 uint64_t cluster_offset
;
540 const uint8_t *src_buf
;
542 uint8_t *cluster_data
= NULL
;
544 QEMUIOVector hd_qiov
;
548 s
->cluster_cache_offset
= -1; /* disable compressed cache */
550 if (qiov
->niov
> 1) {
551 buf
= orig_buf
= qemu_blockalign(bs
, qiov
->size
);
552 qemu_iovec_to_buffer(qiov
, buf
);
555 buf
= (uint8_t *)qiov
->iov
->iov_base
;
558 qemu_co_mutex_lock(&s
->lock
);
560 while (nb_sectors
!= 0) {
562 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
563 n
= s
->cluster_sectors
- index_in_cluster
;
564 if (n
> nb_sectors
) {
567 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 1, 0,
569 index_in_cluster
+ n
);
570 if (!cluster_offset
|| (cluster_offset
& 511) != 0) {
574 if (s
->crypt_method
) {
576 cluster_data
= g_malloc0(s
->cluster_size
);
578 encrypt_sectors(s
, sector_num
, cluster_data
, buf
,
579 n
, 1, &s
->aes_encrypt_key
);
580 src_buf
= cluster_data
;
585 hd_iov
.iov_base
= (void *)src_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_writev(bs
->file
,
590 (cluster_offset
>> 9) + index_in_cluster
,
592 qemu_co_mutex_lock(&s
->lock
);
602 qemu_co_mutex_unlock(&s
->lock
);
604 if (qiov
->niov
> 1) {
605 qemu_vfree(orig_buf
);
607 g_free(cluster_data
);
612 static void qcow_close(BlockDriverState
*bs
)
614 BDRVQcowState
*s
= bs
->opaque
;
618 g_free(s
->cluster_cache
);
619 g_free(s
->cluster_data
);
621 migrate_del_blocker(s
->migration_blocker
);
622 error_free(s
->migration_blocker
);
625 static int qcow_create(const char *filename
, QEMUOptionParameter
*options
)
627 int fd
, header_size
, backing_filename_len
, l1_size
, i
, shift
;
630 int64_t total_size
= 0;
631 const char *backing_file
= NULL
;
635 /* Read out options */
636 while (options
&& options
->name
) {
637 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
638 total_size
= options
->value
.n
/ 512;
639 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
640 backing_file
= options
->value
.s
;
641 } else if (!strcmp(options
->name
, BLOCK_OPT_ENCRYPT
)) {
642 flags
|= options
->value
.n
? BLOCK_FLAG_ENCRYPT
: 0;
647 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0644);
650 memset(&header
, 0, sizeof(header
));
651 header
.magic
= cpu_to_be32(QCOW_MAGIC
);
652 header
.version
= cpu_to_be32(QCOW_VERSION
);
653 header
.size
= cpu_to_be64(total_size
* 512);
654 header_size
= sizeof(header
);
655 backing_filename_len
= 0;
657 if (strcmp(backing_file
, "fat:")) {
658 header
.backing_file_offset
= cpu_to_be64(header_size
);
659 backing_filename_len
= strlen(backing_file
);
660 header
.backing_file_size
= cpu_to_be32(backing_filename_len
);
661 header_size
+= backing_filename_len
;
663 /* special backing file for vvfat */
666 header
.cluster_bits
= 9; /* 512 byte cluster to avoid copying
667 unmodifyed sectors */
668 header
.l2_bits
= 12; /* 32 KB L2 tables */
670 header
.cluster_bits
= 12; /* 4 KB clusters */
671 header
.l2_bits
= 9; /* 4 KB L2 tables */
673 header_size
= (header_size
+ 7) & ~7;
674 shift
= header
.cluster_bits
+ header
.l2_bits
;
675 l1_size
= ((total_size
* 512) + (1LL << shift
) - 1) >> shift
;
677 header
.l1_table_offset
= cpu_to_be64(header_size
);
678 if (flags
& BLOCK_FLAG_ENCRYPT
) {
679 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
681 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
684 /* write all the data */
685 ret
= qemu_write_full(fd
, &header
, sizeof(header
));
686 if (ret
!= sizeof(header
)) {
692 ret
= qemu_write_full(fd
, backing_file
, backing_filename_len
);
693 if (ret
!= backing_filename_len
) {
699 lseek(fd
, header_size
, SEEK_SET
);
701 for(i
= 0;i
< l1_size
; i
++) {
702 ret
= qemu_write_full(fd
, &tmp
, sizeof(tmp
));
703 if (ret
!= sizeof(tmp
)) {
715 static int qcow_make_empty(BlockDriverState
*bs
)
717 BDRVQcowState
*s
= bs
->opaque
;
718 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
721 memset(s
->l1_table
, 0, l1_length
);
722 if (bdrv_pwrite_sync(bs
->file
, s
->l1_table_offset
, s
->l1_table
,
725 ret
= bdrv_truncate(bs
->file
, s
->l1_table_offset
+ l1_length
);
729 memset(s
->l2_cache
, 0, s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
730 memset(s
->l2_cache_offsets
, 0, L2_CACHE_SIZE
* sizeof(uint64_t));
731 memset(s
->l2_cache_counts
, 0, L2_CACHE_SIZE
* sizeof(uint32_t));
736 /* XXX: put compressed sectors first, then all the cluster aligned
737 tables to avoid losing bytes in alignment */
738 static int qcow_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
739 const uint8_t *buf
, int nb_sectors
)
741 BDRVQcowState
*s
= bs
->opaque
;
745 uint64_t cluster_offset
;
747 if (nb_sectors
!= s
->cluster_sectors
)
750 out_buf
= g_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
752 /* best compression, small window, no zlib header */
753 memset(&strm
, 0, sizeof(strm
));
754 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
756 9, Z_DEFAULT_STRATEGY
);
762 strm
.avail_in
= s
->cluster_size
;
763 strm
.next_in
= (uint8_t *)buf
;
764 strm
.avail_out
= s
->cluster_size
;
765 strm
.next_out
= out_buf
;
767 ret
= deflate(&strm
, Z_FINISH
);
768 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
773 out_len
= strm
.next_out
- out_buf
;
777 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
778 /* could not compress: write normal cluster */
779 ret
= bdrv_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
784 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 2,
786 if (cluster_offset
== 0) {
791 cluster_offset
&= s
->cluster_offset_mask
;
792 ret
= bdrv_pwrite(bs
->file
, cluster_offset
, out_buf
, out_len
);
804 static coroutine_fn
int qcow_co_flush(BlockDriverState
*bs
)
806 return bdrv_co_flush(bs
->file
);
809 static int qcow_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
811 BDRVQcowState
*s
= bs
->opaque
;
812 bdi
->cluster_size
= s
->cluster_size
;
817 static QEMUOptionParameter qcow_create_options
[] = {
819 .name
= BLOCK_OPT_SIZE
,
821 .help
= "Virtual disk size"
824 .name
= BLOCK_OPT_BACKING_FILE
,
826 .help
= "File name of a base image"
829 .name
= BLOCK_OPT_ENCRYPT
,
831 .help
= "Encrypt the image"
836 static BlockDriver bdrv_qcow
= {
837 .format_name
= "qcow",
838 .instance_size
= sizeof(BDRVQcowState
),
839 .bdrv_probe
= qcow_probe
,
840 .bdrv_open
= qcow_open
,
841 .bdrv_close
= qcow_close
,
842 .bdrv_create
= qcow_create
,
844 .bdrv_co_readv
= qcow_co_readv
,
845 .bdrv_co_writev
= qcow_co_writev
,
846 .bdrv_co_flush_to_disk
= qcow_co_flush
,
847 .bdrv_is_allocated
= qcow_is_allocated
,
849 .bdrv_set_key
= qcow_set_key
,
850 .bdrv_make_empty
= qcow_make_empty
,
851 .bdrv_write_compressed
= qcow_write_compressed
,
852 .bdrv_get_info
= qcow_get_info
,
854 .create_options
= qcow_create_options
,
857 static void bdrv_qcow_init(void)
859 bdrv_register(&bdrv_qcow
);
862 block_init(bdrv_qcow_init
);