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
) {
116 error_setg(errp
, "Image not in qcow format");
120 if (header
.version
!= QCOW_VERSION
) {
122 snprintf(version
, sizeof(version
), "QCOW version %" PRIu32
,
124 error_set(errp
, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE
,
125 bs
->device_name
, "qcow", version
);
130 if (header
.size
<= 1 || header
.cluster_bits
< 9) {
131 error_setg(errp
, "invalid value in qcow header");
135 if (header
.crypt_method
> QCOW_CRYPT_AES
) {
136 error_setg(errp
, "invalid encryption method in qcow header");
140 s
->crypt_method_header
= header
.crypt_method
;
141 if (s
->crypt_method_header
) {
144 s
->cluster_bits
= header
.cluster_bits
;
145 s
->cluster_size
= 1 << s
->cluster_bits
;
146 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
147 s
->l2_bits
= header
.l2_bits
;
148 s
->l2_size
= 1 << s
->l2_bits
;
149 bs
->total_sectors
= header
.size
/ 512;
150 s
->cluster_offset_mask
= (1LL << (63 - s
->cluster_bits
)) - 1;
152 /* read the level 1 table */
153 shift
= s
->cluster_bits
+ s
->l2_bits
;
154 s
->l1_size
= (header
.size
+ (1LL << shift
) - 1) >> shift
;
156 s
->l1_table_offset
= header
.l1_table_offset
;
157 s
->l1_table
= g_malloc(s
->l1_size
* sizeof(uint64_t));
159 ret
= bdrv_pread(bs
->file
, s
->l1_table_offset
, s
->l1_table
,
160 s
->l1_size
* sizeof(uint64_t));
165 for(i
= 0;i
< s
->l1_size
; i
++) {
166 be64_to_cpus(&s
->l1_table
[i
]);
169 s
->l2_cache
= g_malloc(s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
170 s
->cluster_cache
= g_malloc(s
->cluster_size
);
171 s
->cluster_data
= g_malloc(s
->cluster_size
);
172 s
->cluster_cache_offset
= -1;
174 /* read the backing file name */
175 if (header
.backing_file_offset
!= 0) {
176 len
= header
.backing_file_size
;
180 ret
= bdrv_pread(bs
->file
, header
.backing_file_offset
,
181 bs
->backing_file
, len
);
185 bs
->backing_file
[len
] = '\0';
188 /* Disable migration when qcow images are used */
189 error_set(&s
->migration_blocker
,
190 QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED
,
191 "qcow", bs
->device_name
, "live migration");
192 migrate_add_blocker(s
->migration_blocker
);
194 qemu_co_mutex_init(&s
->lock
);
200 g_free(s
->cluster_cache
);
201 g_free(s
->cluster_data
);
206 /* We have nothing to do for QCOW reopen, stubs just return
208 static int qcow_reopen_prepare(BDRVReopenState
*state
,
209 BlockReopenQueue
*queue
, Error
**errp
)
214 static int qcow_set_key(BlockDriverState
*bs
, const char *key
)
216 BDRVQcowState
*s
= bs
->opaque
;
220 memset(keybuf
, 0, 16);
224 /* XXX: we could compress the chars to 7 bits to increase
226 for(i
= 0;i
< len
;i
++) {
229 s
->crypt_method
= s
->crypt_method_header
;
231 if (AES_set_encrypt_key(keybuf
, 128, &s
->aes_encrypt_key
) != 0)
233 if (AES_set_decrypt_key(keybuf
, 128, &s
->aes_decrypt_key
) != 0)
238 /* The crypt function is compatible with the linux cryptoloop
239 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
241 static void encrypt_sectors(BDRVQcowState
*s
, int64_t sector_num
,
242 uint8_t *out_buf
, const uint8_t *in_buf
,
243 int nb_sectors
, int enc
,
252 for(i
= 0; i
< nb_sectors
; i
++) {
253 ivec
.ll
[0] = cpu_to_le64(sector_num
);
255 AES_cbc_encrypt(in_buf
, out_buf
, 512, key
,
267 * 1 to allocate a normal cluster (for sector indexes 'n_start' to
270 * 2 to allocate a compressed cluster of size
271 * 'compressed_size'. 'compressed_size' must be > 0 and <
274 * return 0 if not allocated.
276 static uint64_t get_cluster_offset(BlockDriverState
*bs
,
277 uint64_t offset
, int allocate
,
279 int n_start
, int n_end
)
281 BDRVQcowState
*s
= bs
->opaque
;
282 int min_index
, i
, j
, l1_index
, l2_index
;
283 uint64_t l2_offset
, *l2_table
, cluster_offset
, tmp
;
287 l1_index
= offset
>> (s
->l2_bits
+ s
->cluster_bits
);
288 l2_offset
= s
->l1_table
[l1_index
];
293 /* allocate a new l2 entry */
294 l2_offset
= bdrv_getlength(bs
->file
);
295 /* round to cluster size */
296 l2_offset
= (l2_offset
+ s
->cluster_size
- 1) & ~(s
->cluster_size
- 1);
297 /* update the L1 entry */
298 s
->l1_table
[l1_index
] = l2_offset
;
299 tmp
= cpu_to_be64(l2_offset
);
300 if (bdrv_pwrite_sync(bs
->file
,
301 s
->l1_table_offset
+ l1_index
* sizeof(tmp
),
302 &tmp
, sizeof(tmp
)) < 0)
306 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
307 if (l2_offset
== s
->l2_cache_offsets
[i
]) {
308 /* increment the hit count */
309 if (++s
->l2_cache_counts
[i
] == 0xffffffff) {
310 for(j
= 0; j
< L2_CACHE_SIZE
; j
++) {
311 s
->l2_cache_counts
[j
] >>= 1;
314 l2_table
= s
->l2_cache
+ (i
<< s
->l2_bits
);
318 /* not found: load a new entry in the least used one */
320 min_count
= 0xffffffff;
321 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
322 if (s
->l2_cache_counts
[i
] < min_count
) {
323 min_count
= s
->l2_cache_counts
[i
];
327 l2_table
= s
->l2_cache
+ (min_index
<< s
->l2_bits
);
329 memset(l2_table
, 0, s
->l2_size
* sizeof(uint64_t));
330 if (bdrv_pwrite_sync(bs
->file
, l2_offset
, l2_table
,
331 s
->l2_size
* sizeof(uint64_t)) < 0)
334 if (bdrv_pread(bs
->file
, l2_offset
, l2_table
, s
->l2_size
* sizeof(uint64_t)) !=
335 s
->l2_size
* sizeof(uint64_t))
338 s
->l2_cache_offsets
[min_index
] = l2_offset
;
339 s
->l2_cache_counts
[min_index
] = 1;
341 l2_index
= (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
342 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
343 if (!cluster_offset
||
344 ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) && allocate
== 1)) {
347 /* allocate a new cluster */
348 if ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) &&
349 (n_end
- n_start
) < s
->cluster_sectors
) {
350 /* if the cluster is already compressed, we must
351 decompress it in the case it is not completely
353 if (decompress_cluster(bs
, cluster_offset
) < 0)
355 cluster_offset
= bdrv_getlength(bs
->file
);
356 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
357 ~(s
->cluster_size
- 1);
358 /* write the cluster content */
359 if (bdrv_pwrite(bs
->file
, cluster_offset
, s
->cluster_cache
, s
->cluster_size
) !=
363 cluster_offset
= bdrv_getlength(bs
->file
);
365 /* round to cluster size */
366 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
367 ~(s
->cluster_size
- 1);
368 bdrv_truncate(bs
->file
, cluster_offset
+ s
->cluster_size
);
369 /* if encrypted, we must initialize the cluster
370 content which won't be written */
371 if (s
->crypt_method
&&
372 (n_end
- n_start
) < s
->cluster_sectors
) {
374 start_sect
= (offset
& ~(s
->cluster_size
- 1)) >> 9;
375 memset(s
->cluster_data
+ 512, 0x00, 512);
376 for(i
= 0; i
< s
->cluster_sectors
; i
++) {
377 if (i
< n_start
|| i
>= n_end
) {
378 encrypt_sectors(s
, start_sect
+ i
,
380 s
->cluster_data
+ 512, 1, 1,
381 &s
->aes_encrypt_key
);
382 if (bdrv_pwrite(bs
->file
, cluster_offset
+ i
* 512,
383 s
->cluster_data
, 512) != 512)
388 } else if (allocate
== 2) {
389 cluster_offset
|= QCOW_OFLAG_COMPRESSED
|
390 (uint64_t)compressed_size
<< (63 - s
->cluster_bits
);
393 /* update L2 table */
394 tmp
= cpu_to_be64(cluster_offset
);
395 l2_table
[l2_index
] = tmp
;
396 if (bdrv_pwrite_sync(bs
->file
, l2_offset
+ l2_index
* sizeof(tmp
),
397 &tmp
, sizeof(tmp
)) < 0)
400 return cluster_offset
;
403 static int64_t coroutine_fn
qcow_co_get_block_status(BlockDriverState
*bs
,
404 int64_t sector_num
, int nb_sectors
, int *pnum
)
406 BDRVQcowState
*s
= bs
->opaque
;
407 int index_in_cluster
, n
;
408 uint64_t cluster_offset
;
410 qemu_co_mutex_lock(&s
->lock
);
411 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 0, 0, 0, 0);
412 qemu_co_mutex_unlock(&s
->lock
);
413 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
414 n
= s
->cluster_sectors
- index_in_cluster
;
418 if (!cluster_offset
) {
421 if ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) || s
->crypt_method
) {
422 return BDRV_BLOCK_DATA
;
424 cluster_offset
|= (index_in_cluster
<< BDRV_SECTOR_BITS
);
425 return BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
| cluster_offset
;
428 static int decompress_buffer(uint8_t *out_buf
, int out_buf_size
,
429 const uint8_t *buf
, int buf_size
)
431 z_stream strm1
, *strm
= &strm1
;
434 memset(strm
, 0, sizeof(*strm
));
436 strm
->next_in
= (uint8_t *)buf
;
437 strm
->avail_in
= buf_size
;
438 strm
->next_out
= out_buf
;
439 strm
->avail_out
= out_buf_size
;
441 ret
= inflateInit2(strm
, -12);
444 ret
= inflate(strm
, Z_FINISH
);
445 out_len
= strm
->next_out
- out_buf
;
446 if ((ret
!= Z_STREAM_END
&& ret
!= Z_BUF_ERROR
) ||
447 out_len
!= out_buf_size
) {
455 static int decompress_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
)
457 BDRVQcowState
*s
= bs
->opaque
;
461 coffset
= cluster_offset
& s
->cluster_offset_mask
;
462 if (s
->cluster_cache_offset
!= coffset
) {
463 csize
= cluster_offset
>> (63 - s
->cluster_bits
);
464 csize
&= (s
->cluster_size
- 1);
465 ret
= bdrv_pread(bs
->file
, coffset
, s
->cluster_data
, csize
);
468 if (decompress_buffer(s
->cluster_cache
, s
->cluster_size
,
469 s
->cluster_data
, csize
) < 0) {
472 s
->cluster_cache_offset
= coffset
;
477 static coroutine_fn
int qcow_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
478 int nb_sectors
, QEMUIOVector
*qiov
)
480 BDRVQcowState
*s
= bs
->opaque
;
481 int index_in_cluster
;
483 uint64_t cluster_offset
;
485 QEMUIOVector hd_qiov
;
489 if (qiov
->niov
> 1) {
490 buf
= orig_buf
= qemu_blockalign(bs
, qiov
->size
);
493 buf
= (uint8_t *)qiov
->iov
->iov_base
;
496 qemu_co_mutex_lock(&s
->lock
);
498 while (nb_sectors
!= 0) {
499 /* prepare next request */
500 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9,
502 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
503 n
= s
->cluster_sectors
- index_in_cluster
;
504 if (n
> nb_sectors
) {
508 if (!cluster_offset
) {
509 if (bs
->backing_hd
) {
510 /* read from the base image */
511 hd_iov
.iov_base
= (void *)buf
;
512 hd_iov
.iov_len
= n
* 512;
513 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
514 qemu_co_mutex_unlock(&s
->lock
);
515 ret
= bdrv_co_readv(bs
->backing_hd
, sector_num
,
517 qemu_co_mutex_lock(&s
->lock
);
522 /* Note: in this case, no need to wait */
523 memset(buf
, 0, 512 * n
);
525 } else if (cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
526 /* add AIO support for compressed blocks ? */
527 if (decompress_cluster(bs
, cluster_offset
) < 0) {
531 s
->cluster_cache
+ index_in_cluster
* 512, 512 * n
);
533 if ((cluster_offset
& 511) != 0) {
536 hd_iov
.iov_base
= (void *)buf
;
537 hd_iov
.iov_len
= n
* 512;
538 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
539 qemu_co_mutex_unlock(&s
->lock
);
540 ret
= bdrv_co_readv(bs
->file
,
541 (cluster_offset
>> 9) + index_in_cluster
,
543 qemu_co_mutex_lock(&s
->lock
);
547 if (s
->crypt_method
) {
548 encrypt_sectors(s
, sector_num
, buf
, buf
,
550 &s
->aes_decrypt_key
);
561 qemu_co_mutex_unlock(&s
->lock
);
563 if (qiov
->niov
> 1) {
564 qemu_iovec_from_buf(qiov
, 0, orig_buf
, qiov
->size
);
565 qemu_vfree(orig_buf
);
575 static coroutine_fn
int qcow_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
576 int nb_sectors
, QEMUIOVector
*qiov
)
578 BDRVQcowState
*s
= bs
->opaque
;
579 int index_in_cluster
;
580 uint64_t cluster_offset
;
581 const uint8_t *src_buf
;
583 uint8_t *cluster_data
= NULL
;
585 QEMUIOVector hd_qiov
;
589 s
->cluster_cache_offset
= -1; /* disable compressed cache */
591 if (qiov
->niov
> 1) {
592 buf
= orig_buf
= qemu_blockalign(bs
, qiov
->size
);
593 qemu_iovec_to_buf(qiov
, 0, buf
, qiov
->size
);
596 buf
= (uint8_t *)qiov
->iov
->iov_base
;
599 qemu_co_mutex_lock(&s
->lock
);
601 while (nb_sectors
!= 0) {
603 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
604 n
= s
->cluster_sectors
- index_in_cluster
;
605 if (n
> nb_sectors
) {
608 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 1, 0,
610 index_in_cluster
+ n
);
611 if (!cluster_offset
|| (cluster_offset
& 511) != 0) {
615 if (s
->crypt_method
) {
617 cluster_data
= g_malloc0(s
->cluster_size
);
619 encrypt_sectors(s
, sector_num
, cluster_data
, buf
,
620 n
, 1, &s
->aes_encrypt_key
);
621 src_buf
= cluster_data
;
626 hd_iov
.iov_base
= (void *)src_buf
;
627 hd_iov
.iov_len
= n
* 512;
628 qemu_iovec_init_external(&hd_qiov
, &hd_iov
, 1);
629 qemu_co_mutex_unlock(&s
->lock
);
630 ret
= bdrv_co_writev(bs
->file
,
631 (cluster_offset
>> 9) + index_in_cluster
,
633 qemu_co_mutex_lock(&s
->lock
);
643 qemu_co_mutex_unlock(&s
->lock
);
645 if (qiov
->niov
> 1) {
646 qemu_vfree(orig_buf
);
648 g_free(cluster_data
);
653 static void qcow_close(BlockDriverState
*bs
)
655 BDRVQcowState
*s
= bs
->opaque
;
659 g_free(s
->cluster_cache
);
660 g_free(s
->cluster_data
);
662 migrate_del_blocker(s
->migration_blocker
);
663 error_free(s
->migration_blocker
);
666 static int qcow_create(const char *filename
, QEMUOptionParameter
*options
,
669 int header_size
, backing_filename_len
, l1_size
, shift
, i
;
672 int64_t total_size
= 0;
673 const char *backing_file
= NULL
;
675 Error
*local_err
= NULL
;
677 BlockDriverState
*qcow_bs
;
679 /* Read out options */
680 while (options
&& options
->name
) {
681 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
682 total_size
= options
->value
.n
/ 512;
683 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
684 backing_file
= options
->value
.s
;
685 } else if (!strcmp(options
->name
, BLOCK_OPT_ENCRYPT
)) {
686 flags
|= options
->value
.n
? BLOCK_FLAG_ENCRYPT
: 0;
691 ret
= bdrv_create_file(filename
, options
, &local_err
);
693 error_propagate(errp
, local_err
);
698 ret
= bdrv_open(&qcow_bs
, filename
, NULL
, NULL
,
699 BDRV_O_RDWR
| BDRV_O_PROTOCOL
, NULL
, &local_err
);
701 error_propagate(errp
, local_err
);
705 ret
= bdrv_truncate(qcow_bs
, 0);
710 memset(&header
, 0, sizeof(header
));
711 header
.magic
= cpu_to_be32(QCOW_MAGIC
);
712 header
.version
= cpu_to_be32(QCOW_VERSION
);
713 header
.size
= cpu_to_be64(total_size
* 512);
714 header_size
= sizeof(header
);
715 backing_filename_len
= 0;
717 if (strcmp(backing_file
, "fat:")) {
718 header
.backing_file_offset
= cpu_to_be64(header_size
);
719 backing_filename_len
= strlen(backing_file
);
720 header
.backing_file_size
= cpu_to_be32(backing_filename_len
);
721 header_size
+= backing_filename_len
;
723 /* special backing file for vvfat */
726 header
.cluster_bits
= 9; /* 512 byte cluster to avoid copying
727 unmodified sectors */
728 header
.l2_bits
= 12; /* 32 KB L2 tables */
730 header
.cluster_bits
= 12; /* 4 KB clusters */
731 header
.l2_bits
= 9; /* 4 KB L2 tables */
733 header_size
= (header_size
+ 7) & ~7;
734 shift
= header
.cluster_bits
+ header
.l2_bits
;
735 l1_size
= ((total_size
* 512) + (1LL << shift
) - 1) >> shift
;
737 header
.l1_table_offset
= cpu_to_be64(header_size
);
738 if (flags
& BLOCK_FLAG_ENCRYPT
) {
739 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
741 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
744 /* write all the data */
745 ret
= bdrv_pwrite(qcow_bs
, 0, &header
, sizeof(header
));
746 if (ret
!= sizeof(header
)) {
751 ret
= bdrv_pwrite(qcow_bs
, sizeof(header
),
752 backing_file
, backing_filename_len
);
753 if (ret
!= backing_filename_len
) {
758 tmp
= g_malloc0(BDRV_SECTOR_SIZE
);
759 for (i
= 0; i
< ((sizeof(uint64_t)*l1_size
+ BDRV_SECTOR_SIZE
- 1)/
760 BDRV_SECTOR_SIZE
); i
++) {
761 ret
= bdrv_pwrite(qcow_bs
, header_size
+
762 BDRV_SECTOR_SIZE
*i
, tmp
, BDRV_SECTOR_SIZE
);
763 if (ret
!= BDRV_SECTOR_SIZE
) {
776 static int qcow_make_empty(BlockDriverState
*bs
)
778 BDRVQcowState
*s
= bs
->opaque
;
779 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
782 memset(s
->l1_table
, 0, l1_length
);
783 if (bdrv_pwrite_sync(bs
->file
, s
->l1_table_offset
, s
->l1_table
,
786 ret
= bdrv_truncate(bs
->file
, s
->l1_table_offset
+ l1_length
);
790 memset(s
->l2_cache
, 0, s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
791 memset(s
->l2_cache_offsets
, 0, L2_CACHE_SIZE
* sizeof(uint64_t));
792 memset(s
->l2_cache_counts
, 0, L2_CACHE_SIZE
* sizeof(uint32_t));
797 /* XXX: put compressed sectors first, then all the cluster aligned
798 tables to avoid losing bytes in alignment */
799 static int qcow_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
800 const uint8_t *buf
, int nb_sectors
)
802 BDRVQcowState
*s
= bs
->opaque
;
806 uint64_t cluster_offset
;
808 if (nb_sectors
!= s
->cluster_sectors
) {
811 /* Zero-pad last write if image size is not cluster aligned */
812 if (sector_num
+ nb_sectors
== bs
->total_sectors
&&
813 nb_sectors
< s
->cluster_sectors
) {
814 uint8_t *pad_buf
= qemu_blockalign(bs
, s
->cluster_size
);
815 memset(pad_buf
, 0, s
->cluster_size
);
816 memcpy(pad_buf
, buf
, nb_sectors
* BDRV_SECTOR_SIZE
);
817 ret
= qcow_write_compressed(bs
, sector_num
,
818 pad_buf
, s
->cluster_sectors
);
824 out_buf
= g_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
826 /* best compression, small window, no zlib header */
827 memset(&strm
, 0, sizeof(strm
));
828 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
830 9, Z_DEFAULT_STRATEGY
);
836 strm
.avail_in
= s
->cluster_size
;
837 strm
.next_in
= (uint8_t *)buf
;
838 strm
.avail_out
= s
->cluster_size
;
839 strm
.next_out
= out_buf
;
841 ret
= deflate(&strm
, Z_FINISH
);
842 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
847 out_len
= strm
.next_out
- out_buf
;
851 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
852 /* could not compress: write normal cluster */
853 ret
= bdrv_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
858 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 2,
860 if (cluster_offset
== 0) {
865 cluster_offset
&= s
->cluster_offset_mask
;
866 ret
= bdrv_pwrite(bs
->file
, cluster_offset
, out_buf
, out_len
);
878 static int qcow_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
880 BDRVQcowState
*s
= bs
->opaque
;
881 bdi
->cluster_size
= s
->cluster_size
;
886 static QEMUOptionParameter qcow_create_options
[] = {
888 .name
= BLOCK_OPT_SIZE
,
890 .help
= "Virtual disk size"
893 .name
= BLOCK_OPT_BACKING_FILE
,
895 .help
= "File name of a base image"
898 .name
= BLOCK_OPT_ENCRYPT
,
900 .help
= "Encrypt the image"
905 static BlockDriver bdrv_qcow
= {
906 .format_name
= "qcow",
907 .instance_size
= sizeof(BDRVQcowState
),
908 .bdrv_probe
= qcow_probe
,
909 .bdrv_open
= qcow_open
,
910 .bdrv_close
= qcow_close
,
911 .bdrv_reopen_prepare
= qcow_reopen_prepare
,
912 .bdrv_create
= qcow_create
,
913 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
915 .bdrv_co_readv
= qcow_co_readv
,
916 .bdrv_co_writev
= qcow_co_writev
,
917 .bdrv_co_get_block_status
= qcow_co_get_block_status
,
919 .bdrv_set_key
= qcow_set_key
,
920 .bdrv_make_empty
= qcow_make_empty
,
921 .bdrv_write_compressed
= qcow_write_compressed
,
922 .bdrv_get_info
= qcow_get_info
,
924 .create_options
= qcow_create_options
,
927 static void bdrv_qcow_init(void)
929 bdrv_register(&bdrv_qcow
);
932 block_init(bdrv_qcow_init
);