2 * Block driver for the QCOW version 2 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
30 #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
31 #define QCOW_VERSION 2
33 #define QCOW_CRYPT_NONE 0
34 #define QCOW_CRYPT_AES 1
36 #define QCOW_MAX_CRYPT_CLUSTERS 32
38 /* indicate that the refcount of the referenced cluster is exactly one. */
39 #define QCOW_OFLAG_COPIED (1LL << 63)
40 /* indicate that the cluster is compressed (they never have the copied flag) */
41 #define QCOW_OFLAG_COMPRESSED (1LL << 62)
43 #define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
45 #define MIN_CLUSTER_BITS 9
46 #define MAX_CLUSTER_BITS 16
48 #define L2_CACHE_SIZE 16
50 typedef struct QCowHeader
{
53 uint64_t backing_file_offset
;
54 uint32_t backing_file_size
;
55 uint32_t cluster_bits
;
56 uint64_t size
; /* in bytes */
57 uint32_t crypt_method
;
58 uint32_t l1_size
; /* XXX: save number of clusters instead ? */
59 uint64_t l1_table_offset
;
60 uint64_t refcount_table_offset
;
61 uint32_t refcount_table_clusters
;
62 uint32_t nb_snapshots
;
63 uint64_t snapshots_offset
;
66 typedef struct QCowSnapshot
{
67 uint64_t l1_table_offset
;
71 uint32_t vm_state_size
;
74 uint64_t vm_clock_nsec
;
77 typedef struct BDRVQcowState
{
85 int l1_vm_state_index
;
88 uint64_t cluster_offset_mask
;
89 uint64_t l1_table_offset
;
92 uint64_t l2_cache_offsets
[L2_CACHE_SIZE
];
93 uint32_t l2_cache_counts
[L2_CACHE_SIZE
];
94 uint8_t *cluster_cache
;
95 uint8_t *cluster_data
;
96 uint64_t cluster_cache_offset
;
98 uint64_t *refcount_table
;
99 uint64_t refcount_table_offset
;
100 uint32_t refcount_table_size
;
101 uint64_t refcount_block_cache_offset
;
102 uint16_t *refcount_block_cache
;
103 int64_t free_cluster_index
;
104 int64_t free_byte_offset
;
106 uint32_t crypt_method
; /* current crypt method, 0 if no key yet */
107 uint32_t crypt_method_header
;
108 AES_KEY aes_encrypt_key
;
109 AES_KEY aes_decrypt_key
;
110 uint64_t snapshots_offset
;
113 QCowSnapshot
*snapshots
;
116 /* XXX: use std qcow open function ? */
117 typedef struct QCowCreateState
{
120 uint16_t *refcount_block
;
121 uint64_t *refcount_table
;
122 int64_t l1_table_offset
;
123 int64_t refcount_table_offset
;
124 int64_t refcount_block_offset
;
127 static int size_to_clusters(BDRVQcowState
*s
, int64_t size
)
129 return (size
+ (s
->cluster_size
- 1)) >> s
->cluster_bits
;
132 // FIXME Need qcow2_ prefix to global functions
134 /* qcow2.c functions */
135 void l2_cache_reset(BlockDriverState
*bs
);
137 /* qcow2-refcount.c functions */
138 int refcount_init(BlockDriverState
*bs
);
139 void refcount_close(BlockDriverState
*bs
);
141 int64_t alloc_clusters(BlockDriverState
*bs
, int64_t size
);
142 int64_t alloc_bytes(BlockDriverState
*bs
, int size
);
143 void free_clusters(BlockDriverState
*bs
,
144 int64_t offset
, int64_t size
);
146 void create_refcount_update(QCowCreateState
*s
, int64_t offset
, int64_t size
);
147 int update_snapshot_refcount(BlockDriverState
*bs
,
148 int64_t l1_table_offset
,
152 int check_refcounts(BlockDriverState
*bs
);