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_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
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 QED_DEFAULT_CLUSTER_SIZE = 64 * 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} 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 BlockDriverAIOCB common;
133 QEMUBH *bh;
134 int bh_ret;
135 QSIMPLEQ_ENTRY(QEDAIOCB) next;
136 int flags;
137 bool *finished;
138 uint64_t end_pos;
139
140
141 QEMUIOVector *qiov;
142 size_t qiov_offset;
143
144
145 QEMUIOVector cur_qiov;
146 uint64_t cur_pos;
147 uint64_t cur_cluster;
148 unsigned int cur_nclusters;
149 int find_cluster_ret;
150
151 QEDRequest request;
152} QEDAIOCB;
153
154typedef struct {
155 BlockDriverState *bs;
156 uint64_t file_size;
157
158 QEDHeader header;
159 QEDTable *l1_table;
160 L2TableCache l2_cache;
161 uint32_t table_nelems;
162 uint32_t l1_shift;
163 uint32_t l2_shift;
164 uint32_t l2_mask;
165
166
167 QSIMPLEQ_HEAD(, QEDAIOCB) allocating_write_reqs;
168 bool allocating_write_reqs_plugged;
169
170
171 QEMUTimer *need_check_timer;
172} BDRVQEDState;
173
174enum {
175 QED_CLUSTER_FOUND,
176 QED_CLUSTER_ZERO,
177 QED_CLUSTER_L2,
178 QED_CLUSTER_L1,
179};
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200typedef void QEDFindClusterFunc(void *opaque, int ret, uint64_t offset, size_t len);
201
202
203
204
205typedef struct {
206 BlockDriverCompletionFunc *cb;
207 void *opaque;
208} GenericCB;
209
210void *gencb_alloc(size_t len, BlockDriverCompletionFunc *cb, void *opaque);
211void gencb_complete(void *opaque, int ret);
212
213
214
215
216int qed_write_header_sync(BDRVQEDState *s);
217
218
219
220
221void qed_init_l2_cache(L2TableCache *l2_cache);
222void qed_free_l2_cache(L2TableCache *l2_cache);
223CachedL2Table *qed_alloc_l2_cache_entry(L2TableCache *l2_cache);
224void qed_unref_l2_cache_entry(CachedL2Table *entry);
225CachedL2Table *qed_find_l2_cache_entry(L2TableCache *l2_cache, uint64_t offset);
226void qed_commit_l2_cache_entry(L2TableCache *l2_cache, CachedL2Table *l2_table);
227
228
229
230
231int qed_read_l1_table_sync(BDRVQEDState *s);
232void qed_write_l1_table(BDRVQEDState *s, unsigned int index, unsigned int n,
233 BlockDriverCompletionFunc *cb, void *opaque);
234int qed_write_l1_table_sync(BDRVQEDState *s, unsigned int index,
235 unsigned int n);
236int qed_read_l2_table_sync(BDRVQEDState *s, QEDRequest *request,
237 uint64_t offset);
238void qed_read_l2_table(BDRVQEDState *s, QEDRequest *request, uint64_t offset,
239 BlockDriverCompletionFunc *cb, void *opaque);
240void qed_write_l2_table(BDRVQEDState *s, QEDRequest *request,
241 unsigned int index, unsigned int n, bool flush,
242 BlockDriverCompletionFunc *cb, void *opaque);
243int qed_write_l2_table_sync(BDRVQEDState *s, QEDRequest *request,
244 unsigned int index, unsigned int n, bool flush);
245
246
247
248
249void qed_find_cluster(BDRVQEDState *s, QEDRequest *request, uint64_t pos,
250 size_t len, QEDFindClusterFunc *cb, void *opaque);
251
252
253
254
255int qed_check(BDRVQEDState *s, BdrvCheckResult *result, bool fix);
256
257QEDTable *qed_alloc_table(BDRVQEDState *s);
258
259
260
261
262static inline uint64_t qed_start_of_cluster(BDRVQEDState *s, uint64_t offset)
263{
264 return offset & ~(uint64_t)(s->header.cluster_size - 1);
265}
266
267static inline uint64_t qed_offset_into_cluster(BDRVQEDState *s, uint64_t offset)
268{
269 return offset & (s->header.cluster_size - 1);
270}
271
272static inline uint64_t qed_bytes_to_clusters(BDRVQEDState *s, uint64_t bytes)
273{
274 return qed_start_of_cluster(s, bytes + (s->header.cluster_size - 1)) /
275 (s->header.cluster_size - 1);
276}
277
278static inline unsigned int qed_l1_index(BDRVQEDState *s, uint64_t pos)
279{
280 return pos >> s->l1_shift;
281}
282
283static inline unsigned int qed_l2_index(BDRVQEDState *s, uint64_t pos)
284{
285 return (pos >> s->l2_shift) & s->l2_mask;
286}
287
288
289
290
291static inline bool qed_check_cluster_offset(BDRVQEDState *s, uint64_t offset)
292{
293 uint64_t header_size = (uint64_t)s->header.header_size *
294 s->header.cluster_size;
295
296 if (offset & (s->header.cluster_size - 1)) {
297 return false;
298 }
299 return offset >= header_size && offset < s->file_size;
300}
301
302
303
304
305static inline bool qed_check_table_offset(BDRVQEDState *s, uint64_t offset)
306{
307 uint64_t end_offset = offset + (s->header.table_size - 1) *
308 s->header.cluster_size;
309
310
311 if (end_offset <= offset) {
312 return false;
313 }
314
315 return qed_check_cluster_offset(s, offset) &&
316 qed_check_cluster_offset(s, end_offset);
317}
318
319static inline bool qed_offset_is_cluster_aligned(BDRVQEDState *s,
320 uint64_t offset)
321{
322 if (qed_offset_into_cluster(s, offset)) {
323 return false;
324 }
325 return true;
326}
327
328static inline bool qed_offset_is_unalloc_cluster(uint64_t offset)
329{
330 if (offset == 0) {
331 return true;
332 }
333 return false;
334}
335
336static inline bool qed_offset_is_zero_cluster(uint64_t offset)
337{
338 if (offset == 1) {
339 return true;
340 }
341 return false;
342}
343
344#endif
345