15
#include "qemu/osdep.h"
16
#include "block/block-io.h"
18
#include "qemu/sockets.h"
20
#include "qemu/bswap.h"
21
#include "qemu/memalign.h"
24
static int coroutine_fn GRAPH_RDLOCK
25
qed_read_table(BDRVQEDState *s, uint64_t offset, QEDTable *table)
27
unsigned int bytes = s->header.cluster_size * s->header.table_size;
32
trace_qed_read_table(s, offset, table);
34
qemu_co_mutex_unlock(&s->table_lock);
35
ret = bdrv_co_pread(s->bs->file, offset, bytes, table->offsets, 0);
36
qemu_co_mutex_lock(&s->table_lock);
42
noffsets = bytes / sizeof(uint64_t);
43
for (i = 0; i < noffsets; i++) {
44
table->offsets[i] = le64_to_cpu(table->offsets[i]);
50
trace_qed_read_table_cb(s, table, ret);
66
static int coroutine_fn GRAPH_RDLOCK
67
qed_write_table(BDRVQEDState *s, uint64_t offset, QEDTable *table,
68
unsigned int index, unsigned int n, bool flush)
70
unsigned int sector_mask = BDRV_SECTOR_SIZE / sizeof(uint64_t) - 1;
71
unsigned int start, end, i;
76
trace_qed_write_table(s, offset, table, index, n);
79
start = index & ~sector_mask;
80
end = (index + n + sector_mask) & ~sector_mask;
82
len_bytes = (end - start) * sizeof(uint64_t);
84
new_table = qemu_blockalign(s->bs, len_bytes);
87
for (i = start; i < end; i++) {
88
uint64_t le_offset = cpu_to_le64(table->offsets[i]);
89
new_table->offsets[i - start] = le_offset;
93
offset += start * sizeof(uint64_t);
95
qemu_co_mutex_unlock(&s->table_lock);
96
ret = bdrv_co_pwrite(s->bs->file, offset, len_bytes, new_table->offsets, 0);
97
qemu_co_mutex_lock(&s->table_lock);
98
trace_qed_write_table_cb(s, table, flush, ret);
104
ret = bdrv_co_flush(s->bs);
112
qemu_vfree(new_table);
116
int coroutine_fn qed_read_l1_table_sync(BDRVQEDState *s)
118
return qed_read_table(s, s->header.l1_table_offset, s->l1_table);
122
int coroutine_fn qed_write_l1_table(BDRVQEDState *s, unsigned int index,
125
BLKDBG_CO_EVENT(s->bs->file, BLKDBG_L1_UPDATE);
126
return qed_write_table(s, s->header.l1_table_offset,
127
s->l1_table, index, n, false);
130
int coroutine_fn qed_write_l1_table_sync(BDRVQEDState *s, unsigned int index,
133
return qed_write_l1_table(s, index, n);
137
int coroutine_fn qed_read_l2_table(BDRVQEDState *s, QEDRequest *request,
142
qed_unref_l2_cache_entry(request->l2_table);
145
request->l2_table = qed_find_l2_cache_entry(&s->l2_cache, offset);
146
if (request->l2_table) {
150
request->l2_table = qed_alloc_l2_cache_entry(&s->l2_cache);
151
request->l2_table->table = qed_alloc_table(s);
153
BLKDBG_CO_EVENT(s->bs->file, BLKDBG_L2_LOAD);
154
ret = qed_read_table(s, offset, request->l2_table->table);
158
qed_unref_l2_cache_entry(request->l2_table);
159
request->l2_table = NULL;
161
request->l2_table->offset = offset;
163
qed_commit_l2_cache_entry(&s->l2_cache, request->l2_table);
168
request->l2_table = qed_find_l2_cache_entry(&s->l2_cache, offset);
169
assert(request->l2_table != NULL);
175
int coroutine_fn qed_read_l2_table_sync(BDRVQEDState *s, QEDRequest *request,
178
return qed_read_l2_table(s, request, offset);
182
int coroutine_fn qed_write_l2_table(BDRVQEDState *s, QEDRequest *request,
183
unsigned int index, unsigned int n,
186
BLKDBG_CO_EVENT(s->bs->file, BLKDBG_L2_UPDATE);
187
return qed_write_table(s, request->l2_table->offset,
188
request->l2_table->table, index, n, flush);
191
int coroutine_fn qed_write_l2_table_sync(BDRVQEDState *s, QEDRequest *request,
192
unsigned int index, unsigned int n,
195
return qed_write_l2_table(s, request, index, n, flush);