2011-01-10 17:15:10 +01:00
|
|
|
/*
|
|
|
|
* L2/refcount table cache for the QCOW2 format
|
|
|
|
*
|
|
|
|
* Copyright (c) 2010 Kevin Wolf <kwolf@redhat.com>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2016-01-18 19:01:42 +01:00
|
|
|
#include "qemu/osdep.h"
|
2011-01-10 17:15:10 +01:00
|
|
|
#include "qcow2.h"
|
2012-03-01 18:36:21 +01:00
|
|
|
#include "trace.h"
|
2011-01-10 17:15:10 +01:00
|
|
|
|
|
|
|
typedef struct Qcow2CachedTable {
|
2015-05-11 14:54:55 +02:00
|
|
|
int64_t offset;
|
|
|
|
uint64_t lru_counter;
|
|
|
|
int ref;
|
2015-08-04 14:14:42 +02:00
|
|
|
bool dirty;
|
2011-01-10 17:15:10 +01:00
|
|
|
} Qcow2CachedTable;
|
|
|
|
|
|
|
|
struct Qcow2Cache {
|
2015-05-11 14:54:59 +02:00
|
|
|
Qcow2CachedTable *entries;
|
|
|
|
struct Qcow2Cache *depends;
|
2011-01-27 11:25:03 +01:00
|
|
|
int size;
|
2018-02-05 15:33:02 +01:00
|
|
|
int table_size;
|
2011-01-14 15:55:38 +01:00
|
|
|
bool depends_on_flush;
|
2015-05-11 14:54:53 +02:00
|
|
|
void *table_array;
|
2015-05-11 14:54:55 +02:00
|
|
|
uint64_t lru_counter;
|
2015-08-04 14:14:40 +02:00
|
|
|
uint64_t cache_clean_lru_counter;
|
2011-01-10 17:15:10 +01:00
|
|
|
};
|
|
|
|
|
2018-02-05 15:33:03 +01:00
|
|
|
static inline void *qcow2_cache_get_table_addr(Qcow2Cache *c, int table)
|
2015-05-11 14:54:53 +02:00
|
|
|
{
|
2018-02-05 15:33:02 +01:00
|
|
|
return (uint8_t *) c->table_array + (size_t) table * c->table_size;
|
2015-05-11 14:54:53 +02:00
|
|
|
}
|
|
|
|
|
2018-02-05 15:33:04 +01:00
|
|
|
static inline int qcow2_cache_get_table_idx(Qcow2Cache *c, void *table)
|
2015-05-11 14:54:54 +02:00
|
|
|
{
|
|
|
|
ptrdiff_t table_offset = (uint8_t *) table - (uint8_t *) c->table_array;
|
2018-02-05 15:33:02 +01:00
|
|
|
int idx = table_offset / c->table_size;
|
|
|
|
assert(idx >= 0 && idx < c->size && table_offset % c->table_size == 0);
|
2015-05-11 14:54:54 +02:00
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
2017-11-10 21:31:11 +01:00
|
|
|
static inline const char *qcow2_cache_get_name(BDRVQcow2State *s, Qcow2Cache *c)
|
|
|
|
{
|
|
|
|
if (c == s->refcount_block_cache) {
|
|
|
|
return "refcount block";
|
|
|
|
} else if (c == s->l2_table_cache) {
|
|
|
|
return "L2 table";
|
|
|
|
} else {
|
|
|
|
/* Do not abort, because this is not critical */
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-05 15:33:05 +01:00
|
|
|
static void qcow2_cache_table_release(Qcow2Cache *c, int i, int num_tables)
|
2015-08-04 14:14:39 +02:00
|
|
|
{
|
2016-11-25 12:27:43 +01:00
|
|
|
/* Using MADV_DONTNEED to discard memory is a Linux-specific feature */
|
|
|
|
#ifdef CONFIG_LINUX
|
2018-02-05 15:33:03 +01:00
|
|
|
void *t = qcow2_cache_get_table_addr(c, i);
|
2019-10-13 04:11:45 +02:00
|
|
|
int align = qemu_real_host_page_size;
|
2018-02-05 15:33:02 +01:00
|
|
|
size_t mem_size = (size_t) c->table_size * num_tables;
|
2015-08-04 14:14:39 +02:00
|
|
|
size_t offset = QEMU_ALIGN_UP((uintptr_t) t, align) - (uintptr_t) t;
|
|
|
|
size_t length = QEMU_ALIGN_DOWN(mem_size - offset, align);
|
2017-11-14 19:41:27 +01:00
|
|
|
if (mem_size > offset && length > 0) {
|
2016-11-25 12:27:43 +01:00
|
|
|
madvise((uint8_t *) t + offset, length, MADV_DONTNEED);
|
2015-08-04 14:14:39 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-08-04 14:14:40 +02:00
|
|
|
static inline bool can_clean_entry(Qcow2Cache *c, int i)
|
|
|
|
{
|
|
|
|
Qcow2CachedTable *t = &c->entries[i];
|
|
|
|
return t->ref == 0 && !t->dirty && t->offset != 0 &&
|
|
|
|
t->lru_counter <= c->cache_clean_lru_counter;
|
|
|
|
}
|
|
|
|
|
2018-02-05 15:33:09 +01:00
|
|
|
void qcow2_cache_clean_unused(Qcow2Cache *c)
|
2015-08-04 14:14:40 +02:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
while (i < c->size) {
|
|
|
|
int to_clean = 0;
|
|
|
|
|
|
|
|
/* Skip the entries that we don't need to clean */
|
|
|
|
while (i < c->size && !can_clean_entry(c, i)) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And count how many we can clean in a row */
|
|
|
|
while (i < c->size && can_clean_entry(c, i)) {
|
|
|
|
c->entries[i].offset = 0;
|
|
|
|
c->entries[i].lru_counter = 0;
|
|
|
|
i++;
|
|
|
|
to_clean++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (to_clean > 0) {
|
2018-02-05 15:33:05 +01:00
|
|
|
qcow2_cache_table_release(c, i - to_clean, to_clean);
|
2015-08-04 14:14:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c->cache_clean_lru_counter = c->lru_counter;
|
|
|
|
}
|
|
|
|
|
2018-02-05 15:33:36 +01:00
|
|
|
Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables,
|
|
|
|
unsigned table_size)
|
2011-01-10 17:15:10 +01:00
|
|
|
{
|
2015-09-07 17:12:56 +02:00
|
|
|
BDRVQcow2State *s = bs->opaque;
|
2011-01-10 17:15:10 +01:00
|
|
|
Qcow2Cache *c;
|
|
|
|
|
2018-02-05 15:33:36 +01:00
|
|
|
assert(num_tables > 0);
|
|
|
|
assert(is_power_of_2(table_size));
|
|
|
|
assert(table_size >= (1 << MIN_CLUSTER_BITS));
|
|
|
|
assert(table_size <= s->cluster_size);
|
|
|
|
|
2014-08-18 22:07:32 +02:00
|
|
|
c = g_new0(Qcow2Cache, 1);
|
2011-01-10 17:15:10 +01:00
|
|
|
c->size = num_tables;
|
2018-02-05 15:33:36 +01:00
|
|
|
c->table_size = table_size;
|
2014-08-18 22:07:32 +02:00
|
|
|
c->entries = g_try_new0(Qcow2CachedTable, num_tables);
|
2015-06-16 14:19:22 +02:00
|
|
|
c->table_array = qemu_try_blockalign(bs->file->bs,
|
2018-02-05 15:33:02 +01:00
|
|
|
(size_t) num_tables * c->table_size);
|
2015-05-11 14:54:53 +02:00
|
|
|
|
|
|
|
if (!c->entries || !c->table_array) {
|
|
|
|
qemu_vfree(c->table_array);
|
|
|
|
g_free(c->entries);
|
|
|
|
g_free(c);
|
|
|
|
c = NULL;
|
2011-01-10 17:15:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2018-02-05 15:33:08 +01:00
|
|
|
int qcow2_cache_destroy(Qcow2Cache *c)
|
2011-01-10 17:15:10 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < c->size; i++) {
|
|
|
|
assert(c->entries[i].ref == 0);
|
|
|
|
}
|
|
|
|
|
2015-05-11 14:54:53 +02:00
|
|
|
qemu_vfree(c->table_array);
|
2011-08-21 05:09:37 +02:00
|
|
|
g_free(c->entries);
|
|
|
|
g_free(c);
|
2011-01-10 17:15:10 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qcow2_cache_flush_dependency(BlockDriverState *bs, Qcow2Cache *c)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = qcow2_cache_flush(bs, c->depends);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
c->depends = NULL;
|
2011-01-14 15:55:38 +01:00
|
|
|
c->depends_on_flush = false;
|
|
|
|
|
2011-01-10 17:15:10 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qcow2_cache_entry_flush(BlockDriverState *bs, Qcow2Cache *c, int i)
|
|
|
|
{
|
2015-09-07 17:12:56 +02:00
|
|
|
BDRVQcow2State *s = bs->opaque;
|
2011-01-14 15:55:38 +01:00
|
|
|
int ret = 0;
|
2011-01-10 17:15:10 +01:00
|
|
|
|
|
|
|
if (!c->entries[i].dirty || !c->entries[i].offset) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-01 18:36:21 +01:00
|
|
|
trace_qcow2_cache_entry_flush(qemu_coroutine_self(),
|
|
|
|
c == s->l2_table_cache, i);
|
|
|
|
|
2011-01-10 17:15:10 +01:00
|
|
|
if (c->depends) {
|
|
|
|
ret = qcow2_cache_flush_dependency(bs, c);
|
2011-01-14 15:55:38 +01:00
|
|
|
} else if (c->depends_on_flush) {
|
2015-06-16 14:19:22 +02:00
|
|
|
ret = bdrv_flush(bs->file->bs);
|
2011-01-14 15:55:38 +01:00
|
|
|
if (ret >= 0) {
|
|
|
|
c->depends_on_flush = false;
|
2011-01-10 17:15:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-14 15:55:38 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-08-30 14:34:26 +02:00
|
|
|
if (c == s->refcount_block_cache) {
|
2013-10-10 11:09:23 +02:00
|
|
|
ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_REFCOUNT_BLOCK,
|
2019-01-15 20:39:06 +01:00
|
|
|
c->entries[i].offset, c->table_size, false);
|
2013-08-30 14:34:26 +02:00
|
|
|
} else if (c == s->l2_table_cache) {
|
2013-10-10 11:09:23 +02:00
|
|
|
ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L2,
|
2019-01-15 20:39:06 +01:00
|
|
|
c->entries[i].offset, c->table_size, false);
|
2013-08-30 14:34:26 +02:00
|
|
|
} else {
|
2013-10-10 11:09:23 +02:00
|
|
|
ret = qcow2_pre_write_overlap_check(bs, 0,
|
2019-01-15 20:39:06 +01:00
|
|
|
c->entries[i].offset, c->table_size, false);
|
2013-08-30 14:34:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-01-10 17:17:28 +01:00
|
|
|
if (c == s->refcount_block_cache) {
|
|
|
|
BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_UPDATE_PART);
|
|
|
|
} else if (c == s->l2_table_cache) {
|
|
|
|
BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE);
|
|
|
|
}
|
|
|
|
|
2016-06-20 20:09:15 +02:00
|
|
|
ret = bdrv_pwrite(bs->file, c->entries[i].offset,
|
2018-02-05 15:33:03 +01:00
|
|
|
qcow2_cache_get_table_addr(c, i), c->table_size);
|
2011-01-10 17:15:10 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
c->entries[i].dirty = false;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-02 17:58:15 +02:00
|
|
|
int qcow2_cache_write(BlockDriverState *bs, Qcow2Cache *c)
|
2011-01-10 17:15:10 +01:00
|
|
|
{
|
2015-09-07 17:12:56 +02:00
|
|
|
BDRVQcow2State *s = bs->opaque;
|
2011-01-10 17:15:10 +01:00
|
|
|
int result = 0;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
2012-03-01 18:36:21 +01:00
|
|
|
trace_qcow2_cache_flush(qemu_coroutine_self(), c == s->l2_table_cache);
|
|
|
|
|
2011-01-10 17:15:10 +01:00
|
|
|
for (i = 0; i < c->size; i++) {
|
|
|
|
ret = qcow2_cache_entry_flush(bs, c, i);
|
|
|
|
if (ret < 0 && result != -ENOSPC) {
|
|
|
|
result = ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-02 17:58:15 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c)
|
|
|
|
{
|
|
|
|
int result = qcow2_cache_write(bs, c);
|
|
|
|
|
2011-01-10 17:15:10 +01:00
|
|
|
if (result == 0) {
|
2016-06-02 17:58:15 +02:00
|
|
|
int ret = bdrv_flush(bs->file->bs);
|
2011-01-10 17:15:10 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
result = ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
|
|
|
|
Qcow2Cache *dependency)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (dependency->depends) {
|
|
|
|
ret = qcow2_cache_flush_dependency(bs, dependency);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c->depends && (c->depends != dependency)) {
|
|
|
|
ret = qcow2_cache_flush_dependency(bs, c);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c->depends = dependency;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-14 15:55:38 +01:00
|
|
|
void qcow2_cache_depends_on_flush(Qcow2Cache *c)
|
|
|
|
{
|
|
|
|
c->depends_on_flush = true;
|
|
|
|
}
|
|
|
|
|
2013-09-03 10:09:51 +02:00
|
|
|
int qcow2_cache_empty(BlockDriverState *bs, Qcow2Cache *c)
|
|
|
|
{
|
|
|
|
int ret, i;
|
|
|
|
|
|
|
|
ret = qcow2_cache_flush(bs, c);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < c->size; i++) {
|
|
|
|
assert(c->entries[i].ref == 0);
|
|
|
|
c->entries[i].offset = 0;
|
2015-05-11 14:54:55 +02:00
|
|
|
c->entries[i].lru_counter = 0;
|
2013-09-03 10:09:51 +02:00
|
|
|
}
|
|
|
|
|
2018-02-05 15:33:05 +01:00
|
|
|
qcow2_cache_table_release(c, 0, c->size);
|
2015-08-04 14:14:39 +02:00
|
|
|
|
2015-05-11 14:54:55 +02:00
|
|
|
c->lru_counter = 0;
|
|
|
|
|
2013-09-03 10:09:51 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-10 17:15:10 +01:00
|
|
|
static int qcow2_cache_do_get(BlockDriverState *bs, Qcow2Cache *c,
|
|
|
|
uint64_t offset, void **table, bool read_from_disk)
|
|
|
|
{
|
2015-09-07 17:12:56 +02:00
|
|
|
BDRVQcow2State *s = bs->opaque;
|
2011-01-10 17:15:10 +01:00
|
|
|
int i;
|
|
|
|
int ret;
|
2015-05-11 14:54:57 +02:00
|
|
|
int lookup_index;
|
2015-05-11 14:54:56 +02:00
|
|
|
uint64_t min_lru_counter = UINT64_MAX;
|
|
|
|
int min_lru_index = -1;
|
2011-01-10 17:15:10 +01:00
|
|
|
|
2017-11-10 21:31:11 +01:00
|
|
|
assert(offset != 0);
|
|
|
|
|
2012-03-01 18:36:21 +01:00
|
|
|
trace_qcow2_cache_get(qemu_coroutine_self(), c == s->l2_table_cache,
|
|
|
|
offset, read_from_disk);
|
|
|
|
|
2018-02-05 15:33:02 +01:00
|
|
|
if (!QEMU_IS_ALIGNED(offset, c->table_size)) {
|
2017-11-10 21:31:11 +01:00
|
|
|
qcow2_signal_corruption(bs, true, -1, -1, "Cannot get entry from %s "
|
|
|
|
"cache: Offset %#" PRIx64 " is unaligned",
|
|
|
|
qcow2_cache_get_name(s, c), offset);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2011-01-10 17:15:10 +01:00
|
|
|
/* Check if the table is already cached */
|
2018-02-05 15:33:02 +01:00
|
|
|
i = lookup_index = (offset / c->table_size * 4) % c->size;
|
2015-05-11 14:54:57 +02:00
|
|
|
do {
|
2015-05-11 14:54:56 +02:00
|
|
|
const Qcow2CachedTable *t = &c->entries[i];
|
|
|
|
if (t->offset == offset) {
|
2011-01-10 17:15:10 +01:00
|
|
|
goto found;
|
|
|
|
}
|
2015-05-11 14:54:56 +02:00
|
|
|
if (t->ref == 0 && t->lru_counter < min_lru_counter) {
|
|
|
|
min_lru_counter = t->lru_counter;
|
|
|
|
min_lru_index = i;
|
|
|
|
}
|
2015-05-11 14:54:57 +02:00
|
|
|
if (++i == c->size) {
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
} while (i != lookup_index);
|
2015-05-11 14:54:56 +02:00
|
|
|
|
|
|
|
if (min_lru_index == -1) {
|
|
|
|
/* This can't happen in current synchronous code, but leave the check
|
|
|
|
* here as a reminder for whoever starts using AIO with the cache */
|
|
|
|
abort();
|
2011-01-10 17:15:10 +01:00
|
|
|
}
|
|
|
|
|
2015-05-11 14:54:56 +02:00
|
|
|
/* Cache miss: write a table back and replace it */
|
|
|
|
i = min_lru_index;
|
2012-03-01 18:36:21 +01:00
|
|
|
trace_qcow2_cache_get_replace_entry(qemu_coroutine_self(),
|
|
|
|
c == s->l2_table_cache, i);
|
2011-01-10 17:15:10 +01:00
|
|
|
|
|
|
|
ret = qcow2_cache_entry_flush(bs, c, i);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-03-01 18:36:21 +01:00
|
|
|
trace_qcow2_cache_get_read(qemu_coroutine_self(),
|
|
|
|
c == s->l2_table_cache, i);
|
2011-01-10 17:15:10 +01:00
|
|
|
c->entries[i].offset = 0;
|
|
|
|
if (read_from_disk) {
|
2011-01-10 17:17:28 +01:00
|
|
|
if (c == s->l2_table_cache) {
|
|
|
|
BLKDBG_EVENT(bs->file, BLKDBG_L2_LOAD);
|
|
|
|
}
|
|
|
|
|
2016-06-20 18:24:02 +02:00
|
|
|
ret = bdrv_pread(bs->file, offset,
|
2018-02-05 15:33:03 +01:00
|
|
|
qcow2_cache_get_table_addr(c, i),
|
2018-02-05 15:33:02 +01:00
|
|
|
c->table_size);
|
2011-01-10 17:15:10 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c->entries[i].offset = offset;
|
|
|
|
|
|
|
|
/* And return the right table */
|
|
|
|
found:
|
|
|
|
c->entries[i].ref++;
|
2018-02-05 15:33:03 +01:00
|
|
|
*table = qcow2_cache_get_table_addr(c, i);
|
2012-03-01 18:36:21 +01:00
|
|
|
|
|
|
|
trace_qcow2_cache_get_done(qemu_coroutine_self(),
|
|
|
|
c == s->l2_table_cache, i);
|
|
|
|
|
2011-01-10 17:15:10 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
|
|
|
|
void **table)
|
|
|
|
{
|
|
|
|
return qcow2_cache_do_get(bs, c, offset, table, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
|
|
|
|
void **table)
|
|
|
|
{
|
|
|
|
return qcow2_cache_do_get(bs, c, offset, table, false);
|
|
|
|
}
|
|
|
|
|
2018-02-05 15:33:07 +01:00
|
|
|
void qcow2_cache_put(Qcow2Cache *c, void **table)
|
2011-01-10 17:15:10 +01:00
|
|
|
{
|
2018-02-05 15:33:04 +01:00
|
|
|
int i = qcow2_cache_get_table_idx(c, *table);
|
2011-01-10 17:15:10 +01:00
|
|
|
|
|
|
|
c->entries[i].ref--;
|
|
|
|
*table = NULL;
|
|
|
|
|
2015-05-11 14:54:55 +02:00
|
|
|
if (c->entries[i].ref == 0) {
|
|
|
|
c->entries[i].lru_counter = ++c->lru_counter;
|
|
|
|
}
|
|
|
|
|
2011-01-10 17:15:10 +01:00
|
|
|
assert(c->entries[i].ref >= 0);
|
|
|
|
}
|
|
|
|
|
2018-02-05 15:33:06 +01:00
|
|
|
void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table)
|
2011-01-10 17:15:10 +01:00
|
|
|
{
|
2018-02-05 15:33:04 +01:00
|
|
|
int i = qcow2_cache_get_table_idx(c, table);
|
2015-05-11 14:54:54 +02:00
|
|
|
assert(c->entries[i].offset != 0);
|
2011-01-10 17:15:10 +01:00
|
|
|
c->entries[i].dirty = true;
|
|
|
|
}
|
2017-09-18 14:42:28 +02:00
|
|
|
|
2018-02-05 15:33:11 +01:00
|
|
|
void *qcow2_cache_is_table_offset(Qcow2Cache *c, uint64_t offset)
|
2017-09-18 14:42:28 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < c->size; i++) {
|
|
|
|
if (c->entries[i].offset == offset) {
|
2018-02-05 15:33:03 +01:00
|
|
|
return qcow2_cache_get_table_addr(c, i);
|
2017-09-18 14:42:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-02-05 15:33:10 +01:00
|
|
|
void qcow2_cache_discard(Qcow2Cache *c, void *table)
|
2017-09-18 14:42:28 +02:00
|
|
|
{
|
2018-02-05 15:33:04 +01:00
|
|
|
int i = qcow2_cache_get_table_idx(c, table);
|
2017-09-18 14:42:28 +02:00
|
|
|
|
|
|
|
assert(c->entries[i].ref == 0);
|
|
|
|
|
|
|
|
c->entries[i].offset = 0;
|
|
|
|
c->entries[i].lru_counter = 0;
|
|
|
|
c->entries[i].dirty = false;
|
|
|
|
|
2018-02-05 15:33:05 +01:00
|
|
|
qcow2_cache_table_release(c, i, 1);
|
2017-09-18 14:42:28 +02:00
|
|
|
}
|