]> git.proxmox.com Git - mirror_qemu.git/blame - block/qcow2-cache.c
qcow2: use one single memory block for the L2/refcount cache tables
[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
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
30typedef struct Qcow2CachedTable {
49381094
KW
31 int64_t offset;
32 bool dirty;
33 int cache_hits;
34 int ref;
35} Qcow2CachedTable;
36
37struct 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;
49381094
KW
43};
44
72e80b89
AG
45static inline void *qcow2_cache_get_table_addr(BlockDriverState *bs,
46 Qcow2Cache *c, int table)
47{
48 BDRVQcowState *s = bs->opaque;
49 return (uint8_t *) c->table_array + (size_t) table * s->cluster_size;
50}
51
6af4e9ea 52Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables)
49381094
KW
53{
54 BDRVQcowState *s = bs->opaque;
55 Qcow2Cache *c;
49381094 56
02004bd4 57 c = g_new0(Qcow2Cache, 1);
49381094 58 c->size = num_tables;
02004bd4 59 c->entries = g_try_new0(Qcow2CachedTable, num_tables);
72e80b89
AG
60 c->table_array = qemu_try_blockalign(bs->file,
61 (size_t) num_tables * s->cluster_size);
62
63 if (!c->entries || !c->table_array) {
64 qemu_vfree(c->table_array);
65 g_free(c->entries);
66 g_free(c);
67 c = NULL;
49381094
KW
68 }
69
70 return c;
71}
72
73int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c)
74{
75 int i;
76
77 for (i = 0; i < c->size; i++) {
78 assert(c->entries[i].ref == 0);
49381094
KW
79 }
80
72e80b89 81 qemu_vfree(c->table_array);
7267c094
AL
82 g_free(c->entries);
83 g_free(c);
49381094
KW
84
85 return 0;
86}
87
88static int qcow2_cache_flush_dependency(BlockDriverState *bs, Qcow2Cache *c)
89{
90 int ret;
91
92 ret = qcow2_cache_flush(bs, c->depends);
93 if (ret < 0) {
94 return ret;
95 }
96
97 c->depends = NULL;
3de0a294
KW
98 c->depends_on_flush = false;
99
49381094
KW
100 return 0;
101}
102
103static int qcow2_cache_entry_flush(BlockDriverState *bs, Qcow2Cache *c, int i)
104{
105 BDRVQcowState *s = bs->opaque;
3de0a294 106 int ret = 0;
49381094
KW
107
108 if (!c->entries[i].dirty || !c->entries[i].offset) {
109 return 0;
110 }
111
3cce16f4
KW
112 trace_qcow2_cache_entry_flush(qemu_coroutine_self(),
113 c == s->l2_table_cache, i);
114
49381094
KW
115 if (c->depends) {
116 ret = qcow2_cache_flush_dependency(bs, c);
3de0a294
KW
117 } else if (c->depends_on_flush) {
118 ret = bdrv_flush(bs->file);
119 if (ret >= 0) {
120 c->depends_on_flush = false;
49381094
KW
121 }
122 }
123
3de0a294
KW
124 if (ret < 0) {
125 return ret;
126 }
127
cf93980e 128 if (c == s->refcount_block_cache) {
231bb267 129 ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_REFCOUNT_BLOCK,
cf93980e
HR
130 c->entries[i].offset, s->cluster_size);
131 } else if (c == s->l2_table_cache) {
231bb267 132 ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L2,
cf93980e
HR
133 c->entries[i].offset, s->cluster_size);
134 } else {
231bb267 135 ret = qcow2_pre_write_overlap_check(bs, 0,
cf93980e
HR
136 c->entries[i].offset, s->cluster_size);
137 }
138
139 if (ret < 0) {
140 return ret;
141 }
142
29c1a730
KW
143 if (c == s->refcount_block_cache) {
144 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_UPDATE_PART);
145 } else if (c == s->l2_table_cache) {
146 BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE);
147 }
148
72e80b89
AG
149 ret = bdrv_pwrite(bs->file, c->entries[i].offset,
150 qcow2_cache_get_table_addr(bs, c, i), s->cluster_size);
49381094
KW
151 if (ret < 0) {
152 return ret;
153 }
154
155 c->entries[i].dirty = false;
156
157 return 0;
158}
159
160int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c)
161{
3cce16f4 162 BDRVQcowState *s = bs->opaque;
49381094
KW
163 int result = 0;
164 int ret;
165 int i;
166
3cce16f4
KW
167 trace_qcow2_cache_flush(qemu_coroutine_self(), c == s->l2_table_cache);
168
49381094
KW
169 for (i = 0; i < c->size; i++) {
170 ret = qcow2_cache_entry_flush(bs, c, i);
171 if (ret < 0 && result != -ENOSPC) {
172 result = ret;
173 }
174 }
175
176 if (result == 0) {
177 ret = bdrv_flush(bs->file);
178 if (ret < 0) {
179 result = ret;
180 }
181 }
182
183 return result;
184}
185
186int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
187 Qcow2Cache *dependency)
188{
189 int ret;
190
191 if (dependency->depends) {
192 ret = qcow2_cache_flush_dependency(bs, dependency);
193 if (ret < 0) {
194 return ret;
195 }
196 }
197
198 if (c->depends && (c->depends != dependency)) {
199 ret = qcow2_cache_flush_dependency(bs, c);
200 if (ret < 0) {
201 return ret;
202 }
203 }
204
205 c->depends = dependency;
206 return 0;
207}
208
3de0a294
KW
209void qcow2_cache_depends_on_flush(Qcow2Cache *c)
210{
211 c->depends_on_flush = true;
212}
213
e7108fea
HR
214int qcow2_cache_empty(BlockDriverState *bs, Qcow2Cache *c)
215{
216 int ret, i;
217
218 ret = qcow2_cache_flush(bs, c);
219 if (ret < 0) {
220 return ret;
221 }
222
223 for (i = 0; i < c->size; i++) {
224 assert(c->entries[i].ref == 0);
225 c->entries[i].offset = 0;
226 c->entries[i].cache_hits = 0;
227 }
228
229 return 0;
230}
231
49381094
KW
232static int qcow2_cache_find_entry_to_replace(Qcow2Cache *c)
233{
234 int i;
235 int min_count = INT_MAX;
236 int min_index = -1;
237
238
239 for (i = 0; i < c->size; i++) {
240 if (c->entries[i].ref) {
241 continue;
242 }
243
244 if (c->entries[i].cache_hits < min_count) {
245 min_index = i;
246 min_count = c->entries[i].cache_hits;
247 }
248
249 /* Give newer hits priority */
250 /* TODO Check how to optimize the replacement strategy */
8e8cb375
AG
251 if (c->entries[i].cache_hits > 1) {
252 c->entries[i].cache_hits /= 2;
253 }
49381094
KW
254 }
255
256 if (min_index == -1) {
257 /* This can't happen in current synchronous code, but leave the check
258 * here as a reminder for whoever starts using AIO with the cache */
259 abort();
260 }
261 return min_index;
262}
263
264static int qcow2_cache_do_get(BlockDriverState *bs, Qcow2Cache *c,
265 uint64_t offset, void **table, bool read_from_disk)
266{
267 BDRVQcowState *s = bs->opaque;
268 int i;
269 int ret;
270
3cce16f4
KW
271 trace_qcow2_cache_get(qemu_coroutine_self(), c == s->l2_table_cache,
272 offset, read_from_disk);
273
49381094
KW
274 /* Check if the table is already cached */
275 for (i = 0; i < c->size; i++) {
276 if (c->entries[i].offset == offset) {
277 goto found;
278 }
279 }
280
281 /* If not, write a table back and replace it */
282 i = qcow2_cache_find_entry_to_replace(c);
3cce16f4
KW
283 trace_qcow2_cache_get_replace_entry(qemu_coroutine_self(),
284 c == s->l2_table_cache, i);
49381094
KW
285 if (i < 0) {
286 return i;
287 }
288
289 ret = qcow2_cache_entry_flush(bs, c, i);
290 if (ret < 0) {
291 return ret;
292 }
293
3cce16f4
KW
294 trace_qcow2_cache_get_read(qemu_coroutine_self(),
295 c == s->l2_table_cache, i);
49381094
KW
296 c->entries[i].offset = 0;
297 if (read_from_disk) {
29c1a730
KW
298 if (c == s->l2_table_cache) {
299 BLKDBG_EVENT(bs->file, BLKDBG_L2_LOAD);
300 }
301
72e80b89
AG
302 ret = bdrv_pread(bs->file, offset, qcow2_cache_get_table_addr(bs, c, i),
303 s->cluster_size);
49381094
KW
304 if (ret < 0) {
305 return ret;
306 }
307 }
308
309 /* Give the table some hits for the start so that it won't be replaced
310 * immediately. The number 32 is completely arbitrary. */
311 c->entries[i].cache_hits = 32;
312 c->entries[i].offset = offset;
313
314 /* And return the right table */
315found:
316 c->entries[i].cache_hits++;
317 c->entries[i].ref++;
72e80b89 318 *table = qcow2_cache_get_table_addr(bs, c, i);
3cce16f4
KW
319
320 trace_qcow2_cache_get_done(qemu_coroutine_self(),
321 c == s->l2_table_cache, i);
322
49381094
KW
323 return 0;
324}
325
326int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
327 void **table)
328{
329 return qcow2_cache_do_get(bs, c, offset, table, true);
330}
331
332int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
333 void **table)
334{
335 return qcow2_cache_do_get(bs, c, offset, table, false);
336}
337
338int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table)
339{
340 int i;
341
342 for (i = 0; i < c->size; i++) {
72e80b89 343 if (qcow2_cache_get_table_addr(bs, c, i) == *table) {
49381094
KW
344 goto found;
345 }
346 }
347 return -ENOENT;
348
349found:
350 c->entries[i].ref--;
351 *table = NULL;
352
353 assert(c->entries[i].ref >= 0);
6af4e9ea 354 return 0;
49381094
KW
355}
356
72e80b89
AG
357void qcow2_cache_entry_mark_dirty(BlockDriverState *bs, Qcow2Cache *c,
358 void *table)
49381094
KW
359{
360 int i;
361
362 for (i = 0; i < c->size; i++) {
72e80b89 363 if (qcow2_cache_get_table_addr(bs, c, i) == table) {
49381094
KW
364 goto found;
365 }
366 }
367 abort();
368
369found:
370 c->entries[i].dirty = true;
371}