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 "block/aes.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
, int flags
)
97 BDRVQcowState
*s
= bs
->opaque
;
98 int len
, i
, shift
, ret
;
101 ret
= bdrv_pread(bs
->file
, 0, &header
, sizeof(header
));
105 be32_to_cpus(&header
.magic
);
106 be32_to_cpus(&header
.version
);
107 be64_to_cpus(&header
.backing_file_offset
);
108 be32_to_cpus(&header
.backing_file_size
);
109 be32_to_cpus(&header
.mtime
);
110 be64_to_cpus(&header
.size
);
111 be32_to_cpus(&header
.crypt_method
);
112 be64_to_cpus(&header
.l1_table_offset
);
114 if (header
.magic
!= QCOW_MAGIC
) {
118 if (header
.version
!= QCOW_VERSION
) {
120 snprintf(version
, sizeof(version
), "QCOW version %d", header
.version
);
121 qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE
,
122 bs
->device_name
, "qcow", version
);
127 if (header
.size
<= 1 || header
.cluster_bits
< 9) {
131 if (header
.crypt_method
> QCOW_CRYPT_AES
) {
135 s
->crypt_method_header
= header
.crypt_method
;
136 if (s
->crypt_method_header
) {
139 s
->cluster_bits
= header
.cluster_bits
;
140 s
->cluster_size
= 1 << s
->cluster_bits
;
141 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
142 s
->l2_bits
= header
.l2_bits
;
143 s
->l2_size
= 1 << s
->l2_bits
;
144 bs
->total_sectors
= header
.size
/ 512;
145 s
->cluster_offset_mask
= (1LL << (63 - s
->cluster_bits
)) - 1;
147 /* read the level 1 table */
148 shift
= s
->cluster_bits
+ s
->l2_bits
;
149 s
->l1_size
= (header
.size
+ (1LL << shift
) - 1) >> shift
;
151 s
->l1_table_offset
= header
.l1_table_offset
;
152 s
->l1_table
= g_malloc(s
->l1_size
* sizeof(uint64_t));
154 ret
= bdrv_pread(bs
->file
, s
->l1_table_offset
, s
->l1_table
,
155 s
->l1_size
* sizeof(uint64_t));
160 for(i
= 0;i
< s
->l1_size
; i
++) {
161 be64_to_cpus(&s
->l1_table
[i
]);
164 s
->l2_cache
= g_malloc(s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
165 s
->cluster_cache
= g_malloc(s
->cluster_size
);
166 s
->cluster_data
= g_malloc(s
->cluster_size
);
167 s
->cluster_cache_offset
= -1;
169 /* read the backing file name */
170 if (header
.backing_file_offset
!= 0) {
171 len
= header
.backing_file_size
;
175 ret
= bdrv_pread(bs
->file
, header
.backing_file_offset
,
176 bs
->backing_file
, len
);
180 bs
->backing_file
[len
] = '\0';
183 /* Disable migration when qcow images are used */
184 error_set(&s
->migration_blocker
,
185 QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED
,
186 "qcow", bs
->device_name
, "live migration");
187 migrate_add_blocker(s
->migration_blocker
);
189 qemu_co_mutex_init(&s
->lock
);
195 g_free(s
->cluster_cache
);
196 g_free(s
->cluster_data
);
201 /* We have nothing to do for QCOW reopen, stubs just return
203 static int qcow_reopen_prepare(BDRVReopenState
*state
,
204 BlockReopenQueue
*queue
, Error
**errp
)
209 static int qcow_set_key(BlockDriverState
*bs
, const char *key
)
211 BDRVQcowState
*s
= bs
->opaque
;
215 memset(keybuf
, 0, 16);
219 /* XXX: we could compress the chars to 7 bits to increase
221 for(i
= 0;i
< len
;i
++) {
224 s
->crypt_method
= s
->crypt_method_header
;
226 if (AES_set_encrypt_key(keybuf
, 128, &s
->aes_encrypt_key
) != 0)
228 if (AES_set_decrypt_key(keybuf
, 128, &s
->aes_decrypt_key
) != 0)
233 /* The crypt function is compatible with the linux cryptoloop
234 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
236 static void encrypt_sectors(BDRVQcowState
*s
, int64_t sector_num
,
237 uint8_t *out_buf
, const uint8_t *in_buf
,
238 int nb_sectors
, int enc
,
247 for(i
= 0; i
< nb_sectors
; i
++) {
248 ivec
.ll
[0] = cpu_to_le64(sector_num
);
250 AES_cbc_encrypt(in_buf
, out_buf
, 512, key
,
262 * 1 to allocate a normal cluster (for sector indexes 'n_start' to
265 * 2 to allocate a compressed cluster of size
266 * 'compressed_size'. 'compressed_size' must be > 0 and <
269 * return 0 if not allocated.
271 static uint64_t get_cluster_offset(BlockDriverState
*bs
,
272 uint64_t offset
, int allocate
,
274 int n_start
, int n_end
)
276 BDRVQcowState
*s
= bs
->opaque
;
277 int min_index
, i
, j
, l1_index
, l2_index
;
278 uint64_t l2_offset
, *l2_table
, cluster_offset
, tmp
;
282 l1_index
= offset
>> (s
->l2_bits
+ s
->cluster_bits
);
283 l2_offset
= s
->l1_table
[l1_index
];
288 /* allocate a new l2 entry */
289 l2_offset
= bdrv_getlength(bs
->file
);
290 /* round to cluster size */
291 l2_offset
= (l2_offset
+ s
->cluster_size
- 1) & ~(s
->cluster_size
- 1);
292 /* update the L1 entry */
293 s
->l1_table
[l1_index
] = l2_offset
;
294 tmp
= cpu_to_be64(l2_offset
);
295 if (bdrv_pwrite_sync(bs
->file
,
296 s
->l1_table_offset
+ l1_index
* sizeof(tmp
),
297 &tmp
, sizeof(tmp
)) < 0)
301 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
302 if (l2_offset
== s
->l2_cache_offsets
[i
]) {
303 /* increment the hit count */
304 if (++s
->l2_cache_counts
[i
] == 0xffffffff) {
305 for(j
= 0; j
< L2_CACHE_SIZE
; j
++) {
306 s
->l2_cache_counts
[j
] >>= 1;
309 l2_table
= s
->l2_cache
+ (i
<< s
->l2_bits
);
313 /* not found: load a new entry in the least used one */
315 min_count
= 0xffffffff;
316 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
317 if (s
->l2_cache_counts
[i
] < min_count
) {
318 min_count
= s
->l2_cache_counts
[i
];
322 l2_table
= s
->l2_cache
+ (min_index
<< s
->l2_bits
);
324 memset(l2_table
, 0, s
->l2_size
* sizeof(uint64_t));
325 if (bdrv_pwrite_sync(bs
->file
, l2_offset
, l2_table
,
326 s
->l2_size
* sizeof(uint64_t)) < 0)
329 if (bdrv_pread(bs
->file
, l2_offset
, l2_table
, s
->l2_size
* sizeof(uint64_t)) !=
330 s
->l2_size
* sizeof(uint64_t))
333 s
->l2_cache_offsets
[min_index
] = l2_offset
;
334 s
->l2_cache_counts
[min_index
] = 1;
336 l2_index
= (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
337 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
338 if (!cluster_offset
||
339 ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) && allocate
== 1)) {
342 /* allocate a new cluster */
343 if ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) &&
344 (n_end
- n_start
) < s
->cluster_sectors
) {
345 /* if the cluster is already compressed, we must
346 decompress it in the case it is not completely
348 if (decompress_cluster(bs
, cluster_offset
) < 0)
350 cluster_offset
= bdrv_getlength(bs
->file
);
351 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
352 ~(s
->cluster_size
- 1);
353 /* write the cluster content */
354 if (bdrv_pwrite(bs
->file
, cluster_offset
, s
->cluster_cache
, s
->cluster_size
) !=
358 cluster_offset
= bdrv_getlength(bs
->file
);
360 /* round to cluster size */
361 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
362 ~(s
->cluster_size
- 1);
363 bdrv_truncate(bs
->file
, cluster_offset
+ s
->cluster_size
);
364 /* if encrypted, we must initialize the cluster
365 content which won't be written */
366 if (s
->crypt_method
&&
367 (n_end
- n_start
) < s
->cluster_sectors
) {
369 start_sect
= (offset
& ~(s
->cluster_size
- 1)) >> 9;
370 memset(s
->cluster_data
+ 512, 0x00, 512);
371 for(i
= 0; i
< s
->cluster_sectors
; i
++) {
372 if (i
< n_start
|| i
>= n_end
) {
373 encrypt_sectors(s
, start_sect
+ i
,
375 s
->cluster_data
+ 512, 1, 1,
376 &s
->aes_encrypt_key
);
377 if (bdrv_pwrite(bs
->file
, cluster_offset
+ i
* 512,
378 s
->cluster_data
, 512) != 512)
383 } else if (allocate
== 2) {
384 cluster_offset
|= QCOW_OFLAG_COMPRESSED
|
385 (uint64_t)compressed_size
<< (63 - s
->cluster_bits
);
388 /* update L2 table */
389 tmp
= cpu_to_be64(cluster_offset
);
390 l2_table
[l2_index
] = tmp
;
391 if (bdrv_pwrite_sync(bs
->file
, l2_offset
+ l2_index
* sizeof(tmp
),
392 &tmp
, sizeof(tmp
)) < 0)
395 return cluster_offset
;
398 static int coroutine_fn
qcow_co_is_allocated(BlockDriverState
*bs
,
399 int64_t sector_num
, int nb_sectors
, int *pnum
)
401 BDRVQcowState
*s
= bs
->opaque
;
402 int index_in_cluster
, n
;
403 uint64_t cluster_offset
;
405 qemu_co_mutex_lock(&s
->lock
);
406 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 0, 0, 0, 0);
407 qemu_co_mutex_unlock(&s
->lock
);
408 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
409 n
= s
->cluster_sectors
- index_in_cluster
;
413 return (cluster_offset
!= 0);
416 static int decompress_buffer(uint8_t *out_buf
, int out_buf_size
,
417 const uint8_t *buf
, int buf_size
)
419 z_stream strm1
, *strm
= &strm1
;
422 memset(strm
, 0, sizeof(*strm
));
424 strm
->next_in
= (uint8_t *)buf
;
425 strm
->avail_in
= buf_size
;
426 strm
->next_out
= out_buf
;
427 strm
->avail_out
= out_buf_size
;
429 ret
= inflateInit2(strm
, -12);
432 ret
= inflate(strm
, Z_FINISH
);
433 out_len
= strm
->next_out
- out_buf
;
434 if ((ret
!= Z_STREAM_END
&& ret
!= Z_BUF_ERROR
) ||
435 out_len
!= out_buf_size
) {
443 static int decompress_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
)
445 BDRVQcowState
*s
= bs
->opaque
;
449 coffset
= cluster_offset
& s
->cluster_offset_mask
;
450 if (s
->cluster_cache_offset
!= coffset
) {
451 csize
= cluster_offset
>> (63 - s
->cluster_bits
);
452 csize
&= (s
->cluster_size
- 1);
453 ret
= bdrv_pread(bs
->file
, coffset
, s
->cluster_data
, csize
);
456 if (decompress_buffer(s
->cluster_cache
, s
->cluster_size
,
457 s
->cluster_data
, csize
) < 0) {
460 s
->cluster_cache_offset
= coffset
;
465 static coroutine_fn
int qcow_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
466 int nb_sectors
, QEMUIOVector
*qiov
)
468 BDRVQcowState
*s
= bs
->opaque
;
469 int index_in_cluster
;
471 uint64_t cluster_offset
;
473 QEMUIOVector hd_qiov
;
477 if (qiov
->niov
> 1) {
478 buf
= orig_buf
= qemu_blockalign(bs
, qiov
->size
);
481 buf
= (uint8_t *)qiov
->iov
->iov_base
;
484 qemu_co_mutex_lock(&s
->lock
);
486 while (nb_sectors
!= 0) {
487 /* prepare next request */
488 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9,
490 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
491 n
= s
->cluster_sectors
- index_in_cluster
;
492 if (n
> nb_sectors
) {
496 if (!cluster_offset
) {
497 if (bs
->backing_hd
) {
498 /* read from the base image */
499 hd_iov
.iov_base
= (void *)buf
;
500 hd_iov
.iov_len
= n
* 512;
501 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
502 qemu_co_mutex_unlock(&s
->lock
);
503 ret
= bdrv_co_readv(bs
->backing_hd
, sector_num
,
505 qemu_co_mutex_lock(&s
->lock
);
510 /* Note: in this case, no need to wait */
511 memset(buf
, 0, 512 * n
);
513 } else if (cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
514 /* add AIO support for compressed blocks ? */
515 if (decompress_cluster(bs
, cluster_offset
) < 0) {
519 s
->cluster_cache
+ index_in_cluster
* 512, 512 * n
);
521 if ((cluster_offset
& 511) != 0) {
524 hd_iov
.iov_base
= (void *)buf
;
525 hd_iov
.iov_len
= n
* 512;
526 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
527 qemu_co_mutex_unlock(&s
->lock
);
528 ret
= bdrv_co_readv(bs
->file
,
529 (cluster_offset
>> 9) + index_in_cluster
,
531 qemu_co_mutex_lock(&s
->lock
);
535 if (s
->crypt_method
) {
536 encrypt_sectors(s
, sector_num
, buf
, buf
,
538 &s
->aes_decrypt_key
);
549 qemu_co_mutex_unlock(&s
->lock
);
551 if (qiov
->niov
> 1) {
552 qemu_iovec_from_buf(qiov
, 0, orig_buf
, qiov
->size
);
553 qemu_vfree(orig_buf
);
563 static coroutine_fn
int qcow_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
564 int nb_sectors
, QEMUIOVector
*qiov
)
566 BDRVQcowState
*s
= bs
->opaque
;
567 int index_in_cluster
;
568 uint64_t cluster_offset
;
569 const uint8_t *src_buf
;
571 uint8_t *cluster_data
= NULL
;
573 QEMUIOVector hd_qiov
;
577 s
->cluster_cache_offset
= -1; /* disable compressed cache */
579 if (qiov
->niov
> 1) {
580 buf
= orig_buf
= qemu_blockalign(bs
, qiov
->size
);
581 qemu_iovec_to_buf(qiov
, 0, buf
, qiov
->size
);
584 buf
= (uint8_t *)qiov
->iov
->iov_base
;
587 qemu_co_mutex_lock(&s
->lock
);
589 while (nb_sectors
!= 0) {
591 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
592 n
= s
->cluster_sectors
- index_in_cluster
;
593 if (n
> nb_sectors
) {
596 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 1, 0,
598 index_in_cluster
+ n
);
599 if (!cluster_offset
|| (cluster_offset
& 511) != 0) {
603 if (s
->crypt_method
) {
605 cluster_data
= g_malloc0(s
->cluster_size
);
607 encrypt_sectors(s
, sector_num
, cluster_data
, buf
,
608 n
, 1, &s
->aes_encrypt_key
);
609 src_buf
= cluster_data
;
614 hd_iov
.iov_base
= (void *)src_buf
;
615 hd_iov
.iov_len
= n
* 512;
616 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
617 qemu_co_mutex_unlock(&s
->lock
);
618 ret
= bdrv_co_writev(bs
->file
,
619 (cluster_offset
>> 9) + index_in_cluster
,
621 qemu_co_mutex_lock(&s
->lock
);
631 qemu_co_mutex_unlock(&s
->lock
);
633 if (qiov
->niov
> 1) {
634 qemu_vfree(orig_buf
);
636 g_free(cluster_data
);
641 static void qcow_close(BlockDriverState
*bs
)
643 BDRVQcowState
*s
= bs
->opaque
;
647 g_free(s
->cluster_cache
);
648 g_free(s
->cluster_data
);
650 migrate_del_blocker(s
->migration_blocker
);
651 error_free(s
->migration_blocker
);
654 static int qcow_create(const char *filename
, QEMUOptionParameter
*options
)
656 int header_size
, backing_filename_len
, l1_size
, shift
, i
;
659 int64_t total_size
= 0;
660 const char *backing_file
= NULL
;
663 BlockDriverState
*qcow_bs
;
665 /* Read out options */
666 while (options
&& options
->name
) {
667 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
668 total_size
= options
->value
.n
/ 512;
669 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
670 backing_file
= options
->value
.s
;
671 } else if (!strcmp(options
->name
, BLOCK_OPT_ENCRYPT
)) {
672 flags
|= options
->value
.n
? BLOCK_FLAG_ENCRYPT
: 0;
677 ret
= bdrv_create_file(filename
, options
);
682 ret
= bdrv_file_open(&qcow_bs
, filename
, BDRV_O_RDWR
);
687 ret
= bdrv_truncate(qcow_bs
, 0);
692 memset(&header
, 0, sizeof(header
));
693 header
.magic
= cpu_to_be32(QCOW_MAGIC
);
694 header
.version
= cpu_to_be32(QCOW_VERSION
);
695 header
.size
= cpu_to_be64(total_size
* 512);
696 header_size
= sizeof(header
);
697 backing_filename_len
= 0;
699 if (strcmp(backing_file
, "fat:")) {
700 header
.backing_file_offset
= cpu_to_be64(header_size
);
701 backing_filename_len
= strlen(backing_file
);
702 header
.backing_file_size
= cpu_to_be32(backing_filename_len
);
703 header_size
+= backing_filename_len
;
705 /* special backing file for vvfat */
708 header
.cluster_bits
= 9; /* 512 byte cluster to avoid copying
709 unmodifyed sectors */
710 header
.l2_bits
= 12; /* 32 KB L2 tables */
712 header
.cluster_bits
= 12; /* 4 KB clusters */
713 header
.l2_bits
= 9; /* 4 KB L2 tables */
715 header_size
= (header_size
+ 7) & ~7;
716 shift
= header
.cluster_bits
+ header
.l2_bits
;
717 l1_size
= ((total_size
* 512) + (1LL << shift
) - 1) >> shift
;
719 header
.l1_table_offset
= cpu_to_be64(header_size
);
720 if (flags
& BLOCK_FLAG_ENCRYPT
) {
721 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
723 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
726 /* write all the data */
727 ret
= bdrv_pwrite(qcow_bs
, 0, &header
, sizeof(header
));
728 if (ret
!= sizeof(header
)) {
733 ret
= bdrv_pwrite(qcow_bs
, sizeof(header
),
734 backing_file
, backing_filename_len
);
735 if (ret
!= backing_filename_len
) {
740 tmp
= g_malloc0(BDRV_SECTOR_SIZE
);
741 for (i
= 0; i
< ((sizeof(uint64_t)*l1_size
+ BDRV_SECTOR_SIZE
- 1)/
742 BDRV_SECTOR_SIZE
); i
++) {
743 ret
= bdrv_pwrite(qcow_bs
, header_size
+
744 BDRV_SECTOR_SIZE
*i
, tmp
, BDRV_SECTOR_SIZE
);
745 if (ret
!= BDRV_SECTOR_SIZE
) {
754 bdrv_delete(qcow_bs
);
758 static int qcow_make_empty(BlockDriverState
*bs
)
760 BDRVQcowState
*s
= bs
->opaque
;
761 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
764 memset(s
->l1_table
, 0, l1_length
);
765 if (bdrv_pwrite_sync(bs
->file
, s
->l1_table_offset
, s
->l1_table
,
768 ret
= bdrv_truncate(bs
->file
, s
->l1_table_offset
+ l1_length
);
772 memset(s
->l2_cache
, 0, s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
773 memset(s
->l2_cache_offsets
, 0, L2_CACHE_SIZE
* sizeof(uint64_t));
774 memset(s
->l2_cache_counts
, 0, L2_CACHE_SIZE
* sizeof(uint32_t));
779 /* XXX: put compressed sectors first, then all the cluster aligned
780 tables to avoid losing bytes in alignment */
781 static int qcow_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
782 const uint8_t *buf
, int nb_sectors
)
784 BDRVQcowState
*s
= bs
->opaque
;
788 uint64_t cluster_offset
;
790 if (nb_sectors
!= s
->cluster_sectors
)
793 out_buf
= g_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
795 /* best compression, small window, no zlib header */
796 memset(&strm
, 0, sizeof(strm
));
797 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
799 9, Z_DEFAULT_STRATEGY
);
805 strm
.avail_in
= s
->cluster_size
;
806 strm
.next_in
= (uint8_t *)buf
;
807 strm
.avail_out
= s
->cluster_size
;
808 strm
.next_out
= out_buf
;
810 ret
= deflate(&strm
, Z_FINISH
);
811 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
816 out_len
= strm
.next_out
- out_buf
;
820 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
821 /* could not compress: write normal cluster */
822 ret
= bdrv_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
827 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 2,
829 if (cluster_offset
== 0) {
834 cluster_offset
&= s
->cluster_offset_mask
;
835 ret
= bdrv_pwrite(bs
->file
, cluster_offset
, out_buf
, out_len
);
847 static int qcow_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
849 BDRVQcowState
*s
= bs
->opaque
;
850 bdi
->cluster_size
= s
->cluster_size
;
855 static QEMUOptionParameter qcow_create_options
[] = {
857 .name
= BLOCK_OPT_SIZE
,
859 .help
= "Virtual disk size"
862 .name
= BLOCK_OPT_BACKING_FILE
,
864 .help
= "File name of a base image"
867 .name
= BLOCK_OPT_ENCRYPT
,
869 .help
= "Encrypt the image"
874 static BlockDriver bdrv_qcow
= {
875 .format_name
= "qcow",
876 .instance_size
= sizeof(BDRVQcowState
),
877 .bdrv_probe
= qcow_probe
,
878 .bdrv_open
= qcow_open
,
879 .bdrv_close
= qcow_close
,
880 .bdrv_reopen_prepare
= qcow_reopen_prepare
,
881 .bdrv_create
= qcow_create
,
883 .bdrv_co_readv
= qcow_co_readv
,
884 .bdrv_co_writev
= qcow_co_writev
,
885 .bdrv_co_is_allocated
= qcow_co_is_allocated
,
887 .bdrv_set_key
= qcow_set_key
,
888 .bdrv_make_empty
= qcow_make_empty
,
889 .bdrv_write_compressed
= qcow_write_compressed
,
890 .bdrv_get_info
= qcow_get_info
,
892 .create_options
= qcow_create_options
,
895 static void bdrv_qcow_init(void)
897 bdrv_register(&bdrv_qcow
);
900 block_init(bdrv_qcow_init
);