root / block / qcow2.h @ 7a3f5fe9
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 | 68d100e9 | Kevin Wolf | #include "qemu-coroutine.h" |
30 | f7d0fe02 | Kevin Wolf | |
31 | 14899cdf | Filip Navara | //#define DEBUG_ALLOC
|
32 | 14899cdf | Filip Navara | //#define DEBUG_ALLOC2
|
33 | 14899cdf | Filip Navara | //#define DEBUG_EXT
|
34 | 14899cdf | Filip Navara | |
35 | f7d0fe02 | Kevin Wolf | #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb) |
36 | f7d0fe02 | Kevin Wolf | #define QCOW_VERSION 2 |
37 | f7d0fe02 | Kevin Wolf | |
38 | f7d0fe02 | Kevin Wolf | #define QCOW_CRYPT_NONE 0 |
39 | f7d0fe02 | Kevin Wolf | #define QCOW_CRYPT_AES 1 |
40 | f7d0fe02 | Kevin Wolf | |
41 | f7d0fe02 | Kevin Wolf | #define QCOW_MAX_CRYPT_CLUSTERS 32 |
42 | f7d0fe02 | Kevin Wolf | |
43 | f7d0fe02 | Kevin Wolf | /* indicate that the refcount of the referenced cluster is exactly one. */
|
44 | f7d0fe02 | Kevin Wolf | #define QCOW_OFLAG_COPIED (1LL << 63) |
45 | f7d0fe02 | Kevin Wolf | /* indicate that the cluster is compressed (they never have the copied flag) */
|
46 | f7d0fe02 | Kevin Wolf | #define QCOW_OFLAG_COMPRESSED (1LL << 62) |
47 | f7d0fe02 | Kevin Wolf | |
48 | f7d0fe02 | Kevin Wolf | #define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */ |
49 | f7d0fe02 | Kevin Wolf | |
50 | f7d0fe02 | Kevin Wolf | #define MIN_CLUSTER_BITS 9 |
51 | 80ee15a6 | Kevin Wolf | #define MAX_CLUSTER_BITS 21 |
52 | f7d0fe02 | Kevin Wolf | |
53 | f7d0fe02 | Kevin Wolf | #define L2_CACHE_SIZE 16 |
54 | f7d0fe02 | Kevin Wolf | |
55 | 29c1a730 | Kevin Wolf | /* Must be at least 4 to cover all cases of refcount table growth */
|
56 | 29c1a730 | Kevin Wolf | #define REFCOUNT_CACHE_SIZE 4 |
57 | 29c1a730 | Kevin Wolf | |
58 | 99cce9fa | Kevin Wolf | #define DEFAULT_CLUSTER_SIZE 65536 |
59 | 99cce9fa | Kevin Wolf | |
60 | f7d0fe02 | Kevin Wolf | typedef struct QCowHeader { |
61 | f7d0fe02 | Kevin Wolf | uint32_t magic; |
62 | f7d0fe02 | Kevin Wolf | uint32_t version; |
63 | f7d0fe02 | Kevin Wolf | uint64_t backing_file_offset; |
64 | f7d0fe02 | Kevin Wolf | uint32_t backing_file_size; |
65 | f7d0fe02 | Kevin Wolf | uint32_t cluster_bits; |
66 | f7d0fe02 | Kevin Wolf | uint64_t size; /* in bytes */
|
67 | f7d0fe02 | Kevin Wolf | uint32_t crypt_method; |
68 | f7d0fe02 | Kevin Wolf | uint32_t l1_size; /* XXX: save number of clusters instead ? */
|
69 | f7d0fe02 | Kevin Wolf | uint64_t l1_table_offset; |
70 | f7d0fe02 | Kevin Wolf | uint64_t refcount_table_offset; |
71 | f7d0fe02 | Kevin Wolf | uint32_t refcount_table_clusters; |
72 | f7d0fe02 | Kevin Wolf | uint32_t nb_snapshots; |
73 | f7d0fe02 | Kevin Wolf | uint64_t snapshots_offset; |
74 | f7d0fe02 | Kevin Wolf | } QCowHeader; |
75 | f7d0fe02 | Kevin Wolf | |
76 | f7d0fe02 | Kevin Wolf | typedef struct QCowSnapshot { |
77 | f7d0fe02 | Kevin Wolf | uint64_t l1_table_offset; |
78 | f7d0fe02 | Kevin Wolf | uint32_t l1_size; |
79 | f7d0fe02 | Kevin Wolf | char *id_str;
|
80 | f7d0fe02 | Kevin Wolf | char *name;
|
81 | f7d0fe02 | Kevin Wolf | uint32_t vm_state_size; |
82 | f7d0fe02 | Kevin Wolf | uint32_t date_sec; |
83 | f7d0fe02 | Kevin Wolf | uint32_t date_nsec; |
84 | f7d0fe02 | Kevin Wolf | uint64_t vm_clock_nsec; |
85 | f7d0fe02 | Kevin Wolf | } QCowSnapshot; |
86 | f7d0fe02 | Kevin Wolf | |
87 | 49381094 | Kevin Wolf | struct Qcow2Cache;
|
88 | 49381094 | Kevin Wolf | typedef struct Qcow2Cache Qcow2Cache; |
89 | 49381094 | Kevin Wolf | |
90 | f7d0fe02 | Kevin Wolf | typedef struct BDRVQcowState { |
91 | f7d0fe02 | Kevin Wolf | int cluster_bits;
|
92 | f7d0fe02 | Kevin Wolf | int cluster_size;
|
93 | f7d0fe02 | Kevin Wolf | int cluster_sectors;
|
94 | f7d0fe02 | Kevin Wolf | int l2_bits;
|
95 | f7d0fe02 | Kevin Wolf | int l2_size;
|
96 | f7d0fe02 | Kevin Wolf | int l1_size;
|
97 | f7d0fe02 | Kevin Wolf | int l1_vm_state_index;
|
98 | f7d0fe02 | Kevin Wolf | int csize_shift;
|
99 | f7d0fe02 | Kevin Wolf | int csize_mask;
|
100 | f7d0fe02 | Kevin Wolf | uint64_t cluster_offset_mask; |
101 | f7d0fe02 | Kevin Wolf | uint64_t l1_table_offset; |
102 | f7d0fe02 | Kevin Wolf | uint64_t *l1_table; |
103 | 29c1a730 | Kevin Wolf | |
104 | 29c1a730 | Kevin Wolf | Qcow2Cache* l2_table_cache; |
105 | 29c1a730 | Kevin Wolf | Qcow2Cache* refcount_block_cache; |
106 | 29c1a730 | Kevin Wolf | |
107 | f7d0fe02 | Kevin Wolf | uint8_t *cluster_cache; |
108 | f7d0fe02 | Kevin Wolf | uint8_t *cluster_data; |
109 | f7d0fe02 | Kevin Wolf | uint64_t cluster_cache_offset; |
110 | 72cf2d4f | Blue Swirl | QLIST_HEAD(QCowClusterAlloc, QCowL2Meta) cluster_allocs; |
111 | f7d0fe02 | Kevin Wolf | |
112 | f7d0fe02 | Kevin Wolf | uint64_t *refcount_table; |
113 | f7d0fe02 | Kevin Wolf | uint64_t refcount_table_offset; |
114 | f7d0fe02 | Kevin Wolf | uint32_t refcount_table_size; |
115 | f7d0fe02 | Kevin Wolf | int64_t free_cluster_index; |
116 | f7d0fe02 | Kevin Wolf | int64_t free_byte_offset; |
117 | f7d0fe02 | Kevin Wolf | |
118 | 68d100e9 | Kevin Wolf | CoMutex lock; |
119 | 68d100e9 | Kevin Wolf | |
120 | f7d0fe02 | Kevin Wolf | uint32_t crypt_method; /* current crypt method, 0 if no key yet */
|
121 | f7d0fe02 | Kevin Wolf | uint32_t crypt_method_header; |
122 | f7d0fe02 | Kevin Wolf | AES_KEY aes_encrypt_key; |
123 | f7d0fe02 | Kevin Wolf | AES_KEY aes_decrypt_key; |
124 | f7d0fe02 | Kevin Wolf | uint64_t snapshots_offset; |
125 | f7d0fe02 | Kevin Wolf | int snapshots_size;
|
126 | f7d0fe02 | Kevin Wolf | int nb_snapshots;
|
127 | f7d0fe02 | Kevin Wolf | QCowSnapshot *snapshots; |
128 | f7d0fe02 | Kevin Wolf | } BDRVQcowState; |
129 | f7d0fe02 | Kevin Wolf | |
130 | f7d0fe02 | Kevin Wolf | /* XXX: use std qcow open function ? */
|
131 | f7d0fe02 | Kevin Wolf | typedef struct QCowCreateState { |
132 | f7d0fe02 | Kevin Wolf | int cluster_size;
|
133 | f7d0fe02 | Kevin Wolf | int cluster_bits;
|
134 | f7d0fe02 | Kevin Wolf | uint16_t *refcount_block; |
135 | f7d0fe02 | Kevin Wolf | uint64_t *refcount_table; |
136 | f7d0fe02 | Kevin Wolf | int64_t l1_table_offset; |
137 | f7d0fe02 | Kevin Wolf | int64_t refcount_table_offset; |
138 | f7d0fe02 | Kevin Wolf | int64_t refcount_block_offset; |
139 | f7d0fe02 | Kevin Wolf | } QCowCreateState; |
140 | f7d0fe02 | Kevin Wolf | |
141 | f214978a | Kevin Wolf | struct QCowAIOCB;
|
142 | f214978a | Kevin Wolf | |
143 | 45aba42f | Kevin Wolf | /* XXX This could be private for qcow2-cluster.c */
|
144 | 45aba42f | Kevin Wolf | typedef struct QCowL2Meta |
145 | 45aba42f | Kevin Wolf | { |
146 | 45aba42f | Kevin Wolf | uint64_t offset; |
147 | 148da7ea | Kevin Wolf | uint64_t cluster_offset; |
148 | 45aba42f | Kevin Wolf | int n_start;
|
149 | 45aba42f | Kevin Wolf | int nb_available;
|
150 | 45aba42f | Kevin Wolf | int nb_clusters;
|
151 | 68d100e9 | Kevin Wolf | CoQueue dependent_requests; |
152 | f214978a | Kevin Wolf | |
153 | 72cf2d4f | Blue Swirl | QLIST_ENTRY(QCowL2Meta) next_in_flight; |
154 | 45aba42f | Kevin Wolf | } QCowL2Meta; |
155 | 45aba42f | Kevin Wolf | |
156 | 45aba42f | Kevin Wolf | static inline int size_to_clusters(BDRVQcowState *s, int64_t size) |
157 | f7d0fe02 | Kevin Wolf | { |
158 | f7d0fe02 | Kevin Wolf | return (size + (s->cluster_size - 1)) >> s->cluster_bits; |
159 | f7d0fe02 | Kevin Wolf | } |
160 | f7d0fe02 | Kevin Wolf | |
161 | 419b19d9 | Stefan Hajnoczi | static inline int size_to_l1(BDRVQcowState *s, int64_t size) |
162 | 419b19d9 | Stefan Hajnoczi | { |
163 | 419b19d9 | Stefan Hajnoczi | int shift = s->cluster_bits + s->l2_bits;
|
164 | 419b19d9 | Stefan Hajnoczi | return (size + (1ULL << shift) - 1) >> shift; |
165 | 419b19d9 | Stefan Hajnoczi | } |
166 | 419b19d9 | Stefan Hajnoczi | |
167 | c142442b | Kevin Wolf | static inline int64_t align_offset(int64_t offset, int n) |
168 | c142442b | Kevin Wolf | { |
169 | c142442b | Kevin Wolf | offset = (offset + n - 1) & ~(n - 1); |
170 | c142442b | Kevin Wolf | return offset;
|
171 | c142442b | Kevin Wolf | } |
172 | c142442b | Kevin Wolf | |
173 | c142442b | Kevin Wolf | |
174 | f7d0fe02 | Kevin Wolf | // FIXME Need qcow2_ prefix to global functions
|
175 | f7d0fe02 | Kevin Wolf | |
176 | f7d0fe02 | Kevin Wolf | /* qcow2.c functions */
|
177 | bd28f835 | Kevin Wolf | int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
|
178 | bd28f835 | Kevin Wolf | int64_t sector_num, int nb_sectors);
|
179 | f7d0fe02 | Kevin Wolf | |
180 | f7d0fe02 | Kevin Wolf | /* qcow2-refcount.c functions */
|
181 | ed6ccf0f | Kevin Wolf | int qcow2_refcount_init(BlockDriverState *bs);
|
182 | ed6ccf0f | Kevin Wolf | void qcow2_refcount_close(BlockDriverState *bs);
|
183 | f7d0fe02 | Kevin Wolf | |
184 | ed6ccf0f | Kevin Wolf | int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size); |
185 | ed6ccf0f | Kevin Wolf | int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
|
186 | ed6ccf0f | Kevin Wolf | void qcow2_free_clusters(BlockDriverState *bs,
|
187 | 45aba42f | Kevin Wolf | int64_t offset, int64_t size); |
188 | ed6ccf0f | Kevin Wolf | void qcow2_free_any_clusters(BlockDriverState *bs,
|
189 | 45aba42f | Kevin Wolf | uint64_t cluster_offset, int nb_clusters);
|
190 | f7d0fe02 | Kevin Wolf | |
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 | 93913dfd | Kevin Wolf | bool qcow2_cache_set_writethrough(BlockDriverState *bs, Qcow2Cache *c,
|
232 | 93913dfd | Kevin Wolf | bool enable);
|
233 | 49381094 | Kevin Wolf | |
234 | 49381094 | Kevin Wolf | void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table); |
235 | 49381094 | Kevin Wolf | int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
|
236 | 49381094 | Kevin Wolf | int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
|
237 | 49381094 | Kevin Wolf | Qcow2Cache *dependency); |
238 | 3de0a294 | Kevin Wolf | void qcow2_cache_depends_on_flush(Qcow2Cache *c);
|
239 | 49381094 | Kevin Wolf | |
240 | 49381094 | Kevin Wolf | int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
|
241 | 49381094 | Kevin Wolf | void **table);
|
242 | 49381094 | Kevin Wolf | int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
|
243 | 49381094 | Kevin Wolf | void **table);
|
244 | 49381094 | Kevin Wolf | int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table); |
245 | 49381094 | Kevin Wolf | |
246 | f7d0fe02 | Kevin Wolf | #endif |