]>
Commit | Line | Data |
---|---|---|
75411d23 SH |
1 | /* |
2 | * QEMU Enhanced Disk Format | |
3 | * | |
4 | * Copyright IBM, Corp. 2010 | |
5 | * | |
6 | * Authors: | |
7 | * Stefan Hajnoczi <[email protected]> | |
8 | * Anthony Liguori <[email protected]> | |
9 | * | |
10 | * This work is licensed under the terms of the GNU LGPL, version 2 or later. | |
11 | * See the COPYING.LIB file in the top-level directory. | |
12 | * | |
13 | */ | |
14 | ||
15 | #ifndef BLOCK_QED_H | |
16 | #define BLOCK_QED_H | |
17 | ||
737e150e | 18 | #include "block/block_int.h" |
75411d23 SH |
19 | |
20 | /* The layout of a QED file is as follows: | |
21 | * | |
22 | * +--------+----------+----------+----------+-----+ | |
23 | * | header | L1 table | cluster0 | cluster1 | ... | | |
24 | * +--------+----------+----------+----------+-----+ | |
25 | * | |
26 | * There is a 2-level pagetable for cluster allocation: | |
27 | * | |
28 | * +----------+ | |
29 | * | L1 table | | |
30 | * +----------+ | |
31 | * ,------' | '------. | |
32 | * +----------+ | +----------+ | |
33 | * | L2 table | ... | L2 table | | |
34 | * +----------+ +----------+ | |
35 | * ,------' | '------. | |
36 | * +----------+ | +----------+ | |
37 | * | Data | ... | Data | | |
38 | * +----------+ +----------+ | |
39 | * | |
40 | * The L1 table is fixed size and always present. L2 tables are allocated on | |
41 | * demand. The L1 table size determines the maximum possible image size; it | |
42 | * can be influenced using the cluster_size and table_size values. | |
43 | * | |
44 | * All fields are little-endian on disk. | |
45 | */ | |
7ab74849 | 46 | #define QED_DEFAULT_CLUSTER_SIZE 65536 |
75411d23 SH |
47 | enum { |
48 | QED_MAGIC = 'Q' | 'E' << 8 | 'D' << 16 | '\0' << 24, | |
49 | ||
50 | /* The image supports a backing file */ | |
51 | QED_F_BACKING_FILE = 0x01, | |
52 | ||
01979a98 SH |
53 | /* The image needs a consistency check before use */ |
54 | QED_F_NEED_CHECK = 0x02, | |
55 | ||
75411d23 SH |
56 | /* The backing file format must not be probed, treat as raw image */ |
57 | QED_F_BACKING_FORMAT_NO_PROBE = 0x04, | |
58 | ||
59 | /* Feature bits must be used when the on-disk format changes */ | |
60 | QED_FEATURE_MASK = QED_F_BACKING_FILE | /* supported feature bits */ | |
01979a98 | 61 | QED_F_NEED_CHECK | |
75411d23 SH |
62 | QED_F_BACKING_FORMAT_NO_PROBE, |
63 | QED_COMPAT_FEATURE_MASK = 0, /* supported compat feature bits */ | |
64 | QED_AUTOCLEAR_FEATURE_MASK = 0, /* supported autoclear feature bits */ | |
65 | ||
66 | /* Data is stored in groups of sectors called clusters. Cluster size must | |
67 | * be large to avoid keeping too much metadata. I/O requests that have | |
68 | * sub-cluster size will require read-modify-write. | |
69 | */ | |
70 | QED_MIN_CLUSTER_SIZE = 4 * 1024, /* in bytes */ | |
71 | QED_MAX_CLUSTER_SIZE = 64 * 1024 * 1024, | |
75411d23 SH |
72 | |
73 | /* Allocated clusters are tracked using a 2-level pagetable. Table size is | |
74 | * a multiple of clusters so large maximum image sizes can be supported | |
75 | * without jacking up the cluster size too much. | |
76 | */ | |
77 | QED_MIN_TABLE_SIZE = 1, /* in clusters */ | |
78 | QED_MAX_TABLE_SIZE = 16, | |
79 | QED_DEFAULT_TABLE_SIZE = 4, | |
6f321e93 SH |
80 | |
81 | /* Delay to flush and clean image after last allocating write completes */ | |
82 | QED_NEED_CHECK_TIMEOUT = 5, /* in seconds */ | |
75411d23 SH |
83 | }; |
84 | ||
85 | typedef struct { | |
86 | uint32_t magic; /* QED\0 */ | |
87 | ||
88 | uint32_t cluster_size; /* in bytes */ | |
89 | uint32_t table_size; /* for L1 and L2 tables, in clusters */ | |
90 | uint32_t header_size; /* in clusters */ | |
91 | ||
92 | uint64_t features; /* format feature bits */ | |
93 | uint64_t compat_features; /* compatible feature bits */ | |
94 | uint64_t autoclear_features; /* self-resetting feature bits */ | |
95 | ||
96 | uint64_t l1_table_offset; /* in bytes */ | |
97 | uint64_t image_size; /* total logical image size, in bytes */ | |
98 | ||
99 | /* if (features & QED_F_BACKING_FILE) */ | |
100 | uint32_t backing_filename_offset; /* in bytes from start of header */ | |
101 | uint32_t backing_filename_size; /* in bytes */ | |
687fb893 | 102 | } QEMU_PACKED QEDHeader; |
75411d23 | 103 | |
298800ca SH |
104 | typedef struct { |
105 | uint64_t offsets[0]; /* in bytes */ | |
106 | } QEDTable; | |
107 | ||
108 | /* The L2 cache is a simple write-through cache for L2 structures */ | |
109 | typedef struct CachedL2Table { | |
110 | QEDTable *table; | |
111 | uint64_t offset; /* offset=0 indicates an invalidate entry */ | |
112 | QTAILQ_ENTRY(CachedL2Table) node; | |
113 | int ref; | |
114 | } CachedL2Table; | |
115 | ||
116 | typedef struct { | |
117 | QTAILQ_HEAD(, CachedL2Table) entries; | |
118 | unsigned int n_entries; | |
119 | } L2TableCache; | |
120 | ||
121 | typedef struct QEDRequest { | |
122 | CachedL2Table *l2_table; | |
123 | } QEDRequest; | |
124 | ||
6e4f59bd SH |
125 | enum { |
126 | QED_AIOCB_WRITE = 0x0001, /* read or write? */ | |
0e71be19 | 127 | QED_AIOCB_ZERO = 0x0002, /* zero write, used with QED_AIOCB_WRITE */ |
6e4f59bd SH |
128 | }; |
129 | ||
eabba580 | 130 | typedef struct QEDAIOCB { |
7c84b1b8 | 131 | BlockAIOCB common; |
eabba580 SH |
132 | QEMUBH *bh; |
133 | int bh_ret; /* final return status for completion bh */ | |
134 | QSIMPLEQ_ENTRY(QEDAIOCB) next; /* next request */ | |
6e4f59bd | 135 | int flags; /* QED_AIOCB_* bits ORed together */ |
eabba580 SH |
136 | uint64_t end_pos; /* request end on block device, in bytes */ |
137 | ||
138 | /* User scatter-gather list */ | |
139 | QEMUIOVector *qiov; | |
140 | size_t qiov_offset; /* byte count already processed */ | |
141 | ||
142 | /* Current cluster scatter-gather list */ | |
143 | QEMUIOVector cur_qiov; | |
f06ee3d4 | 144 | QEMUIOVector *backing_qiov; |
eabba580 SH |
145 | uint64_t cur_pos; /* position on block device, in bytes */ |
146 | uint64_t cur_cluster; /* cluster offset in image file */ | |
147 | unsigned int cur_nclusters; /* number of clusters being accessed */ | |
148 | int find_cluster_ret; /* used for L1/L2 update */ | |
149 | ||
150 | QEDRequest request; | |
151 | } QEDAIOCB; | |
152 | ||
75411d23 SH |
153 | typedef struct { |
154 | BlockDriverState *bs; /* device */ | |
155 | uint64_t file_size; /* length of image file, in bytes */ | |
156 | ||
157 | QEDHeader header; /* always cpu-endian */ | |
298800ca SH |
158 | QEDTable *l1_table; |
159 | L2TableCache l2_cache; /* l2 table cache */ | |
75411d23 SH |
160 | uint32_t table_nelems; |
161 | uint32_t l1_shift; | |
162 | uint32_t l2_shift; | |
163 | uint32_t l2_mask; | |
eabba580 SH |
164 | |
165 | /* Allocating write request queue */ | |
166 | QSIMPLEQ_HEAD(, QEDAIOCB) allocating_write_reqs; | |
6f321e93 SH |
167 | bool allocating_write_reqs_plugged; |
168 | ||
169 | /* Periodic flush and clear need check flag */ | |
170 | QEMUTimer *need_check_timer; | |
75411d23 SH |
171 | } BDRVQEDState; |
172 | ||
298800ca SH |
173 | enum { |
174 | QED_CLUSTER_FOUND, /* cluster found */ | |
21df65b6 | 175 | QED_CLUSTER_ZERO, /* zero cluster found */ |
298800ca SH |
176 | QED_CLUSTER_L2, /* cluster missing in L2 */ |
177 | QED_CLUSTER_L1, /* cluster missing in L1 */ | |
178 | }; | |
179 | ||
180 | /** | |
181 | * qed_find_cluster() completion callback | |
182 | * | |
183 | * @opaque: User data for completion callback | |
184 | * @ret: QED_CLUSTER_FOUND Success | |
185 | * QED_CLUSTER_L2 Data cluster unallocated in L2 | |
186 | * QED_CLUSTER_L1 L2 unallocated in L1 | |
187 | * -errno POSIX error occurred | |
188 | * @offset: Data cluster offset | |
189 | * @len: Contiguous bytes starting from cluster offset | |
190 | * | |
191 | * This function is invoked when qed_find_cluster() completes. | |
192 | * | |
193 | * On success ret is QED_CLUSTER_FOUND and offset/len are a contiguous range | |
194 | * in the image file. | |
195 | * | |
196 | * On failure ret is QED_CLUSTER_L2 or QED_CLUSTER_L1 for missing L2 or L1 | |
197 | * table offset, respectively. len is number of contiguous unallocated bytes. | |
198 | */ | |
199 | typedef void QEDFindClusterFunc(void *opaque, int ret, uint64_t offset, size_t len); | |
200 | ||
201 | /** | |
202 | * Generic callback for chaining async callbacks | |
203 | */ | |
204 | typedef struct { | |
097310b5 | 205 | BlockCompletionFunc *cb; |
298800ca SH |
206 | void *opaque; |
207 | } GenericCB; | |
208 | ||
097310b5 | 209 | void *gencb_alloc(size_t len, BlockCompletionFunc *cb, void *opaque); |
298800ca SH |
210 | void gencb_complete(void *opaque, int ret); |
211 | ||
b10170ac SH |
212 | /** |
213 | * Header functions | |
214 | */ | |
215 | int qed_write_header_sync(BDRVQEDState *s); | |
216 | ||
298800ca SH |
217 | /** |
218 | * L2 cache functions | |
219 | */ | |
220 | void qed_init_l2_cache(L2TableCache *l2_cache); | |
221 | void qed_free_l2_cache(L2TableCache *l2_cache); | |
222 | CachedL2Table *qed_alloc_l2_cache_entry(L2TableCache *l2_cache); | |
223 | void qed_unref_l2_cache_entry(CachedL2Table *entry); | |
224 | CachedL2Table *qed_find_l2_cache_entry(L2TableCache *l2_cache, uint64_t offset); | |
225 | void qed_commit_l2_cache_entry(L2TableCache *l2_cache, CachedL2Table *l2_table); | |
226 | ||
227 | /** | |
228 | * Table I/O functions | |
229 | */ | |
230 | int qed_read_l1_table_sync(BDRVQEDState *s); | |
231 | void qed_write_l1_table(BDRVQEDState *s, unsigned int index, unsigned int n, | |
097310b5 | 232 | BlockCompletionFunc *cb, void *opaque); |
298800ca SH |
233 | int qed_write_l1_table_sync(BDRVQEDState *s, unsigned int index, |
234 | unsigned int n); | |
235 | int qed_read_l2_table_sync(BDRVQEDState *s, QEDRequest *request, | |
236 | uint64_t offset); | |
237 | void qed_read_l2_table(BDRVQEDState *s, QEDRequest *request, uint64_t offset, | |
097310b5 | 238 | BlockCompletionFunc *cb, void *opaque); |
298800ca SH |
239 | void qed_write_l2_table(BDRVQEDState *s, QEDRequest *request, |
240 | unsigned int index, unsigned int n, bool flush, | |
097310b5 | 241 | BlockCompletionFunc *cb, void *opaque); |
298800ca SH |
242 | int qed_write_l2_table_sync(BDRVQEDState *s, QEDRequest *request, |
243 | unsigned int index, unsigned int n, bool flush); | |
244 | ||
245 | /** | |
246 | * Cluster functions | |
247 | */ | |
248 | void qed_find_cluster(BDRVQEDState *s, QEDRequest *request, uint64_t pos, | |
249 | size_t len, QEDFindClusterFunc *cb, void *opaque); | |
250 | ||
251 | /** | |
252 | * Consistency check | |
253 | */ | |
254 | int qed_check(BDRVQEDState *s, BdrvCheckResult *result, bool fix); | |
255 | ||
256 | QEDTable *qed_alloc_table(BDRVQEDState *s); | |
257 | ||
75411d23 SH |
258 | /** |
259 | * Round down to the start of a cluster | |
260 | */ | |
261 | static 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 | ||
298800ca SH |
266 | static inline uint64_t qed_offset_into_cluster(BDRVQEDState *s, uint64_t offset) |
267 | { | |
268 | return offset & (s->header.cluster_size - 1); | |
269 | } | |
270 | ||
19dfc44a | 271 | static inline uint64_t qed_bytes_to_clusters(BDRVQEDState *s, uint64_t bytes) |
298800ca SH |
272 | { |
273 | return qed_start_of_cluster(s, bytes + (s->header.cluster_size - 1)) / | |
274 | (s->header.cluster_size - 1); | |
275 | } | |
276 | ||
277 | static inline unsigned int qed_l1_index(BDRVQEDState *s, uint64_t pos) | |
278 | { | |
279 | return pos >> s->l1_shift; | |
280 | } | |
281 | ||
282 | static inline unsigned int qed_l2_index(BDRVQEDState *s, uint64_t pos) | |
283 | { | |
284 | return (pos >> s->l2_shift) & s->l2_mask; | |
285 | } | |
286 | ||
75411d23 SH |
287 | /** |
288 | * Test if a cluster offset is valid | |
289 | */ | |
290 | static 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 | * Test if a table offset is valid | |
303 | */ | |
304 | static 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 | /* Overflow check */ | |
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 | ||
21df65b6 AL |
318 | static 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 | ||
327 | static inline bool qed_offset_is_unalloc_cluster(uint64_t offset) | |
328 | { | |
329 | if (offset == 0) { | |
330 | return true; | |
331 | } | |
332 | return false; | |
333 | } | |
334 | ||
335 | static inline bool qed_offset_is_zero_cluster(uint64_t offset) | |
336 | { | |
337 | if (offset == 1) { | |
338 | return true; | |
339 | } | |
340 | return false; | |
341 | } | |
342 | ||
75411d23 | 343 | #endif /* BLOCK_QED_H */ |