linux-headers: update to 3.12-rc1
[qemu/ar7.git] / block / qcow2-cache.c
blob7bcae09a69f45f044784fb2a411f401a2bbae1e2
1 /*
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
22 * THE SOFTWARE.
25 #include "block/block_int.h"
26 #include "qemu-common.h"
27 #include "qcow2.h"
28 #include "trace.h"
30 typedef struct Qcow2CachedTable {
31 void* table;
32 int64_t offset;
33 bool dirty;
34 int cache_hits;
35 int ref;
36 } Qcow2CachedTable;
38 struct Qcow2Cache {
39 Qcow2CachedTable* entries;
40 struct Qcow2Cache* depends;
41 int size;
42 bool depends_on_flush;
45 Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables)
47 BDRVQcowState *s = bs->opaque;
48 Qcow2Cache *c;
49 int i;
51 c = g_malloc0(sizeof(*c));
52 c->size = num_tables;
53 c->entries = g_malloc0(sizeof(*c->entries) * num_tables);
55 for (i = 0; i < c->size; i++) {
56 c->entries[i].table = qemu_blockalign(bs, s->cluster_size);
59 return c;
62 int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c)
64 int i;
66 for (i = 0; i < c->size; i++) {
67 assert(c->entries[i].ref == 0);
68 qemu_vfree(c->entries[i].table);
71 g_free(c->entries);
72 g_free(c);
74 return 0;
77 static int qcow2_cache_flush_dependency(BlockDriverState *bs, Qcow2Cache *c)
79 int ret;
81 ret = qcow2_cache_flush(bs, c->depends);
82 if (ret < 0) {
83 return ret;
86 c->depends = NULL;
87 c->depends_on_flush = false;
89 return 0;
92 static int qcow2_cache_entry_flush(BlockDriverState *bs, Qcow2Cache *c, int i)
94 BDRVQcowState *s = bs->opaque;
95 int ret = 0;
97 if (!c->entries[i].dirty || !c->entries[i].offset) {
98 return 0;
101 trace_qcow2_cache_entry_flush(qemu_coroutine_self(),
102 c == s->l2_table_cache, i);
104 if (c->depends) {
105 ret = qcow2_cache_flush_dependency(bs, c);
106 } else if (c->depends_on_flush) {
107 ret = bdrv_flush(bs->file);
108 if (ret >= 0) {
109 c->depends_on_flush = false;
113 if (ret < 0) {
114 return ret;
117 if (c == s->refcount_block_cache) {
118 ret = qcow2_pre_write_overlap_check(bs,
119 QCOW2_OL_DEFAULT & ~QCOW2_OL_REFCOUNT_BLOCK,
120 c->entries[i].offset, s->cluster_size);
121 } else if (c == s->l2_table_cache) {
122 ret = qcow2_pre_write_overlap_check(bs,
123 QCOW2_OL_DEFAULT & ~QCOW2_OL_ACTIVE_L2,
124 c->entries[i].offset, s->cluster_size);
125 } else {
126 ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_DEFAULT,
127 c->entries[i].offset, s->cluster_size);
130 if (ret < 0) {
131 return ret;
134 if (c == s->refcount_block_cache) {
135 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_UPDATE_PART);
136 } else if (c == s->l2_table_cache) {
137 BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE);
140 ret = bdrv_pwrite(bs->file, c->entries[i].offset, c->entries[i].table,
141 s->cluster_size);
142 if (ret < 0) {
143 return ret;
146 c->entries[i].dirty = false;
148 return 0;
151 int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c)
153 BDRVQcowState *s = bs->opaque;
154 int result = 0;
155 int ret;
156 int i;
158 trace_qcow2_cache_flush(qemu_coroutine_self(), c == s->l2_table_cache);
160 for (i = 0; i < c->size; i++) {
161 ret = qcow2_cache_entry_flush(bs, c, i);
162 if (ret < 0 && result != -ENOSPC) {
163 result = ret;
167 if (result == 0) {
168 ret = bdrv_flush(bs->file);
169 if (ret < 0) {
170 result = ret;
174 return result;
177 int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
178 Qcow2Cache *dependency)
180 int ret;
182 if (dependency->depends) {
183 ret = qcow2_cache_flush_dependency(bs, dependency);
184 if (ret < 0) {
185 return ret;
189 if (c->depends && (c->depends != dependency)) {
190 ret = qcow2_cache_flush_dependency(bs, c);
191 if (ret < 0) {
192 return ret;
196 c->depends = dependency;
197 return 0;
200 void qcow2_cache_depends_on_flush(Qcow2Cache *c)
202 c->depends_on_flush = true;
205 static int qcow2_cache_find_entry_to_replace(Qcow2Cache *c)
207 int i;
208 int min_count = INT_MAX;
209 int min_index = -1;
212 for (i = 0; i < c->size; i++) {
213 if (c->entries[i].ref) {
214 continue;
217 if (c->entries[i].cache_hits < min_count) {
218 min_index = i;
219 min_count = c->entries[i].cache_hits;
222 /* Give newer hits priority */
223 /* TODO Check how to optimize the replacement strategy */
224 c->entries[i].cache_hits /= 2;
227 if (min_index == -1) {
228 /* This can't happen in current synchronous code, but leave the check
229 * here as a reminder for whoever starts using AIO with the cache */
230 abort();
232 return min_index;
235 static int qcow2_cache_do_get(BlockDriverState *bs, Qcow2Cache *c,
236 uint64_t offset, void **table, bool read_from_disk)
238 BDRVQcowState *s = bs->opaque;
239 int i;
240 int ret;
242 trace_qcow2_cache_get(qemu_coroutine_self(), c == s->l2_table_cache,
243 offset, read_from_disk);
245 /* Check if the table is already cached */
246 for (i = 0; i < c->size; i++) {
247 if (c->entries[i].offset == offset) {
248 goto found;
252 /* If not, write a table back and replace it */
253 i = qcow2_cache_find_entry_to_replace(c);
254 trace_qcow2_cache_get_replace_entry(qemu_coroutine_self(),
255 c == s->l2_table_cache, i);
256 if (i < 0) {
257 return i;
260 ret = qcow2_cache_entry_flush(bs, c, i);
261 if (ret < 0) {
262 return ret;
265 trace_qcow2_cache_get_read(qemu_coroutine_self(),
266 c == s->l2_table_cache, i);
267 c->entries[i].offset = 0;
268 if (read_from_disk) {
269 if (c == s->l2_table_cache) {
270 BLKDBG_EVENT(bs->file, BLKDBG_L2_LOAD);
273 ret = bdrv_pread(bs->file, offset, c->entries[i].table, s->cluster_size);
274 if (ret < 0) {
275 return ret;
279 /* Give the table some hits for the start so that it won't be replaced
280 * immediately. The number 32 is completely arbitrary. */
281 c->entries[i].cache_hits = 32;
282 c->entries[i].offset = offset;
284 /* And return the right table */
285 found:
286 c->entries[i].cache_hits++;
287 c->entries[i].ref++;
288 *table = c->entries[i].table;
290 trace_qcow2_cache_get_done(qemu_coroutine_self(),
291 c == s->l2_table_cache, i);
293 return 0;
296 int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
297 void **table)
299 return qcow2_cache_do_get(bs, c, offset, table, true);
302 int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
303 void **table)
305 return qcow2_cache_do_get(bs, c, offset, table, false);
308 int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table)
310 int i;
312 for (i = 0; i < c->size; i++) {
313 if (c->entries[i].table == *table) {
314 goto found;
317 return -ENOENT;
319 found:
320 c->entries[i].ref--;
321 *table = NULL;
323 assert(c->entries[i].ref >= 0);
324 return 0;
327 void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table)
329 int i;
331 for (i = 0; i < c->size; i++) {
332 if (c->entries[i].table == table) {
333 goto found;
336 abort();
338 found:
339 c->entries[i].dirty = true;