timer: use an inline function for free
[qemu/ar7.git] / block / qed-table.c
blobb12c298a8acbc39f34f916c206ab956b8997b5d5
1 /*
2 * QEMU Enhanced Disk Format Table I/O
4 * Copyright IBM, Corp. 2010
6 * Authors:
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
8 * Anthony Liguori <aliguori@us.ibm.com>
10 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
11 * See the COPYING.LIB file in the top-level directory.
15 #include "qemu/osdep.h"
16 #include "trace.h"
17 #include "qemu/sockets.h" /* for EINPROGRESS on Windows */
18 #include "qed.h"
19 #include "qemu/bswap.h"
21 typedef struct {
22 GenericCB gencb;
23 BDRVQEDState *s;
24 QEDTable *table;
26 struct iovec iov;
27 QEMUIOVector qiov;
28 } QEDReadTableCB;
30 static void qed_read_table_cb(void *opaque, int ret)
32 QEDReadTableCB *read_table_cb = opaque;
33 QEDTable *table = read_table_cb->table;
34 BDRVQEDState *s = read_table_cb->s;
35 int noffsets = read_table_cb->qiov.size / sizeof(uint64_t);
36 int i;
38 /* Handle I/O error */
39 if (ret) {
40 goto out;
43 /* Byteswap offsets */
44 qed_acquire(s);
45 for (i = 0; i < noffsets; i++) {
46 table->offsets[i] = le64_to_cpu(table->offsets[i]);
48 qed_release(s);
50 out:
51 /* Completion */
52 trace_qed_read_table_cb(s, read_table_cb->table, ret);
53 gencb_complete(&read_table_cb->gencb, ret);
56 static void qed_read_table(BDRVQEDState *s, uint64_t offset, QEDTable *table,
57 BlockCompletionFunc *cb, void *opaque)
59 QEDReadTableCB *read_table_cb = gencb_alloc(sizeof(*read_table_cb),
60 cb, opaque);
61 QEMUIOVector *qiov = &read_table_cb->qiov;
63 trace_qed_read_table(s, offset, table);
65 read_table_cb->s = s;
66 read_table_cb->table = table;
67 read_table_cb->iov.iov_base = table->offsets,
68 read_table_cb->iov.iov_len = s->header.cluster_size * s->header.table_size,
70 qemu_iovec_init_external(qiov, &read_table_cb->iov, 1);
71 bdrv_aio_readv(s->bs->file, offset / BDRV_SECTOR_SIZE, qiov,
72 qiov->size / BDRV_SECTOR_SIZE,
73 qed_read_table_cb, read_table_cb);
76 typedef struct {
77 GenericCB gencb;
78 BDRVQEDState *s;
79 QEDTable *orig_table;
80 QEDTable *table;
81 bool flush; /* flush after write? */
83 struct iovec iov;
84 QEMUIOVector qiov;
85 } QEDWriteTableCB;
87 static void qed_write_table_cb(void *opaque, int ret)
89 QEDWriteTableCB *write_table_cb = opaque;
90 BDRVQEDState *s = write_table_cb->s;
92 trace_qed_write_table_cb(s,
93 write_table_cb->orig_table,
94 write_table_cb->flush,
95 ret);
97 if (ret) {
98 goto out;
101 if (write_table_cb->flush) {
102 /* We still need to flush first */
103 write_table_cb->flush = false;
104 qed_acquire(s);
105 bdrv_aio_flush(write_table_cb->s->bs, qed_write_table_cb,
106 write_table_cb);
107 qed_release(s);
108 return;
111 out:
112 qemu_vfree(write_table_cb->table);
113 gencb_complete(&write_table_cb->gencb, ret);
117 * Write out an updated part or all of a table
119 * @s: QED state
120 * @offset: Offset of table in image file, in bytes
121 * @table: Table
122 * @index: Index of first element
123 * @n: Number of elements
124 * @flush: Whether or not to sync to disk
125 * @cb: Completion function
126 * @opaque: Argument for completion function
128 static void qed_write_table(BDRVQEDState *s, uint64_t offset, QEDTable *table,
129 unsigned int index, unsigned int n, bool flush,
130 BlockCompletionFunc *cb, void *opaque)
132 QEDWriteTableCB *write_table_cb;
133 unsigned int sector_mask = BDRV_SECTOR_SIZE / sizeof(uint64_t) - 1;
134 unsigned int start, end, i;
135 size_t len_bytes;
137 trace_qed_write_table(s, offset, table, index, n);
139 /* Calculate indices of the first and one after last elements */
140 start = index & ~sector_mask;
141 end = (index + n + sector_mask) & ~sector_mask;
143 len_bytes = (end - start) * sizeof(uint64_t);
145 write_table_cb = gencb_alloc(sizeof(*write_table_cb), cb, opaque);
146 write_table_cb->s = s;
147 write_table_cb->orig_table = table;
148 write_table_cb->flush = flush;
149 write_table_cb->table = qemu_blockalign(s->bs, len_bytes);
150 write_table_cb->iov.iov_base = write_table_cb->table->offsets;
151 write_table_cb->iov.iov_len = len_bytes;
152 qemu_iovec_init_external(&write_table_cb->qiov, &write_table_cb->iov, 1);
154 /* Byteswap table */
155 for (i = start; i < end; i++) {
156 uint64_t le_offset = cpu_to_le64(table->offsets[i]);
157 write_table_cb->table->offsets[i - start] = le_offset;
160 /* Adjust for offset into table */
161 offset += start * sizeof(uint64_t);
163 bdrv_aio_writev(s->bs->file, offset / BDRV_SECTOR_SIZE,
164 &write_table_cb->qiov,
165 write_table_cb->qiov.size / BDRV_SECTOR_SIZE,
166 qed_write_table_cb, write_table_cb);
170 * Propagate return value from async callback
172 static void qed_sync_cb(void *opaque, int ret)
174 *(int *)opaque = ret;
177 int qed_read_l1_table_sync(BDRVQEDState *s)
179 int ret = -EINPROGRESS;
181 qed_read_table(s, s->header.l1_table_offset,
182 s->l1_table, qed_sync_cb, &ret);
183 BDRV_POLL_WHILE(s->bs, ret == -EINPROGRESS);
185 return ret;
188 void qed_write_l1_table(BDRVQEDState *s, unsigned int index, unsigned int n,
189 BlockCompletionFunc *cb, void *opaque)
191 BLKDBG_EVENT(s->bs->file, BLKDBG_L1_UPDATE);
192 qed_write_table(s, s->header.l1_table_offset,
193 s->l1_table, index, n, false, cb, opaque);
196 int qed_write_l1_table_sync(BDRVQEDState *s, unsigned int index,
197 unsigned int n)
199 int ret = -EINPROGRESS;
201 qed_write_l1_table(s, index, n, qed_sync_cb, &ret);
202 BDRV_POLL_WHILE(s->bs, ret == -EINPROGRESS);
204 return ret;
207 typedef struct {
208 GenericCB gencb;
209 BDRVQEDState *s;
210 uint64_t l2_offset;
211 QEDRequest *request;
212 } QEDReadL2TableCB;
214 static void qed_read_l2_table_cb(void *opaque, int ret)
216 QEDReadL2TableCB *read_l2_table_cb = opaque;
217 QEDRequest *request = read_l2_table_cb->request;
218 BDRVQEDState *s = read_l2_table_cb->s;
219 CachedL2Table *l2_table = request->l2_table;
220 uint64_t l2_offset = read_l2_table_cb->l2_offset;
222 qed_acquire(s);
223 if (ret) {
224 /* can't trust loaded L2 table anymore */
225 qed_unref_l2_cache_entry(l2_table);
226 request->l2_table = NULL;
227 } else {
228 l2_table->offset = l2_offset;
230 qed_commit_l2_cache_entry(&s->l2_cache, l2_table);
232 /* This is guaranteed to succeed because we just committed the entry
233 * to the cache.
235 request->l2_table = qed_find_l2_cache_entry(&s->l2_cache, l2_offset);
236 assert(request->l2_table != NULL);
238 qed_release(s);
240 gencb_complete(&read_l2_table_cb->gencb, ret);
243 void qed_read_l2_table(BDRVQEDState *s, QEDRequest *request, uint64_t offset,
244 BlockCompletionFunc *cb, void *opaque)
246 QEDReadL2TableCB *read_l2_table_cb;
248 qed_unref_l2_cache_entry(request->l2_table);
250 /* Check for cached L2 entry */
251 request->l2_table = qed_find_l2_cache_entry(&s->l2_cache, offset);
252 if (request->l2_table) {
253 cb(opaque, 0);
254 return;
257 request->l2_table = qed_alloc_l2_cache_entry(&s->l2_cache);
258 request->l2_table->table = qed_alloc_table(s);
260 read_l2_table_cb = gencb_alloc(sizeof(*read_l2_table_cb), cb, opaque);
261 read_l2_table_cb->s = s;
262 read_l2_table_cb->l2_offset = offset;
263 read_l2_table_cb->request = request;
265 BLKDBG_EVENT(s->bs->file, BLKDBG_L2_LOAD);
266 qed_read_table(s, offset, request->l2_table->table,
267 qed_read_l2_table_cb, read_l2_table_cb);
270 int qed_read_l2_table_sync(BDRVQEDState *s, QEDRequest *request, uint64_t offset)
272 int ret = -EINPROGRESS;
274 qed_read_l2_table(s, request, offset, qed_sync_cb, &ret);
275 BDRV_POLL_WHILE(s->bs, ret == -EINPROGRESS);
277 return ret;
280 void qed_write_l2_table(BDRVQEDState *s, QEDRequest *request,
281 unsigned int index, unsigned int n, bool flush,
282 BlockCompletionFunc *cb, void *opaque)
284 BLKDBG_EVENT(s->bs->file, BLKDBG_L2_UPDATE);
285 qed_write_table(s, request->l2_table->offset,
286 request->l2_table->table, index, n, flush, cb, opaque);
289 int qed_write_l2_table_sync(BDRVQEDState *s, QEDRequest *request,
290 unsigned int index, unsigned int n, bool flush)
292 int ret = -EINPROGRESS;
294 qed_write_l2_table(s, request, index, n, flush, qed_sync_cb, &ret);
295 BDRV_POLL_WHILE(s->bs, ret == -EINPROGRESS);
297 return ret;