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
= qemu_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
= qemu_malloc(s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
145 s
->cluster_cache
= qemu_malloc(s
->cluster_size
);
146 if (!s
->cluster_cache
)
148 s
->cluster_data
= qemu_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';
165 qemu_free(s
->l1_table
);
166 qemu_free(s
->l2_cache
);
167 qemu_free(s
->cluster_cache
);
168 qemu_free(s
->cluster_data
);
172 static int qcow_set_key(BlockDriverState
*bs
, const char *key
)
174 BDRVQcowState
*s
= bs
->opaque
;
178 memset(keybuf
, 0, 16);
182 /* XXX: we could compress the chars to 7 bits to increase
184 for(i
= 0;i
< len
;i
++) {
187 s
->crypt_method
= s
->crypt_method_header
;
189 if (AES_set_encrypt_key(keybuf
, 128, &s
->aes_encrypt_key
) != 0)
191 if (AES_set_decrypt_key(keybuf
, 128, &s
->aes_decrypt_key
) != 0)
201 AES_encrypt(in
, tmp
, &s
->aes_encrypt_key
);
202 AES_decrypt(tmp
, out
, &s
->aes_decrypt_key
);
203 for(i
= 0; i
< 16; i
++)
204 printf(" %02x", tmp
[i
]);
206 for(i
= 0; i
< 16; i
++)
207 printf(" %02x", out
[i
]);
214 /* The crypt function is compatible with the linux cryptoloop
215 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
217 static void encrypt_sectors(BDRVQcowState
*s
, int64_t sector_num
,
218 uint8_t *out_buf
, const uint8_t *in_buf
,
219 int nb_sectors
, int enc
,
228 for(i
= 0; i
< nb_sectors
; i
++) {
229 ivec
.ll
[0] = cpu_to_le64(sector_num
);
231 AES_cbc_encrypt(in_buf
, out_buf
, 512, key
,
243 * 1 to allocate a normal cluster (for sector indexes 'n_start' to
246 * 2 to allocate a compressed cluster of size
247 * 'compressed_size'. 'compressed_size' must be > 0 and <
250 * return 0 if not allocated.
252 static uint64_t get_cluster_offset(BlockDriverState
*bs
,
253 uint64_t offset
, int allocate
,
255 int n_start
, int n_end
)
257 BDRVQcowState
*s
= bs
->opaque
;
258 int min_index
, i
, j
, l1_index
, l2_index
;
259 uint64_t l2_offset
, *l2_table
, cluster_offset
, tmp
;
263 l1_index
= offset
>> (s
->l2_bits
+ s
->cluster_bits
);
264 l2_offset
= s
->l1_table
[l1_index
];
269 /* allocate a new l2 entry */
270 l2_offset
= bdrv_getlength(bs
->file
);
271 /* round to cluster size */
272 l2_offset
= (l2_offset
+ s
->cluster_size
- 1) & ~(s
->cluster_size
- 1);
273 /* update the L1 entry */
274 s
->l1_table
[l1_index
] = l2_offset
;
275 tmp
= cpu_to_be64(l2_offset
);
276 if (bdrv_pwrite_sync(bs
->file
,
277 s
->l1_table_offset
+ l1_index
* sizeof(tmp
),
278 &tmp
, sizeof(tmp
)) < 0)
282 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
283 if (l2_offset
== s
->l2_cache_offsets
[i
]) {
284 /* increment the hit count */
285 if (++s
->l2_cache_counts
[i
] == 0xffffffff) {
286 for(j
= 0; j
< L2_CACHE_SIZE
; j
++) {
287 s
->l2_cache_counts
[j
] >>= 1;
290 l2_table
= s
->l2_cache
+ (i
<< s
->l2_bits
);
294 /* not found: load a new entry in the least used one */
296 min_count
= 0xffffffff;
297 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
298 if (s
->l2_cache_counts
[i
] < min_count
) {
299 min_count
= s
->l2_cache_counts
[i
];
303 l2_table
= s
->l2_cache
+ (min_index
<< s
->l2_bits
);
305 memset(l2_table
, 0, s
->l2_size
* sizeof(uint64_t));
306 if (bdrv_pwrite_sync(bs
->file
, l2_offset
, l2_table
,
307 s
->l2_size
* sizeof(uint64_t)) < 0)
310 if (bdrv_pread(bs
->file
, l2_offset
, l2_table
, s
->l2_size
* sizeof(uint64_t)) !=
311 s
->l2_size
* sizeof(uint64_t))
314 s
->l2_cache_offsets
[min_index
] = l2_offset
;
315 s
->l2_cache_counts
[min_index
] = 1;
317 l2_index
= (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
318 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
319 if (!cluster_offset
||
320 ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) && allocate
== 1)) {
323 /* allocate a new cluster */
324 if ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) &&
325 (n_end
- n_start
) < s
->cluster_sectors
) {
326 /* if the cluster is already compressed, we must
327 decompress it in the case it is not completely
329 if (decompress_cluster(bs
, cluster_offset
) < 0)
331 cluster_offset
= bdrv_getlength(bs
->file
);
332 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
333 ~(s
->cluster_size
- 1);
334 /* write the cluster content */
335 if (bdrv_pwrite(bs
->file
, cluster_offset
, s
->cluster_cache
, s
->cluster_size
) !=
339 cluster_offset
= bdrv_getlength(bs
->file
);
341 /* round to cluster size */
342 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
343 ~(s
->cluster_size
- 1);
344 bdrv_truncate(bs
->file
, cluster_offset
+ s
->cluster_size
);
345 /* if encrypted, we must initialize the cluster
346 content which won't be written */
347 if (s
->crypt_method
&&
348 (n_end
- n_start
) < s
->cluster_sectors
) {
350 start_sect
= (offset
& ~(s
->cluster_size
- 1)) >> 9;
351 memset(s
->cluster_data
+ 512, 0x00, 512);
352 for(i
= 0; i
< s
->cluster_sectors
; i
++) {
353 if (i
< n_start
|| i
>= n_end
) {
354 encrypt_sectors(s
, start_sect
+ i
,
356 s
->cluster_data
+ 512, 1, 1,
357 &s
->aes_encrypt_key
);
358 if (bdrv_pwrite(bs
->file
, cluster_offset
+ i
* 512,
359 s
->cluster_data
, 512) != 512)
364 } else if (allocate
== 2) {
365 cluster_offset
|= QCOW_OFLAG_COMPRESSED
|
366 (uint64_t)compressed_size
<< (63 - s
->cluster_bits
);
369 /* update L2 table */
370 tmp
= cpu_to_be64(cluster_offset
);
371 l2_table
[l2_index
] = tmp
;
372 if (bdrv_pwrite_sync(bs
->file
, l2_offset
+ l2_index
* sizeof(tmp
),
373 &tmp
, sizeof(tmp
)) < 0)
376 return cluster_offset
;
379 static int qcow_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
380 int nb_sectors
, int *pnum
)
382 BDRVQcowState
*s
= bs
->opaque
;
383 int index_in_cluster
, n
;
384 uint64_t cluster_offset
;
386 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 0, 0, 0, 0);
387 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
388 n
= s
->cluster_sectors
- index_in_cluster
;
392 return (cluster_offset
!= 0);
395 static int decompress_buffer(uint8_t *out_buf
, int out_buf_size
,
396 const uint8_t *buf
, int buf_size
)
398 z_stream strm1
, *strm
= &strm1
;
401 memset(strm
, 0, sizeof(*strm
));
403 strm
->next_in
= (uint8_t *)buf
;
404 strm
->avail_in
= buf_size
;
405 strm
->next_out
= out_buf
;
406 strm
->avail_out
= out_buf_size
;
408 ret
= inflateInit2(strm
, -12);
411 ret
= inflate(strm
, Z_FINISH
);
412 out_len
= strm
->next_out
- out_buf
;
413 if ((ret
!= Z_STREAM_END
&& ret
!= Z_BUF_ERROR
) ||
414 out_len
!= out_buf_size
) {
422 static int decompress_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
)
424 BDRVQcowState
*s
= bs
->opaque
;
428 coffset
= cluster_offset
& s
->cluster_offset_mask
;
429 if (s
->cluster_cache_offset
!= coffset
) {
430 csize
= cluster_offset
>> (63 - s
->cluster_bits
);
431 csize
&= (s
->cluster_size
- 1);
432 ret
= bdrv_pread(bs
->file
, coffset
, s
->cluster_data
, csize
);
435 if (decompress_buffer(s
->cluster_cache
, s
->cluster_size
,
436 s
->cluster_data
, csize
) < 0) {
439 s
->cluster_cache_offset
= coffset
;
446 static int qcow_read(BlockDriverState
*bs
, int64_t sector_num
,
447 uint8_t *buf
, int nb_sectors
)
449 BDRVQcowState
*s
= bs
->opaque
;
450 int ret
, index_in_cluster
, n
;
451 uint64_t cluster_offset
;
453 while (nb_sectors
> 0) {
454 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 0, 0, 0, 0);
455 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
456 n
= s
->cluster_sectors
- index_in_cluster
;
459 if (!cluster_offset
) {
460 if (bs
->backing_hd
) {
461 /* read from the base image */
462 ret
= bdrv_read(bs
->backing_hd
, sector_num
, buf
, n
);
466 memset(buf
, 0, 512 * n
);
468 } else if (cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
469 if (decompress_cluster(bs
, cluster_offset
) < 0)
471 memcpy(buf
, s
->cluster_cache
+ index_in_cluster
* 512, 512 * n
);
473 ret
= bdrv_pread(bs
->file
, cluster_offset
+ index_in_cluster
* 512, buf
, n
* 512);
476 if (s
->crypt_method
) {
477 encrypt_sectors(s
, sector_num
, buf
, buf
, n
, 0,
478 &s
->aes_decrypt_key
);
489 typedef struct QCowAIOCB
{
490 BlockDriverAIOCB common
;
497 uint64_t cluster_offset
;
498 uint8_t *cluster_data
;
502 QEMUIOVector hd_qiov
;
503 BlockDriverAIOCB
*hd_aiocb
;
506 static void qcow_aio_cancel(BlockDriverAIOCB
*blockacb
)
508 QCowAIOCB
*acb
= container_of(blockacb
, QCowAIOCB
, common
);
510 bdrv_aio_cancel(acb
->hd_aiocb
);
511 qemu_aio_release(acb
);
514 static AIOPool qcow_aio_pool
= {
515 .aiocb_size
= sizeof(QCowAIOCB
),
516 .cancel
= qcow_aio_cancel
,
519 static QCowAIOCB
*qcow_aio_setup(BlockDriverState
*bs
,
520 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
525 acb
= qemu_aio_get(&qcow_aio_pool
, bs
, NULL
, NULL
);
528 acb
->hd_aiocb
= NULL
;
529 acb
->sector_num
= sector_num
;
531 acb
->is_write
= is_write
;
533 if (qiov
->niov
> 1) {
534 acb
->buf
= acb
->orig_buf
= qemu_blockalign(bs
, qiov
->size
);
536 qemu_iovec_to_buffer(qiov
, acb
->buf
);
538 acb
->buf
= (uint8_t *)qiov
->iov
->iov_base
;
540 acb
->nb_sectors
= nb_sectors
;
542 acb
->cluster_offset
= 0;
546 static int qcow_aio_read_cb(void *opaque
)
548 QCowAIOCB
*acb
= opaque
;
549 BlockDriverState
*bs
= acb
->common
.bs
;
550 BDRVQcowState
*s
= bs
->opaque
;
551 int index_in_cluster
;
554 acb
->hd_aiocb
= NULL
;
557 /* post process the read buffer */
558 if (!acb
->cluster_offset
) {
560 } else if (acb
->cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
563 if (s
->crypt_method
) {
564 encrypt_sectors(s
, acb
->sector_num
, acb
->buf
, acb
->buf
,
566 &s
->aes_decrypt_key
);
570 acb
->nb_sectors
-= acb
->n
;
571 acb
->sector_num
+= acb
->n
;
572 acb
->buf
+= acb
->n
* 512;
574 if (acb
->nb_sectors
== 0) {
575 /* request completed */
579 /* prepare next AIO request */
580 acb
->cluster_offset
= get_cluster_offset(bs
, acb
->sector_num
<< 9,
582 index_in_cluster
= acb
->sector_num
& (s
->cluster_sectors
- 1);
583 acb
->n
= s
->cluster_sectors
- index_in_cluster
;
584 if (acb
->n
> acb
->nb_sectors
)
585 acb
->n
= acb
->nb_sectors
;
587 if (!acb
->cluster_offset
) {
588 if (bs
->backing_hd
) {
589 /* read from the base image */
590 acb
->hd_iov
.iov_base
= (void *)acb
->buf
;
591 acb
->hd_iov
.iov_len
= acb
->n
* 512;
592 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
593 qemu_co_mutex_unlock(&s
->lock
);
594 ret
= bdrv_co_readv(bs
->backing_hd
, acb
->sector_num
,
595 acb
->n
, &acb
->hd_qiov
);
596 qemu_co_mutex_lock(&s
->lock
);
601 /* Note: in this case, no need to wait */
602 memset(acb
->buf
, 0, 512 * acb
->n
);
605 } else if (acb
->cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
606 /* add AIO support for compressed blocks ? */
607 if (decompress_cluster(bs
, acb
->cluster_offset
) < 0) {
611 s
->cluster_cache
+ index_in_cluster
* 512, 512 * acb
->n
);
614 if ((acb
->cluster_offset
& 511) != 0) {
617 acb
->hd_iov
.iov_base
= (void *)acb
->buf
;
618 acb
->hd_iov
.iov_len
= acb
->n
* 512;
619 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
620 qemu_co_mutex_unlock(&s
->lock
);
621 ret
= bdrv_co_readv(bs
->file
,
622 (acb
->cluster_offset
>> 9) + index_in_cluster
,
623 acb
->n
, &acb
->hd_qiov
);
624 qemu_co_mutex_lock(&s
->lock
);
633 static int qcow_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
634 int nb_sectors
, QEMUIOVector
*qiov
)
636 BDRVQcowState
*s
= bs
->opaque
;
640 acb
= qcow_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, 0);
642 qemu_co_mutex_lock(&s
->lock
);
644 ret
= qcow_aio_read_cb(acb
);
646 qemu_co_mutex_unlock(&s
->lock
);
648 if (acb
->qiov
->niov
> 1) {
649 qemu_iovec_from_buffer(acb
->qiov
, acb
->orig_buf
, acb
->qiov
->size
);
650 qemu_vfree(acb
->orig_buf
);
652 qemu_aio_release(acb
);
657 static int qcow_aio_write_cb(void *opaque
)
659 QCowAIOCB
*acb
= opaque
;
660 BlockDriverState
*bs
= acb
->common
.bs
;
661 BDRVQcowState
*s
= bs
->opaque
;
662 int index_in_cluster
;
663 uint64_t cluster_offset
;
664 const uint8_t *src_buf
;
667 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 */
678 index_in_cluster
= acb
->sector_num
& (s
->cluster_sectors
- 1);
679 acb
->n
= s
->cluster_sectors
- index_in_cluster
;
680 if (acb
->n
> acb
->nb_sectors
)
681 acb
->n
= acb
->nb_sectors
;
682 cluster_offset
= get_cluster_offset(bs
, acb
->sector_num
<< 9, 1, 0,
684 index_in_cluster
+ acb
->n
);
685 if (!cluster_offset
|| (cluster_offset
& 511) != 0) {
688 if (s
->crypt_method
) {
689 if (!acb
->cluster_data
) {
690 acb
->cluster_data
= qemu_mallocz(s
->cluster_size
);
692 encrypt_sectors(s
, acb
->sector_num
, acb
->cluster_data
, acb
->buf
,
693 acb
->n
, 1, &s
->aes_encrypt_key
);
694 src_buf
= acb
->cluster_data
;
699 acb
->hd_iov
.iov_base
= (void *)src_buf
;
700 acb
->hd_iov
.iov_len
= acb
->n
* 512;
701 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
702 qemu_co_mutex_unlock(&s
->lock
);
703 ret
= bdrv_co_writev(bs
->file
,
704 (cluster_offset
>> 9) + index_in_cluster
,
705 acb
->n
, &acb
->hd_qiov
);
706 qemu_co_mutex_lock(&s
->lock
);
713 static int qcow_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
714 int nb_sectors
, QEMUIOVector
*qiov
)
716 BDRVQcowState
*s
= bs
->opaque
;
720 s
->cluster_cache_offset
= -1; /* disable compressed cache */
722 acb
= qcow_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, 1);
724 qemu_co_mutex_lock(&s
->lock
);
726 ret
= qcow_aio_write_cb(acb
);
728 qemu_co_mutex_unlock(&s
->lock
);
730 if (acb
->qiov
->niov
> 1) {
731 qemu_vfree(acb
->orig_buf
);
733 qemu_aio_release(acb
);
738 static void qcow_close(BlockDriverState
*bs
)
740 BDRVQcowState
*s
= bs
->opaque
;
741 qemu_free(s
->l1_table
);
742 qemu_free(s
->l2_cache
);
743 qemu_free(s
->cluster_cache
);
744 qemu_free(s
->cluster_data
);
747 static int qcow_create(const char *filename
, QEMUOptionParameter
*options
)
749 int fd
, header_size
, backing_filename_len
, l1_size
, i
, shift
;
752 int64_t total_size
= 0;
753 const char *backing_file
= NULL
;
757 /* Read out options */
758 while (options
&& options
->name
) {
759 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
760 total_size
= options
->value
.n
/ 512;
761 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
762 backing_file
= options
->value
.s
;
763 } else if (!strcmp(options
->name
, BLOCK_OPT_ENCRYPT
)) {
764 flags
|= options
->value
.n
? BLOCK_FLAG_ENCRYPT
: 0;
769 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0644);
772 memset(&header
, 0, sizeof(header
));
773 header
.magic
= cpu_to_be32(QCOW_MAGIC
);
774 header
.version
= cpu_to_be32(QCOW_VERSION
);
775 header
.size
= cpu_to_be64(total_size
* 512);
776 header_size
= sizeof(header
);
777 backing_filename_len
= 0;
779 if (strcmp(backing_file
, "fat:")) {
780 header
.backing_file_offset
= cpu_to_be64(header_size
);
781 backing_filename_len
= strlen(backing_file
);
782 header
.backing_file_size
= cpu_to_be32(backing_filename_len
);
783 header_size
+= backing_filename_len
;
785 /* special backing file for vvfat */
788 header
.cluster_bits
= 9; /* 512 byte cluster to avoid copying
789 unmodifyed sectors */
790 header
.l2_bits
= 12; /* 32 KB L2 tables */
792 header
.cluster_bits
= 12; /* 4 KB clusters */
793 header
.l2_bits
= 9; /* 4 KB L2 tables */
795 header_size
= (header_size
+ 7) & ~7;
796 shift
= header
.cluster_bits
+ header
.l2_bits
;
797 l1_size
= ((total_size
* 512) + (1LL << shift
) - 1) >> shift
;
799 header
.l1_table_offset
= cpu_to_be64(header_size
);
800 if (flags
& BLOCK_FLAG_ENCRYPT
) {
801 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
803 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
806 /* write all the data */
807 ret
= qemu_write_full(fd
, &header
, sizeof(header
));
808 if (ret
!= sizeof(header
)) {
814 ret
= qemu_write_full(fd
, backing_file
, backing_filename_len
);
815 if (ret
!= backing_filename_len
) {
821 lseek(fd
, header_size
, SEEK_SET
);
823 for(i
= 0;i
< l1_size
; i
++) {
824 ret
= qemu_write_full(fd
, &tmp
, sizeof(tmp
));
825 if (ret
!= sizeof(tmp
)) {
837 static int qcow_make_empty(BlockDriverState
*bs
)
839 BDRVQcowState
*s
= bs
->opaque
;
840 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
843 memset(s
->l1_table
, 0, l1_length
);
844 if (bdrv_pwrite_sync(bs
->file
, s
->l1_table_offset
, s
->l1_table
,
847 ret
= bdrv_truncate(bs
->file
, s
->l1_table_offset
+ l1_length
);
851 memset(s
->l2_cache
, 0, s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
852 memset(s
->l2_cache_offsets
, 0, L2_CACHE_SIZE
* sizeof(uint64_t));
853 memset(s
->l2_cache_counts
, 0, L2_CACHE_SIZE
* sizeof(uint32_t));
858 /* XXX: put compressed sectors first, then all the cluster aligned
859 tables to avoid losing bytes in alignment */
860 static int qcow_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
861 const uint8_t *buf
, int nb_sectors
)
863 BDRVQcowState
*s
= bs
->opaque
;
867 uint64_t cluster_offset
;
869 if (nb_sectors
!= s
->cluster_sectors
)
872 out_buf
= qemu_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
876 /* best compression, small window, no zlib header */
877 memset(&strm
, 0, sizeof(strm
));
878 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
880 9, Z_DEFAULT_STRATEGY
);
886 strm
.avail_in
= s
->cluster_size
;
887 strm
.next_in
= (uint8_t *)buf
;
888 strm
.avail_out
= s
->cluster_size
;
889 strm
.next_out
= out_buf
;
891 ret
= deflate(&strm
, Z_FINISH
);
892 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
897 out_len
= strm
.next_out
- out_buf
;
901 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
902 /* could not compress: write normal cluster */
903 bdrv_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
905 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 2,
907 cluster_offset
&= s
->cluster_offset_mask
;
908 if (bdrv_pwrite(bs
->file
, cluster_offset
, out_buf
, out_len
) != out_len
) {
918 static int qcow_flush(BlockDriverState
*bs
)
920 return bdrv_flush(bs
->file
);
923 static BlockDriverAIOCB
*qcow_aio_flush(BlockDriverState
*bs
,
924 BlockDriverCompletionFunc
*cb
, void *opaque
)
926 return bdrv_aio_flush(bs
->file
, cb
, opaque
);
929 static int qcow_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
931 BDRVQcowState
*s
= bs
->opaque
;
932 bdi
->cluster_size
= s
->cluster_size
;
937 static QEMUOptionParameter qcow_create_options
[] = {
939 .name
= BLOCK_OPT_SIZE
,
941 .help
= "Virtual disk size"
944 .name
= BLOCK_OPT_BACKING_FILE
,
946 .help
= "File name of a base image"
949 .name
= BLOCK_OPT_ENCRYPT
,
951 .help
= "Encrypt the image"
956 static BlockDriver bdrv_qcow
= {
957 .format_name
= "qcow",
958 .instance_size
= sizeof(BDRVQcowState
),
959 .bdrv_probe
= qcow_probe
,
960 .bdrv_open
= qcow_open
,
961 .bdrv_close
= qcow_close
,
962 .bdrv_create
= qcow_create
,
963 .bdrv_flush
= qcow_flush
,
964 .bdrv_is_allocated
= qcow_is_allocated
,
965 .bdrv_set_key
= qcow_set_key
,
966 .bdrv_make_empty
= qcow_make_empty
,
967 .bdrv_co_readv
= qcow_co_readv
,
968 .bdrv_co_writev
= qcow_co_writev
,
969 .bdrv_aio_flush
= qcow_aio_flush
,
970 .bdrv_write_compressed
= qcow_write_compressed
,
971 .bdrv_get_info
= qcow_get_info
,
973 .create_options
= qcow_create_options
,
976 static void bdrv_qcow_init(void)
978 bdrv_register(&bdrv_qcow
);
981 block_init(bdrv_qcow_init
);