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