2010-12-06 19:08:01 +03:00
|
|
|
/*
|
|
|
|
* QEMU Enhanced Disk Format Table I/O
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2010
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU LGPL, version 2 or later.
|
|
|
|
* See the COPYING.LIB file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-01-18 21:01:42 +03:00
|
|
|
#include "qemu/osdep.h"
|
2022-12-21 16:35:49 +03:00
|
|
|
#include "block/block-io.h"
|
2010-12-06 19:08:01 +03:00
|
|
|
#include "trace.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/sockets.h" /* for EINPROGRESS on Windows */
|
2010-12-06 19:08:01 +03:00
|
|
|
#include "qed.h"
|
2016-03-15 19:22:36 +03:00
|
|
|
#include "qemu/bswap.h"
|
2022-02-26 21:07:23 +03:00
|
|
|
#include "qemu/memalign.h"
|
2010-12-06 19:08:01 +03:00
|
|
|
|
2018-03-01 19:36:19 +03:00
|
|
|
/* Called with table_lock held. */
|
2023-02-03 18:21:50 +03:00
|
|
|
static int coroutine_fn GRAPH_RDLOCK
|
|
|
|
qed_read_table(BDRVQEDState *s, uint64_t offset, QEDTable *table)
|
2016-11-14 16:56:32 +03:00
|
|
|
{
|
2019-04-22 17:58:33 +03:00
|
|
|
unsigned int bytes = s->header.cluster_size * s->header.table_size;
|
|
|
|
|
2016-11-14 16:56:32 +03:00
|
|
|
int noffsets;
|
|
|
|
int i, ret;
|
2010-12-06 19:08:01 +03:00
|
|
|
|
2016-11-14 16:56:32 +03:00
|
|
|
trace_qed_read_table(s, offset, table);
|
|
|
|
|
2018-03-01 19:36:19 +03:00
|
|
|
qemu_co_mutex_unlock(&s->table_lock);
|
2019-04-22 17:58:33 +03:00
|
|
|
ret = bdrv_co_pread(s->bs->file, offset, bytes, table->offsets, 0);
|
2018-03-01 19:36:19 +03:00
|
|
|
qemu_co_mutex_lock(&s->table_lock);
|
2016-11-14 16:56:32 +03:00
|
|
|
if (ret < 0) {
|
2010-12-06 19:08:01 +03:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Byteswap offsets */
|
2019-04-22 17:58:33 +03:00
|
|
|
noffsets = bytes / sizeof(uint64_t);
|
2010-12-06 19:08:01 +03:00
|
|
|
for (i = 0; i < noffsets; i++) {
|
|
|
|
table->offsets[i] = le64_to_cpu(table->offsets[i]);
|
|
|
|
}
|
|
|
|
|
2016-11-14 16:56:32 +03:00
|
|
|
ret = 0;
|
2010-12-06 19:08:01 +03:00
|
|
|
out:
|
|
|
|
/* Completion */
|
2016-11-14 16:56:32 +03:00
|
|
|
trace_qed_read_table_cb(s, table, ret);
|
2016-11-14 18:08:44 +03:00
|
|
|
return ret;
|
2010-12-06 19:08:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write out an updated part or all of a table
|
|
|
|
*
|
|
|
|
* @s: QED state
|
|
|
|
* @offset: Offset of table in image file, in bytes
|
|
|
|
* @table: Table
|
|
|
|
* @index: Index of first element
|
|
|
|
* @n: Number of elements
|
|
|
|
* @flush: Whether or not to sync to disk
|
2017-06-29 16:27:47 +03:00
|
|
|
*
|
2018-03-01 19:36:19 +03:00
|
|
|
* Called with table_lock held.
|
2010-12-06 19:08:01 +03:00
|
|
|
*/
|
2023-02-03 18:21:46 +03:00
|
|
|
static int coroutine_fn GRAPH_RDLOCK
|
|
|
|
qed_write_table(BDRVQEDState *s, uint64_t offset, QEDTable *table,
|
|
|
|
unsigned int index, unsigned int n, bool flush)
|
2010-12-06 19:08:01 +03:00
|
|
|
{
|
|
|
|
unsigned int sector_mask = BDRV_SECTOR_SIZE / sizeof(uint64_t) - 1;
|
|
|
|
unsigned int start, end, i;
|
2016-11-14 16:56:32 +03:00
|
|
|
QEDTable *new_table;
|
2010-12-06 19:08:01 +03:00
|
|
|
size_t len_bytes;
|
2016-11-14 16:56:32 +03:00
|
|
|
int ret;
|
2010-12-06 19:08:01 +03:00
|
|
|
|
|
|
|
trace_qed_write_table(s, offset, table, index, n);
|
|
|
|
|
|
|
|
/* Calculate indices of the first and one after last elements */
|
|
|
|
start = index & ~sector_mask;
|
|
|
|
end = (index + n + sector_mask) & ~sector_mask;
|
|
|
|
|
|
|
|
len_bytes = (end - start) * sizeof(uint64_t);
|
|
|
|
|
2016-11-14 16:56:32 +03:00
|
|
|
new_table = qemu_blockalign(s->bs, len_bytes);
|
2010-12-06 19:08:01 +03:00
|
|
|
|
|
|
|
/* Byteswap table */
|
|
|
|
for (i = start; i < end; i++) {
|
|
|
|
uint64_t le_offset = cpu_to_le64(table->offsets[i]);
|
2016-11-14 16:56:32 +03:00
|
|
|
new_table->offsets[i - start] = le_offset;
|
2010-12-06 19:08:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Adjust for offset into table */
|
|
|
|
offset += start * sizeof(uint64_t);
|
|
|
|
|
2018-03-01 19:36:19 +03:00
|
|
|
qemu_co_mutex_unlock(&s->table_lock);
|
2019-04-22 17:58:33 +03:00
|
|
|
ret = bdrv_co_pwrite(s->bs->file, offset, len_bytes, new_table->offsets, 0);
|
2018-03-01 19:36:19 +03:00
|
|
|
qemu_co_mutex_lock(&s->table_lock);
|
2016-11-14 16:56:32 +03:00
|
|
|
trace_qed_write_table_cb(s, table, flush, ret);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flush) {
|
2022-10-13 15:37:07 +03:00
|
|
|
ret = bdrv_co_flush(s->bs);
|
2016-11-14 16:56:32 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
out:
|
|
|
|
qemu_vfree(new_table);
|
2016-11-15 13:14:01 +03:00
|
|
|
return ret;
|
2010-12-06 19:08:01 +03:00
|
|
|
}
|
|
|
|
|
2019-04-30 15:36:11 +03:00
|
|
|
int coroutine_fn qed_read_l1_table_sync(BDRVQEDState *s)
|
2010-12-06 19:08:01 +03:00
|
|
|
{
|
2016-11-14 18:08:44 +03:00
|
|
|
return qed_read_table(s, s->header.l1_table_offset, s->l1_table);
|
2010-12-06 19:08:01 +03:00
|
|
|
}
|
|
|
|
|
2018-03-01 19:36:19 +03:00
|
|
|
/* Called with table_lock held. */
|
2019-04-30 15:36:11 +03:00
|
|
|
int coroutine_fn qed_write_l1_table(BDRVQEDState *s, unsigned int index,
|
|
|
|
unsigned int n)
|
2010-12-06 19:08:01 +03:00
|
|
|
{
|
2023-06-01 14:51:45 +03:00
|
|
|
BLKDBG_CO_EVENT(s->bs->file, BLKDBG_L1_UPDATE);
|
2016-11-15 13:14:01 +03:00
|
|
|
return qed_write_table(s, s->header.l1_table_offset,
|
|
|
|
s->l1_table, index, n, false);
|
2010-12-06 19:08:01 +03:00
|
|
|
}
|
|
|
|
|
2019-04-30 15:36:11 +03:00
|
|
|
int coroutine_fn qed_write_l1_table_sync(BDRVQEDState *s, unsigned int index,
|
|
|
|
unsigned int n)
|
2010-12-06 19:08:01 +03:00
|
|
|
{
|
2016-11-15 13:14:01 +03:00
|
|
|
return qed_write_l1_table(s, index, n);
|
2010-12-06 19:08:01 +03:00
|
|
|
}
|
|
|
|
|
2018-03-01 19:36:19 +03:00
|
|
|
/* Called with table_lock held. */
|
2019-04-30 15:36:11 +03:00
|
|
|
int coroutine_fn qed_read_l2_table(BDRVQEDState *s, QEDRequest *request,
|
|
|
|
uint64_t offset)
|
2010-12-06 19:08:01 +03:00
|
|
|
{
|
2016-11-14 18:08:44 +03:00
|
|
|
int ret;
|
2010-12-06 19:08:01 +03:00
|
|
|
|
|
|
|
qed_unref_l2_cache_entry(request->l2_table);
|
|
|
|
|
|
|
|
/* Check for cached L2 entry */
|
|
|
|
request->l2_table = qed_find_l2_cache_entry(&s->l2_cache, offset);
|
|
|
|
if (request->l2_table) {
|
2016-11-14 18:26:14 +03:00
|
|
|
return 0;
|
2010-12-06 19:08:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
request->l2_table = qed_alloc_l2_cache_entry(&s->l2_cache);
|
|
|
|
request->l2_table->table = qed_alloc_table(s);
|
|
|
|
|
2023-06-01 14:51:45 +03:00
|
|
|
BLKDBG_CO_EVENT(s->bs->file, BLKDBG_L2_LOAD);
|
2016-11-14 18:08:44 +03:00
|
|
|
ret = qed_read_table(s, offset, request->l2_table->table);
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
/* can't trust loaded L2 table anymore */
|
|
|
|
qed_unref_l2_cache_entry(request->l2_table);
|
|
|
|
request->l2_table = NULL;
|
|
|
|
} else {
|
|
|
|
request->l2_table->offset = offset;
|
|
|
|
|
|
|
|
qed_commit_l2_cache_entry(&s->l2_cache, request->l2_table);
|
|
|
|
|
|
|
|
/* This is guaranteed to succeed because we just committed the entry
|
|
|
|
* to the cache.
|
|
|
|
*/
|
|
|
|
request->l2_table = qed_find_l2_cache_entry(&s->l2_cache, offset);
|
|
|
|
assert(request->l2_table != NULL);
|
|
|
|
}
|
|
|
|
|
2016-11-14 18:26:14 +03:00
|
|
|
return ret;
|
2010-12-06 19:08:01 +03:00
|
|
|
}
|
|
|
|
|
2019-04-30 15:36:11 +03:00
|
|
|
int coroutine_fn qed_read_l2_table_sync(BDRVQEDState *s, QEDRequest *request,
|
|
|
|
uint64_t offset)
|
2010-12-06 19:08:01 +03:00
|
|
|
{
|
2016-11-14 18:26:14 +03:00
|
|
|
return qed_read_l2_table(s, request, offset);
|
2010-12-06 19:08:01 +03:00
|
|
|
}
|
|
|
|
|
2018-03-01 19:36:19 +03:00
|
|
|
/* Called with table_lock held. */
|
2019-04-30 15:36:11 +03:00
|
|
|
int coroutine_fn qed_write_l2_table(BDRVQEDState *s, QEDRequest *request,
|
|
|
|
unsigned int index, unsigned int n,
|
|
|
|
bool flush)
|
2010-12-06 19:08:01 +03:00
|
|
|
{
|
2023-06-01 14:51:45 +03:00
|
|
|
BLKDBG_CO_EVENT(s->bs->file, BLKDBG_L2_UPDATE);
|
2016-11-15 13:14:01 +03:00
|
|
|
return qed_write_table(s, request->l2_table->offset,
|
|
|
|
request->l2_table->table, index, n, flush);
|
2010-12-06 19:08:01 +03:00
|
|
|
}
|
|
|
|
|
2019-04-30 15:36:11 +03:00
|
|
|
int coroutine_fn qed_write_l2_table_sync(BDRVQEDState *s, QEDRequest *request,
|
|
|
|
unsigned int index, unsigned int n,
|
|
|
|
bool flush)
|
2010-12-06 19:08:01 +03:00
|
|
|
{
|
2016-11-15 13:14:01 +03:00
|
|
|
return qed_write_l2_table(s, request, index, n, flush);
|
2010-12-06 19:08:01 +03:00
|
|
|
}
|