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
31 //#define DEBUG_ALLOC2
34 #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
35 #define QCOW_VERSION 2
37 #define QCOW_CRYPT_NONE 0
38 #define QCOW_CRYPT_AES 1
40 #define QCOW_MAX_CRYPT_CLUSTERS 32
42 /* indicate that the refcount of the referenced cluster is exactly one. */
43 #define QCOW_OFLAG_COPIED (1LL << 63)
44 /* indicate that the cluster is compressed (they never have the copied flag) */
45 #define QCOW_OFLAG_COMPRESSED (1LL << 62)
47 #define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
49 #define MIN_CLUSTER_BITS 9
50 #define MAX_CLUSTER_BITS 21
52 #define L2_CACHE_SIZE 16
54 typedef struct QCowHeader
{
57 uint64_t backing_file_offset
;
58 uint32_t backing_file_size
;
59 uint32_t cluster_bits
;
60 uint64_t size
; /* in bytes */
61 uint32_t crypt_method
;
62 uint32_t l1_size
; /* XXX: save number of clusters instead ? */
63 uint64_t l1_table_offset
;
64 uint64_t refcount_table_offset
;
65 uint32_t refcount_table_clusters
;
66 uint32_t nb_snapshots
;
67 uint64_t snapshots_offset
;
70 typedef struct QCowSnapshot
{
71 uint64_t l1_table_offset
;
75 uint32_t vm_state_size
;
78 uint64_t vm_clock_nsec
;
81 typedef struct BDRVQcowState
{
89 int l1_vm_state_index
;
92 uint64_t cluster_offset_mask
;
93 uint64_t l1_table_offset
;
96 uint64_t l2_cache_offsets
[L2_CACHE_SIZE
];
97 uint32_t l2_cache_counts
[L2_CACHE_SIZE
];
98 uint8_t *cluster_cache
;
99 uint8_t *cluster_data
;
100 uint64_t cluster_cache_offset
;
101 QLIST_HEAD(QCowClusterAlloc
, QCowL2Meta
) cluster_allocs
;
103 uint64_t *refcount_table
;
104 uint64_t refcount_table_offset
;
105 uint32_t refcount_table_size
;
106 uint64_t refcount_block_cache_offset
;
107 uint16_t *refcount_block_cache
;
108 int64_t free_cluster_index
;
109 int64_t free_byte_offset
;
111 uint32_t crypt_method
; /* current crypt method, 0 if no key yet */
112 uint32_t crypt_method_header
;
113 AES_KEY aes_encrypt_key
;
114 AES_KEY aes_decrypt_key
;
115 uint64_t snapshots_offset
;
118 QCowSnapshot
*snapshots
;
121 /* XXX: use std qcow open function ? */
122 typedef struct QCowCreateState
{
125 uint16_t *refcount_block
;
126 uint64_t *refcount_table
;
127 int64_t l1_table_offset
;
128 int64_t refcount_table_offset
;
129 int64_t refcount_block_offset
;
134 /* XXX This could be private for qcow2-cluster.c */
135 typedef struct QCowL2Meta
138 uint64_t cluster_offset
;
142 struct QCowL2Meta
*depends_on
;
143 QLIST_HEAD(QCowAioDependencies
, QCowAIOCB
) dependent_requests
;
145 QLIST_ENTRY(QCowL2Meta
) next_in_flight
;
148 static inline int size_to_clusters(BDRVQcowState
*s
, int64_t size
)
150 return (size
+ (s
->cluster_size
- 1)) >> s
->cluster_bits
;
153 static inline int size_to_l1(BDRVQcowState
*s
, int64_t size
)
155 int shift
= s
->cluster_bits
+ s
->l2_bits
;
156 return (size
+ (1ULL << shift
) - 1) >> shift
;
159 static inline int64_t align_offset(int64_t offset
, int n
)
161 offset
= (offset
+ n
- 1) & ~(n
- 1);
166 // FIXME Need qcow2_ prefix to global functions
168 /* qcow2.c functions */
169 int qcow2_backing_read1(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
170 int64_t sector_num
, int nb_sectors
);
172 /* qcow2-refcount.c functions */
173 int qcow2_refcount_init(BlockDriverState
*bs
);
174 void qcow2_refcount_close(BlockDriverState
*bs
);
176 int64_t qcow2_alloc_clusters(BlockDriverState
*bs
, int64_t size
);
177 int64_t qcow2_alloc_bytes(BlockDriverState
*bs
, int size
);
178 void qcow2_free_clusters(BlockDriverState
*bs
,
179 int64_t offset
, int64_t size
);
180 void qcow2_free_any_clusters(BlockDriverState
*bs
,
181 uint64_t cluster_offset
, int nb_clusters
);
183 void qcow2_create_refcount_update(QCowCreateState
*s
, int64_t offset
,
185 int qcow2_update_snapshot_refcount(BlockDriverState
*bs
,
186 int64_t l1_table_offset
, int l1_size
, int addend
);
188 int qcow2_check_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
);
190 /* qcow2-cluster.c functions */
191 int qcow2_grow_l1_table(BlockDriverState
*bs
, int min_size
, bool exact_size
);
192 void qcow2_l2_cache_reset(BlockDriverState
*bs
);
193 int qcow2_decompress_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
);
194 void qcow2_encrypt_sectors(BDRVQcowState
*s
, int64_t sector_num
,
195 uint8_t *out_buf
, const uint8_t *in_buf
,
196 int nb_sectors
, int enc
,
199 int qcow2_get_cluster_offset(BlockDriverState
*bs
, uint64_t offset
,
200 int *num
, uint64_t *cluster_offset
);
201 int qcow2_alloc_cluster_offset(BlockDriverState
*bs
, uint64_t offset
,
202 int n_start
, int n_end
, int *num
, QCowL2Meta
*m
);
203 uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState
*bs
,
205 int compressed_size
);
207 int qcow2_alloc_cluster_link_l2(BlockDriverState
*bs
, QCowL2Meta
*m
);
209 /* qcow2-snapshot.c functions */
210 int qcow2_snapshot_create(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
);
211 int qcow2_snapshot_goto(BlockDriverState
*bs
, const char *snapshot_id
);
212 int qcow2_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
);
213 int qcow2_snapshot_list(BlockDriverState
*bs
, QEMUSnapshotInfo
**psn_tab
);
214 int qcow2_snapshot_load_tmp(BlockDriverState
*bs
, const char *snapshot_name
);
216 void qcow2_free_snapshots(BlockDriverState
*bs
);
217 int qcow2_read_snapshots(BlockDriverState
*bs
);