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