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"
30 /**************************************************************/
31 /* QEMU COW block driver with compression and encryption support */
33 #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
34 #define QCOW_VERSION 1
36 #define QCOW_CRYPT_NONE 0
37 #define QCOW_CRYPT_AES 1
39 #define QCOW_OFLAG_COMPRESSED (1LL << 63)
41 typedef struct QCowHeader
{
44 uint64_t backing_file_offset
;
45 uint32_t backing_file_size
;
47 uint64_t size
; /* in bytes */
50 uint32_t crypt_method
;
51 uint64_t l1_table_offset
;
54 #define L2_CACHE_SIZE 16
56 typedef struct BDRVQcowState
{
63 uint64_t cluster_offset_mask
;
64 uint64_t l1_table_offset
;
67 uint64_t l2_cache_offsets
[L2_CACHE_SIZE
];
68 uint32_t l2_cache_counts
[L2_CACHE_SIZE
];
69 uint8_t *cluster_cache
;
70 uint8_t *cluster_data
;
71 uint64_t cluster_cache_offset
;
72 uint32_t crypt_method
; /* current crypt method, 0 if no key yet */
73 uint32_t crypt_method_header
;
74 AES_KEY aes_encrypt_key
;
75 AES_KEY aes_decrypt_key
;
79 static int decompress_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
);
81 static int qcow_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
83 const QCowHeader
*cow_header
= (const void *)buf
;
85 if (buf_size
>= sizeof(QCowHeader
) &&
86 be32_to_cpu(cow_header
->magic
) == QCOW_MAGIC
&&
87 be32_to_cpu(cow_header
->version
) == QCOW_VERSION
)
93 static int qcow_open(BlockDriverState
*bs
, int flags
)
95 BDRVQcowState
*s
= bs
->opaque
;
99 if (bdrv_pread(bs
->file
, 0, &header
, sizeof(header
)) != sizeof(header
))
101 be32_to_cpus(&header
.magic
);
102 be32_to_cpus(&header
.version
);
103 be64_to_cpus(&header
.backing_file_offset
);
104 be32_to_cpus(&header
.backing_file_size
);
105 be32_to_cpus(&header
.mtime
);
106 be64_to_cpus(&header
.size
);
107 be32_to_cpus(&header
.crypt_method
);
108 be64_to_cpus(&header
.l1_table_offset
);
110 if (header
.magic
!= QCOW_MAGIC
|| header
.version
!= QCOW_VERSION
)
112 if (header
.size
<= 1 || header
.cluster_bits
< 9)
114 if (header
.crypt_method
> QCOW_CRYPT_AES
)
116 s
->crypt_method_header
= header
.crypt_method
;
117 if (s
->crypt_method_header
)
119 s
->cluster_bits
= header
.cluster_bits
;
120 s
->cluster_size
= 1 << s
->cluster_bits
;
121 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
122 s
->l2_bits
= header
.l2_bits
;
123 s
->l2_size
= 1 << s
->l2_bits
;
124 bs
->total_sectors
= header
.size
/ 512;
125 s
->cluster_offset_mask
= (1LL << (63 - s
->cluster_bits
)) - 1;
127 /* read the level 1 table */
128 shift
= s
->cluster_bits
+ s
->l2_bits
;
129 s
->l1_size
= (header
.size
+ (1LL << shift
) - 1) >> shift
;
131 s
->l1_table_offset
= header
.l1_table_offset
;
132 s
->l1_table
= g_malloc(s
->l1_size
* sizeof(uint64_t));
135 if (bdrv_pread(bs
->file
, s
->l1_table_offset
, s
->l1_table
, s
->l1_size
* sizeof(uint64_t)) !=
136 s
->l1_size
* sizeof(uint64_t))
138 for(i
= 0;i
< s
->l1_size
; i
++) {
139 be64_to_cpus(&s
->l1_table
[i
]);
142 s
->l2_cache
= g_malloc(s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
145 s
->cluster_cache
= g_malloc(s
->cluster_size
);
146 if (!s
->cluster_cache
)
148 s
->cluster_data
= g_malloc(s
->cluster_size
);
149 if (!s
->cluster_data
)
151 s
->cluster_cache_offset
= -1;
153 /* read the backing file name */
154 if (header
.backing_file_offset
!= 0) {
155 len
= header
.backing_file_size
;
158 if (bdrv_pread(bs
->file
, header
.backing_file_offset
, bs
->backing_file
, len
) != len
)
160 bs
->backing_file
[len
] = '\0';
163 qemu_co_mutex_init(&s
->lock
);
169 g_free(s
->cluster_cache
);
170 g_free(s
->cluster_data
);
174 static int qcow_set_key(BlockDriverState
*bs
, const char *key
)
176 BDRVQcowState
*s
= bs
->opaque
;
180 memset(keybuf
, 0, 16);
184 /* XXX: we could compress the chars to 7 bits to increase
186 for(i
= 0;i
< len
;i
++) {
189 s
->crypt_method
= s
->crypt_method_header
;
191 if (AES_set_encrypt_key(keybuf
, 128, &s
->aes_encrypt_key
) != 0)
193 if (AES_set_decrypt_key(keybuf
, 128, &s
->aes_decrypt_key
) != 0)
198 /* The crypt function is compatible with the linux cryptoloop
199 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
201 static void encrypt_sectors(BDRVQcowState
*s
, int64_t sector_num
,
202 uint8_t *out_buf
, const uint8_t *in_buf
,
203 int nb_sectors
, int enc
,
212 for(i
= 0; i
< nb_sectors
; i
++) {
213 ivec
.ll
[0] = cpu_to_le64(sector_num
);
215 AES_cbc_encrypt(in_buf
, out_buf
, 512, key
,
227 * 1 to allocate a normal cluster (for sector indexes 'n_start' to
230 * 2 to allocate a compressed cluster of size
231 * 'compressed_size'. 'compressed_size' must be > 0 and <
234 * return 0 if not allocated.
236 static uint64_t get_cluster_offset(BlockDriverState
*bs
,
237 uint64_t offset
, int allocate
,
239 int n_start
, int n_end
)
241 BDRVQcowState
*s
= bs
->opaque
;
242 int min_index
, i
, j
, l1_index
, l2_index
;
243 uint64_t l2_offset
, *l2_table
, cluster_offset
, tmp
;
247 l1_index
= offset
>> (s
->l2_bits
+ s
->cluster_bits
);
248 l2_offset
= s
->l1_table
[l1_index
];
253 /* allocate a new l2 entry */
254 l2_offset
= bdrv_getlength(bs
->file
);
255 /* round to cluster size */
256 l2_offset
= (l2_offset
+ s
->cluster_size
- 1) & ~(s
->cluster_size
- 1);
257 /* update the L1 entry */
258 s
->l1_table
[l1_index
] = l2_offset
;
259 tmp
= cpu_to_be64(l2_offset
);
260 if (bdrv_pwrite_sync(bs
->file
,
261 s
->l1_table_offset
+ l1_index
* sizeof(tmp
),
262 &tmp
, sizeof(tmp
)) < 0)
266 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
267 if (l2_offset
== s
->l2_cache_offsets
[i
]) {
268 /* increment the hit count */
269 if (++s
->l2_cache_counts
[i
] == 0xffffffff) {
270 for(j
= 0; j
< L2_CACHE_SIZE
; j
++) {
271 s
->l2_cache_counts
[j
] >>= 1;
274 l2_table
= s
->l2_cache
+ (i
<< s
->l2_bits
);
278 /* not found: load a new entry in the least used one */
280 min_count
= 0xffffffff;
281 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
282 if (s
->l2_cache_counts
[i
] < min_count
) {
283 min_count
= s
->l2_cache_counts
[i
];
287 l2_table
= s
->l2_cache
+ (min_index
<< s
->l2_bits
);
289 memset(l2_table
, 0, s
->l2_size
* sizeof(uint64_t));
290 if (bdrv_pwrite_sync(bs
->file
, l2_offset
, l2_table
,
291 s
->l2_size
* sizeof(uint64_t)) < 0)
294 if (bdrv_pread(bs
->file
, l2_offset
, l2_table
, s
->l2_size
* sizeof(uint64_t)) !=
295 s
->l2_size
* sizeof(uint64_t))
298 s
->l2_cache_offsets
[min_index
] = l2_offset
;
299 s
->l2_cache_counts
[min_index
] = 1;
301 l2_index
= (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
302 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
303 if (!cluster_offset
||
304 ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) && allocate
== 1)) {
307 /* allocate a new cluster */
308 if ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) &&
309 (n_end
- n_start
) < s
->cluster_sectors
) {
310 /* if the cluster is already compressed, we must
311 decompress it in the case it is not completely
313 if (decompress_cluster(bs
, cluster_offset
) < 0)
315 cluster_offset
= bdrv_getlength(bs
->file
);
316 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
317 ~(s
->cluster_size
- 1);
318 /* write the cluster content */
319 if (bdrv_pwrite(bs
->file
, cluster_offset
, s
->cluster_cache
, s
->cluster_size
) !=
323 cluster_offset
= bdrv_getlength(bs
->file
);
325 /* round to cluster size */
326 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
327 ~(s
->cluster_size
- 1);
328 bdrv_truncate(bs
->file
, cluster_offset
+ s
->cluster_size
);
329 /* if encrypted, we must initialize the cluster
330 content which won't be written */
331 if (s
->crypt_method
&&
332 (n_end
- n_start
) < s
->cluster_sectors
) {
334 start_sect
= (offset
& ~(s
->cluster_size
- 1)) >> 9;
335 memset(s
->cluster_data
+ 512, 0x00, 512);
336 for(i
= 0; i
< s
->cluster_sectors
; i
++) {
337 if (i
< n_start
|| i
>= n_end
) {
338 encrypt_sectors(s
, start_sect
+ i
,
340 s
->cluster_data
+ 512, 1, 1,
341 &s
->aes_encrypt_key
);
342 if (bdrv_pwrite(bs
->file
, cluster_offset
+ i
* 512,
343 s
->cluster_data
, 512) != 512)
348 } else if (allocate
== 2) {
349 cluster_offset
|= QCOW_OFLAG_COMPRESSED
|
350 (uint64_t)compressed_size
<< (63 - s
->cluster_bits
);
353 /* update L2 table */
354 tmp
= cpu_to_be64(cluster_offset
);
355 l2_table
[l2_index
] = tmp
;
356 if (bdrv_pwrite_sync(bs
->file
, l2_offset
+ l2_index
* sizeof(tmp
),
357 &tmp
, sizeof(tmp
)) < 0)
360 return cluster_offset
;
363 static int qcow_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
364 int nb_sectors
, int *pnum
)
366 BDRVQcowState
*s
= bs
->opaque
;
367 int index_in_cluster
, n
;
368 uint64_t cluster_offset
;
370 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 0, 0, 0, 0);
371 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
372 n
= s
->cluster_sectors
- index_in_cluster
;
376 return (cluster_offset
!= 0);
379 static int decompress_buffer(uint8_t *out_buf
, int out_buf_size
,
380 const uint8_t *buf
, int buf_size
)
382 z_stream strm1
, *strm
= &strm1
;
385 memset(strm
, 0, sizeof(*strm
));
387 strm
->next_in
= (uint8_t *)buf
;
388 strm
->avail_in
= buf_size
;
389 strm
->next_out
= out_buf
;
390 strm
->avail_out
= out_buf_size
;
392 ret
= inflateInit2(strm
, -12);
395 ret
= inflate(strm
, Z_FINISH
);
396 out_len
= strm
->next_out
- out_buf
;
397 if ((ret
!= Z_STREAM_END
&& ret
!= Z_BUF_ERROR
) ||
398 out_len
!= out_buf_size
) {
406 static int decompress_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
)
408 BDRVQcowState
*s
= bs
->opaque
;
412 coffset
= cluster_offset
& s
->cluster_offset_mask
;
413 if (s
->cluster_cache_offset
!= coffset
) {
414 csize
= cluster_offset
>> (63 - s
->cluster_bits
);
415 csize
&= (s
->cluster_size
- 1);
416 ret
= bdrv_pread(bs
->file
, coffset
, s
->cluster_data
, csize
);
419 if (decompress_buffer(s
->cluster_cache
, s
->cluster_size
,
420 s
->cluster_data
, csize
) < 0) {
423 s
->cluster_cache_offset
= coffset
;
428 static int qcow_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
429 int nb_sectors
, QEMUIOVector
*qiov
)
431 BDRVQcowState
*s
= bs
->opaque
;
432 int index_in_cluster
;
434 uint64_t cluster_offset
;
436 QEMUIOVector hd_qiov
;
440 if (qiov
->niov
> 1) {
441 buf
= orig_buf
= qemu_blockalign(bs
, qiov
->size
);
444 buf
= (uint8_t *)qiov
->iov
->iov_base
;
447 qemu_co_mutex_lock(&s
->lock
);
449 while (nb_sectors
!= 0) {
450 /* prepare next request */
451 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9,
453 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
454 n
= s
->cluster_sectors
- index_in_cluster
;
455 if (n
> nb_sectors
) {
459 if (!cluster_offset
) {
460 if (bs
->backing_hd
) {
461 /* read from the base image */
462 hd_iov
.iov_base
= (void *)buf
;
463 hd_iov
.iov_len
= n
* 512;
464 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
465 qemu_co_mutex_unlock(&s
->lock
);
466 ret
= bdrv_co_readv(bs
->backing_hd
, sector_num
,
468 qemu_co_mutex_lock(&s
->lock
);
473 /* Note: in this case, no need to wait */
474 memset(buf
, 0, 512 * n
);
476 } else if (cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
477 /* add AIO support for compressed blocks ? */
478 if (decompress_cluster(bs
, cluster_offset
) < 0) {
482 s
->cluster_cache
+ index_in_cluster
* 512, 512 * n
);
484 if ((cluster_offset
& 511) != 0) {
487 hd_iov
.iov_base
= (void *)buf
;
488 hd_iov
.iov_len
= n
* 512;
489 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
490 qemu_co_mutex_unlock(&s
->lock
);
491 ret
= bdrv_co_readv(bs
->file
,
492 (cluster_offset
>> 9) + index_in_cluster
,
494 qemu_co_mutex_lock(&s
->lock
);
498 if (s
->crypt_method
) {
499 encrypt_sectors(s
, sector_num
, buf
, buf
,
501 &s
->aes_decrypt_key
);
512 qemu_co_mutex_unlock(&s
->lock
);
514 if (qiov
->niov
> 1) {
515 qemu_iovec_from_buffer(qiov
, orig_buf
, qiov
->size
);
516 qemu_vfree(orig_buf
);
526 static int qcow_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
527 int nb_sectors
, QEMUIOVector
*qiov
)
529 BDRVQcowState
*s
= bs
->opaque
;
530 int index_in_cluster
;
531 uint64_t cluster_offset
;
532 const uint8_t *src_buf
;
534 uint8_t *cluster_data
= NULL
;
536 QEMUIOVector hd_qiov
;
540 s
->cluster_cache_offset
= -1; /* disable compressed cache */
542 if (qiov
->niov
> 1) {
543 buf
= orig_buf
= qemu_blockalign(bs
, qiov
->size
);
544 qemu_iovec_to_buffer(qiov
, buf
);
547 buf
= (uint8_t *)qiov
->iov
->iov_base
;
550 qemu_co_mutex_lock(&s
->lock
);
552 while (nb_sectors
!= 0) {
554 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
555 n
= s
->cluster_sectors
- index_in_cluster
;
556 if (n
> nb_sectors
) {
559 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 1, 0,
561 index_in_cluster
+ n
);
562 if (!cluster_offset
|| (cluster_offset
& 511) != 0) {
566 if (s
->crypt_method
) {
568 cluster_data
= g_malloc0(s
->cluster_size
);
570 encrypt_sectors(s
, sector_num
, cluster_data
, buf
,
571 n
, 1, &s
->aes_encrypt_key
);
572 src_buf
= cluster_data
;
577 hd_iov
.iov_base
= (void *)src_buf
;
578 hd_iov
.iov_len
= n
* 512;
579 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
580 qemu_co_mutex_unlock(&s
->lock
);
581 ret
= bdrv_co_writev(bs
->file
,
582 (cluster_offset
>> 9) + index_in_cluster
,
584 qemu_co_mutex_lock(&s
->lock
);
594 qemu_co_mutex_unlock(&s
->lock
);
596 if (qiov
->niov
> 1) {
597 qemu_vfree(orig_buf
);
599 g_free(cluster_data
);
604 static void qcow_close(BlockDriverState
*bs
)
606 BDRVQcowState
*s
= bs
->opaque
;
609 g_free(s
->cluster_cache
);
610 g_free(s
->cluster_data
);
613 static int qcow_create(const char *filename
, QEMUOptionParameter
*options
)
615 int fd
, header_size
, backing_filename_len
, l1_size
, i
, shift
;
618 int64_t total_size
= 0;
619 const char *backing_file
= NULL
;
623 /* Read out options */
624 while (options
&& options
->name
) {
625 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
626 total_size
= options
->value
.n
/ 512;
627 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
628 backing_file
= options
->value
.s
;
629 } else if (!strcmp(options
->name
, BLOCK_OPT_ENCRYPT
)) {
630 flags
|= options
->value
.n
? BLOCK_FLAG_ENCRYPT
: 0;
635 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0644);
638 memset(&header
, 0, sizeof(header
));
639 header
.magic
= cpu_to_be32(QCOW_MAGIC
);
640 header
.version
= cpu_to_be32(QCOW_VERSION
);
641 header
.size
= cpu_to_be64(total_size
* 512);
642 header_size
= sizeof(header
);
643 backing_filename_len
= 0;
645 if (strcmp(backing_file
, "fat:")) {
646 header
.backing_file_offset
= cpu_to_be64(header_size
);
647 backing_filename_len
= strlen(backing_file
);
648 header
.backing_file_size
= cpu_to_be32(backing_filename_len
);
649 header_size
+= backing_filename_len
;
651 /* special backing file for vvfat */
654 header
.cluster_bits
= 9; /* 512 byte cluster to avoid copying
655 unmodifyed sectors */
656 header
.l2_bits
= 12; /* 32 KB L2 tables */
658 header
.cluster_bits
= 12; /* 4 KB clusters */
659 header
.l2_bits
= 9; /* 4 KB L2 tables */
661 header_size
= (header_size
+ 7) & ~7;
662 shift
= header
.cluster_bits
+ header
.l2_bits
;
663 l1_size
= ((total_size
* 512) + (1LL << shift
) - 1) >> shift
;
665 header
.l1_table_offset
= cpu_to_be64(header_size
);
666 if (flags
& BLOCK_FLAG_ENCRYPT
) {
667 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
669 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
672 /* write all the data */
673 ret
= qemu_write_full(fd
, &header
, sizeof(header
));
674 if (ret
!= sizeof(header
)) {
680 ret
= qemu_write_full(fd
, backing_file
, backing_filename_len
);
681 if (ret
!= backing_filename_len
) {
687 lseek(fd
, header_size
, SEEK_SET
);
689 for(i
= 0;i
< l1_size
; i
++) {
690 ret
= qemu_write_full(fd
, &tmp
, sizeof(tmp
));
691 if (ret
!= sizeof(tmp
)) {
703 static int qcow_make_empty(BlockDriverState
*bs
)
705 BDRVQcowState
*s
= bs
->opaque
;
706 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
709 memset(s
->l1_table
, 0, l1_length
);
710 if (bdrv_pwrite_sync(bs
->file
, s
->l1_table_offset
, s
->l1_table
,
713 ret
= bdrv_truncate(bs
->file
, s
->l1_table_offset
+ l1_length
);
717 memset(s
->l2_cache
, 0, s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
718 memset(s
->l2_cache_offsets
, 0, L2_CACHE_SIZE
* sizeof(uint64_t));
719 memset(s
->l2_cache_counts
, 0, L2_CACHE_SIZE
* sizeof(uint32_t));
724 /* XXX: put compressed sectors first, then all the cluster aligned
725 tables to avoid losing bytes in alignment */
726 static int qcow_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
727 const uint8_t *buf
, int nb_sectors
)
729 BDRVQcowState
*s
= bs
->opaque
;
733 uint64_t cluster_offset
;
735 if (nb_sectors
!= s
->cluster_sectors
)
738 out_buf
= g_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
742 /* best compression, small window, no zlib header */
743 memset(&strm
, 0, sizeof(strm
));
744 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
746 9, Z_DEFAULT_STRATEGY
);
752 strm
.avail_in
= s
->cluster_size
;
753 strm
.next_in
= (uint8_t *)buf
;
754 strm
.avail_out
= s
->cluster_size
;
755 strm
.next_out
= out_buf
;
757 ret
= deflate(&strm
, Z_FINISH
);
758 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
763 out_len
= strm
.next_out
- out_buf
;
767 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
768 /* could not compress: write normal cluster */
769 bdrv_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
771 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 2,
773 cluster_offset
&= s
->cluster_offset_mask
;
774 if (bdrv_pwrite(bs
->file
, cluster_offset
, out_buf
, out_len
) != out_len
) {
784 static int qcow_flush(BlockDriverState
*bs
)
786 return bdrv_flush(bs
->file
);
789 static BlockDriverAIOCB
*qcow_aio_flush(BlockDriverState
*bs
,
790 BlockDriverCompletionFunc
*cb
, void *opaque
)
792 return bdrv_aio_flush(bs
->file
, cb
, opaque
);
795 static int qcow_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
797 BDRVQcowState
*s
= bs
->opaque
;
798 bdi
->cluster_size
= s
->cluster_size
;
803 static QEMUOptionParameter qcow_create_options
[] = {
805 .name
= BLOCK_OPT_SIZE
,
807 .help
= "Virtual disk size"
810 .name
= BLOCK_OPT_BACKING_FILE
,
812 .help
= "File name of a base image"
815 .name
= BLOCK_OPT_ENCRYPT
,
817 .help
= "Encrypt the image"
822 static BlockDriver bdrv_qcow
= {
823 .format_name
= "qcow",
824 .instance_size
= sizeof(BDRVQcowState
),
825 .bdrv_probe
= qcow_probe
,
826 .bdrv_open
= qcow_open
,
827 .bdrv_close
= qcow_close
,
828 .bdrv_create
= qcow_create
,
829 .bdrv_flush
= qcow_flush
,
830 .bdrv_is_allocated
= qcow_is_allocated
,
831 .bdrv_set_key
= qcow_set_key
,
832 .bdrv_make_empty
= qcow_make_empty
,
833 .bdrv_co_readv
= qcow_co_readv
,
834 .bdrv_co_writev
= qcow_co_writev
,
835 .bdrv_aio_flush
= qcow_aio_flush
,
836 .bdrv_write_compressed
= qcow_write_compressed
,
837 .bdrv_get_info
= qcow_get_info
,
839 .create_options
= qcow_create_options
,
842 static void bdrv_qcow_init(void)
844 bdrv_register(&bdrv_qcow
);
847 block_init(bdrv_qcow_init
);