xref: /openbmc/qemu/block/qed-table.c (revision 48151859)
1 /*
2  * QEMU Enhanced Disk Format Table I/O
3  *
4  * Copyright IBM, Corp. 2010
5  *
6  * Authors:
7  *  Stefan Hajnoczi   <stefanha@linux.vnet.ibm.com>
8  *  Anthony Liguori   <aliguori@us.ibm.com>
9  *
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.
12  *
13  */
14 
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"
20 
21 typedef struct {
22     GenericCB gencb;
23     BDRVQEDState *s;
24     QEDTable *table;
25 
26     struct iovec iov;
27     QEMUIOVector qiov;
28 } QEDReadTableCB;
29 
30 static void qed_read_table_cb(void *opaque, int ret)
31 {
32     QEDReadTableCB *read_table_cb = opaque;
33     QEDTable *table = read_table_cb->table;
34     int noffsets = read_table_cb->qiov.size / sizeof(uint64_t);
35     int i;
36 
37     /* Handle I/O error */
38     if (ret) {
39         goto out;
40     }
41 
42     /* Byteswap offsets */
43     for (i = 0; i < noffsets; i++) {
44         table->offsets[i] = le64_to_cpu(table->offsets[i]);
45     }
46 
47 out:
48     /* Completion */
49     trace_qed_read_table_cb(read_table_cb->s, read_table_cb->table, ret);
50     gencb_complete(&read_table_cb->gencb, ret);
51 }
52 
53 static void qed_read_table(BDRVQEDState *s, uint64_t offset, QEDTable *table,
54                            BlockCompletionFunc *cb, void *opaque)
55 {
56     QEDReadTableCB *read_table_cb = gencb_alloc(sizeof(*read_table_cb),
57                                                 cb, opaque);
58     QEMUIOVector *qiov = &read_table_cb->qiov;
59 
60     trace_qed_read_table(s, offset, table);
61 
62     read_table_cb->s = s;
63     read_table_cb->table = table;
64     read_table_cb->iov.iov_base = table->offsets,
65     read_table_cb->iov.iov_len = s->header.cluster_size * s->header.table_size,
66 
67     qemu_iovec_init_external(qiov, &read_table_cb->iov, 1);
68     bdrv_aio_readv(s->bs->file, offset / BDRV_SECTOR_SIZE, qiov,
69                    qiov->size / BDRV_SECTOR_SIZE,
70                    qed_read_table_cb, read_table_cb);
71 }
72 
73 typedef struct {
74     GenericCB gencb;
75     BDRVQEDState *s;
76     QEDTable *orig_table;
77     QEDTable *table;
78     bool flush;             /* flush after write? */
79 
80     struct iovec iov;
81     QEMUIOVector qiov;
82 } QEDWriteTableCB;
83 
84 static void qed_write_table_cb(void *opaque, int ret)
85 {
86     QEDWriteTableCB *write_table_cb = opaque;
87 
88     trace_qed_write_table_cb(write_table_cb->s,
89                              write_table_cb->orig_table,
90                              write_table_cb->flush,
91                              ret);
92 
93     if (ret) {
94         goto out;
95     }
96 
97     if (write_table_cb->flush) {
98         /* We still need to flush first */
99         write_table_cb->flush = false;
100         bdrv_aio_flush(write_table_cb->s->bs, qed_write_table_cb,
101                        write_table_cb);
102         return;
103     }
104 
105 out:
106     qemu_vfree(write_table_cb->table);
107     gencb_complete(&write_table_cb->gencb, ret);
108 }
109 
110 /**
111  * Write out an updated part or all of a table
112  *
113  * @s:          QED state
114  * @offset:     Offset of table in image file, in bytes
115  * @table:      Table
116  * @index:      Index of first element
117  * @n:          Number of elements
118  * @flush:      Whether or not to sync to disk
119  * @cb:         Completion function
120  * @opaque:     Argument for completion function
121  */
122 static void qed_write_table(BDRVQEDState *s, uint64_t offset, QEDTable *table,
123                             unsigned int index, unsigned int n, bool flush,
124                             BlockCompletionFunc *cb, void *opaque)
125 {
126     QEDWriteTableCB *write_table_cb;
127     unsigned int sector_mask = BDRV_SECTOR_SIZE / sizeof(uint64_t) - 1;
128     unsigned int start, end, i;
129     size_t len_bytes;
130 
131     trace_qed_write_table(s, offset, table, index, n);
132 
133     /* Calculate indices of the first and one after last elements */
134     start = index & ~sector_mask;
135     end = (index + n + sector_mask) & ~sector_mask;
136 
137     len_bytes = (end - start) * sizeof(uint64_t);
138 
139     write_table_cb = gencb_alloc(sizeof(*write_table_cb), cb, opaque);
140     write_table_cb->s = s;
141     write_table_cb->orig_table = table;
142     write_table_cb->flush = flush;
143     write_table_cb->table = qemu_blockalign(s->bs, len_bytes);
144     write_table_cb->iov.iov_base = write_table_cb->table->offsets;
145     write_table_cb->iov.iov_len = len_bytes;
146     qemu_iovec_init_external(&write_table_cb->qiov, &write_table_cb->iov, 1);
147 
148     /* Byteswap table */
149     for (i = start; i < end; i++) {
150         uint64_t le_offset = cpu_to_le64(table->offsets[i]);
151         write_table_cb->table->offsets[i - start] = le_offset;
152     }
153 
154     /* Adjust for offset into table */
155     offset += start * sizeof(uint64_t);
156 
157     bdrv_aio_writev(s->bs->file, offset / BDRV_SECTOR_SIZE,
158                     &write_table_cb->qiov,
159                     write_table_cb->qiov.size / BDRV_SECTOR_SIZE,
160                     qed_write_table_cb, write_table_cb);
161 }
162 
163 /**
164  * Propagate return value from async callback
165  */
166 static void qed_sync_cb(void *opaque, int ret)
167 {
168     *(int *)opaque = ret;
169 }
170 
171 int qed_read_l1_table_sync(BDRVQEDState *s)
172 {
173     int ret = -EINPROGRESS;
174 
175     qed_read_table(s, s->header.l1_table_offset,
176                    s->l1_table, qed_sync_cb, &ret);
177     while (ret == -EINPROGRESS) {
178         aio_poll(bdrv_get_aio_context(s->bs), true);
179     }
180 
181     return ret;
182 }
183 
184 void qed_write_l1_table(BDRVQEDState *s, unsigned int index, unsigned int n,
185                         BlockCompletionFunc *cb, void *opaque)
186 {
187     BLKDBG_EVENT(s->bs->file, BLKDBG_L1_UPDATE);
188     qed_write_table(s, s->header.l1_table_offset,
189                     s->l1_table, index, n, false, cb, opaque);
190 }
191 
192 int qed_write_l1_table_sync(BDRVQEDState *s, unsigned int index,
193                             unsigned int n)
194 {
195     int ret = -EINPROGRESS;
196 
197     qed_write_l1_table(s, index, n, qed_sync_cb, &ret);
198     while (ret == -EINPROGRESS) {
199         aio_poll(bdrv_get_aio_context(s->bs), true);
200     }
201 
202     return ret;
203 }
204 
205 typedef struct {
206     GenericCB gencb;
207     BDRVQEDState *s;
208     uint64_t l2_offset;
209     QEDRequest *request;
210 } QEDReadL2TableCB;
211 
212 static void qed_read_l2_table_cb(void *opaque, int ret)
213 {
214     QEDReadL2TableCB *read_l2_table_cb = opaque;
215     QEDRequest *request = read_l2_table_cb->request;
216     BDRVQEDState *s = read_l2_table_cb->s;
217     CachedL2Table *l2_table = request->l2_table;
218     uint64_t l2_offset = read_l2_table_cb->l2_offset;
219 
220     if (ret) {
221         /* can't trust loaded L2 table anymore */
222         qed_unref_l2_cache_entry(l2_table);
223         request->l2_table = NULL;
224     } else {
225         l2_table->offset = l2_offset;
226 
227         qed_commit_l2_cache_entry(&s->l2_cache, l2_table);
228 
229         /* This is guaranteed to succeed because we just committed the entry
230          * to the cache.
231          */
232         request->l2_table = qed_find_l2_cache_entry(&s->l2_cache, l2_offset);
233         assert(request->l2_table != NULL);
234     }
235 
236     gencb_complete(&read_l2_table_cb->gencb, ret);
237 }
238 
239 void qed_read_l2_table(BDRVQEDState *s, QEDRequest *request, uint64_t offset,
240                        BlockCompletionFunc *cb, void *opaque)
241 {
242     QEDReadL2TableCB *read_l2_table_cb;
243 
244     qed_unref_l2_cache_entry(request->l2_table);
245 
246     /* Check for cached L2 entry */
247     request->l2_table = qed_find_l2_cache_entry(&s->l2_cache, offset);
248     if (request->l2_table) {
249         cb(opaque, 0);
250         return;
251     }
252 
253     request->l2_table = qed_alloc_l2_cache_entry(&s->l2_cache);
254     request->l2_table->table = qed_alloc_table(s);
255 
256     read_l2_table_cb = gencb_alloc(sizeof(*read_l2_table_cb), cb, opaque);
257     read_l2_table_cb->s = s;
258     read_l2_table_cb->l2_offset = offset;
259     read_l2_table_cb->request = request;
260 
261     BLKDBG_EVENT(s->bs->file, BLKDBG_L2_LOAD);
262     qed_read_table(s, offset, request->l2_table->table,
263                    qed_read_l2_table_cb, read_l2_table_cb);
264 }
265 
266 int qed_read_l2_table_sync(BDRVQEDState *s, QEDRequest *request, uint64_t offset)
267 {
268     int ret = -EINPROGRESS;
269 
270     qed_read_l2_table(s, request, offset, qed_sync_cb, &ret);
271     while (ret == -EINPROGRESS) {
272         aio_poll(bdrv_get_aio_context(s->bs), true);
273     }
274 
275     return ret;
276 }
277 
278 void qed_write_l2_table(BDRVQEDState *s, QEDRequest *request,
279                         unsigned int index, unsigned int n, bool flush,
280                         BlockCompletionFunc *cb, void *opaque)
281 {
282     BLKDBG_EVENT(s->bs->file, BLKDBG_L2_UPDATE);
283     qed_write_table(s, request->l2_table->offset,
284                     request->l2_table->table, index, n, flush, cb, opaque);
285 }
286 
287 int qed_write_l2_table_sync(BDRVQEDState *s, QEDRequest *request,
288                             unsigned int index, unsigned int n, bool flush)
289 {
290     int ret = -EINPROGRESS;
291 
292     qed_write_l2_table(s, request, index, n, flush, qed_sync_cb, &ret);
293     while (ret == -EINPROGRESS) {
294         aio_poll(bdrv_get_aio_context(s->bs), true);
295     }
296 
297     return ret;
298 }
299