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