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)
203 AES_encrypt(in
, tmp
, &s
->aes_encrypt_key
);
204 AES_decrypt(tmp
, out
, &s
->aes_decrypt_key
);
205 for(i
= 0; i
< 16; i
++)
206 printf(" %02x", tmp
[i
]);
208 for(i
= 0; i
< 16; i
++)
209 printf(" %02x", out
[i
]);
216 /* The crypt function is compatible with the linux cryptoloop
217 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
219 static void encrypt_sectors(BDRVQcowState
*s
, int64_t sector_num
,
220 uint8_t *out_buf
, const uint8_t *in_buf
,
221 int nb_sectors
, int enc
,
230 for(i
= 0; i
< nb_sectors
; i
++) {
231 ivec
.ll
[0] = cpu_to_le64(sector_num
);
233 AES_cbc_encrypt(in_buf
, out_buf
, 512, key
,
245 * 1 to allocate a normal cluster (for sector indexes 'n_start' to
248 * 2 to allocate a compressed cluster of size
249 * 'compressed_size'. 'compressed_size' must be > 0 and <
252 * return 0 if not allocated.
254 static uint64_t get_cluster_offset(BlockDriverState
*bs
,
255 uint64_t offset
, int allocate
,
257 int n_start
, int n_end
)
259 BDRVQcowState
*s
= bs
->opaque
;
260 int min_index
, i
, j
, l1_index
, l2_index
;
261 uint64_t l2_offset
, *l2_table
, cluster_offset
, tmp
;
265 l1_index
= offset
>> (s
->l2_bits
+ s
->cluster_bits
);
266 l2_offset
= s
->l1_table
[l1_index
];
271 /* allocate a new l2 entry */
272 l2_offset
= bdrv_getlength(bs
->file
);
273 /* round to cluster size */
274 l2_offset
= (l2_offset
+ s
->cluster_size
- 1) & ~(s
->cluster_size
- 1);
275 /* update the L1 entry */
276 s
->l1_table
[l1_index
] = l2_offset
;
277 tmp
= cpu_to_be64(l2_offset
);
278 if (bdrv_pwrite_sync(bs
->file
,
279 s
->l1_table_offset
+ l1_index
* sizeof(tmp
),
280 &tmp
, sizeof(tmp
)) < 0)
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_sync(bs
->file
, l2_offset
, l2_table
,
309 s
->l2_size
* sizeof(uint64_t)) < 0)
312 if (bdrv_pread(bs
->file
, 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(bs
, cluster_offset
) < 0)
333 cluster_offset
= bdrv_getlength(bs
->file
);
334 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
335 ~(s
->cluster_size
- 1);
336 /* write the cluster content */
337 if (bdrv_pwrite(bs
->file
, cluster_offset
, s
->cluster_cache
, s
->cluster_size
) !=
341 cluster_offset
= bdrv_getlength(bs
->file
);
343 /* round to cluster size */
344 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
345 ~(s
->cluster_size
- 1);
346 bdrv_truncate(bs
->file
, 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(bs
->file
, 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_sync(bs
->file
, l2_offset
+ l2_index
* sizeof(tmp
),
375 &tmp
, sizeof(tmp
)) < 0)
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(BlockDriverState
*bs
, uint64_t cluster_offset
)
426 BDRVQcowState
*s
= bs
->opaque
;
430 coffset
= cluster_offset
& s
->cluster_offset_mask
;
431 if (s
->cluster_cache_offset
!= coffset
) {
432 csize
= cluster_offset
>> (63 - s
->cluster_bits
);
433 csize
&= (s
->cluster_size
- 1);
434 ret
= bdrv_pread(bs
->file
, coffset
, s
->cluster_data
, csize
);
437 if (decompress_buffer(s
->cluster_cache
, s
->cluster_size
,
438 s
->cluster_data
, csize
) < 0) {
441 s
->cluster_cache_offset
= coffset
;
448 static int qcow_read(BlockDriverState
*bs
, int64_t sector_num
,
449 uint8_t *buf
, int nb_sectors
)
451 BDRVQcowState
*s
= bs
->opaque
;
452 int ret
, index_in_cluster
, n
;
453 uint64_t cluster_offset
;
455 while (nb_sectors
> 0) {
456 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 0, 0, 0, 0);
457 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
458 n
= s
->cluster_sectors
- index_in_cluster
;
461 if (!cluster_offset
) {
462 if (bs
->backing_hd
) {
463 /* read from the base image */
464 ret
= bdrv_read(bs
->backing_hd
, sector_num
, buf
, n
);
468 memset(buf
, 0, 512 * n
);
470 } else if (cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
471 if (decompress_cluster(bs
, cluster_offset
) < 0)
473 memcpy(buf
, s
->cluster_cache
+ index_in_cluster
* 512, 512 * n
);
475 ret
= bdrv_pread(bs
->file
, cluster_offset
+ index_in_cluster
* 512, buf
, n
* 512);
478 if (s
->crypt_method
) {
479 encrypt_sectors(s
, sector_num
, buf
, buf
, n
, 0,
480 &s
->aes_decrypt_key
);
491 typedef struct QCowAIOCB
{
492 BlockDriverAIOCB common
;
499 uint64_t cluster_offset
;
500 uint8_t *cluster_data
;
504 QEMUIOVector hd_qiov
;
505 BlockDriverAIOCB
*hd_aiocb
;
508 static QCowAIOCB
*qcow_aio_setup(BlockDriverState
*bs
,
509 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
510 int is_write
, QCowAIOCB
*acb
)
512 memset(acb
, 0, sizeof(*acb
));
514 acb
->hd_aiocb
= NULL
;
515 acb
->sector_num
= sector_num
;
517 acb
->is_write
= is_write
;
519 if (qiov
->niov
> 1) {
520 acb
->buf
= acb
->orig_buf
= qemu_blockalign(bs
, qiov
->size
);
522 qemu_iovec_to_buffer(qiov
, acb
->buf
);
524 acb
->buf
= (uint8_t *)qiov
->iov
->iov_base
;
526 acb
->nb_sectors
= nb_sectors
;
528 acb
->cluster_offset
= 0;
532 static int qcow_aio_read_cb(QCowAIOCB
*acb
)
534 BlockDriverState
*bs
= acb
->common
.bs
;
535 BDRVQcowState
*s
= bs
->opaque
;
536 int index_in_cluster
;
539 acb
->hd_aiocb
= NULL
;
542 /* post process the read buffer */
543 if (!acb
->cluster_offset
) {
545 } else if (acb
->cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
548 if (s
->crypt_method
) {
549 encrypt_sectors(s
, acb
->sector_num
, acb
->buf
, acb
->buf
,
551 &s
->aes_decrypt_key
);
555 acb
->nb_sectors
-= acb
->n
;
556 acb
->sector_num
+= acb
->n
;
557 acb
->buf
+= acb
->n
* 512;
559 if (acb
->nb_sectors
== 0) {
560 /* request completed */
564 /* prepare next AIO request */
565 acb
->cluster_offset
= get_cluster_offset(bs
, acb
->sector_num
<< 9,
567 index_in_cluster
= acb
->sector_num
& (s
->cluster_sectors
- 1);
568 acb
->n
= s
->cluster_sectors
- index_in_cluster
;
569 if (acb
->n
> acb
->nb_sectors
)
570 acb
->n
= acb
->nb_sectors
;
572 if (!acb
->cluster_offset
) {
573 if (bs
->backing_hd
) {
574 /* read from the base image */
575 acb
->hd_iov
.iov_base
= (void *)acb
->buf
;
576 acb
->hd_iov
.iov_len
= acb
->n
* 512;
577 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
578 qemu_co_mutex_unlock(&s
->lock
);
579 ret
= bdrv_co_readv(bs
->backing_hd
, acb
->sector_num
,
580 acb
->n
, &acb
->hd_qiov
);
581 qemu_co_mutex_lock(&s
->lock
);
586 /* Note: in this case, no need to wait */
587 memset(acb
->buf
, 0, 512 * acb
->n
);
590 } else if (acb
->cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
591 /* add AIO support for compressed blocks ? */
592 if (decompress_cluster(bs
, acb
->cluster_offset
) < 0) {
596 s
->cluster_cache
+ index_in_cluster
* 512, 512 * acb
->n
);
599 if ((acb
->cluster_offset
& 511) != 0) {
602 acb
->hd_iov
.iov_base
= (void *)acb
->buf
;
603 acb
->hd_iov
.iov_len
= acb
->n
* 512;
604 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
605 qemu_co_mutex_unlock(&s
->lock
);
606 ret
= bdrv_co_readv(bs
->file
,
607 (acb
->cluster_offset
>> 9) + index_in_cluster
,
608 acb
->n
, &acb
->hd_qiov
);
609 qemu_co_mutex_lock(&s
->lock
);
618 static int qcow_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
619 int nb_sectors
, QEMUIOVector
*qiov
)
621 BDRVQcowState
*s
= bs
->opaque
;
625 qcow_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, 0, &acb
);
627 qemu_co_mutex_lock(&s
->lock
);
629 ret
= qcow_aio_read_cb(&acb
);
631 qemu_co_mutex_unlock(&s
->lock
);
633 if (acb
.qiov
->niov
> 1) {
634 qemu_iovec_from_buffer(acb
.qiov
, acb
.orig_buf
, acb
.qiov
->size
);
635 qemu_vfree(acb
.orig_buf
);
641 static int qcow_aio_write_cb(QCowAIOCB
*acb
)
643 BlockDriverState
*bs
= acb
->common
.bs
;
644 BDRVQcowState
*s
= bs
->opaque
;
645 int index_in_cluster
;
646 uint64_t cluster_offset
;
647 const uint8_t *src_buf
;
650 acb
->hd_aiocb
= NULL
;
652 acb
->nb_sectors
-= acb
->n
;
653 acb
->sector_num
+= acb
->n
;
654 acb
->buf
+= acb
->n
* 512;
656 if (acb
->nb_sectors
== 0) {
657 /* request completed */
661 index_in_cluster
= acb
->sector_num
& (s
->cluster_sectors
- 1);
662 acb
->n
= s
->cluster_sectors
- index_in_cluster
;
663 if (acb
->n
> acb
->nb_sectors
)
664 acb
->n
= acb
->nb_sectors
;
665 cluster_offset
= get_cluster_offset(bs
, acb
->sector_num
<< 9, 1, 0,
667 index_in_cluster
+ acb
->n
);
668 if (!cluster_offset
|| (cluster_offset
& 511) != 0) {
671 if (s
->crypt_method
) {
672 if (!acb
->cluster_data
) {
673 acb
->cluster_data
= g_malloc0(s
->cluster_size
);
675 encrypt_sectors(s
, acb
->sector_num
, acb
->cluster_data
, acb
->buf
,
676 acb
->n
, 1, &s
->aes_encrypt_key
);
677 src_buf
= acb
->cluster_data
;
682 acb
->hd_iov
.iov_base
= (void *)src_buf
;
683 acb
->hd_iov
.iov_len
= acb
->n
* 512;
684 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
685 qemu_co_mutex_unlock(&s
->lock
);
686 ret
= bdrv_co_writev(bs
->file
,
687 (cluster_offset
>> 9) + index_in_cluster
,
688 acb
->n
, &acb
->hd_qiov
);
689 qemu_co_mutex_lock(&s
->lock
);
696 static int qcow_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
697 int nb_sectors
, QEMUIOVector
*qiov
)
699 BDRVQcowState
*s
= bs
->opaque
;
703 s
->cluster_cache_offset
= -1; /* disable compressed cache */
705 qcow_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, 1, &acb
);
707 qemu_co_mutex_lock(&s
->lock
);
709 ret
= qcow_aio_write_cb(&acb
);
711 qemu_co_mutex_unlock(&s
->lock
);
713 if (acb
.qiov
->niov
> 1) {
714 qemu_vfree(acb
.orig_buf
);
720 static void qcow_close(BlockDriverState
*bs
)
722 BDRVQcowState
*s
= bs
->opaque
;
725 g_free(s
->cluster_cache
);
726 g_free(s
->cluster_data
);
729 static int qcow_create(const char *filename
, QEMUOptionParameter
*options
)
731 int fd
, header_size
, backing_filename_len
, l1_size
, i
, shift
;
734 int64_t total_size
= 0;
735 const char *backing_file
= NULL
;
739 /* Read out options */
740 while (options
&& options
->name
) {
741 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
742 total_size
= options
->value
.n
/ 512;
743 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
744 backing_file
= options
->value
.s
;
745 } else if (!strcmp(options
->name
, BLOCK_OPT_ENCRYPT
)) {
746 flags
|= options
->value
.n
? BLOCK_FLAG_ENCRYPT
: 0;
751 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0644);
754 memset(&header
, 0, sizeof(header
));
755 header
.magic
= cpu_to_be32(QCOW_MAGIC
);
756 header
.version
= cpu_to_be32(QCOW_VERSION
);
757 header
.size
= cpu_to_be64(total_size
* 512);
758 header_size
= sizeof(header
);
759 backing_filename_len
= 0;
761 if (strcmp(backing_file
, "fat:")) {
762 header
.backing_file_offset
= cpu_to_be64(header_size
);
763 backing_filename_len
= strlen(backing_file
);
764 header
.backing_file_size
= cpu_to_be32(backing_filename_len
);
765 header_size
+= backing_filename_len
;
767 /* special backing file for vvfat */
770 header
.cluster_bits
= 9; /* 512 byte cluster to avoid copying
771 unmodifyed sectors */
772 header
.l2_bits
= 12; /* 32 KB L2 tables */
774 header
.cluster_bits
= 12; /* 4 KB clusters */
775 header
.l2_bits
= 9; /* 4 KB L2 tables */
777 header_size
= (header_size
+ 7) & ~7;
778 shift
= header
.cluster_bits
+ header
.l2_bits
;
779 l1_size
= ((total_size
* 512) + (1LL << shift
) - 1) >> shift
;
781 header
.l1_table_offset
= cpu_to_be64(header_size
);
782 if (flags
& BLOCK_FLAG_ENCRYPT
) {
783 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
785 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
788 /* write all the data */
789 ret
= qemu_write_full(fd
, &header
, sizeof(header
));
790 if (ret
!= sizeof(header
)) {
796 ret
= qemu_write_full(fd
, backing_file
, backing_filename_len
);
797 if (ret
!= backing_filename_len
) {
803 lseek(fd
, header_size
, SEEK_SET
);
805 for(i
= 0;i
< l1_size
; i
++) {
806 ret
= qemu_write_full(fd
, &tmp
, sizeof(tmp
));
807 if (ret
!= sizeof(tmp
)) {
819 static int qcow_make_empty(BlockDriverState
*bs
)
821 BDRVQcowState
*s
= bs
->opaque
;
822 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
825 memset(s
->l1_table
, 0, l1_length
);
826 if (bdrv_pwrite_sync(bs
->file
, s
->l1_table_offset
, s
->l1_table
,
829 ret
= bdrv_truncate(bs
->file
, s
->l1_table_offset
+ l1_length
);
833 memset(s
->l2_cache
, 0, s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
834 memset(s
->l2_cache_offsets
, 0, L2_CACHE_SIZE
* sizeof(uint64_t));
835 memset(s
->l2_cache_counts
, 0, L2_CACHE_SIZE
* sizeof(uint32_t));
840 /* XXX: put compressed sectors first, then all the cluster aligned
841 tables to avoid losing bytes in alignment */
842 static int qcow_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
843 const uint8_t *buf
, int nb_sectors
)
845 BDRVQcowState
*s
= bs
->opaque
;
849 uint64_t cluster_offset
;
851 if (nb_sectors
!= s
->cluster_sectors
)
854 out_buf
= g_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
858 /* best compression, small window, no zlib header */
859 memset(&strm
, 0, sizeof(strm
));
860 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
862 9, Z_DEFAULT_STRATEGY
);
868 strm
.avail_in
= s
->cluster_size
;
869 strm
.next_in
= (uint8_t *)buf
;
870 strm
.avail_out
= s
->cluster_size
;
871 strm
.next_out
= out_buf
;
873 ret
= deflate(&strm
, Z_FINISH
);
874 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
879 out_len
= strm
.next_out
- out_buf
;
883 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
884 /* could not compress: write normal cluster */
885 bdrv_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
887 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 2,
889 cluster_offset
&= s
->cluster_offset_mask
;
890 if (bdrv_pwrite(bs
->file
, cluster_offset
, out_buf
, out_len
) != out_len
) {
900 static int qcow_flush(BlockDriverState
*bs
)
902 return bdrv_flush(bs
->file
);
905 static BlockDriverAIOCB
*qcow_aio_flush(BlockDriverState
*bs
,
906 BlockDriverCompletionFunc
*cb
, void *opaque
)
908 return bdrv_aio_flush(bs
->file
, cb
, opaque
);
911 static int qcow_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
913 BDRVQcowState
*s
= bs
->opaque
;
914 bdi
->cluster_size
= s
->cluster_size
;
919 static QEMUOptionParameter qcow_create_options
[] = {
921 .name
= BLOCK_OPT_SIZE
,
923 .help
= "Virtual disk size"
926 .name
= BLOCK_OPT_BACKING_FILE
,
928 .help
= "File name of a base image"
931 .name
= BLOCK_OPT_ENCRYPT
,
933 .help
= "Encrypt the image"
938 static BlockDriver bdrv_qcow
= {
939 .format_name
= "qcow",
940 .instance_size
= sizeof(BDRVQcowState
),
941 .bdrv_probe
= qcow_probe
,
942 .bdrv_open
= qcow_open
,
943 .bdrv_close
= qcow_close
,
944 .bdrv_create
= qcow_create
,
945 .bdrv_flush
= qcow_flush
,
946 .bdrv_is_allocated
= qcow_is_allocated
,
947 .bdrv_set_key
= qcow_set_key
,
948 .bdrv_make_empty
= qcow_make_empty
,
949 .bdrv_co_readv
= qcow_co_readv
,
950 .bdrv_co_writev
= qcow_co_writev
,
951 .bdrv_aio_flush
= qcow_aio_flush
,
952 .bdrv_write_compressed
= qcow_write_compressed
,
953 .bdrv_get_info
= qcow_get_info
,
955 .create_options
= qcow_create_options
,
958 static void bdrv_qcow_init(void)
960 bdrv_register(&bdrv_qcow
);
963 block_init(bdrv_qcow_init
);