root / block / qcow2.h @ f6317a6e
History | View | Annotate | Download (7.8 kB)
1 | f7d0fe02 | Kevin Wolf | /*
|
---|---|---|---|
2 | f7d0fe02 | Kevin Wolf | * Block driver for the QCOW version 2 format
|
3 | f7d0fe02 | Kevin Wolf | *
|
4 | f7d0fe02 | Kevin Wolf | * Copyright (c) 2004-2006 Fabrice Bellard
|
5 | f7d0fe02 | Kevin Wolf | *
|
6 | f7d0fe02 | Kevin Wolf | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | f7d0fe02 | Kevin Wolf | * of this software and associated documentation files (the "Software"), to deal
|
8 | f7d0fe02 | Kevin Wolf | * in the Software without restriction, including without limitation the rights
|
9 | f7d0fe02 | Kevin Wolf | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | f7d0fe02 | Kevin Wolf | * copies of the Software, and to permit persons to whom the Software is
|
11 | f7d0fe02 | Kevin Wolf | * furnished to do so, subject to the following conditions:
|
12 | f7d0fe02 | Kevin Wolf | *
|
13 | f7d0fe02 | Kevin Wolf | * The above copyright notice and this permission notice shall be included in
|
14 | f7d0fe02 | Kevin Wolf | * all copies or substantial portions of the Software.
|
15 | f7d0fe02 | Kevin Wolf | *
|
16 | f7d0fe02 | Kevin Wolf | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | f7d0fe02 | Kevin Wolf | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | f7d0fe02 | Kevin Wolf | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | f7d0fe02 | Kevin Wolf | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | f7d0fe02 | Kevin Wolf | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | f7d0fe02 | Kevin Wolf | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | f7d0fe02 | Kevin Wolf | * THE SOFTWARE.
|
23 | f7d0fe02 | Kevin Wolf | */
|
24 | f7d0fe02 | Kevin Wolf | |
25 | f7d0fe02 | Kevin Wolf | #ifndef BLOCK_QCOW2_H
|
26 | f7d0fe02 | Kevin Wolf | #define BLOCK_QCOW2_H
|
27 | f7d0fe02 | Kevin Wolf | |
28 | f7d0fe02 | Kevin Wolf | #include "aes.h" |
29 | f7d0fe02 | Kevin Wolf | |
30 | 14899cdf | Filip Navara | //#define DEBUG_ALLOC
|
31 | 14899cdf | Filip Navara | //#define DEBUG_ALLOC2
|
32 | 14899cdf | Filip Navara | //#define DEBUG_EXT
|
33 | 14899cdf | Filip Navara | |
34 | f7d0fe02 | Kevin Wolf | #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb) |
35 | f7d0fe02 | Kevin Wolf | #define QCOW_VERSION 2 |
36 | f7d0fe02 | Kevin Wolf | |
37 | f7d0fe02 | Kevin Wolf | #define QCOW_CRYPT_NONE 0 |
38 | f7d0fe02 | Kevin Wolf | #define QCOW_CRYPT_AES 1 |
39 | f7d0fe02 | Kevin Wolf | |
40 | f7d0fe02 | Kevin Wolf | #define QCOW_MAX_CRYPT_CLUSTERS 32 |
41 | f7d0fe02 | Kevin Wolf | |
42 | f7d0fe02 | Kevin Wolf | /* indicate that the refcount of the referenced cluster is exactly one. */
|
43 | f7d0fe02 | Kevin Wolf | #define QCOW_OFLAG_COPIED (1LL << 63) |
44 | f7d0fe02 | Kevin Wolf | /* indicate that the cluster is compressed (they never have the copied flag) */
|
45 | f7d0fe02 | Kevin Wolf | #define QCOW_OFLAG_COMPRESSED (1LL << 62) |
46 | f7d0fe02 | Kevin Wolf | |
47 | f7d0fe02 | Kevin Wolf | #define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */ |
48 | f7d0fe02 | Kevin Wolf | |
49 | f7d0fe02 | Kevin Wolf | #define MIN_CLUSTER_BITS 9 |
50 | 80ee15a6 | Kevin Wolf | #define MAX_CLUSTER_BITS 21 |
51 | f7d0fe02 | Kevin Wolf | |
52 | f7d0fe02 | Kevin Wolf | #define L2_CACHE_SIZE 16 |
53 | f7d0fe02 | Kevin Wolf | |
54 | 29c1a730 | Kevin Wolf | /* Must be at least 4 to cover all cases of refcount table growth */
|
55 | 29c1a730 | Kevin Wolf | #define REFCOUNT_CACHE_SIZE 4 |
56 | 29c1a730 | Kevin Wolf | |
57 | f7d0fe02 | Kevin Wolf | typedef struct QCowHeader { |
58 | f7d0fe02 | Kevin Wolf | uint32_t magic; |
59 | f7d0fe02 | Kevin Wolf | uint32_t version; |
60 | f7d0fe02 | Kevin Wolf | uint64_t backing_file_offset; |
61 | f7d0fe02 | Kevin Wolf | uint32_t backing_file_size; |
62 | f7d0fe02 | Kevin Wolf | uint32_t cluster_bits; |
63 | f7d0fe02 | Kevin Wolf | uint64_t size; /* in bytes */
|
64 | f7d0fe02 | Kevin Wolf | uint32_t crypt_method; |
65 | f7d0fe02 | Kevin Wolf | uint32_t l1_size; /* XXX: save number of clusters instead ? */
|
66 | f7d0fe02 | Kevin Wolf | uint64_t l1_table_offset; |
67 | f7d0fe02 | Kevin Wolf | uint64_t refcount_table_offset; |
68 | f7d0fe02 | Kevin Wolf | uint32_t refcount_table_clusters; |
69 | f7d0fe02 | Kevin Wolf | uint32_t nb_snapshots; |
70 | f7d0fe02 | Kevin Wolf | uint64_t snapshots_offset; |
71 | f7d0fe02 | Kevin Wolf | } QCowHeader; |
72 | f7d0fe02 | Kevin Wolf | |
73 | f7d0fe02 | Kevin Wolf | typedef struct QCowSnapshot { |
74 | f7d0fe02 | Kevin Wolf | uint64_t l1_table_offset; |
75 | f7d0fe02 | Kevin Wolf | uint32_t l1_size; |
76 | f7d0fe02 | Kevin Wolf | char *id_str;
|
77 | f7d0fe02 | Kevin Wolf | char *name;
|
78 | f7d0fe02 | Kevin Wolf | uint32_t vm_state_size; |
79 | f7d0fe02 | Kevin Wolf | uint32_t date_sec; |
80 | f7d0fe02 | Kevin Wolf | uint32_t date_nsec; |
81 | f7d0fe02 | Kevin Wolf | uint64_t vm_clock_nsec; |
82 | f7d0fe02 | Kevin Wolf | } QCowSnapshot; |
83 | f7d0fe02 | Kevin Wolf | |
84 | 49381094 | Kevin Wolf | struct Qcow2Cache;
|
85 | 49381094 | Kevin Wolf | typedef struct Qcow2Cache Qcow2Cache; |
86 | 49381094 | Kevin Wolf | |
87 | f7d0fe02 | Kevin Wolf | typedef struct BDRVQcowState { |
88 | f7d0fe02 | Kevin Wolf | int cluster_bits;
|
89 | f7d0fe02 | Kevin Wolf | int cluster_size;
|
90 | f7d0fe02 | Kevin Wolf | int cluster_sectors;
|
91 | f7d0fe02 | Kevin Wolf | int l2_bits;
|
92 | f7d0fe02 | Kevin Wolf | int l2_size;
|
93 | f7d0fe02 | Kevin Wolf | int l1_size;
|
94 | f7d0fe02 | Kevin Wolf | int l1_vm_state_index;
|
95 | f7d0fe02 | Kevin Wolf | int csize_shift;
|
96 | f7d0fe02 | Kevin Wolf | int csize_mask;
|
97 | f7d0fe02 | Kevin Wolf | uint64_t cluster_offset_mask; |
98 | f7d0fe02 | Kevin Wolf | uint64_t l1_table_offset; |
99 | f7d0fe02 | Kevin Wolf | uint64_t *l1_table; |
100 | 29c1a730 | Kevin Wolf | |
101 | 29c1a730 | Kevin Wolf | Qcow2Cache* l2_table_cache; |
102 | 29c1a730 | Kevin Wolf | Qcow2Cache* refcount_block_cache; |
103 | 29c1a730 | Kevin Wolf | |
104 | f7d0fe02 | Kevin Wolf | uint8_t *cluster_cache; |
105 | f7d0fe02 | Kevin Wolf | uint8_t *cluster_data; |
106 | f7d0fe02 | Kevin Wolf | uint64_t cluster_cache_offset; |
107 | 72cf2d4f | Blue Swirl | QLIST_HEAD(QCowClusterAlloc, QCowL2Meta) cluster_allocs; |
108 | f7d0fe02 | Kevin Wolf | |
109 | f7d0fe02 | Kevin Wolf | uint64_t *refcount_table; |
110 | f7d0fe02 | Kevin Wolf | uint64_t refcount_table_offset; |
111 | f7d0fe02 | Kevin Wolf | uint32_t refcount_table_size; |
112 | f7d0fe02 | Kevin Wolf | int64_t free_cluster_index; |
113 | f7d0fe02 | Kevin Wolf | int64_t free_byte_offset; |
114 | f7d0fe02 | Kevin Wolf | |
115 | f7d0fe02 | Kevin Wolf | uint32_t crypt_method; /* current crypt method, 0 if no key yet */
|
116 | f7d0fe02 | Kevin Wolf | uint32_t crypt_method_header; |
117 | f7d0fe02 | Kevin Wolf | AES_KEY aes_encrypt_key; |
118 | f7d0fe02 | Kevin Wolf | AES_KEY aes_decrypt_key; |
119 | f7d0fe02 | Kevin Wolf | uint64_t snapshots_offset; |
120 | f7d0fe02 | Kevin Wolf | int snapshots_size;
|
121 | f7d0fe02 | Kevin Wolf | int nb_snapshots;
|
122 | f7d0fe02 | Kevin Wolf | QCowSnapshot *snapshots; |
123 | f7d0fe02 | Kevin Wolf | } BDRVQcowState; |
124 | f7d0fe02 | Kevin Wolf | |
125 | f7d0fe02 | Kevin Wolf | /* XXX: use std qcow open function ? */
|
126 | f7d0fe02 | Kevin Wolf | typedef struct QCowCreateState { |
127 | f7d0fe02 | Kevin Wolf | int cluster_size;
|
128 | f7d0fe02 | Kevin Wolf | int cluster_bits;
|
129 | f7d0fe02 | Kevin Wolf | uint16_t *refcount_block; |
130 | f7d0fe02 | Kevin Wolf | uint64_t *refcount_table; |
131 | f7d0fe02 | Kevin Wolf | int64_t l1_table_offset; |
132 | f7d0fe02 | Kevin Wolf | int64_t refcount_table_offset; |
133 | f7d0fe02 | Kevin Wolf | int64_t refcount_block_offset; |
134 | f7d0fe02 | Kevin Wolf | } QCowCreateState; |
135 | f7d0fe02 | Kevin Wolf | |
136 | f214978a | Kevin Wolf | struct QCowAIOCB;
|
137 | f214978a | Kevin Wolf | |
138 | 45aba42f | Kevin Wolf | /* XXX This could be private for qcow2-cluster.c */
|
139 | 45aba42f | Kevin Wolf | typedef struct QCowL2Meta |
140 | 45aba42f | Kevin Wolf | { |
141 | 45aba42f | Kevin Wolf | uint64_t offset; |
142 | 148da7ea | Kevin Wolf | uint64_t cluster_offset; |
143 | 45aba42f | Kevin Wolf | int n_start;
|
144 | 45aba42f | Kevin Wolf | int nb_available;
|
145 | 45aba42f | Kevin Wolf | int nb_clusters;
|
146 | f214978a | Kevin Wolf | struct QCowL2Meta *depends_on;
|
147 | 72cf2d4f | Blue Swirl | QLIST_HEAD(QCowAioDependencies, QCowAIOCB) dependent_requests; |
148 | f214978a | Kevin Wolf | |
149 | 72cf2d4f | Blue Swirl | QLIST_ENTRY(QCowL2Meta) next_in_flight; |
150 | 45aba42f | Kevin Wolf | } QCowL2Meta; |
151 | 45aba42f | Kevin Wolf | |
152 | 45aba42f | Kevin Wolf | static inline int size_to_clusters(BDRVQcowState *s, int64_t size) |
153 | f7d0fe02 | Kevin Wolf | { |
154 | f7d0fe02 | Kevin Wolf | return (size + (s->cluster_size - 1)) >> s->cluster_bits; |
155 | f7d0fe02 | Kevin Wolf | } |
156 | f7d0fe02 | Kevin Wolf | |
157 | 419b19d9 | Stefan Hajnoczi | static inline int size_to_l1(BDRVQcowState *s, int64_t size) |
158 | 419b19d9 | Stefan Hajnoczi | { |
159 | 419b19d9 | Stefan Hajnoczi | int shift = s->cluster_bits + s->l2_bits;
|
160 | 419b19d9 | Stefan Hajnoczi | return (size + (1ULL << shift) - 1) >> shift; |
161 | 419b19d9 | Stefan Hajnoczi | } |
162 | 419b19d9 | Stefan Hajnoczi | |
163 | c142442b | Kevin Wolf | static inline int64_t align_offset(int64_t offset, int n) |
164 | c142442b | Kevin Wolf | { |
165 | c142442b | Kevin Wolf | offset = (offset + n - 1) & ~(n - 1); |
166 | c142442b | Kevin Wolf | return offset;
|
167 | c142442b | Kevin Wolf | } |
168 | c142442b | Kevin Wolf | |
169 | c142442b | Kevin Wolf | |
170 | f7d0fe02 | Kevin Wolf | // FIXME Need qcow2_ prefix to global functions
|
171 | f7d0fe02 | Kevin Wolf | |
172 | f7d0fe02 | Kevin Wolf | /* qcow2.c functions */
|
173 | bd28f835 | Kevin Wolf | int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
|
174 | bd28f835 | Kevin Wolf | int64_t sector_num, int nb_sectors);
|
175 | f7d0fe02 | Kevin Wolf | |
176 | f7d0fe02 | Kevin Wolf | /* qcow2-refcount.c functions */
|
177 | ed6ccf0f | Kevin Wolf | int qcow2_refcount_init(BlockDriverState *bs);
|
178 | ed6ccf0f | Kevin Wolf | void qcow2_refcount_close(BlockDriverState *bs);
|
179 | f7d0fe02 | Kevin Wolf | |
180 | ed6ccf0f | Kevin Wolf | int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size); |
181 | ed6ccf0f | Kevin Wolf | int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
|
182 | ed6ccf0f | Kevin Wolf | void qcow2_free_clusters(BlockDriverState *bs,
|
183 | 45aba42f | Kevin Wolf | int64_t offset, int64_t size); |
184 | ed6ccf0f | Kevin Wolf | void qcow2_free_any_clusters(BlockDriverState *bs,
|
185 | 45aba42f | Kevin Wolf | uint64_t cluster_offset, int nb_clusters);
|
186 | f7d0fe02 | Kevin Wolf | |
187 | ed6ccf0f | Kevin Wolf | void qcow2_create_refcount_update(QCowCreateState *s, int64_t offset,
|
188 | ed6ccf0f | Kevin Wolf | int64_t size); |
189 | ed6ccf0f | Kevin Wolf | int qcow2_update_snapshot_refcount(BlockDriverState *bs,
|
190 | ed6ccf0f | Kevin Wolf | int64_t l1_table_offset, int l1_size, int addend); |
191 | f7d0fe02 | Kevin Wolf | |
192 | 9ac228e0 | Kevin Wolf | int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res);
|
193 | f7d0fe02 | Kevin Wolf | |
194 | 45aba42f | Kevin Wolf | /* qcow2-cluster.c functions */
|
195 | 72893756 | Stefan Hajnoczi | int qcow2_grow_l1_table(BlockDriverState *bs, int min_size, bool exact_size); |
196 | ed6ccf0f | Kevin Wolf | void qcow2_l2_cache_reset(BlockDriverState *bs);
|
197 | 66f82cee | Kevin Wolf | int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
|
198 | ed6ccf0f | Kevin Wolf | void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
|
199 | 45aba42f | Kevin Wolf | uint8_t *out_buf, const uint8_t *in_buf,
|
200 | 45aba42f | Kevin Wolf | int nb_sectors, int enc, |
201 | 45aba42f | Kevin Wolf | const AES_KEY *key);
|
202 | 45aba42f | Kevin Wolf | |
203 | 1c46efaa | Kevin Wolf | int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
|
204 | 1c46efaa | Kevin Wolf | int *num, uint64_t *cluster_offset);
|
205 | f4f0d391 | Kevin Wolf | int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
|
206 | f4f0d391 | Kevin Wolf | int n_start, int n_end, int *num, QCowL2Meta *m); |
207 | ed6ccf0f | Kevin Wolf | uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs, |
208 | 45aba42f | Kevin Wolf | uint64_t offset, |
209 | 45aba42f | Kevin Wolf | int compressed_size);
|
210 | 45aba42f | Kevin Wolf | |
211 | 148da7ea | Kevin Wolf | int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
|
212 | 5ea929e3 | Kevin Wolf | int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
|
213 | 5ea929e3 | Kevin Wolf | int nb_sectors);
|
214 | 45aba42f | Kevin Wolf | |
215 | c142442b | Kevin Wolf | /* qcow2-snapshot.c functions */
|
216 | ed6ccf0f | Kevin Wolf | int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
|
217 | ed6ccf0f | Kevin Wolf | int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id); |
218 | ed6ccf0f | Kevin Wolf | int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id); |
219 | ed6ccf0f | Kevin Wolf | int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
|
220 | 51ef6727 | edison | int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name); |
221 | c142442b | Kevin Wolf | |
222 | ed6ccf0f | Kevin Wolf | void qcow2_free_snapshots(BlockDriverState *bs);
|
223 | ed6ccf0f | Kevin Wolf | int qcow2_read_snapshots(BlockDriverState *bs);
|
224 | c142442b | Kevin Wolf | |
225 | 49381094 | Kevin Wolf | /* qcow2-cache.c functions */
|
226 | 49381094 | Kevin Wolf | Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables,
|
227 | 49381094 | Kevin Wolf | bool writethrough);
|
228 | 49381094 | Kevin Wolf | int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c);
|
229 | 49381094 | Kevin Wolf | |
230 | 49381094 | Kevin Wolf | void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table); |
231 | 49381094 | Kevin Wolf | int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
|
232 | 49381094 | Kevin Wolf | int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
|
233 | 49381094 | Kevin Wolf | Qcow2Cache *dependency); |
234 | 3de0a294 | Kevin Wolf | void qcow2_cache_depends_on_flush(Qcow2Cache *c);
|
235 | 49381094 | Kevin Wolf | |
236 | 49381094 | Kevin Wolf | int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
|
237 | 49381094 | Kevin Wolf | void **table);
|
238 | 49381094 | Kevin Wolf | int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
|
239 | 49381094 | Kevin Wolf | void **table);
|
240 | 49381094 | Kevin Wolf | int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table); |
241 | 49381094 | Kevin Wolf | |
242 | f7d0fe02 | Kevin Wolf | #endif |