2 * L2/refcount table cache for the QCOW2 format
4 * Copyright (c) 2010 Kevin Wolf <kwolf@redhat.com>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 /* Needed for CONFIG_MADVISE */
26 #include "qemu/osdep.h"
27 #include "block/block_int.h"
28 #include "qemu-common.h"
32 typedef struct Qcow2CachedTable {
40 Qcow2CachedTable *entries;
41 struct Qcow2Cache *depends;
43 bool depends_on_flush;
46 uint64_t cache_clean_lru_counter;
49 static inline void *qcow2_cache_get_table_addr(BlockDriverState *bs,
50 Qcow2Cache *c, int table)
52 BDRVQcow2State *s = bs->opaque;
53 return (uint8_t *) c->table_array + (size_t) table * s->cluster_size;
56 static inline int qcow2_cache_get_table_idx(BlockDriverState *bs,
57 Qcow2Cache *c, void *table)
59 BDRVQcow2State *s = bs->opaque;
60 ptrdiff_t table_offset = (uint8_t *) table - (uint8_t *) c->table_array;
61 int idx = table_offset / s->cluster_size;
62 assert(idx >= 0 && idx < c->size && table_offset % s->cluster_size == 0);
66 static void qcow2_cache_table_release(BlockDriverState *bs, Qcow2Cache *c,
67 int i, int num_tables)
69 /* Using MADV_DONTNEED to discard memory is a Linux-specific feature */
71 BDRVQcow2State *s = bs->opaque;
72 void *t = qcow2_cache_get_table_addr(bs, c, i);
73 int align = getpagesize();
74 size_t mem_size = (size_t) s->cluster_size * num_tables;
75 size_t offset = QEMU_ALIGN_UP((uintptr_t) t, align) - (uintptr_t) t;
76 size_t length = QEMU_ALIGN_DOWN(mem_size - offset, align);
78 madvise((uint8_t *) t + offset, length, MADV_DONTNEED);
83 static inline bool can_clean_entry(Qcow2Cache *c, int i)
85 Qcow2CachedTable *t = &c->entries[i];
86 return t->ref == 0 && !t->dirty && t->offset != 0 &&
87 t->lru_counter <= c->cache_clean_lru_counter;
90 void qcow2_cache_clean_unused(BlockDriverState *bs, Qcow2Cache *c)
96 /* Skip the entries that we don't need to clean */
97 while (i < c->size && !can_clean_entry(c, i)) {
101 /* And count how many we can clean in a row */
102 while (i < c->size && can_clean_entry(c, i)) {
103 c->entries[i].offset = 0;
104 c->entries[i].lru_counter = 0;
110 qcow2_cache_table_release(bs, c, i - to_clean, to_clean);
114 c->cache_clean_lru_counter = c->lru_counter;
117 Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables)
119 BDRVQcow2State *s = bs->opaque;
122 c = g_new0(Qcow2Cache, 1);
123 c->size = num_tables;
124 c->entries = g_try_new0(Qcow2CachedTable, num_tables);
125 c->table_array = qemu_try_blockalign(bs->file->bs,
126 (size_t) num_tables * s->cluster_size);
128 if (!c->entries || !c->table_array) {
129 qemu_vfree(c->table_array);
138 int qcow2_cache_destroy(BlockDriverState *bs, Qcow2Cache *c)
142 for (i = 0; i < c->size; i++) {
143 assert(c->entries[i].ref == 0);
146 qemu_vfree(c->table_array);
153 static int qcow2_cache_flush_dependency(BlockDriverState *bs, Qcow2Cache *c)
157 ret = qcow2_cache_flush(bs, c->depends);
163 c->depends_on_flush = false;
168 static int qcow2_cache_entry_flush(BlockDriverState *bs, Qcow2Cache *c, int i)
170 BDRVQcow2State *s = bs->opaque;
173 if (!c->entries[i].dirty || !c->entries[i].offset) {
177 trace_qcow2_cache_entry_flush(qemu_coroutine_self(),
178 c == s->l2_table_cache, i);
181 ret = qcow2_cache_flush_dependency(bs, c);
182 } else if (c->depends_on_flush) {
183 ret = bdrv_flush(bs->file->bs);
185 c->depends_on_flush = false;
193 if (c == s->refcount_block_cache) {
194 ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_REFCOUNT_BLOCK,
195 c->entries[i].offset, s->cluster_size);
196 } else if (c == s->l2_table_cache) {
197 ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L2,
198 c->entries[i].offset, s->cluster_size);
200 ret = qcow2_pre_write_overlap_check(bs, 0,
201 c->entries[i].offset, s->cluster_size);
208 if (c == s->refcount_block_cache) {
209 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_UPDATE_PART);
210 } else if (c == s->l2_table_cache) {
211 BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE);
214 ret = bdrv_pwrite(bs->file, c->entries[i].offset,
215 qcow2_cache_get_table_addr(bs, c, i), s->cluster_size);
220 c->entries[i].dirty = false;
225 int qcow2_cache_write(BlockDriverState *bs, Qcow2Cache *c)
227 BDRVQcow2State *s = bs->opaque;
232 trace_qcow2_cache_flush(qemu_coroutine_self(), c == s->l2_table_cache);
234 for (i = 0; i < c->size; i++) {
235 ret = qcow2_cache_entry_flush(bs, c, i);
236 if (ret < 0 && result != -ENOSPC) {
244 int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c)
246 int result = qcow2_cache_write(bs, c);
249 int ret = bdrv_flush(bs->file->bs);
258 int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
259 Qcow2Cache *dependency)
263 if (dependency->depends) {
264 ret = qcow2_cache_flush_dependency(bs, dependency);
270 if (c->depends && (c->depends != dependency)) {
271 ret = qcow2_cache_flush_dependency(bs, c);
277 c->depends = dependency;
281 void qcow2_cache_depends_on_flush(Qcow2Cache *c)
283 c->depends_on_flush = true;
286 int qcow2_cache_empty(BlockDriverState *bs, Qcow2Cache *c)
290 ret = qcow2_cache_flush(bs, c);
295 for (i = 0; i < c->size; i++) {
296 assert(c->entries[i].ref == 0);
297 c->entries[i].offset = 0;
298 c->entries[i].lru_counter = 0;
301 qcow2_cache_table_release(bs, c, 0, c->size);
308 static int qcow2_cache_do_get(BlockDriverState *bs, Qcow2Cache *c,
309 uint64_t offset, void **table, bool read_from_disk)
311 BDRVQcow2State *s = bs->opaque;
315 uint64_t min_lru_counter = UINT64_MAX;
316 int min_lru_index = -1;
318 trace_qcow2_cache_get(qemu_coroutine_self(), c == s->l2_table_cache,
319 offset, read_from_disk);
321 /* Check if the table is already cached */
322 i = lookup_index = (offset / s->cluster_size * 4) % c->size;
324 const Qcow2CachedTable *t = &c->entries[i];
325 if (t->offset == offset) {
328 if (t->ref == 0 && t->lru_counter < min_lru_counter) {
329 min_lru_counter = t->lru_counter;
332 if (++i == c->size) {
335 } while (i != lookup_index);
337 if (min_lru_index == -1) {
338 /* This can't happen in current synchronous code, but leave the check
339 * here as a reminder for whoever starts using AIO with the cache */
343 /* Cache miss: write a table back and replace it */
345 trace_qcow2_cache_get_replace_entry(qemu_coroutine_self(),
346 c == s->l2_table_cache, i);
348 ret = qcow2_cache_entry_flush(bs, c, i);
353 trace_qcow2_cache_get_read(qemu_coroutine_self(),
354 c == s->l2_table_cache, i);
355 c->entries[i].offset = 0;
356 if (read_from_disk) {
357 if (c == s->l2_table_cache) {
358 BLKDBG_EVENT(bs->file, BLKDBG_L2_LOAD);
361 ret = bdrv_pread(bs->file, offset,
362 qcow2_cache_get_table_addr(bs, c, i),
369 c->entries[i].offset = offset;
371 /* And return the right table */
374 *table = qcow2_cache_get_table_addr(bs, c, i);
376 trace_qcow2_cache_get_done(qemu_coroutine_self(),
377 c == s->l2_table_cache, i);
382 int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
385 return qcow2_cache_do_get(bs, c, offset, table, true);
388 int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
391 return qcow2_cache_do_get(bs, c, offset, table, false);
394 void qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table)
396 int i = qcow2_cache_get_table_idx(bs, c, *table);
401 if (c->entries[i].ref == 0) {
402 c->entries[i].lru_counter = ++c->lru_counter;
405 assert(c->entries[i].ref >= 0);
408 void qcow2_cache_entry_mark_dirty(BlockDriverState *bs, Qcow2Cache *c,
411 int i = qcow2_cache_get_table_idx(bs, c, table);
412 assert(c->entries[i].offset != 0);
413 c->entries[i].dirty = true;