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 /**************************************************************/
30 /* QEMU COW block driver with compression and encryption support */
32 #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
33 #define QCOW_VERSION 1
35 #define QCOW_CRYPT_NONE 0
36 #define QCOW_CRYPT_AES 1
38 #define QCOW_OFLAG_COMPRESSED (1LL << 63)
40 typedef struct QCowHeader
{
43 uint64_t backing_file_offset
;
44 uint32_t backing_file_size
;
46 uint64_t size
; /* in bytes */
49 uint32_t crypt_method
;
50 uint64_t l1_table_offset
;
53 #define L2_CACHE_SIZE 16
55 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
;
78 static int decompress_cluster(BDRVQcowState
*s
, uint64_t cluster_offset
);
80 static int qcow_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
82 const QCowHeader
*cow_header
= (const void *)buf
;
84 if (buf_size
>= sizeof(QCowHeader
) &&
85 be32_to_cpu(cow_header
->magic
) == QCOW_MAGIC
&&
86 be32_to_cpu(cow_header
->version
) == QCOW_VERSION
)
92 static int qcow_open(BlockDriverState
*bs
, const char *filename
, int flags
)
94 BDRVQcowState
*s
= bs
->opaque
;
95 int len
, i
, shift
, ret
;
98 ret
= bdrv_file_open(&s
->hd
, filename
, flags
);
101 if (bdrv_pread(s
->hd
, 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
= qemu_malloc(s
->l1_size
* sizeof(uint64_t));
137 if (bdrv_pread(s
->hd
, 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
= qemu_malloc(s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
147 s
->cluster_cache
= qemu_malloc(s
->cluster_size
);
148 if (!s
->cluster_cache
)
150 s
->cluster_data
= qemu_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(s
->hd
, header
.backing_file_offset
, bs
->backing_file
, len
) != len
)
162 bs
->backing_file
[len
] = '\0';
167 qemu_free(s
->l1_table
);
168 qemu_free(s
->l2_cache
);
169 qemu_free(s
->cluster_cache
);
170 qemu_free(s
->cluster_data
);
175 static int qcow_set_key(BlockDriverState
*bs
, const char *key
)
177 BDRVQcowState
*s
= bs
->opaque
;
181 memset(keybuf
, 0, 16);
185 /* XXX: we could compress the chars to 7 bits to increase
187 for(i
= 0;i
< len
;i
++) {
190 s
->crypt_method
= s
->crypt_method_header
;
192 if (AES_set_encrypt_key(keybuf
, 128, &s
->aes_encrypt_key
) != 0)
194 if (AES_set_decrypt_key(keybuf
, 128, &s
->aes_decrypt_key
) != 0)
204 AES_encrypt(in
, tmp
, &s
->aes_encrypt_key
);
205 AES_decrypt(tmp
, out
, &s
->aes_decrypt_key
);
206 for(i
= 0; i
< 16; i
++)
207 printf(" %02x", tmp
[i
]);
209 for(i
= 0; i
< 16; i
++)
210 printf(" %02x", out
[i
]);
217 /* The crypt function is compatible with the linux cryptoloop
218 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
220 static void encrypt_sectors(BDRVQcowState
*s
, int64_t sector_num
,
221 uint8_t *out_buf
, const uint8_t *in_buf
,
222 int nb_sectors
, int enc
,
231 for(i
= 0; i
< nb_sectors
; i
++) {
232 ivec
.ll
[0] = cpu_to_le64(sector_num
);
234 AES_cbc_encrypt(in_buf
, out_buf
, 512, key
,
246 * 1 to allocate a normal cluster (for sector indexes 'n_start' to
249 * 2 to allocate a compressed cluster of size
250 * 'compressed_size'. 'compressed_size' must be > 0 and <
253 * return 0 if not allocated.
255 static uint64_t get_cluster_offset(BlockDriverState
*bs
,
256 uint64_t offset
, int allocate
,
258 int n_start
, int n_end
)
260 BDRVQcowState
*s
= bs
->opaque
;
261 int min_index
, i
, j
, l1_index
, l2_index
;
262 uint64_t l2_offset
, *l2_table
, cluster_offset
, tmp
;
266 l1_index
= offset
>> (s
->l2_bits
+ s
->cluster_bits
);
267 l2_offset
= s
->l1_table
[l1_index
];
272 /* allocate a new l2 entry */
273 l2_offset
= bdrv_getlength(s
->hd
);
274 /* round to cluster size */
275 l2_offset
= (l2_offset
+ s
->cluster_size
- 1) & ~(s
->cluster_size
- 1);
276 /* update the L1 entry */
277 s
->l1_table
[l1_index
] = l2_offset
;
278 tmp
= cpu_to_be64(l2_offset
);
279 if (bdrv_pwrite(s
->hd
, s
->l1_table_offset
+ l1_index
* sizeof(tmp
),
280 &tmp
, sizeof(tmp
)) != sizeof(tmp
))
284 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
285 if (l2_offset
== s
->l2_cache_offsets
[i
]) {
286 /* increment the hit count */
287 if (++s
->l2_cache_counts
[i
] == 0xffffffff) {
288 for(j
= 0; j
< L2_CACHE_SIZE
; j
++) {
289 s
->l2_cache_counts
[j
] >>= 1;
292 l2_table
= s
->l2_cache
+ (i
<< s
->l2_bits
);
296 /* not found: load a new entry in the least used one */
298 min_count
= 0xffffffff;
299 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
300 if (s
->l2_cache_counts
[i
] < min_count
) {
301 min_count
= s
->l2_cache_counts
[i
];
305 l2_table
= s
->l2_cache
+ (min_index
<< s
->l2_bits
);
307 memset(l2_table
, 0, s
->l2_size
* sizeof(uint64_t));
308 if (bdrv_pwrite(s
->hd
, l2_offset
, l2_table
, s
->l2_size
* sizeof(uint64_t)) !=
309 s
->l2_size
* sizeof(uint64_t))
312 if (bdrv_pread(s
->hd
, l2_offset
, l2_table
, s
->l2_size
* sizeof(uint64_t)) !=
313 s
->l2_size
* sizeof(uint64_t))
316 s
->l2_cache_offsets
[min_index
] = l2_offset
;
317 s
->l2_cache_counts
[min_index
] = 1;
319 l2_index
= (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
320 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
321 if (!cluster_offset
||
322 ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) && allocate
== 1)) {
325 /* allocate a new cluster */
326 if ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) &&
327 (n_end
- n_start
) < s
->cluster_sectors
) {
328 /* if the cluster is already compressed, we must
329 decompress it in the case it is not completely
331 if (decompress_cluster(s
, cluster_offset
) < 0)
333 cluster_offset
= bdrv_getlength(s
->hd
);
334 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
335 ~(s
->cluster_size
- 1);
336 /* write the cluster content */
337 if (bdrv_pwrite(s
->hd
, cluster_offset
, s
->cluster_cache
, s
->cluster_size
) !=
341 cluster_offset
= bdrv_getlength(s
->hd
);
343 /* round to cluster size */
344 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
345 ~(s
->cluster_size
- 1);
346 bdrv_truncate(s
->hd
, cluster_offset
+ s
->cluster_size
);
347 /* if encrypted, we must initialize the cluster
348 content which won't be written */
349 if (s
->crypt_method
&&
350 (n_end
- n_start
) < s
->cluster_sectors
) {
352 start_sect
= (offset
& ~(s
->cluster_size
- 1)) >> 9;
353 memset(s
->cluster_data
+ 512, 0x00, 512);
354 for(i
= 0; i
< s
->cluster_sectors
; i
++) {
355 if (i
< n_start
|| i
>= n_end
) {
356 encrypt_sectors(s
, start_sect
+ i
,
358 s
->cluster_data
+ 512, 1, 1,
359 &s
->aes_encrypt_key
);
360 if (bdrv_pwrite(s
->hd
, cluster_offset
+ i
* 512,
361 s
->cluster_data
, 512) != 512)
366 } else if (allocate
== 2) {
367 cluster_offset
|= QCOW_OFLAG_COMPRESSED
|
368 (uint64_t)compressed_size
<< (63 - s
->cluster_bits
);
371 /* update L2 table */
372 tmp
= cpu_to_be64(cluster_offset
);
373 l2_table
[l2_index
] = tmp
;
374 if (bdrv_pwrite(s
->hd
,
375 l2_offset
+ l2_index
* sizeof(tmp
), &tmp
, sizeof(tmp
)) != sizeof(tmp
))
378 return cluster_offset
;
381 static int qcow_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
382 int nb_sectors
, int *pnum
)
384 BDRVQcowState
*s
= bs
->opaque
;
385 int index_in_cluster
, n
;
386 uint64_t cluster_offset
;
388 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 0, 0, 0, 0);
389 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
390 n
= s
->cluster_sectors
- index_in_cluster
;
394 return (cluster_offset
!= 0);
397 static int decompress_buffer(uint8_t *out_buf
, int out_buf_size
,
398 const uint8_t *buf
, int buf_size
)
400 z_stream strm1
, *strm
= &strm1
;
403 memset(strm
, 0, sizeof(*strm
));
405 strm
->next_in
= (uint8_t *)buf
;
406 strm
->avail_in
= buf_size
;
407 strm
->next_out
= out_buf
;
408 strm
->avail_out
= out_buf_size
;
410 ret
= inflateInit2(strm
, -12);
413 ret
= inflate(strm
, Z_FINISH
);
414 out_len
= strm
->next_out
- out_buf
;
415 if ((ret
!= Z_STREAM_END
&& ret
!= Z_BUF_ERROR
) ||
416 out_len
!= out_buf_size
) {
424 static int decompress_cluster(BDRVQcowState
*s
, uint64_t cluster_offset
)
429 coffset
= cluster_offset
& s
->cluster_offset_mask
;
430 if (s
->cluster_cache_offset
!= coffset
) {
431 csize
= cluster_offset
>> (63 - s
->cluster_bits
);
432 csize
&= (s
->cluster_size
- 1);
433 ret
= bdrv_pread(s
->hd
, coffset
, s
->cluster_data
, csize
);
436 if (decompress_buffer(s
->cluster_cache
, s
->cluster_size
,
437 s
->cluster_data
, csize
) < 0) {
440 s
->cluster_cache_offset
= coffset
;
447 static int qcow_read(BlockDriverState
*bs
, int64_t sector_num
,
448 uint8_t *buf
, int nb_sectors
)
450 BDRVQcowState
*s
= bs
->opaque
;
451 int ret
, index_in_cluster
, n
;
452 uint64_t cluster_offset
;
454 while (nb_sectors
> 0) {
455 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 0, 0, 0, 0);
456 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
457 n
= s
->cluster_sectors
- index_in_cluster
;
460 if (!cluster_offset
) {
461 if (bs
->backing_hd
) {
462 /* read from the base image */
463 ret
= bdrv_read(bs
->backing_hd
, sector_num
, buf
, n
);
467 memset(buf
, 0, 512 * n
);
469 } else if (cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
470 if (decompress_cluster(s
, cluster_offset
) < 0)
472 memcpy(buf
, s
->cluster_cache
+ index_in_cluster
* 512, 512 * n
);
474 ret
= bdrv_pread(s
->hd
, cluster_offset
+ index_in_cluster
* 512, buf
, n
* 512);
477 if (s
->crypt_method
) {
478 encrypt_sectors(s
, sector_num
, buf
, buf
, n
, 0,
479 &s
->aes_decrypt_key
);
490 static int qcow_write(BlockDriverState
*bs
, int64_t sector_num
,
491 const uint8_t *buf
, int nb_sectors
)
493 BDRVQcowState
*s
= bs
->opaque
;
494 int ret
, index_in_cluster
, n
;
495 uint64_t cluster_offset
;
497 while (nb_sectors
> 0) {
498 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
499 n
= s
->cluster_sectors
- index_in_cluster
;
502 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 1, 0,
504 index_in_cluster
+ n
);
507 if (s
->crypt_method
) {
508 encrypt_sectors(s
, sector_num
, s
->cluster_data
, buf
, n
, 1,
509 &s
->aes_encrypt_key
);
510 ret
= bdrv_pwrite(s
->hd
, cluster_offset
+ index_in_cluster
* 512,
511 s
->cluster_data
, n
* 512);
513 ret
= bdrv_pwrite(s
->hd
, cluster_offset
+ index_in_cluster
* 512, buf
, n
* 512);
521 s
->cluster_cache_offset
= -1; /* disable compressed cache */
525 typedef struct QCowAIOCB
{
526 BlockDriverAIOCB common
;
533 uint64_t cluster_offset
;
534 uint8_t *cluster_data
;
536 QEMUIOVector hd_qiov
;
537 BlockDriverAIOCB
*hd_aiocb
;
540 static void qcow_aio_read_cb(void *opaque
, int ret
)
542 QCowAIOCB
*acb
= opaque
;
543 BlockDriverState
*bs
= acb
->common
.bs
;
544 BDRVQcowState
*s
= bs
->opaque
;
545 int index_in_cluster
;
547 acb
->hd_aiocb
= NULL
;
552 /* post process the read buffer */
553 if (!acb
->cluster_offset
) {
555 } else if (acb
->cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
558 if (s
->crypt_method
) {
559 encrypt_sectors(s
, acb
->sector_num
, acb
->buf
, acb
->buf
,
561 &s
->aes_decrypt_key
);
565 acb
->nb_sectors
-= acb
->n
;
566 acb
->sector_num
+= acb
->n
;
567 acb
->buf
+= acb
->n
* 512;
569 if (acb
->nb_sectors
== 0) {
570 /* request completed */
575 /* prepare next AIO request */
576 acb
->cluster_offset
= get_cluster_offset(bs
, acb
->sector_num
<< 9,
578 index_in_cluster
= acb
->sector_num
& (s
->cluster_sectors
- 1);
579 acb
->n
= s
->cluster_sectors
- index_in_cluster
;
580 if (acb
->n
> acb
->nb_sectors
)
581 acb
->n
= acb
->nb_sectors
;
583 if (!acb
->cluster_offset
) {
584 if (bs
->backing_hd
) {
585 /* read from the base image */
586 acb
->hd_iov
.iov_base
= (void *)acb
->buf
;
587 acb
->hd_iov
.iov_len
= acb
->n
* 512;
588 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
589 acb
->hd_aiocb
= bdrv_aio_readv(bs
->backing_hd
, acb
->sector_num
,
590 &acb
->hd_qiov
, acb
->n
, qcow_aio_read_cb
, acb
);
591 if (acb
->hd_aiocb
== NULL
)
594 /* Note: in this case, no need to wait */
595 memset(acb
->buf
, 0, 512 * acb
->n
);
598 } else if (acb
->cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
599 /* add AIO support for compressed blocks ? */
600 if (decompress_cluster(s
, acb
->cluster_offset
) < 0)
603 s
->cluster_cache
+ index_in_cluster
* 512, 512 * acb
->n
);
606 if ((acb
->cluster_offset
& 511) != 0) {
610 acb
->hd_iov
.iov_base
= (void *)acb
->buf
;
611 acb
->hd_iov
.iov_len
= acb
->n
* 512;
612 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
613 acb
->hd_aiocb
= bdrv_aio_readv(s
->hd
,
614 (acb
->cluster_offset
>> 9) + index_in_cluster
,
615 &acb
->hd_qiov
, acb
->n
, qcow_aio_read_cb
, acb
);
616 if (acb
->hd_aiocb
== NULL
)
623 if (acb
->qiov
->niov
> 1) {
624 qemu_iovec_from_buffer(acb
->qiov
, acb
->orig_buf
, acb
->qiov
->size
);
625 qemu_vfree(acb
->orig_buf
);
627 acb
->common
.cb(acb
->common
.opaque
, ret
);
628 qemu_aio_release(acb
);
631 static BlockDriverAIOCB
*qcow_aio_readv(BlockDriverState
*bs
,
632 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
633 BlockDriverCompletionFunc
*cb
, void *opaque
)
637 acb
= qemu_aio_get(bs
, cb
, opaque
);
640 acb
->hd_aiocb
= NULL
;
641 acb
->sector_num
= sector_num
;
644 acb
->buf
= acb
->orig_buf
= qemu_blockalign(bs
, qiov
->size
);
646 acb
->buf
= (uint8_t *)qiov
->iov
->iov_base
;
647 acb
->nb_sectors
= nb_sectors
;
649 acb
->cluster_offset
= 0;
651 qcow_aio_read_cb(acb
, 0);
655 static void qcow_aio_write_cb(void *opaque
, int ret
)
657 QCowAIOCB
*acb
= opaque
;
658 BlockDriverState
*bs
= acb
->common
.bs
;
659 BDRVQcowState
*s
= bs
->opaque
;
660 int index_in_cluster
;
661 uint64_t cluster_offset
;
662 const uint8_t *src_buf
;
664 acb
->hd_aiocb
= NULL
;
669 acb
->nb_sectors
-= acb
->n
;
670 acb
->sector_num
+= acb
->n
;
671 acb
->buf
+= acb
->n
* 512;
673 if (acb
->nb_sectors
== 0) {
674 /* request completed */
679 index_in_cluster
= acb
->sector_num
& (s
->cluster_sectors
- 1);
680 acb
->n
= s
->cluster_sectors
- index_in_cluster
;
681 if (acb
->n
> acb
->nb_sectors
)
682 acb
->n
= acb
->nb_sectors
;
683 cluster_offset
= get_cluster_offset(bs
, acb
->sector_num
<< 9, 1, 0,
685 index_in_cluster
+ acb
->n
);
686 if (!cluster_offset
|| (cluster_offset
& 511) != 0) {
690 if (s
->crypt_method
) {
691 if (!acb
->cluster_data
) {
692 acb
->cluster_data
= qemu_mallocz(s
->cluster_size
);
693 if (!acb
->cluster_data
) {
698 encrypt_sectors(s
, acb
->sector_num
, acb
->cluster_data
, acb
->buf
,
699 acb
->n
, 1, &s
->aes_encrypt_key
);
700 src_buf
= acb
->cluster_data
;
705 acb
->hd_iov
.iov_base
= (void *)src_buf
;
706 acb
->hd_iov
.iov_len
= acb
->n
* 512;
707 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
708 acb
->hd_aiocb
= bdrv_aio_writev(s
->hd
,
709 (cluster_offset
>> 9) + index_in_cluster
,
710 &acb
->hd_qiov
, acb
->n
,
711 qcow_aio_write_cb
, acb
);
712 if (acb
->hd_aiocb
== NULL
)
717 if (acb
->qiov
->niov
> 1)
718 qemu_vfree(acb
->orig_buf
);
719 acb
->common
.cb(acb
->common
.opaque
, ret
);
720 qemu_aio_release(acb
);
723 static BlockDriverAIOCB
*qcow_aio_writev(BlockDriverState
*bs
,
724 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
725 BlockDriverCompletionFunc
*cb
, void *opaque
)
727 BDRVQcowState
*s
= bs
->opaque
;
730 s
->cluster_cache_offset
= -1; /* disable compressed cache */
732 acb
= qemu_aio_get(bs
, cb
, opaque
);
735 acb
->hd_aiocb
= NULL
;
736 acb
->sector_num
= sector_num
;
738 if (qiov
->niov
> 1) {
739 acb
->buf
= acb
->orig_buf
= qemu_blockalign(bs
, qiov
->size
);
740 qemu_iovec_to_buffer(qiov
, acb
->buf
);
742 acb
->buf
= (uint8_t *)qiov
->iov
->iov_base
;
744 acb
->nb_sectors
= nb_sectors
;
747 qcow_aio_write_cb(acb
, 0);
751 static void qcow_aio_cancel(BlockDriverAIOCB
*blockacb
)
753 QCowAIOCB
*acb
= (QCowAIOCB
*)blockacb
;
755 bdrv_aio_cancel(acb
->hd_aiocb
);
756 qemu_aio_release(acb
);
759 static void qcow_close(BlockDriverState
*bs
)
761 BDRVQcowState
*s
= bs
->opaque
;
762 qemu_free(s
->l1_table
);
763 qemu_free(s
->l2_cache
);
764 qemu_free(s
->cluster_cache
);
765 qemu_free(s
->cluster_data
);
769 static int qcow_create(const char *filename
, int64_t total_size
,
770 const char *backing_file
, int flags
)
772 int fd
, header_size
, backing_filename_len
, l1_size
, i
, shift
;
776 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0644);
779 memset(&header
, 0, sizeof(header
));
780 header
.magic
= cpu_to_be32(QCOW_MAGIC
);
781 header
.version
= cpu_to_be32(QCOW_VERSION
);
782 header
.size
= cpu_to_be64(total_size
* 512);
783 header_size
= sizeof(header
);
784 backing_filename_len
= 0;
786 if (strcmp(backing_file
, "fat:")) {
787 header
.backing_file_offset
= cpu_to_be64(header_size
);
788 backing_filename_len
= strlen(backing_file
);
789 header
.backing_file_size
= cpu_to_be32(backing_filename_len
);
790 header_size
+= backing_filename_len
;
792 /* special backing file for vvfat */
795 header
.cluster_bits
= 9; /* 512 byte cluster to avoid copying
796 unmodifyed sectors */
797 header
.l2_bits
= 12; /* 32 KB L2 tables */
799 header
.cluster_bits
= 12; /* 4 KB clusters */
800 header
.l2_bits
= 9; /* 4 KB L2 tables */
802 header_size
= (header_size
+ 7) & ~7;
803 shift
= header
.cluster_bits
+ header
.l2_bits
;
804 l1_size
= ((total_size
* 512) + (1LL << shift
) - 1) >> shift
;
806 header
.l1_table_offset
= cpu_to_be64(header_size
);
807 if (flags
& BLOCK_FLAG_ENCRYPT
) {
808 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
810 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
813 /* write all the data */
814 write(fd
, &header
, sizeof(header
));
816 write(fd
, backing_file
, backing_filename_len
);
818 lseek(fd
, header_size
, SEEK_SET
);
820 for(i
= 0;i
< l1_size
; i
++) {
821 write(fd
, &tmp
, sizeof(tmp
));
827 static int qcow_make_empty(BlockDriverState
*bs
)
829 BDRVQcowState
*s
= bs
->opaque
;
830 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
833 memset(s
->l1_table
, 0, l1_length
);
834 if (bdrv_pwrite(s
->hd
, s
->l1_table_offset
, s
->l1_table
, l1_length
) < 0)
836 ret
= bdrv_truncate(s
->hd
, s
->l1_table_offset
+ l1_length
);
840 memset(s
->l2_cache
, 0, s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
841 memset(s
->l2_cache_offsets
, 0, L2_CACHE_SIZE
* sizeof(uint64_t));
842 memset(s
->l2_cache_counts
, 0, L2_CACHE_SIZE
* sizeof(uint32_t));
847 /* XXX: put compressed sectors first, then all the cluster aligned
848 tables to avoid losing bytes in alignment */
849 static int qcow_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
850 const uint8_t *buf
, int nb_sectors
)
852 BDRVQcowState
*s
= bs
->opaque
;
856 uint64_t cluster_offset
;
858 if (nb_sectors
!= s
->cluster_sectors
)
861 out_buf
= qemu_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
865 /* best compression, small window, no zlib header */
866 memset(&strm
, 0, sizeof(strm
));
867 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
869 9, Z_DEFAULT_STRATEGY
);
875 strm
.avail_in
= s
->cluster_size
;
876 strm
.next_in
= (uint8_t *)buf
;
877 strm
.avail_out
= s
->cluster_size
;
878 strm
.next_out
= out_buf
;
880 ret
= deflate(&strm
, Z_FINISH
);
881 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
886 out_len
= strm
.next_out
- out_buf
;
890 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
891 /* could not compress: write normal cluster */
892 qcow_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
894 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 2,
896 cluster_offset
&= s
->cluster_offset_mask
;
897 if (bdrv_pwrite(s
->hd
, cluster_offset
, out_buf
, out_len
) != out_len
) {
907 static void qcow_flush(BlockDriverState
*bs
)
909 BDRVQcowState
*s
= bs
->opaque
;
913 static int qcow_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
915 BDRVQcowState
*s
= bs
->opaque
;
916 bdi
->cluster_size
= s
->cluster_size
;
920 BlockDriver bdrv_qcow
= {
921 .format_name
= "qcow",
922 .instance_size
= sizeof(BDRVQcowState
),
923 .bdrv_probe
= qcow_probe
,
924 .bdrv_open
= qcow_open
,
925 .bdrv_close
= qcow_close
,
926 .bdrv_create
= qcow_create
,
927 .bdrv_flush
= qcow_flush
,
928 .bdrv_is_allocated
= qcow_is_allocated
,
929 .bdrv_set_key
= qcow_set_key
,
930 .bdrv_make_empty
= qcow_make_empty
,
931 .bdrv_aio_readv
= qcow_aio_readv
,
932 .bdrv_aio_writev
= qcow_aio_writev
,
933 .bdrv_aio_cancel
= qcow_aio_cancel
,
934 .aiocb_size
= sizeof(QCowAIOCB
),
935 .bdrv_write_compressed
= qcow_write_compressed
,
936 .bdrv_get_info
= qcow_get_info
,