root / block / qcow2.h @ 99cce9fa
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 | 99cce9fa | Kevin Wolf | #define DEFAULT_CLUSTER_SIZE 65536 |
58 | 99cce9fa | Kevin Wolf | |
59 | f7d0fe02 | Kevin Wolf | typedef struct QCowHeader { |
60 | f7d0fe02 | Kevin Wolf | uint32_t magic; |
61 | f7d0fe02 | Kevin Wolf | uint32_t version; |
62 | f7d0fe02 | Kevin Wolf | uint64_t backing_file_offset; |
63 | f7d0fe02 | Kevin Wolf | uint32_t backing_file_size; |
64 | f7d0fe02 | Kevin Wolf | uint32_t cluster_bits; |
65 | f7d0fe02 | Kevin Wolf | uint64_t size; /* in bytes */
|
66 | f7d0fe02 | Kevin Wolf | uint32_t crypt_method; |
67 | f7d0fe02 | Kevin Wolf | uint32_t l1_size; /* XXX: save number of clusters instead ? */
|
68 | f7d0fe02 | Kevin Wolf | uint64_t l1_table_offset; |
69 | f7d0fe02 | Kevin Wolf | uint64_t refcount_table_offset; |
70 | f7d0fe02 | Kevin Wolf | uint32_t refcount_table_clusters; |
71 | f7d0fe02 | Kevin Wolf | uint32_t nb_snapshots; |
72 | f7d0fe02 | Kevin Wolf | uint64_t snapshots_offset; |
73 | f7d0fe02 | Kevin Wolf | } QCowHeader; |
74 | f7d0fe02 | Kevin Wolf | |
75 | f7d0fe02 | Kevin Wolf | typedef struct QCowSnapshot { |
76 | f7d0fe02 | Kevin Wolf | uint64_t l1_table_offset; |
77 | f7d0fe02 | Kevin Wolf | uint32_t l1_size; |
78 | f7d0fe02 | Kevin Wolf | char *id_str;
|
79 | f7d0fe02 | Kevin Wolf | char *name;
|
80 | f7d0fe02 | Kevin Wolf | uint32_t vm_state_size; |
81 | f7d0fe02 | Kevin Wolf | uint32_t date_sec; |
82 | f7d0fe02 | Kevin Wolf | uint32_t date_nsec; |
83 | f7d0fe02 | Kevin Wolf | uint64_t vm_clock_nsec; |
84 | f7d0fe02 | Kevin Wolf | } QCowSnapshot; |
85 | f7d0fe02 | Kevin Wolf | |
86 | 49381094 | Kevin Wolf | struct Qcow2Cache;
|
87 | 49381094 | Kevin Wolf | typedef struct Qcow2Cache Qcow2Cache; |
88 | 49381094 | Kevin Wolf | |
89 | f7d0fe02 | Kevin Wolf | typedef struct BDRVQcowState { |
90 | f7d0fe02 | Kevin Wolf | int cluster_bits;
|
91 | f7d0fe02 | Kevin Wolf | int cluster_size;
|
92 | f7d0fe02 | Kevin Wolf | int cluster_sectors;
|
93 | f7d0fe02 | Kevin Wolf | int l2_bits;
|
94 | f7d0fe02 | Kevin Wolf | int l2_size;
|
95 | f7d0fe02 | Kevin Wolf | int l1_size;
|
96 | f7d0fe02 | Kevin Wolf | int l1_vm_state_index;
|
97 | f7d0fe02 | Kevin Wolf | int csize_shift;
|
98 | f7d0fe02 | Kevin Wolf | int csize_mask;
|
99 | f7d0fe02 | Kevin Wolf | uint64_t cluster_offset_mask; |
100 | f7d0fe02 | Kevin Wolf | uint64_t l1_table_offset; |
101 | f7d0fe02 | Kevin Wolf | uint64_t *l1_table; |
102 | 29c1a730 | Kevin Wolf | |
103 | 29c1a730 | Kevin Wolf | Qcow2Cache* l2_table_cache; |
104 | 29c1a730 | Kevin Wolf | Qcow2Cache* refcount_block_cache; |
105 | 29c1a730 | Kevin Wolf | |
106 | f7d0fe02 | Kevin Wolf | uint8_t *cluster_cache; |
107 | f7d0fe02 | Kevin Wolf | uint8_t *cluster_data; |
108 | f7d0fe02 | Kevin Wolf | uint64_t cluster_cache_offset; |
109 | 72cf2d4f | Blue Swirl | QLIST_HEAD(QCowClusterAlloc, QCowL2Meta) cluster_allocs; |
110 | f7d0fe02 | Kevin Wolf | |
111 | f7d0fe02 | Kevin Wolf | uint64_t *refcount_table; |
112 | f7d0fe02 | Kevin Wolf | uint64_t refcount_table_offset; |
113 | f7d0fe02 | Kevin Wolf | uint32_t refcount_table_size; |
114 | f7d0fe02 | Kevin Wolf | int64_t free_cluster_index; |
115 | f7d0fe02 | Kevin Wolf | int64_t free_byte_offset; |
116 | f7d0fe02 | Kevin Wolf | |
117 | f7d0fe02 | Kevin Wolf | uint32_t crypt_method; /* current crypt method, 0 if no key yet */
|
118 | f7d0fe02 | Kevin Wolf | uint32_t crypt_method_header; |
119 | f7d0fe02 | Kevin Wolf | AES_KEY aes_encrypt_key; |
120 | f7d0fe02 | Kevin Wolf | AES_KEY aes_decrypt_key; |
121 | f7d0fe02 | Kevin Wolf | uint64_t snapshots_offset; |
122 | f7d0fe02 | Kevin Wolf | int snapshots_size;
|
123 | f7d0fe02 | Kevin Wolf | int nb_snapshots;
|
124 | f7d0fe02 | Kevin Wolf | QCowSnapshot *snapshots; |
125 | f7d0fe02 | Kevin Wolf | } BDRVQcowState; |
126 | f7d0fe02 | Kevin Wolf | |
127 | f7d0fe02 | Kevin Wolf | /* XXX: use std qcow open function ? */
|
128 | f7d0fe02 | Kevin Wolf | typedef struct QCowCreateState { |
129 | f7d0fe02 | Kevin Wolf | int cluster_size;
|
130 | f7d0fe02 | Kevin Wolf | int cluster_bits;
|
131 | f7d0fe02 | Kevin Wolf | uint16_t *refcount_block; |
132 | f7d0fe02 | Kevin Wolf | uint64_t *refcount_table; |
133 | f7d0fe02 | Kevin Wolf | int64_t l1_table_offset; |
134 | f7d0fe02 | Kevin Wolf | int64_t refcount_table_offset; |
135 | f7d0fe02 | Kevin Wolf | int64_t refcount_block_offset; |
136 | f7d0fe02 | Kevin Wolf | } QCowCreateState; |
137 | f7d0fe02 | Kevin Wolf | |
138 | f214978a | Kevin Wolf | struct QCowAIOCB;
|
139 | f214978a | Kevin Wolf | |
140 | 45aba42f | Kevin Wolf | /* XXX This could be private for qcow2-cluster.c */
|
141 | 45aba42f | Kevin Wolf | typedef struct QCowL2Meta |
142 | 45aba42f | Kevin Wolf | { |
143 | 45aba42f | Kevin Wolf | uint64_t offset; |
144 | 148da7ea | Kevin Wolf | uint64_t cluster_offset; |
145 | 45aba42f | Kevin Wolf | int n_start;
|
146 | 45aba42f | Kevin Wolf | int nb_available;
|
147 | 45aba42f | Kevin Wolf | int nb_clusters;
|
148 | f214978a | Kevin Wolf | struct QCowL2Meta *depends_on;
|
149 | 72cf2d4f | Blue Swirl | QLIST_HEAD(QCowAioDependencies, QCowAIOCB) dependent_requests; |
150 | f214978a | Kevin Wolf | |
151 | 72cf2d4f | Blue Swirl | QLIST_ENTRY(QCowL2Meta) next_in_flight; |
152 | 45aba42f | Kevin Wolf | } QCowL2Meta; |
153 | 45aba42f | Kevin Wolf | |
154 | 45aba42f | Kevin Wolf | static inline int size_to_clusters(BDRVQcowState *s, int64_t size) |
155 | f7d0fe02 | Kevin Wolf | { |
156 | f7d0fe02 | Kevin Wolf | return (size + (s->cluster_size - 1)) >> s->cluster_bits; |
157 | f7d0fe02 | Kevin Wolf | } |
158 | f7d0fe02 | Kevin Wolf | |
159 | 419b19d9 | Stefan Hajnoczi | static inline int size_to_l1(BDRVQcowState *s, int64_t size) |
160 | 419b19d9 | Stefan Hajnoczi | { |
161 | 419b19d9 | Stefan Hajnoczi | int shift = s->cluster_bits + s->l2_bits;
|
162 | 419b19d9 | Stefan Hajnoczi | return (size + (1ULL << shift) - 1) >> shift; |
163 | 419b19d9 | Stefan Hajnoczi | } |
164 | 419b19d9 | Stefan Hajnoczi | |
165 | c142442b | Kevin Wolf | static inline int64_t align_offset(int64_t offset, int n) |
166 | c142442b | Kevin Wolf | { |
167 | c142442b | Kevin Wolf | offset = (offset + n - 1) & ~(n - 1); |
168 | c142442b | Kevin Wolf | return offset;
|
169 | c142442b | Kevin Wolf | } |
170 | c142442b | Kevin Wolf | |
171 | c142442b | Kevin Wolf | |
172 | f7d0fe02 | Kevin Wolf | // FIXME Need qcow2_ prefix to global functions
|
173 | f7d0fe02 | Kevin Wolf | |
174 | f7d0fe02 | Kevin Wolf | /* qcow2.c functions */
|
175 | bd28f835 | Kevin Wolf | int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
|
176 | bd28f835 | Kevin Wolf | int64_t sector_num, int nb_sectors);
|
177 | f7d0fe02 | Kevin Wolf | |
178 | f7d0fe02 | Kevin Wolf | /* qcow2-refcount.c functions */
|
179 | ed6ccf0f | Kevin Wolf | int qcow2_refcount_init(BlockDriverState *bs);
|
180 | ed6ccf0f | Kevin Wolf | void qcow2_refcount_close(BlockDriverState *bs);
|
181 | f7d0fe02 | Kevin Wolf | |
182 | ed6ccf0f | Kevin Wolf | int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size); |
183 | ed6ccf0f | Kevin Wolf | int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
|
184 | ed6ccf0f | Kevin Wolf | void qcow2_free_clusters(BlockDriverState *bs,
|
185 | 45aba42f | Kevin Wolf | int64_t offset, int64_t size); |
186 | ed6ccf0f | Kevin Wolf | void qcow2_free_any_clusters(BlockDriverState *bs,
|
187 | 45aba42f | Kevin Wolf | uint64_t cluster_offset, int nb_clusters);
|
188 | f7d0fe02 | Kevin Wolf | |
189 | ed6ccf0f | Kevin Wolf | void qcow2_create_refcount_update(QCowCreateState *s, int64_t offset,
|
190 | ed6ccf0f | Kevin Wolf | int64_t size); |
191 | ed6ccf0f | Kevin Wolf | int qcow2_update_snapshot_refcount(BlockDriverState *bs,
|
192 | ed6ccf0f | Kevin Wolf | int64_t l1_table_offset, int l1_size, int addend); |
193 | f7d0fe02 | Kevin Wolf | |
194 | 9ac228e0 | Kevin Wolf | int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res);
|
195 | f7d0fe02 | Kevin Wolf | |
196 | 45aba42f | Kevin Wolf | /* qcow2-cluster.c functions */
|
197 | 72893756 | Stefan Hajnoczi | int qcow2_grow_l1_table(BlockDriverState *bs, int min_size, bool exact_size); |
198 | ed6ccf0f | Kevin Wolf | void qcow2_l2_cache_reset(BlockDriverState *bs);
|
199 | 66f82cee | Kevin Wolf | int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
|
200 | ed6ccf0f | Kevin Wolf | void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
|
201 | 45aba42f | Kevin Wolf | uint8_t *out_buf, const uint8_t *in_buf,
|
202 | 45aba42f | Kevin Wolf | int nb_sectors, int enc, |
203 | 45aba42f | Kevin Wolf | const AES_KEY *key);
|
204 | 45aba42f | Kevin Wolf | |
205 | 1c46efaa | Kevin Wolf | int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
|
206 | 1c46efaa | Kevin Wolf | int *num, uint64_t *cluster_offset);
|
207 | f4f0d391 | Kevin Wolf | int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
|
208 | f4f0d391 | Kevin Wolf | int n_start, int n_end, int *num, QCowL2Meta *m); |
209 | ed6ccf0f | Kevin Wolf | uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs, |
210 | 45aba42f | Kevin Wolf | uint64_t offset, |
211 | 45aba42f | Kevin Wolf | int compressed_size);
|
212 | 45aba42f | Kevin Wolf | |
213 | 148da7ea | Kevin Wolf | int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
|
214 | 5ea929e3 | Kevin Wolf | int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
|
215 | 5ea929e3 | Kevin Wolf | int nb_sectors);
|
216 | 45aba42f | Kevin Wolf | |
217 | c142442b | Kevin Wolf | /* qcow2-snapshot.c functions */
|
218 | ed6ccf0f | Kevin Wolf | int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
|
219 | ed6ccf0f | Kevin Wolf | int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id); |
220 | ed6ccf0f | Kevin Wolf | int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id); |
221 | ed6ccf0f | Kevin Wolf | int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
|
222 | 51ef6727 | edison | int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name); |
223 | c142442b | Kevin Wolf | |
224 | ed6ccf0f | Kevin Wolf | void qcow2_free_snapshots(BlockDriverState *bs);
|
225 | ed6ccf0f | Kevin Wolf | int qcow2_read_snapshots(BlockDriverState *bs);
|
226 | c142442b | Kevin Wolf | |
227 | 49381094 | Kevin Wolf | /* qcow2-cache.c functions */
|
228 | 49381094 | Kevin Wolf | Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables,
|
229 | 49381094 | Kevin Wolf | bool writethrough);
|
230 | 49381094 | Kevin Wolf | int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c);
|
231 | 49381094 | Kevin Wolf | |
232 | 49381094 | Kevin Wolf | void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table); |
233 | 49381094 | Kevin Wolf | int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
|
234 | 49381094 | Kevin Wolf | int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
|
235 | 49381094 | Kevin Wolf | Qcow2Cache *dependency); |
236 | 3de0a294 | Kevin Wolf | void qcow2_cache_depends_on_flush(Qcow2Cache *c);
|
237 | 49381094 | Kevin Wolf | |
238 | 49381094 | Kevin Wolf | int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
|
239 | 49381094 | Kevin Wolf | void **table);
|
240 | 49381094 | Kevin Wolf | int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
|
241 | 49381094 | Kevin Wolf | void **table);
|
242 | 49381094 | Kevin Wolf | int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table); |
243 | 49381094 | Kevin Wolf | |
244 | f7d0fe02 | Kevin Wolf | #endif |