2011-01-10 19:15:10 +03: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.
|
|
|
|
*/
|
|
|
|
|
2012-12-17 21:19:44 +04:00
|
|
|
#include "block/block_int.h"
|
2011-01-10 19:15:10 +03:00
|
|
|
#include "qemu-common.h"
|
|
|
|
#include "qcow2.h"
|
2012-03-01 21:36:21 +04:00
|
|
|
#include "trace.h"
|
2011-01-10 19:15:10 +03:00
|
|
|
|
|
|
|
typedef struct Qcow2CachedTable {
|
2015-05-11 15:54:55 +03:00
|
|
|
int64_t offset;
|
|
|
|
bool dirty;
|
|
|
|
uint64_t lru_counter;
|
|
|
|
int ref;
|
2011-01-10 19:15:10 +03:00
|
|
|
} Qcow2CachedTable;
|
|
|
|
|
|
|
|
struct Qcow2Cache {
|
2015-05-11 15:54:59 +03:00
|
|
|
Qcow2CachedTable *entries;
|
|
|
|
struct Qcow2Cache *depends;
|
2011-01-27 13:25:03 +03:00
|
|
|
int size;
|
2011-01-14 17:55:38 +03:00
|
|
|
bool depends_on_flush;
|
2015-05-11 15:54:53 +03:00
|
|
|
void *table_array;
|
2015-05-11 15:54:55 +03:00
|
|
|
uint64_t lru_counter;
|
2011-01-10 19:15:10 +03:00
|
|
|
};
|
|
|
|
|
2015-05-11 15:54:53 +03:00
|
|
|
static inline void *qcow2_cache_get_table_addr(BlockDriverState *bs,
|
|
|
|
Qcow2Cache *c, int table)
|
|
|
|
{
|
|
|
|
BDRVQcowState *s = bs->opaque;
|
|
|
|
return (uint8_t *) c->table_array + (size_t) table * s->cluster_size;
|
|
|
|
}
|
|
|
|
|
2015-05-11 15:54:54 +03:00
|
|
|
static inline int qcow2_cache_get_table_idx(BlockDriverState *bs,
|
|
|
|
Qcow2Cache *c, void *table)
|
|
|
|
{
|
|
|
|
BDRVQcowState *s = bs->opaque;
|
|
|
|
ptrdiff_t table_offset = (uint8_t *) table - (uint8_t *) c->table_array;
|
|
|
|
int idx = table_offset / s->cluster_size;
|
|
|
|
assert(idx >= 0 && idx < c->size && table_offset % s->cluster_size == 0);
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
2012-06-06 02:04:55 +04:00
|
|
|
Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables)
|
2011-01-10 19:15:10 +03:00
|
|
|
{
|
|
|
|
BDRVQcowState *s = bs->opaque;
|
|
|
|
Qcow2Cache *c;
|
|
|
|
|
2014-08-19 00:07:32 +04:00
|
|
|
c = g_new0(Qcow2Cache, 1);
|
2011-01-10 19:15:10 +03:00
|
|
|
c->size = num_tables;
|
2014-08-19 00:07:32 +04:00
|
|
|
c->entries = g_try_new0(Qcow2CachedTable, num_tables);
|
2015-05-11 15:54:53 +03:00
|
|
|
c->table_array = qemu_try_blockalign(bs->file,
|
|
|
|
(size_t) num_tables * s->cluster_size);
|
|
|
|
|
|
|
|
if (!c->entries || !c->table_array) {
|
|
|
|
qemu_vfree(c->table_array);
|
|
|
|
g_free(c->entries);
|
|
|
|
g_free(c);
|
|
|
|
c = NULL;
|
2011-01-10 19:15:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2015-05-11 15:54:59 +03:00
|
|
|
int qcow2_cache_destroy(BlockDriverState *bs, Qcow2Cache *c)
|
2011-01-10 19:15:10 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < c->size; i++) {
|
|
|
|
assert(c->entries[i].ref == 0);
|
|
|
|
}
|
|
|
|
|
2015-05-11 15:54:53 +03:00
|
|
|
qemu_vfree(c->table_array);
|
2011-08-21 07:09:37 +04:00
|
|
|
g_free(c->entries);
|
|
|
|
g_free(c);
|
2011-01-10 19:15:10 +03: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 17:55:38 +03:00
|
|
|
c->depends_on_flush = false;
|
|
|
|
|
2011-01-10 19:15:10 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qcow2_cache_entry_flush(BlockDriverState *bs, Qcow2Cache *c, int i)
|
|
|
|
{
|
|
|
|
BDRVQcowState *s = bs->opaque;
|
2011-01-14 17:55:38 +03:00
|
|
|
int ret = 0;
|
2011-01-10 19:15:10 +03:00
|
|
|
|
|
|
|
if (!c->entries[i].dirty || !c->entries[i].offset) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-01 21:36:21 +04:00
|
|
|
trace_qcow2_cache_entry_flush(qemu_coroutine_self(),
|
|
|
|
c == s->l2_table_cache, i);
|
|
|
|
|
2011-01-10 19:15:10 +03:00
|
|
|
if (c->depends) {
|
|
|
|
ret = qcow2_cache_flush_dependency(bs, c);
|
2011-01-14 17:55:38 +03:00
|
|
|
} else if (c->depends_on_flush) {
|
|
|
|
ret = bdrv_flush(bs->file);
|
|
|
|
if (ret >= 0) {
|
|
|
|
c->depends_on_flush = false;
|
2011-01-10 19:15:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-14 17:55:38 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-08-30 16:34:26 +04:00
|
|
|
if (c == s->refcount_block_cache) {
|
2013-10-10 13:09:23 +04:00
|
|
|
ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_REFCOUNT_BLOCK,
|
2013-08-30 16:34:26 +04:00
|
|
|
c->entries[i].offset, s->cluster_size);
|
|
|
|
} else if (c == s->l2_table_cache) {
|
2013-10-10 13:09:23 +04:00
|
|
|
ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L2,
|
2013-08-30 16:34:26 +04:00
|
|
|
c->entries[i].offset, s->cluster_size);
|
|
|
|
} else {
|
2013-10-10 13:09:23 +04:00
|
|
|
ret = qcow2_pre_write_overlap_check(bs, 0,
|
2013-08-30 16:34:26 +04:00
|
|
|
c->entries[i].offset, s->cluster_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-01-10 19:17:28 +03: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);
|
|
|
|
}
|
|
|
|
|
2015-05-11 15:54:53 +03:00
|
|
|
ret = bdrv_pwrite(bs->file, c->entries[i].offset,
|
|
|
|
qcow2_cache_get_table_addr(bs, c, i), s->cluster_size);
|
2011-01-10 19:15:10 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
c->entries[i].dirty = false;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c)
|
|
|
|
{
|
2012-03-01 21:36:21 +04:00
|
|
|
BDRVQcowState *s = bs->opaque;
|
2011-01-10 19:15:10 +03:00
|
|
|
int result = 0;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
2012-03-01 21:36:21 +04:00
|
|
|
trace_qcow2_cache_flush(qemu_coroutine_self(), c == s->l2_table_cache);
|
|
|
|
|
2011-01-10 19:15:10 +03:00
|
|
|
for (i = 0; i < c->size; i++) {
|
|
|
|
ret = qcow2_cache_entry_flush(bs, c, i);
|
|
|
|
if (ret < 0 && result != -ENOSPC) {
|
|
|
|
result = ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == 0) {
|
|
|
|
ret = bdrv_flush(bs->file);
|
|
|
|
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 17:55:38 +03:00
|
|
|
void qcow2_cache_depends_on_flush(Qcow2Cache *c)
|
|
|
|
{
|
|
|
|
c->depends_on_flush = true;
|
|
|
|
}
|
|
|
|
|
2013-09-03 12:09:51 +04: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 15:54:55 +03:00
|
|
|
c->entries[i].lru_counter = 0;
|
2013-09-03 12:09:51 +04:00
|
|
|
}
|
|
|
|
|
2015-05-11 15:54:55 +03:00
|
|
|
c->lru_counter = 0;
|
|
|
|
|
2013-09-03 12:09:51 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-10 19:15:10 +03:00
|
|
|
static int qcow2_cache_do_get(BlockDriverState *bs, Qcow2Cache *c,
|
|
|
|
uint64_t offset, void **table, bool read_from_disk)
|
|
|
|
{
|
|
|
|
BDRVQcowState *s = bs->opaque;
|
|
|
|
int i;
|
|
|
|
int ret;
|
2015-05-11 15:54:57 +03:00
|
|
|
int lookup_index;
|
2015-05-11 15:54:56 +03:00
|
|
|
uint64_t min_lru_counter = UINT64_MAX;
|
|
|
|
int min_lru_index = -1;
|
2011-01-10 19:15:10 +03:00
|
|
|
|
2012-03-01 21:36:21 +04:00
|
|
|
trace_qcow2_cache_get(qemu_coroutine_self(), c == s->l2_table_cache,
|
|
|
|
offset, read_from_disk);
|
|
|
|
|
2011-01-10 19:15:10 +03:00
|
|
|
/* Check if the table is already cached */
|
2015-05-11 15:54:57 +03:00
|
|
|
i = lookup_index = (offset / s->cluster_size * 4) % c->size;
|
|
|
|
do {
|
2015-05-11 15:54:56 +03:00
|
|
|
const Qcow2CachedTable *t = &c->entries[i];
|
|
|
|
if (t->offset == offset) {
|
2011-01-10 19:15:10 +03:00
|
|
|
goto found;
|
|
|
|
}
|
2015-05-11 15:54:56 +03:00
|
|
|
if (t->ref == 0 && t->lru_counter < min_lru_counter) {
|
|
|
|
min_lru_counter = t->lru_counter;
|
|
|
|
min_lru_index = i;
|
|
|
|
}
|
2015-05-11 15:54:57 +03:00
|
|
|
if (++i == c->size) {
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
} while (i != lookup_index);
|
2015-05-11 15:54:56 +03: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 19:15:10 +03:00
|
|
|
}
|
|
|
|
|
2015-05-11 15:54:56 +03:00
|
|
|
/* Cache miss: write a table back and replace it */
|
|
|
|
i = min_lru_index;
|
2012-03-01 21:36:21 +04:00
|
|
|
trace_qcow2_cache_get_replace_entry(qemu_coroutine_self(),
|
|
|
|
c == s->l2_table_cache, i);
|
2011-01-10 19:15:10 +03:00
|
|
|
|
|
|
|
ret = qcow2_cache_entry_flush(bs, c, i);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-03-01 21:36:21 +04:00
|
|
|
trace_qcow2_cache_get_read(qemu_coroutine_self(),
|
|
|
|
c == s->l2_table_cache, i);
|
2011-01-10 19:15:10 +03:00
|
|
|
c->entries[i].offset = 0;
|
|
|
|
if (read_from_disk) {
|
2011-01-10 19:17:28 +03:00
|
|
|
if (c == s->l2_table_cache) {
|
|
|
|
BLKDBG_EVENT(bs->file, BLKDBG_L2_LOAD);
|
|
|
|
}
|
|
|
|
|
2015-05-11 15:54:53 +03:00
|
|
|
ret = bdrv_pread(bs->file, offset, qcow2_cache_get_table_addr(bs, c, i),
|
|
|
|
s->cluster_size);
|
2011-01-10 19:15:10 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c->entries[i].offset = offset;
|
|
|
|
|
|
|
|
/* And return the right table */
|
|
|
|
found:
|
|
|
|
c->entries[i].ref++;
|
2015-05-11 15:54:53 +03:00
|
|
|
*table = qcow2_cache_get_table_addr(bs, c, i);
|
2012-03-01 21:36:21 +04:00
|
|
|
|
|
|
|
trace_qcow2_cache_get_done(qemu_coroutine_self(),
|
|
|
|
c == s->l2_table_cache, i);
|
|
|
|
|
2011-01-10 19:15:10 +03: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);
|
|
|
|
}
|
|
|
|
|
2015-05-11 15:54:58 +03:00
|
|
|
void qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table)
|
2011-01-10 19:15:10 +03:00
|
|
|
{
|
2015-05-11 15:54:54 +03:00
|
|
|
int i = qcow2_cache_get_table_idx(bs, c, *table);
|
2011-01-10 19:15:10 +03:00
|
|
|
|
|
|
|
c->entries[i].ref--;
|
|
|
|
*table = NULL;
|
|
|
|
|
2015-05-11 15:54:55 +03:00
|
|
|
if (c->entries[i].ref == 0) {
|
|
|
|
c->entries[i].lru_counter = ++c->lru_counter;
|
|
|
|
}
|
|
|
|
|
2011-01-10 19:15:10 +03:00
|
|
|
assert(c->entries[i].ref >= 0);
|
|
|
|
}
|
|
|
|
|
2015-05-11 15:54:53 +03:00
|
|
|
void qcow2_cache_entry_mark_dirty(BlockDriverState *bs, Qcow2Cache *c,
|
|
|
|
void *table)
|
2011-01-10 19:15:10 +03:00
|
|
|
{
|
2015-05-11 15:54:54 +03:00
|
|
|
int i = qcow2_cache_get_table_idx(bs, c, table);
|
|
|
|
assert(c->entries[i].offset != 0);
|
2011-01-10 19:15:10 +03:00
|
|
|
c->entries[i].dirty = true;
|
|
|
|
}
|