]> git.proxmox.com Git - mirror_qemu.git/blame - block/qcow2-cache.c
qcow2: Add table size field to Qcow2Cache
[mirror_qemu.git] / block / qcow2-cache.c
CommitLineData
49381094
KW
1/*
2 * L2/refcount table cache for the QCOW2 format
3 *
4 * Copyright (c) 2010 Kevin Wolf <kwolf@redhat.com>
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
80c71a24 25#include "qemu/osdep.h"
737e150e 26#include "block/block_int.h"
49381094
KW
27#include "qemu-common.h"
28#include "qcow2.h"
3cce16f4 29#include "trace.h"
49381094
KW
30
31typedef struct Qcow2CachedTable {
2693310e 32 int64_t offset;
2693310e
AG
33 uint64_t lru_counter;
34 int ref;
909c260c 35 bool dirty;
49381094
KW
36} Qcow2CachedTable;
37
38struct Qcow2Cache {
d1b4efe5
AG
39 Qcow2CachedTable *entries;
40 struct Qcow2Cache *depends;
bf595021 41 int size;
03019d73 42 int table_size;
3de0a294 43 bool depends_on_flush;
72e80b89 44 void *table_array;
2693310e 45 uint64_t lru_counter;
279621c0 46 uint64_t cache_clean_lru_counter;
49381094
KW
47};
48
72e80b89
AG
49static inline void *qcow2_cache_get_table_addr(BlockDriverState *bs,
50 Qcow2Cache *c, int table)
51{
03019d73 52 return (uint8_t *) c->table_array + (size_t) table * c->table_size;
72e80b89
AG
53}
54
baf07d60
AG
55static inline int qcow2_cache_get_table_idx(BlockDriverState *bs,
56 Qcow2Cache *c, void *table)
57{
baf07d60 58 ptrdiff_t table_offset = (uint8_t *) table - (uint8_t *) c->table_array;
03019d73
AG
59 int idx = table_offset / c->table_size;
60 assert(idx >= 0 && idx < c->size && table_offset % c->table_size == 0);
baf07d60
AG
61 return idx;
62}
63
4efb1f7c
HR
64static inline const char *qcow2_cache_get_name(BDRVQcow2State *s, Qcow2Cache *c)
65{
66 if (c == s->refcount_block_cache) {
67 return "refcount block";
68 } else if (c == s->l2_table_cache) {
69 return "L2 table";
70 } else {
71 /* Do not abort, because this is not critical */
72 return "unknown";
73 }
74}
75
355ee2d0
AG
76static void qcow2_cache_table_release(BlockDriverState *bs, Qcow2Cache *c,
77 int i, int num_tables)
78{
2f2c8d6b
AG
79/* Using MADV_DONTNEED to discard memory is a Linux-specific feature */
80#ifdef CONFIG_LINUX
355ee2d0
AG
81 void *t = qcow2_cache_get_table_addr(bs, c, i);
82 int align = getpagesize();
03019d73 83 size_t mem_size = (size_t) c->table_size * num_tables;
355ee2d0
AG
84 size_t offset = QEMU_ALIGN_UP((uintptr_t) t, align) - (uintptr_t) t;
85 size_t length = QEMU_ALIGN_DOWN(mem_size - offset, align);
08546bcf 86 if (mem_size > offset && length > 0) {
2f2c8d6b 87 madvise((uint8_t *) t + offset, length, MADV_DONTNEED);
355ee2d0
AG
88 }
89#endif
90}
91
279621c0
AG
92static inline bool can_clean_entry(Qcow2Cache *c, int i)
93{
94 Qcow2CachedTable *t = &c->entries[i];
95 return t->ref == 0 && !t->dirty && t->offset != 0 &&
96 t->lru_counter <= c->cache_clean_lru_counter;
97}
98
99void qcow2_cache_clean_unused(BlockDriverState *bs, Qcow2Cache *c)
100{
101 int i = 0;
102 while (i < c->size) {
103 int to_clean = 0;
104
105 /* Skip the entries that we don't need to clean */
106 while (i < c->size && !can_clean_entry(c, i)) {
107 i++;
108 }
109
110 /* And count how many we can clean in a row */
111 while (i < c->size && can_clean_entry(c, i)) {
112 c->entries[i].offset = 0;
113 c->entries[i].lru_counter = 0;
114 i++;
115 to_clean++;
116 }
117
118 if (to_clean > 0) {
119 qcow2_cache_table_release(bs, c, i - to_clean, to_clean);
120 }
121 }
122
123 c->cache_clean_lru_counter = c->lru_counter;
124}
125
6af4e9ea 126Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables)
49381094 127{
ff99129a 128 BDRVQcow2State *s = bs->opaque;
49381094 129 Qcow2Cache *c;
49381094 130
02004bd4 131 c = g_new0(Qcow2Cache, 1);
49381094 132 c->size = num_tables;
03019d73 133 c->table_size = s->cluster_size;
02004bd4 134 c->entries = g_try_new0(Qcow2CachedTable, num_tables);
9a4f4c31 135 c->table_array = qemu_try_blockalign(bs->file->bs,
03019d73 136 (size_t) num_tables * c->table_size);
72e80b89
AG
137
138 if (!c->entries || !c->table_array) {
139 qemu_vfree(c->table_array);
140 g_free(c->entries);
141 g_free(c);
142 c = NULL;
49381094
KW
143 }
144
145 return c;
146}
147
d1b4efe5 148int qcow2_cache_destroy(BlockDriverState *bs, Qcow2Cache *c)
49381094
KW
149{
150 int i;
151
152 for (i = 0; i < c->size; i++) {
153 assert(c->entries[i].ref == 0);
49381094
KW
154 }
155
72e80b89 156 qemu_vfree(c->table_array);
7267c094
AL
157 g_free(c->entries);
158 g_free(c);
49381094
KW
159
160 return 0;
161}
162
163static int qcow2_cache_flush_dependency(BlockDriverState *bs, Qcow2Cache *c)
164{
165 int ret;
166
167 ret = qcow2_cache_flush(bs, c->depends);
168 if (ret < 0) {
169 return ret;
170 }
171
172 c->depends = NULL;
3de0a294
KW
173 c->depends_on_flush = false;
174
49381094
KW
175 return 0;
176}
177
178static int qcow2_cache_entry_flush(BlockDriverState *bs, Qcow2Cache *c, int i)
179{
ff99129a 180 BDRVQcow2State *s = bs->opaque;
3de0a294 181 int ret = 0;
49381094
KW
182
183 if (!c->entries[i].dirty || !c->entries[i].offset) {
184 return 0;
185 }
186
3cce16f4
KW
187 trace_qcow2_cache_entry_flush(qemu_coroutine_self(),
188 c == s->l2_table_cache, i);
189
49381094
KW
190 if (c->depends) {
191 ret = qcow2_cache_flush_dependency(bs, c);
3de0a294 192 } else if (c->depends_on_flush) {
9a4f4c31 193 ret = bdrv_flush(bs->file->bs);
3de0a294
KW
194 if (ret >= 0) {
195 c->depends_on_flush = false;
49381094
KW
196 }
197 }
198
3de0a294
KW
199 if (ret < 0) {
200 return ret;
201 }
202
cf93980e 203 if (c == s->refcount_block_cache) {
231bb267 204 ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_REFCOUNT_BLOCK,
03019d73 205 c->entries[i].offset, c->table_size);
cf93980e 206 } else if (c == s->l2_table_cache) {
231bb267 207 ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L2,
03019d73 208 c->entries[i].offset, c->table_size);
cf93980e 209 } else {
231bb267 210 ret = qcow2_pre_write_overlap_check(bs, 0,
03019d73 211 c->entries[i].offset, c->table_size);
cf93980e
HR
212 }
213
214 if (ret < 0) {
215 return ret;
216 }
217
29c1a730
KW
218 if (c == s->refcount_block_cache) {
219 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_UPDATE_PART);
220 } else if (c == s->l2_table_cache) {
221 BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE);
222 }
223
d9ca2ea2 224 ret = bdrv_pwrite(bs->file, c->entries[i].offset,
03019d73 225 qcow2_cache_get_table_addr(bs, c, i), c->table_size);
49381094
KW
226 if (ret < 0) {
227 return ret;
228 }
229
230 c->entries[i].dirty = false;
231
232 return 0;
233}
234
f3c3b87d 235int qcow2_cache_write(BlockDriverState *bs, Qcow2Cache *c)
49381094 236{
ff99129a 237 BDRVQcow2State *s = bs->opaque;
49381094
KW
238 int result = 0;
239 int ret;
240 int i;
241
3cce16f4
KW
242 trace_qcow2_cache_flush(qemu_coroutine_self(), c == s->l2_table_cache);
243
49381094
KW
244 for (i = 0; i < c->size; i++) {
245 ret = qcow2_cache_entry_flush(bs, c, i);
246 if (ret < 0 && result != -ENOSPC) {
247 result = ret;
248 }
249 }
250
f3c3b87d
DL
251 return result;
252}
253
254int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c)
255{
256 int result = qcow2_cache_write(bs, c);
257
49381094 258 if (result == 0) {
f3c3b87d 259 int ret = bdrv_flush(bs->file->bs);
49381094
KW
260 if (ret < 0) {
261 result = ret;
262 }
263 }
264
265 return result;
266}
267
268int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
269 Qcow2Cache *dependency)
270{
271 int ret;
272
273 if (dependency->depends) {
274 ret = qcow2_cache_flush_dependency(bs, dependency);
275 if (ret < 0) {
276 return ret;
277 }
278 }
279
280 if (c->depends && (c->depends != dependency)) {
281 ret = qcow2_cache_flush_dependency(bs, c);
282 if (ret < 0) {
283 return ret;
284 }
285 }
286
287 c->depends = dependency;
288 return 0;
289}
290
3de0a294
KW
291void qcow2_cache_depends_on_flush(Qcow2Cache *c)
292{
293 c->depends_on_flush = true;
294}
295
e7108fea
HR
296int qcow2_cache_empty(BlockDriverState *bs, Qcow2Cache *c)
297{
298 int ret, i;
299
300 ret = qcow2_cache_flush(bs, c);
301 if (ret < 0) {
302 return ret;
303 }
304
305 for (i = 0; i < c->size; i++) {
306 assert(c->entries[i].ref == 0);
307 c->entries[i].offset = 0;
2693310e 308 c->entries[i].lru_counter = 0;
e7108fea
HR
309 }
310
355ee2d0
AG
311 qcow2_cache_table_release(bs, c, 0, c->size);
312
2693310e
AG
313 c->lru_counter = 0;
314
e7108fea
HR
315 return 0;
316}
317
49381094
KW
318static int qcow2_cache_do_get(BlockDriverState *bs, Qcow2Cache *c,
319 uint64_t offset, void **table, bool read_from_disk)
320{
ff99129a 321 BDRVQcow2State *s = bs->opaque;
49381094
KW
322 int i;
323 int ret;
812e4082 324 int lookup_index;
fdfbca82
AG
325 uint64_t min_lru_counter = UINT64_MAX;
326 int min_lru_index = -1;
49381094 327
4efb1f7c
HR
328 assert(offset != 0);
329
3cce16f4
KW
330 trace_qcow2_cache_get(qemu_coroutine_self(), c == s->l2_table_cache,
331 offset, read_from_disk);
332
03019d73 333 if (!QEMU_IS_ALIGNED(offset, c->table_size)) {
4efb1f7c
HR
334 qcow2_signal_corruption(bs, true, -1, -1, "Cannot get entry from %s "
335 "cache: Offset %#" PRIx64 " is unaligned",
336 qcow2_cache_get_name(s, c), offset);
337 return -EIO;
338 }
339
49381094 340 /* Check if the table is already cached */
03019d73 341 i = lookup_index = (offset / c->table_size * 4) % c->size;
812e4082 342 do {
fdfbca82
AG
343 const Qcow2CachedTable *t = &c->entries[i];
344 if (t->offset == offset) {
49381094
KW
345 goto found;
346 }
fdfbca82
AG
347 if (t->ref == 0 && t->lru_counter < min_lru_counter) {
348 min_lru_counter = t->lru_counter;
349 min_lru_index = i;
350 }
812e4082
AG
351 if (++i == c->size) {
352 i = 0;
353 }
354 } while (i != lookup_index);
fdfbca82
AG
355
356 if (min_lru_index == -1) {
357 /* This can't happen in current synchronous code, but leave the check
358 * here as a reminder for whoever starts using AIO with the cache */
359 abort();
49381094
KW
360 }
361
fdfbca82
AG
362 /* Cache miss: write a table back and replace it */
363 i = min_lru_index;
3cce16f4
KW
364 trace_qcow2_cache_get_replace_entry(qemu_coroutine_self(),
365 c == s->l2_table_cache, i);
49381094
KW
366
367 ret = qcow2_cache_entry_flush(bs, c, i);
368 if (ret < 0) {
369 return ret;
370 }
371
3cce16f4
KW
372 trace_qcow2_cache_get_read(qemu_coroutine_self(),
373 c == s->l2_table_cache, i);
49381094
KW
374 c->entries[i].offset = 0;
375 if (read_from_disk) {
29c1a730
KW
376 if (c == s->l2_table_cache) {
377 BLKDBG_EVENT(bs->file, BLKDBG_L2_LOAD);
378 }
379
cf2ab8fc 380 ret = bdrv_pread(bs->file, offset,
9a4f4c31 381 qcow2_cache_get_table_addr(bs, c, i),
03019d73 382 c->table_size);
49381094
KW
383 if (ret < 0) {
384 return ret;
385 }
386 }
387
49381094
KW
388 c->entries[i].offset = offset;
389
390 /* And return the right table */
391found:
49381094 392 c->entries[i].ref++;
72e80b89 393 *table = qcow2_cache_get_table_addr(bs, c, i);
3cce16f4
KW
394
395 trace_qcow2_cache_get_done(qemu_coroutine_self(),
396 c == s->l2_table_cache, i);
397
49381094
KW
398 return 0;
399}
400
401int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
402 void **table)
403{
404 return qcow2_cache_do_get(bs, c, offset, table, true);
405}
406
407int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
408 void **table)
409{
410 return qcow2_cache_do_get(bs, c, offset, table, false);
411}
412
a3f1afb4 413void qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table)
49381094 414{
baf07d60 415 int i = qcow2_cache_get_table_idx(bs, c, *table);
49381094 416
49381094
KW
417 c->entries[i].ref--;
418 *table = NULL;
419
2693310e
AG
420 if (c->entries[i].ref == 0) {
421 c->entries[i].lru_counter = ++c->lru_counter;
422 }
423
49381094 424 assert(c->entries[i].ref >= 0);
49381094
KW
425}
426
72e80b89
AG
427void qcow2_cache_entry_mark_dirty(BlockDriverState *bs, Qcow2Cache *c,
428 void *table)
49381094 429{
baf07d60
AG
430 int i = qcow2_cache_get_table_idx(bs, c, table);
431 assert(c->entries[i].offset != 0);
49381094
KW
432 c->entries[i].dirty = true;
433}
f71c08ea
PB
434
435void *qcow2_cache_is_table_offset(BlockDriverState *bs, Qcow2Cache *c,
436 uint64_t offset)
437{
438 int i;
439
440 for (i = 0; i < c->size; i++) {
441 if (c->entries[i].offset == offset) {
442 return qcow2_cache_get_table_addr(bs, c, i);
443 }
444 }
445 return NULL;
446}
447
448void qcow2_cache_discard(BlockDriverState *bs, Qcow2Cache *c, void *table)
449{
450 int i = qcow2_cache_get_table_idx(bs, c, table);
451
452 assert(c->entries[i].ref == 0);
453
454 c->entries[i].offset = 0;
455 c->entries[i].lru_counter = 0;
456 c->entries[i].dirty = false;
457
458 qcow2_cache_table_release(bs, c, i, 1);
459}