1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#ifndef BLOCK_QED_H
16#define BLOCK_QED_H
17
18#include "block/block_int.h"
19#include "qemu/cutils.h"
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47#define QED_DEFAULT_CLUSTER_SIZE 65536
48enum {
49 QED_MAGIC = 'Q' | 'E' << 8 | 'D' << 16 | '\0' << 24,
50
51
52 QED_F_BACKING_FILE = 0x01,
53
54
55 QED_F_NEED_CHECK = 0x02,
56
57
58 QED_F_BACKING_FORMAT_NO_PROBE = 0x04,
59
60
61 QED_FEATURE_MASK = QED_F_BACKING_FILE |
62 QED_F_NEED_CHECK |
63 QED_F_BACKING_FORMAT_NO_PROBE,
64 QED_COMPAT_FEATURE_MASK = 0,
65 QED_AUTOCLEAR_FEATURE_MASK = 0,
66
67
68
69
70
71 QED_MIN_CLUSTER_SIZE = 4 * 1024,
72 QED_MAX_CLUSTER_SIZE = 64 * 1024 * 1024,
73
74
75
76
77
78 QED_MIN_TABLE_SIZE = 1,
79 QED_MAX_TABLE_SIZE = 16,
80 QED_DEFAULT_TABLE_SIZE = 4,
81
82
83 QED_NEED_CHECK_TIMEOUT = 5,
84};
85
86typedef struct {
87 uint32_t magic;
88
89 uint32_t cluster_size;
90 uint32_t table_size;
91 uint32_t header_size;
92
93 uint64_t features;
94 uint64_t compat_features;
95 uint64_t autoclear_features;
96
97 uint64_t l1_table_offset;
98 uint64_t image_size;
99
100
101 uint32_t backing_filename_offset;
102 uint32_t backing_filename_size;
103} QEMU_PACKED QEDHeader;
104
105typedef struct {
106 uint64_t offsets[0];
107} QEDTable;
108
109
110typedef struct CachedL2Table {
111 QEDTable *table;
112 uint64_t offset;
113 QTAILQ_ENTRY(CachedL2Table) node;
114 int ref;
115} CachedL2Table;
116
117typedef struct {
118 QTAILQ_HEAD(, CachedL2Table) entries;
119 unsigned int n_entries;
120} L2TableCache;
121
122typedef struct QEDRequest {
123 CachedL2Table *l2_table;
124} QEDRequest;
125
126enum {
127 QED_AIOCB_WRITE = 0x0001,
128 QED_AIOCB_ZERO = 0x0002,
129};
130
131typedef struct QEDAIOCB {
132 BlockDriverState *bs;
133 QSIMPLEQ_ENTRY(QEDAIOCB) next;
134 int flags;
135 uint64_t end_pos;
136
137
138 QEMUIOVector *qiov;
139 size_t qiov_offset;
140
141
142 QEMUIOVector cur_qiov;
143 uint64_t cur_pos;
144 uint64_t cur_cluster;
145 unsigned int cur_nclusters;
146 int find_cluster_ret;
147
148 QEDRequest request;
149} QEDAIOCB;
150
151typedef struct {
152 BlockDriverState *bs;
153
154
155
156
157 QEDHeader header;
158
159
160 CoMutex table_lock;
161 QEDTable *l1_table;
162 L2TableCache l2_cache;
163 uint32_t table_nelems;
164 uint32_t l1_shift;
165 uint32_t l2_shift;
166 uint32_t l2_mask;
167 uint64_t file_size;
168
169
170 QEDAIOCB *allocating_acb;
171 CoQueue allocating_write_reqs;
172 bool allocating_write_reqs_plugged;
173
174
175 QEMUTimer *need_check_timer;
176} BDRVQEDState;
177
178enum {
179 QED_CLUSTER_FOUND,
180 QED_CLUSTER_ZERO,
181 QED_CLUSTER_L2,
182 QED_CLUSTER_L1,
183};
184
185
186
187
188int qed_write_header_sync(BDRVQEDState *s);
189
190
191
192
193void qed_init_l2_cache(L2TableCache *l2_cache);
194void qed_free_l2_cache(L2TableCache *l2_cache);
195CachedL2Table *qed_alloc_l2_cache_entry(L2TableCache *l2_cache);
196void qed_unref_l2_cache_entry(CachedL2Table *entry);
197CachedL2Table *qed_find_l2_cache_entry(L2TableCache *l2_cache, uint64_t offset);
198void qed_commit_l2_cache_entry(L2TableCache *l2_cache, CachedL2Table *l2_table);
199
200
201
202
203int coroutine_fn qed_read_l1_table_sync(BDRVQEDState *s);
204int coroutine_fn qed_write_l1_table(BDRVQEDState *s, unsigned int index,
205 unsigned int n);
206int coroutine_fn qed_write_l1_table_sync(BDRVQEDState *s, unsigned int index,
207 unsigned int n);
208int coroutine_fn qed_read_l2_table_sync(BDRVQEDState *s, QEDRequest *request,
209 uint64_t offset);
210int coroutine_fn qed_read_l2_table(BDRVQEDState *s, QEDRequest *request,
211 uint64_t offset);
212int coroutine_fn qed_write_l2_table(BDRVQEDState *s, QEDRequest *request,
213 unsigned int index, unsigned int n,
214 bool flush);
215int coroutine_fn qed_write_l2_table_sync(BDRVQEDState *s, QEDRequest *request,
216 unsigned int index, unsigned int n,
217 bool flush);
218
219
220
221
222int coroutine_fn qed_find_cluster(BDRVQEDState *s, QEDRequest *request,
223 uint64_t pos, size_t *len,
224 uint64_t *img_offset);
225
226
227
228
229int coroutine_fn qed_check(BDRVQEDState *s, BdrvCheckResult *result, bool fix);
230
231QEDTable *qed_alloc_table(BDRVQEDState *s);
232
233
234
235
236static inline uint64_t qed_start_of_cluster(BDRVQEDState *s, uint64_t offset)
237{
238 return offset & ~(uint64_t)(s->header.cluster_size - 1);
239}
240
241static inline uint64_t qed_offset_into_cluster(BDRVQEDState *s, uint64_t offset)
242{
243 return offset & (s->header.cluster_size - 1);
244}
245
246static inline uint64_t qed_bytes_to_clusters(BDRVQEDState *s, uint64_t bytes)
247{
248 return qed_start_of_cluster(s, bytes + (s->header.cluster_size - 1)) /
249 (s->header.cluster_size - 1);
250}
251
252static inline unsigned int qed_l1_index(BDRVQEDState *s, uint64_t pos)
253{
254 return pos >> s->l1_shift;
255}
256
257static inline unsigned int qed_l2_index(BDRVQEDState *s, uint64_t pos)
258{
259 return (pos >> s->l2_shift) & s->l2_mask;
260}
261
262
263
264
265static inline bool qed_check_cluster_offset(BDRVQEDState *s, uint64_t offset)
266{
267 uint64_t header_size = (uint64_t)s->header.header_size *
268 s->header.cluster_size;
269
270 if (offset & (s->header.cluster_size - 1)) {
271 return false;
272 }
273 return offset >= header_size && offset < s->file_size;
274}
275
276
277
278
279static inline bool qed_check_table_offset(BDRVQEDState *s, uint64_t offset)
280{
281 uint64_t end_offset = offset + (s->header.table_size - 1) *
282 s->header.cluster_size;
283
284
285 if (end_offset <= offset) {
286 return false;
287 }
288
289 return qed_check_cluster_offset(s, offset) &&
290 qed_check_cluster_offset(s, end_offset);
291}
292
293static inline bool qed_offset_is_cluster_aligned(BDRVQEDState *s,
294 uint64_t offset)
295{
296 if (qed_offset_into_cluster(s, offset)) {
297 return false;
298 }
299 return true;
300}
301
302static inline bool qed_offset_is_unalloc_cluster(uint64_t offset)
303{
304 if (offset == 0) {
305 return true;
306 }
307 return false;
308}
309
310static inline bool qed_offset_is_zero_cluster(uint64_t offset)
311{
312 if (offset == 1) {
313 return true;
314 }
315 return false;
316}
317
318#endif
319