]>
Commit | Line | Data |
---|---|---|
49381094 KW |
1 | /* |
2 | * L2/refcount table cache for the QCOW2 format | |
3 | * | |
4 | * Copyright (c) 2010 Kevin Wolf <[email protected]> | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | ||
737e150e | 25 | #include "block/block_int.h" |
49381094 KW |
26 | #include "qemu-common.h" |
27 | #include "qcow2.h" | |
3cce16f4 | 28 | #include "trace.h" |
49381094 KW |
29 | |
30 | typedef struct Qcow2CachedTable { | |
2693310e AG |
31 | int64_t offset; |
32 | bool dirty; | |
33 | uint64_t lru_counter; | |
34 | int ref; | |
49381094 KW |
35 | } Qcow2CachedTable; |
36 | ||
37 | struct Qcow2Cache { | |
49381094 KW |
38 | Qcow2CachedTable* entries; |
39 | struct Qcow2Cache* depends; | |
bf595021 | 40 | int size; |
3de0a294 | 41 | bool depends_on_flush; |
72e80b89 | 42 | void *table_array; |
2693310e | 43 | uint64_t lru_counter; |
49381094 KW |
44 | }; |
45 | ||
72e80b89 AG |
46 | static inline void *qcow2_cache_get_table_addr(BlockDriverState *bs, |
47 | Qcow2Cache *c, int table) | |
48 | { | |
49 | BDRVQcowState *s = bs->opaque; | |
50 | return (uint8_t *) c->table_array + (size_t) table * s->cluster_size; | |
51 | } | |
52 | ||
baf07d60 AG |
53 | static inline int qcow2_cache_get_table_idx(BlockDriverState *bs, |
54 | Qcow2Cache *c, void *table) | |
55 | { | |
56 | BDRVQcowState *s = bs->opaque; | |
57 | ptrdiff_t table_offset = (uint8_t *) table - (uint8_t *) c->table_array; | |
58 | int idx = table_offset / s->cluster_size; | |
59 | assert(idx >= 0 && idx < c->size && table_offset % s->cluster_size == 0); | |
60 | return idx; | |
61 | } | |
62 | ||
6af4e9ea | 63 | Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables) |
49381094 KW |
64 | { |
65 | BDRVQcowState *s = bs->opaque; | |
66 | Qcow2Cache *c; | |
49381094 | 67 | |
02004bd4 | 68 | c = g_new0(Qcow2Cache, 1); |
49381094 | 69 | c->size = num_tables; |
02004bd4 | 70 | c->entries = g_try_new0(Qcow2CachedTable, num_tables); |
72e80b89 AG |
71 | c->table_array = qemu_try_blockalign(bs->file, |
72 | (size_t) num_tables * s->cluster_size); | |
73 | ||
74 | if (!c->entries || !c->table_array) { | |
75 | qemu_vfree(c->table_array); | |
76 | g_free(c->entries); | |
77 | g_free(c); | |
78 | c = NULL; | |
49381094 KW |
79 | } |
80 | ||
81 | return c; | |
82 | } | |
83 | ||
84 | int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c) | |
85 | { | |
86 | int i; | |
87 | ||
88 | for (i = 0; i < c->size; i++) { | |
89 | assert(c->entries[i].ref == 0); | |
49381094 KW |
90 | } |
91 | ||
72e80b89 | 92 | qemu_vfree(c->table_array); |
7267c094 AL |
93 | g_free(c->entries); |
94 | g_free(c); | |
49381094 KW |
95 | |
96 | return 0; | |
97 | } | |
98 | ||
99 | static int qcow2_cache_flush_dependency(BlockDriverState *bs, Qcow2Cache *c) | |
100 | { | |
101 | int ret; | |
102 | ||
103 | ret = qcow2_cache_flush(bs, c->depends); | |
104 | if (ret < 0) { | |
105 | return ret; | |
106 | } | |
107 | ||
108 | c->depends = NULL; | |
3de0a294 KW |
109 | c->depends_on_flush = false; |
110 | ||
49381094 KW |
111 | return 0; |
112 | } | |
113 | ||
114 | static int qcow2_cache_entry_flush(BlockDriverState *bs, Qcow2Cache *c, int i) | |
115 | { | |
116 | BDRVQcowState *s = bs->opaque; | |
3de0a294 | 117 | int ret = 0; |
49381094 KW |
118 | |
119 | if (!c->entries[i].dirty || !c->entries[i].offset) { | |
120 | return 0; | |
121 | } | |
122 | ||
3cce16f4 KW |
123 | trace_qcow2_cache_entry_flush(qemu_coroutine_self(), |
124 | c == s->l2_table_cache, i); | |
125 | ||
49381094 KW |
126 | if (c->depends) { |
127 | ret = qcow2_cache_flush_dependency(bs, c); | |
3de0a294 KW |
128 | } else if (c->depends_on_flush) { |
129 | ret = bdrv_flush(bs->file); | |
130 | if (ret >= 0) { | |
131 | c->depends_on_flush = false; | |
49381094 KW |
132 | } |
133 | } | |
134 | ||
3de0a294 KW |
135 | if (ret < 0) { |
136 | return ret; | |
137 | } | |
138 | ||
cf93980e | 139 | if (c == s->refcount_block_cache) { |
231bb267 | 140 | ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_REFCOUNT_BLOCK, |
cf93980e HR |
141 | c->entries[i].offset, s->cluster_size); |
142 | } else if (c == s->l2_table_cache) { | |
231bb267 | 143 | ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L2, |
cf93980e HR |
144 | c->entries[i].offset, s->cluster_size); |
145 | } else { | |
231bb267 | 146 | ret = qcow2_pre_write_overlap_check(bs, 0, |
cf93980e HR |
147 | c->entries[i].offset, s->cluster_size); |
148 | } | |
149 | ||
150 | if (ret < 0) { | |
151 | return ret; | |
152 | } | |
153 | ||
29c1a730 KW |
154 | if (c == s->refcount_block_cache) { |
155 | BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_UPDATE_PART); | |
156 | } else if (c == s->l2_table_cache) { | |
157 | BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE); | |
158 | } | |
159 | ||
72e80b89 AG |
160 | ret = bdrv_pwrite(bs->file, c->entries[i].offset, |
161 | qcow2_cache_get_table_addr(bs, c, i), s->cluster_size); | |
49381094 KW |
162 | if (ret < 0) { |
163 | return ret; | |
164 | } | |
165 | ||
166 | c->entries[i].dirty = false; | |
167 | ||
168 | return 0; | |
169 | } | |
170 | ||
171 | int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c) | |
172 | { | |
3cce16f4 | 173 | BDRVQcowState *s = bs->opaque; |
49381094 KW |
174 | int result = 0; |
175 | int ret; | |
176 | int i; | |
177 | ||
3cce16f4 KW |
178 | trace_qcow2_cache_flush(qemu_coroutine_self(), c == s->l2_table_cache); |
179 | ||
49381094 KW |
180 | for (i = 0; i < c->size; i++) { |
181 | ret = qcow2_cache_entry_flush(bs, c, i); | |
182 | if (ret < 0 && result != -ENOSPC) { | |
183 | result = ret; | |
184 | } | |
185 | } | |
186 | ||
187 | if (result == 0) { | |
188 | ret = bdrv_flush(bs->file); | |
189 | if (ret < 0) { | |
190 | result = ret; | |
191 | } | |
192 | } | |
193 | ||
194 | return result; | |
195 | } | |
196 | ||
197 | int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c, | |
198 | Qcow2Cache *dependency) | |
199 | { | |
200 | int ret; | |
201 | ||
202 | if (dependency->depends) { | |
203 | ret = qcow2_cache_flush_dependency(bs, dependency); | |
204 | if (ret < 0) { | |
205 | return ret; | |
206 | } | |
207 | } | |
208 | ||
209 | if (c->depends && (c->depends != dependency)) { | |
210 | ret = qcow2_cache_flush_dependency(bs, c); | |
211 | if (ret < 0) { | |
212 | return ret; | |
213 | } | |
214 | } | |
215 | ||
216 | c->depends = dependency; | |
217 | return 0; | |
218 | } | |
219 | ||
3de0a294 KW |
220 | void qcow2_cache_depends_on_flush(Qcow2Cache *c) |
221 | { | |
222 | c->depends_on_flush = true; | |
223 | } | |
224 | ||
e7108fea HR |
225 | int qcow2_cache_empty(BlockDriverState *bs, Qcow2Cache *c) |
226 | { | |
227 | int ret, i; | |
228 | ||
229 | ret = qcow2_cache_flush(bs, c); | |
230 | if (ret < 0) { | |
231 | return ret; | |
232 | } | |
233 | ||
234 | for (i = 0; i < c->size; i++) { | |
235 | assert(c->entries[i].ref == 0); | |
236 | c->entries[i].offset = 0; | |
2693310e | 237 | c->entries[i].lru_counter = 0; |
e7108fea HR |
238 | } |
239 | ||
2693310e AG |
240 | c->lru_counter = 0; |
241 | ||
e7108fea HR |
242 | return 0; |
243 | } | |
244 | ||
49381094 KW |
245 | static int qcow2_cache_do_get(BlockDriverState *bs, Qcow2Cache *c, |
246 | uint64_t offset, void **table, bool read_from_disk) | |
247 | { | |
248 | BDRVQcowState *s = bs->opaque; | |
249 | int i; | |
250 | int ret; | |
812e4082 | 251 | int lookup_index; |
fdfbca82 AG |
252 | uint64_t min_lru_counter = UINT64_MAX; |
253 | int min_lru_index = -1; | |
49381094 | 254 | |
3cce16f4 KW |
255 | trace_qcow2_cache_get(qemu_coroutine_self(), c == s->l2_table_cache, |
256 | offset, read_from_disk); | |
257 | ||
49381094 | 258 | /* Check if the table is already cached */ |
812e4082 AG |
259 | i = lookup_index = (offset / s->cluster_size * 4) % c->size; |
260 | do { | |
fdfbca82 AG |
261 | const Qcow2CachedTable *t = &c->entries[i]; |
262 | if (t->offset == offset) { | |
49381094 KW |
263 | goto found; |
264 | } | |
fdfbca82 AG |
265 | if (t->ref == 0 && t->lru_counter < min_lru_counter) { |
266 | min_lru_counter = t->lru_counter; | |
267 | min_lru_index = i; | |
268 | } | |
812e4082 AG |
269 | if (++i == c->size) { |
270 | i = 0; | |
271 | } | |
272 | } while (i != lookup_index); | |
fdfbca82 AG |
273 | |
274 | if (min_lru_index == -1) { | |
275 | /* This can't happen in current synchronous code, but leave the check | |
276 | * here as a reminder for whoever starts using AIO with the cache */ | |
277 | abort(); | |
49381094 KW |
278 | } |
279 | ||
fdfbca82 AG |
280 | /* Cache miss: write a table back and replace it */ |
281 | i = min_lru_index; | |
3cce16f4 KW |
282 | trace_qcow2_cache_get_replace_entry(qemu_coroutine_self(), |
283 | c == s->l2_table_cache, i); | |
49381094 KW |
284 | if (i < 0) { |
285 | return i; | |
286 | } | |
287 | ||
288 | ret = qcow2_cache_entry_flush(bs, c, i); | |
289 | if (ret < 0) { | |
290 | return ret; | |
291 | } | |
292 | ||
3cce16f4 KW |
293 | trace_qcow2_cache_get_read(qemu_coroutine_self(), |
294 | c == s->l2_table_cache, i); | |
49381094 KW |
295 | c->entries[i].offset = 0; |
296 | if (read_from_disk) { | |
29c1a730 KW |
297 | if (c == s->l2_table_cache) { |
298 | BLKDBG_EVENT(bs->file, BLKDBG_L2_LOAD); | |
299 | } | |
300 | ||
72e80b89 AG |
301 | ret = bdrv_pread(bs->file, offset, qcow2_cache_get_table_addr(bs, c, i), |
302 | s->cluster_size); | |
49381094 KW |
303 | if (ret < 0) { |
304 | return ret; | |
305 | } | |
306 | } | |
307 | ||
49381094 KW |
308 | c->entries[i].offset = offset; |
309 | ||
310 | /* And return the right table */ | |
311 | found: | |
49381094 | 312 | c->entries[i].ref++; |
72e80b89 | 313 | *table = qcow2_cache_get_table_addr(bs, c, i); |
3cce16f4 KW |
314 | |
315 | trace_qcow2_cache_get_done(qemu_coroutine_self(), | |
316 | c == s->l2_table_cache, i); | |
317 | ||
49381094 KW |
318 | return 0; |
319 | } | |
320 | ||
321 | int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset, | |
322 | void **table) | |
323 | { | |
324 | return qcow2_cache_do_get(bs, c, offset, table, true); | |
325 | } | |
326 | ||
327 | int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset, | |
328 | void **table) | |
329 | { | |
330 | return qcow2_cache_do_get(bs, c, offset, table, false); | |
331 | } | |
332 | ||
333 | int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table) | |
334 | { | |
baf07d60 | 335 | int i = qcow2_cache_get_table_idx(bs, c, *table); |
49381094 | 336 | |
baf07d60 AG |
337 | if (c->entries[i].offset == 0) { |
338 | return -ENOENT; | |
49381094 | 339 | } |
49381094 | 340 | |
49381094 KW |
341 | c->entries[i].ref--; |
342 | *table = NULL; | |
343 | ||
2693310e AG |
344 | if (c->entries[i].ref == 0) { |
345 | c->entries[i].lru_counter = ++c->lru_counter; | |
346 | } | |
347 | ||
49381094 | 348 | assert(c->entries[i].ref >= 0); |
6af4e9ea | 349 | return 0; |
49381094 KW |
350 | } |
351 | ||
72e80b89 AG |
352 | void qcow2_cache_entry_mark_dirty(BlockDriverState *bs, Qcow2Cache *c, |
353 | void *table) | |
49381094 | 354 | { |
baf07d60 AG |
355 | int i = qcow2_cache_get_table_idx(bs, c, table); |
356 | assert(c->entries[i].offset != 0); | |
49381094 KW |
357 | c->entries[i].dirty = true; |
358 | } |