]> git.proxmox.com Git - mirror_qemu.git/blob - block/qed-table.c
qed: Make qed_write_table() synchronous
[mirror_qemu.git] / block / qed-table.c
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 static int qed_read_table(BDRVQEDState *s, uint64_t offset, QEDTable *table)
22 {
23 QEMUIOVector qiov;
24 int noffsets;
25 int i, ret;
26
27 struct iovec iov = {
28 .iov_base = table->offsets,
29 .iov_len = s->header.cluster_size * s->header.table_size,
30 };
31 qemu_iovec_init_external(&qiov, &iov, 1);
32
33 trace_qed_read_table(s, offset, table);
34
35 ret = bdrv_preadv(s->bs->file, offset, &qiov);
36 if (ret < 0) {
37 goto out;
38 }
39
40 /* Byteswap offsets */
41 qed_acquire(s);
42 noffsets = qiov.size / sizeof(uint64_t);
43 for (i = 0; i < noffsets; i++) {
44 table->offsets[i] = le64_to_cpu(table->offsets[i]);
45 }
46 qed_release(s);
47
48 ret = 0;
49 out:
50 /* Completion */
51 trace_qed_read_table_cb(s, table, ret);
52 return ret;
53 }
54
55 /**
56 * Write out an updated part or all of a table
57 *
58 * @s: QED state
59 * @offset: Offset of table in image file, in bytes
60 * @table: Table
61 * @index: Index of first element
62 * @n: Number of elements
63 * @flush: Whether or not to sync to disk
64 * @cb: Completion function
65 * @opaque: Argument for completion function
66 */
67 static void qed_write_table(BDRVQEDState *s, uint64_t offset, QEDTable *table,
68 unsigned int index, unsigned int n, bool flush,
69 BlockCompletionFunc *cb, void *opaque)
70 {
71 unsigned int sector_mask = BDRV_SECTOR_SIZE / sizeof(uint64_t) - 1;
72 unsigned int start, end, i;
73 QEDTable *new_table;
74 struct iovec iov;
75 QEMUIOVector qiov;
76 size_t len_bytes;
77 int ret;
78
79 trace_qed_write_table(s, offset, table, index, n);
80
81 /* Calculate indices of the first and one after last elements */
82 start = index & ~sector_mask;
83 end = (index + n + sector_mask) & ~sector_mask;
84
85 len_bytes = (end - start) * sizeof(uint64_t);
86
87 new_table = qemu_blockalign(s->bs, len_bytes);
88 iov = (struct iovec) {
89 .iov_base = new_table->offsets,
90 .iov_len = len_bytes,
91 };
92 qemu_iovec_init_external(&qiov, &iov, 1);
93
94 /* Byteswap table */
95 for (i = start; i < end; i++) {
96 uint64_t le_offset = cpu_to_le64(table->offsets[i]);
97 new_table->offsets[i - start] = le_offset;
98 }
99
100 /* Adjust for offset into table */
101 offset += start * sizeof(uint64_t);
102
103 ret = bdrv_pwritev(s->bs->file, offset, &qiov);
104 trace_qed_write_table_cb(s, table, flush, ret);
105 if (ret < 0) {
106 goto out;
107 }
108
109 if (flush) {
110 qed_acquire(s);
111 ret = bdrv_flush(s->bs);
112 qed_release(s);
113 if (ret < 0) {
114 goto out;
115 }
116 }
117
118 ret = 0;
119 out:
120 qemu_vfree(new_table);
121 cb(opaque, ret);
122 }
123
124 /**
125 * Propagate return value from async callback
126 */
127 static void qed_sync_cb(void *opaque, int ret)
128 {
129 *(int *)opaque = ret;
130 }
131
132 int qed_read_l1_table_sync(BDRVQEDState *s)
133 {
134 return qed_read_table(s, s->header.l1_table_offset, s->l1_table);
135 }
136
137 void qed_write_l1_table(BDRVQEDState *s, unsigned int index, unsigned int n,
138 BlockCompletionFunc *cb, void *opaque)
139 {
140 BLKDBG_EVENT(s->bs->file, BLKDBG_L1_UPDATE);
141 qed_write_table(s, s->header.l1_table_offset,
142 s->l1_table, index, n, false, cb, opaque);
143 }
144
145 int qed_write_l1_table_sync(BDRVQEDState *s, unsigned int index,
146 unsigned int n)
147 {
148 int ret = -EINPROGRESS;
149
150 qed_write_l1_table(s, index, n, qed_sync_cb, &ret);
151 BDRV_POLL_WHILE(s->bs, ret == -EINPROGRESS);
152
153 return ret;
154 }
155
156 int qed_read_l2_table(BDRVQEDState *s, QEDRequest *request, uint64_t offset)
157 {
158 int ret;
159
160 qed_unref_l2_cache_entry(request->l2_table);
161
162 /* Check for cached L2 entry */
163 request->l2_table = qed_find_l2_cache_entry(&s->l2_cache, offset);
164 if (request->l2_table) {
165 return 0;
166 }
167
168 request->l2_table = qed_alloc_l2_cache_entry(&s->l2_cache);
169 request->l2_table->table = qed_alloc_table(s);
170
171 BLKDBG_EVENT(s->bs->file, BLKDBG_L2_LOAD);
172 ret = qed_read_table(s, offset, request->l2_table->table);
173
174 qed_acquire(s);
175 if (ret) {
176 /* can't trust loaded L2 table anymore */
177 qed_unref_l2_cache_entry(request->l2_table);
178 request->l2_table = NULL;
179 } else {
180 request->l2_table->offset = offset;
181
182 qed_commit_l2_cache_entry(&s->l2_cache, request->l2_table);
183
184 /* This is guaranteed to succeed because we just committed the entry
185 * to the cache.
186 */
187 request->l2_table = qed_find_l2_cache_entry(&s->l2_cache, offset);
188 assert(request->l2_table != NULL);
189 }
190 qed_release(s);
191
192 return ret;
193 }
194
195 int qed_read_l2_table_sync(BDRVQEDState *s, QEDRequest *request, uint64_t offset)
196 {
197 return qed_read_l2_table(s, request, offset);
198 }
199
200 void qed_write_l2_table(BDRVQEDState *s, QEDRequest *request,
201 unsigned int index, unsigned int n, bool flush,
202 BlockCompletionFunc *cb, void *opaque)
203 {
204 BLKDBG_EVENT(s->bs->file, BLKDBG_L2_UPDATE);
205 qed_write_table(s, request->l2_table->offset,
206 request->l2_table->table, index, n, flush, cb, opaque);
207 }
208
209 int qed_write_l2_table_sync(BDRVQEDState *s, QEDRequest *request,
210 unsigned int index, unsigned int n, bool flush)
211 {
212 int ret = -EINPROGRESS;
213
214 qed_write_l2_table(s, request, index, n, flush, qed_sync_cb, &ret);
215 BDRV_POLL_WHILE(s->bs, ret == -EINPROGRESS);
216
217 return ret;
218 }