2 * Block driver for the QCOW format
4 * Copyright (c) 2004 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
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
)
94 BDRVQcowState
*s
= bs
->opaque
;
95 int fd
, len
, i
, shift
;
98 fd
= open(filename
, O_RDWR
| O_BINARY
| O_LARGEFILE
);
100 fd
= open(filename
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
105 if (read(fd
, &header
, sizeof(header
)) != sizeof(header
))
107 be32_to_cpus(&header
.magic
);
108 be32_to_cpus(&header
.version
);
109 be64_to_cpus(&header
.backing_file_offset
);
110 be32_to_cpus(&header
.backing_file_size
);
111 be32_to_cpus(&header
.mtime
);
112 be64_to_cpus(&header
.size
);
113 be32_to_cpus(&header
.crypt_method
);
114 be64_to_cpus(&header
.l1_table_offset
);
116 if (header
.magic
!= QCOW_MAGIC
|| header
.version
!= QCOW_VERSION
)
118 if (header
.size
<= 1 || header
.cluster_bits
< 9)
120 if (header
.crypt_method
> QCOW_CRYPT_AES
)
122 s
->crypt_method_header
= header
.crypt_method
;
123 if (s
->crypt_method_header
)
125 s
->cluster_bits
= header
.cluster_bits
;
126 s
->cluster_size
= 1 << s
->cluster_bits
;
127 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
128 s
->l2_bits
= header
.l2_bits
;
129 s
->l2_size
= 1 << s
->l2_bits
;
130 bs
->total_sectors
= header
.size
/ 512;
131 s
->cluster_offset_mask
= (1LL << (63 - s
->cluster_bits
)) - 1;
133 /* read the level 1 table */
134 shift
= s
->cluster_bits
+ s
->l2_bits
;
135 s
->l1_size
= (header
.size
+ (1LL << shift
) - 1) >> shift
;
137 s
->l1_table_offset
= header
.l1_table_offset
;
138 s
->l1_table
= qemu_malloc(s
->l1_size
* sizeof(uint64_t));
141 lseek(fd
, s
->l1_table_offset
, SEEK_SET
);
142 if (read(fd
, s
->l1_table
, s
->l1_size
* sizeof(uint64_t)) !=
143 s
->l1_size
* sizeof(uint64_t))
145 for(i
= 0;i
< s
->l1_size
; i
++) {
146 be64_to_cpus(&s
->l1_table
[i
]);
149 s
->l2_cache
= qemu_malloc(s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
152 s
->cluster_cache
= qemu_malloc(s
->cluster_size
);
153 if (!s
->cluster_cache
)
155 s
->cluster_data
= qemu_malloc(s
->cluster_size
);
156 if (!s
->cluster_data
)
158 s
->cluster_cache_offset
= -1;
160 /* read the backing file name */
161 if (header
.backing_file_offset
!= 0) {
162 len
= header
.backing_file_size
;
165 lseek(fd
, header
.backing_file_offset
, SEEK_SET
);
166 if (read(fd
, bs
->backing_file
, len
) != len
)
168 bs
->backing_file
[len
] = '\0';
173 qemu_free(s
->l1_table
);
174 qemu_free(s
->l2_cache
);
175 qemu_free(s
->cluster_cache
);
176 qemu_free(s
->cluster_data
);
181 static int qcow_set_key(BlockDriverState
*bs
, const char *key
)
183 BDRVQcowState
*s
= bs
->opaque
;
187 memset(keybuf
, 0, 16);
191 /* XXX: we could compress the chars to 7 bits to increase
193 for(i
= 0;i
< len
;i
++) {
196 s
->crypt_method
= s
->crypt_method_header
;
198 if (AES_set_encrypt_key(keybuf
, 128, &s
->aes_encrypt_key
) != 0)
200 if (AES_set_decrypt_key(keybuf
, 128, &s
->aes_decrypt_key
) != 0)
210 AES_encrypt(in
, tmp
, &s
->aes_encrypt_key
);
211 AES_decrypt(tmp
, out
, &s
->aes_decrypt_key
);
212 for(i
= 0; i
< 16; i
++)
213 printf(" %02x", tmp
[i
]);
215 for(i
= 0; i
< 16; i
++)
216 printf(" %02x", out
[i
]);
223 /* The crypt function is compatible with the linux cryptoloop
224 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
226 static void encrypt_sectors(BDRVQcowState
*s
, int64_t sector_num
,
227 uint8_t *out_buf
, const uint8_t *in_buf
,
228 int nb_sectors
, int enc
,
237 for(i
= 0; i
< nb_sectors
; i
++) {
238 ivec
.ll
[0] = cpu_to_le64(sector_num
);
240 AES_cbc_encrypt(in_buf
, out_buf
, 512, key
,
252 * 1 to allocate a normal cluster (for sector indexes 'n_start' to
255 * 2 to allocate a compressed cluster of size
256 * 'compressed_size'. 'compressed_size' must be > 0 and <
259 * return 0 if not allocated.
261 static uint64_t get_cluster_offset(BlockDriverState
*bs
,
262 uint64_t offset
, int allocate
,
264 int n_start
, int n_end
)
266 BDRVQcowState
*s
= bs
->opaque
;
267 int min_index
, i
, j
, l1_index
, l2_index
;
268 uint64_t l2_offset
, *l2_table
, cluster_offset
, tmp
;
272 l1_index
= offset
>> (s
->l2_bits
+ s
->cluster_bits
);
273 l2_offset
= s
->l1_table
[l1_index
];
278 /* allocate a new l2 entry */
279 l2_offset
= lseek(s
->fd
, 0, SEEK_END
);
280 /* round to cluster size */
281 l2_offset
= (l2_offset
+ s
->cluster_size
- 1) & ~(s
->cluster_size
- 1);
282 /* update the L1 entry */
283 s
->l1_table
[l1_index
] = l2_offset
;
284 tmp
= cpu_to_be64(l2_offset
);
285 lseek(s
->fd
, s
->l1_table_offset
+ l1_index
* sizeof(tmp
), SEEK_SET
);
286 if (write(s
->fd
, &tmp
, sizeof(tmp
)) != sizeof(tmp
))
290 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
291 if (l2_offset
== s
->l2_cache_offsets
[i
]) {
292 /* increment the hit count */
293 if (++s
->l2_cache_counts
[i
] == 0xffffffff) {
294 for(j
= 0; j
< L2_CACHE_SIZE
; j
++) {
295 s
->l2_cache_counts
[j
] >>= 1;
298 l2_table
= s
->l2_cache
+ (i
<< s
->l2_bits
);
302 /* not found: load a new entry in the least used one */
304 min_count
= 0xffffffff;
305 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
306 if (s
->l2_cache_counts
[i
] < min_count
) {
307 min_count
= s
->l2_cache_counts
[i
];
311 l2_table
= s
->l2_cache
+ (min_index
<< s
->l2_bits
);
312 lseek(s
->fd
, l2_offset
, SEEK_SET
);
314 memset(l2_table
, 0, s
->l2_size
* sizeof(uint64_t));
315 if (write(s
->fd
, l2_table
, s
->l2_size
* sizeof(uint64_t)) !=
316 s
->l2_size
* sizeof(uint64_t))
319 if (read(s
->fd
, l2_table
, s
->l2_size
* sizeof(uint64_t)) !=
320 s
->l2_size
* sizeof(uint64_t))
323 s
->l2_cache_offsets
[min_index
] = l2_offset
;
324 s
->l2_cache_counts
[min_index
] = 1;
326 l2_index
= (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
327 cluster_offset
= be64_to_cpu(l2_table
[l2_index
]);
328 if (!cluster_offset
||
329 ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) && allocate
== 1)) {
332 /* allocate a new cluster */
333 if ((cluster_offset
& QCOW_OFLAG_COMPRESSED
) &&
334 (n_end
- n_start
) < s
->cluster_sectors
) {
335 /* if the cluster is already compressed, we must
336 decompress it in the case it is not completely
338 if (decompress_cluster(s
, cluster_offset
) < 0)
340 cluster_offset
= lseek(s
->fd
, 0, SEEK_END
);
341 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
342 ~(s
->cluster_size
- 1);
343 /* write the cluster content */
344 lseek(s
->fd
, cluster_offset
, SEEK_SET
);
345 if (write(s
->fd
, s
->cluster_cache
, s
->cluster_size
) !=
349 cluster_offset
= lseek(s
->fd
, 0, SEEK_END
);
351 /* round to cluster size */
352 cluster_offset
= (cluster_offset
+ s
->cluster_size
- 1) &
353 ~(s
->cluster_size
- 1);
354 ftruncate(s
->fd
, cluster_offset
+ s
->cluster_size
);
355 /* if encrypted, we must initialize the cluster
356 content which won't be written */
357 if (s
->crypt_method
&&
358 (n_end
- n_start
) < s
->cluster_sectors
) {
360 start_sect
= (offset
& ~(s
->cluster_size
- 1)) >> 9;
361 memset(s
->cluster_data
+ 512, 0xaa, 512);
362 for(i
= 0; i
< s
->cluster_sectors
; i
++) {
363 if (i
< n_start
|| i
>= n_end
) {
364 encrypt_sectors(s
, start_sect
+ i
,
366 s
->cluster_data
+ 512, 1, 1,
367 &s
->aes_encrypt_key
);
368 lseek(s
->fd
, cluster_offset
+ i
* 512, SEEK_SET
);
369 if (write(s
->fd
, s
->cluster_data
, 512) != 512)
375 cluster_offset
|= QCOW_OFLAG_COMPRESSED
|
376 (uint64_t)compressed_size
<< (63 - s
->cluster_bits
);
379 /* update L2 table */
380 tmp
= cpu_to_be64(cluster_offset
);
381 l2_table
[l2_index
] = tmp
;
382 lseek(s
->fd
, l2_offset
+ l2_index
* sizeof(tmp
), SEEK_SET
);
383 if (write(s
->fd
, &tmp
, sizeof(tmp
)) != sizeof(tmp
))
386 return cluster_offset
;
389 static int qcow_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
390 int nb_sectors
, int *pnum
)
392 BDRVQcowState
*s
= bs
->opaque
;
393 int index_in_cluster
, n
;
394 uint64_t cluster_offset
;
396 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 0, 0, 0, 0);
397 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
398 n
= s
->cluster_sectors
- index_in_cluster
;
402 return (cluster_offset
!= 0);
405 static int decompress_buffer(uint8_t *out_buf
, int out_buf_size
,
406 const uint8_t *buf
, int buf_size
)
408 z_stream strm1
, *strm
= &strm1
;
411 memset(strm
, 0, sizeof(*strm
));
413 strm
->next_in
= (uint8_t *)buf
;
414 strm
->avail_in
= buf_size
;
415 strm
->next_out
= out_buf
;
416 strm
->avail_out
= out_buf_size
;
418 ret
= inflateInit2(strm
, -12);
421 ret
= inflate(strm
, Z_FINISH
);
422 out_len
= strm
->next_out
- out_buf
;
423 if ((ret
!= Z_STREAM_END
&& ret
!= Z_BUF_ERROR
) ||
424 out_len
!= out_buf_size
) {
432 static int decompress_cluster(BDRVQcowState
*s
, uint64_t cluster_offset
)
437 coffset
= cluster_offset
& s
->cluster_offset_mask
;
438 if (s
->cluster_cache_offset
!= coffset
) {
439 csize
= cluster_offset
>> (63 - s
->cluster_bits
);
440 csize
&= (s
->cluster_size
- 1);
441 lseek(s
->fd
, coffset
, SEEK_SET
);
442 ret
= read(s
->fd
, s
->cluster_data
, csize
);
445 if (decompress_buffer(s
->cluster_cache
, s
->cluster_size
,
446 s
->cluster_data
, csize
) < 0) {
449 s
->cluster_cache_offset
= coffset
;
454 static int qcow_read(BlockDriverState
*bs
, int64_t sector_num
,
455 uint8_t *buf
, int nb_sectors
)
457 BDRVQcowState
*s
= bs
->opaque
;
458 int ret
, index_in_cluster
, n
;
459 uint64_t cluster_offset
;
461 while (nb_sectors
> 0) {
462 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 0, 0, 0, 0);
463 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
464 n
= s
->cluster_sectors
- index_in_cluster
;
467 if (!cluster_offset
) {
468 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 lseek(s
->fd
, cluster_offset
+ index_in_cluster
* 512, SEEK_SET
);
475 ret
= read(s
->fd
, buf
, n
* 512);
478 if (s
->crypt_method
) {
479 encrypt_sectors(s
, sector_num
, buf
, buf
, n
, 0,
480 &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 lseek(s
->fd
, cluster_offset
+ index_in_cluster
* 512, SEEK_SET
);
508 if (s
->crypt_method
) {
509 encrypt_sectors(s
, sector_num
, s
->cluster_data
, buf
, n
, 1,
510 &s
->aes_encrypt_key
);
511 ret
= write(s
->fd
, s
->cluster_data
, n
* 512);
513 ret
= write(s
->fd
, buf
, n
* 512);
521 s
->cluster_cache_offset
= -1; /* disable compressed cache */
525 static void qcow_close(BlockDriverState
*bs
)
527 BDRVQcowState
*s
= bs
->opaque
;
528 qemu_free(s
->l1_table
);
529 qemu_free(s
->l2_cache
);
530 qemu_free(s
->cluster_cache
);
531 qemu_free(s
->cluster_data
);
535 static int qcow_create(const char *filename
, int64_t total_size
,
536 const char *backing_file
, int flags
)
538 int fd
, header_size
, backing_filename_len
, l1_size
, i
, shift
;
540 char backing_filename
[1024];
544 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
,
548 memset(&header
, 0, sizeof(header
));
549 header
.magic
= cpu_to_be32(QCOW_MAGIC
);
550 header
.version
= cpu_to_be32(QCOW_VERSION
);
551 header
.size
= cpu_to_be64(total_size
* 512);
552 header_size
= sizeof(header
);
553 backing_filename_len
= 0;
555 if (strcmp(backing_file
, "fat:")) {
557 /* XXX: this is a hack: we do not attempt to check for URL
559 p
= strchr(backing_file
, ':');
560 if (p
&& (p
- backing_file
) >= 2) {
561 /* URL like but exclude "c:" like filenames */
562 pstrcpy(backing_filename
, sizeof(backing_filename
),
565 realpath(backing_file
, backing_filename
);
566 if (stat(backing_filename
, &st
) != 0) {
570 header
.backing_file_offset
= cpu_to_be64(header_size
);
571 backing_filename_len
= strlen(backing_filename
);
572 header
.backing_file_size
= cpu_to_be32(backing_filename_len
);
573 header_size
+= backing_filename_len
;
576 header
.mtime
= cpu_to_be32(st
.st_mtime
);
577 header
.cluster_bits
= 9; /* 512 byte cluster to avoid copying
578 unmodifyed sectors */
579 header
.l2_bits
= 12; /* 32 KB L2 tables */
581 header
.cluster_bits
= 12; /* 4 KB clusters */
582 header
.l2_bits
= 9; /* 4 KB L2 tables */
584 header_size
= (header_size
+ 7) & ~7;
585 shift
= header
.cluster_bits
+ header
.l2_bits
;
586 l1_size
= ((total_size
* 512) + (1LL << shift
) - 1) >> shift
;
588 header
.l1_table_offset
= cpu_to_be64(header_size
);
590 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
592 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
595 /* write all the data */
596 write(fd
, &header
, sizeof(header
));
598 write(fd
, backing_filename
, backing_filename_len
);
600 lseek(fd
, header_size
, SEEK_SET
);
602 for(i
= 0;i
< l1_size
; i
++) {
603 write(fd
, &tmp
, sizeof(tmp
));
609 int qcow_make_empty(BlockDriverState
*bs
)
611 BDRVQcowState
*s
= bs
->opaque
;
612 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
614 memset(s
->l1_table
, 0, l1_length
);
615 lseek(s
->fd
, s
->l1_table_offset
, SEEK_SET
);
616 if (write(s
->fd
, s
->l1_table
, l1_length
) < 0)
618 ftruncate(s
->fd
, s
->l1_table_offset
+ l1_length
);
620 memset(s
->l2_cache
, 0, s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
621 memset(s
->l2_cache_offsets
, 0, L2_CACHE_SIZE
* sizeof(uint64_t));
622 memset(s
->l2_cache_counts
, 0, L2_CACHE_SIZE
* sizeof(uint32_t));
627 int qcow_get_cluster_size(BlockDriverState
*bs
)
629 BDRVQcowState
*s
= bs
->opaque
;
630 if (bs
->drv
!= &bdrv_qcow
)
632 return s
->cluster_size
;
635 /* XXX: put compressed sectors first, then all the cluster aligned
636 tables to avoid losing bytes in alignment */
637 int qcow_compress_cluster(BlockDriverState
*bs
, int64_t sector_num
,
640 BDRVQcowState
*s
= bs
->opaque
;
644 uint64_t cluster_offset
;
646 if (bs
->drv
!= &bdrv_qcow
)
649 out_buf
= qemu_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
653 /* best compression, small window, no zlib header */
654 memset(&strm
, 0, sizeof(strm
));
655 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
657 9, Z_DEFAULT_STRATEGY
);
663 strm
.avail_in
= s
->cluster_size
;
664 strm
.next_in
= (uint8_t *)buf
;
665 strm
.avail_out
= s
->cluster_size
;
666 strm
.next_out
= out_buf
;
668 ret
= deflate(&strm
, Z_FINISH
);
669 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
674 out_len
= strm
.next_out
- out_buf
;
678 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
679 /* could not compress: write normal cluster */
680 qcow_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
682 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 2,
684 cluster_offset
&= s
->cluster_offset_mask
;
685 lseek(s
->fd
, cluster_offset
, SEEK_SET
);
686 if (write(s
->fd
, out_buf
, out_len
) != out_len
) {
696 BlockDriver bdrv_qcow
= {
698 sizeof(BDRVQcowState
),