2 * QEMU Enhanced Disk Format Table I/O
4 * Copyright IBM, Corp. 2010
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.
16 #include "qemu_socket.h" /* for EINPROGRESS on Windows */
28 static void qed_read_table_cb(void *opaque
, int ret
)
30 QEDReadTableCB
*read_table_cb
= opaque
;
31 QEDTable
*table
= read_table_cb
->table
;
32 int noffsets
= read_table_cb
->iov
.iov_len
/ sizeof(uint64_t);
35 /* Handle I/O error */
40 /* Byteswap offsets */
41 for (i
= 0; i
< noffsets
; i
++) {
42 table
->offsets
[i
] = le64_to_cpu(table
->offsets
[i
]);
47 trace_qed_read_table_cb(read_table_cb
->s
, read_table_cb
->table
, ret
);
48 gencb_complete(&read_table_cb
->gencb
, ret
);
51 static void qed_read_table(BDRVQEDState
*s
, uint64_t offset
, QEDTable
*table
,
52 BlockDriverCompletionFunc
*cb
, void *opaque
)
54 QEDReadTableCB
*read_table_cb
= gencb_alloc(sizeof(*read_table_cb
),
56 QEMUIOVector
*qiov
= &read_table_cb
->qiov
;
57 BlockDriverAIOCB
*aiocb
;
59 trace_qed_read_table(s
, offset
, table
);
62 read_table_cb
->table
= table
;
63 read_table_cb
->iov
.iov_base
= table
->offsets
,
64 read_table_cb
->iov
.iov_len
= s
->header
.cluster_size
* s
->header
.table_size
,
66 qemu_iovec_init_external(qiov
, &read_table_cb
->iov
, 1);
67 aiocb
= bdrv_aio_readv(s
->bs
->file
, offset
/ BDRV_SECTOR_SIZE
, qiov
,
68 read_table_cb
->iov
.iov_len
/ BDRV_SECTOR_SIZE
,
69 qed_read_table_cb
, read_table_cb
);
71 qed_read_table_cb(read_table_cb
, -EIO
);
80 bool flush
; /* flush after write? */
86 static void qed_write_table_cb(void *opaque
, int ret
)
88 QEDWriteTableCB
*write_table_cb
= opaque
;
90 trace_qed_write_table_cb(write_table_cb
->s
,
91 write_table_cb
->orig_table
,
92 write_table_cb
->flush
,
99 if (write_table_cb
->flush
) {
100 /* We still need to flush first */
101 write_table_cb
->flush
= false;
102 bdrv_aio_flush(write_table_cb
->s
->bs
, qed_write_table_cb
,
108 qemu_vfree(write_table_cb
->table
);
109 gencb_complete(&write_table_cb
->gencb
, ret
);
114 * Write out an updated part or all of a table
117 * @offset: Offset of table in image file, in bytes
119 * @index: Index of first element
120 * @n: Number of elements
121 * @flush: Whether or not to sync to disk
122 * @cb: Completion function
123 * @opaque: Argument for completion function
125 static void qed_write_table(BDRVQEDState
*s
, uint64_t offset
, QEDTable
*table
,
126 unsigned int index
, unsigned int n
, bool flush
,
127 BlockDriverCompletionFunc
*cb
, void *opaque
)
129 QEDWriteTableCB
*write_table_cb
;
130 BlockDriverAIOCB
*aiocb
;
131 unsigned int sector_mask
= BDRV_SECTOR_SIZE
/ sizeof(uint64_t) - 1;
132 unsigned int start
, end
, i
;
135 trace_qed_write_table(s
, offset
, table
, index
, n
);
137 /* Calculate indices of the first and one after last elements */
138 start
= index
& ~sector_mask
;
139 end
= (index
+ n
+ sector_mask
) & ~sector_mask
;
141 len_bytes
= (end
- start
) * sizeof(uint64_t);
143 write_table_cb
= gencb_alloc(sizeof(*write_table_cb
), cb
, opaque
);
144 write_table_cb
->s
= s
;
145 write_table_cb
->orig_table
= table
;
146 write_table_cb
->flush
= flush
;
147 write_table_cb
->table
= qemu_blockalign(s
->bs
, len_bytes
);
148 write_table_cb
->iov
.iov_base
= write_table_cb
->table
->offsets
;
149 write_table_cb
->iov
.iov_len
= len_bytes
;
150 qemu_iovec_init_external(&write_table_cb
->qiov
, &write_table_cb
->iov
, 1);
153 for (i
= start
; i
< end
; i
++) {
154 uint64_t le_offset
= cpu_to_le64(table
->offsets
[i
]);
155 write_table_cb
->table
->offsets
[i
- start
] = le_offset
;
158 /* Adjust for offset into table */
159 offset
+= start
* sizeof(uint64_t);
161 aiocb
= bdrv_aio_writev(s
->bs
->file
, offset
/ BDRV_SECTOR_SIZE
,
162 &write_table_cb
->qiov
,
163 write_table_cb
->iov
.iov_len
/ BDRV_SECTOR_SIZE
,
164 qed_write_table_cb
, write_table_cb
);
166 qed_write_table_cb(write_table_cb
, -EIO
);
171 * Propagate return value from async callback
173 static void qed_sync_cb(void *opaque
, int ret
)
175 *(int *)opaque
= ret
;
178 int qed_read_l1_table_sync(BDRVQEDState
*s
)
180 int ret
= -EINPROGRESS
;
182 qed_read_table(s
, s
->header
.l1_table_offset
,
183 s
->l1_table
, qed_sync_cb
, &ret
);
184 while (ret
== -EINPROGRESS
) {
191 void qed_write_l1_table(BDRVQEDState
*s
, unsigned int index
, unsigned int n
,
192 BlockDriverCompletionFunc
*cb
, void *opaque
)
194 BLKDBG_EVENT(s
->bs
->file
, BLKDBG_L1_UPDATE
);
195 qed_write_table(s
, s
->header
.l1_table_offset
,
196 s
->l1_table
, index
, n
, false, cb
, opaque
);
199 int qed_write_l1_table_sync(BDRVQEDState
*s
, unsigned int index
,
202 int ret
= -EINPROGRESS
;
204 qed_write_l1_table(s
, index
, n
, qed_sync_cb
, &ret
);
205 while (ret
== -EINPROGRESS
) {
219 static void qed_read_l2_table_cb(void *opaque
, int ret
)
221 QEDReadL2TableCB
*read_l2_table_cb
= opaque
;
222 QEDRequest
*request
= read_l2_table_cb
->request
;
223 BDRVQEDState
*s
= read_l2_table_cb
->s
;
224 CachedL2Table
*l2_table
= request
->l2_table
;
225 uint64_t l2_offset
= read_l2_table_cb
->l2_offset
;
228 /* can't trust loaded L2 table anymore */
229 qed_unref_l2_cache_entry(l2_table
);
230 request
->l2_table
= NULL
;
232 l2_table
->offset
= l2_offset
;
234 qed_commit_l2_cache_entry(&s
->l2_cache
, l2_table
);
236 /* This is guaranteed to succeed because we just committed the entry
239 request
->l2_table
= qed_find_l2_cache_entry(&s
->l2_cache
, l2_offset
);
240 assert(request
->l2_table
!= NULL
);
243 gencb_complete(&read_l2_table_cb
->gencb
, ret
);
246 void qed_read_l2_table(BDRVQEDState
*s
, QEDRequest
*request
, uint64_t offset
,
247 BlockDriverCompletionFunc
*cb
, void *opaque
)
249 QEDReadL2TableCB
*read_l2_table_cb
;
251 qed_unref_l2_cache_entry(request
->l2_table
);
253 /* Check for cached L2 entry */
254 request
->l2_table
= qed_find_l2_cache_entry(&s
->l2_cache
, offset
);
255 if (request
->l2_table
) {
260 request
->l2_table
= qed_alloc_l2_cache_entry(&s
->l2_cache
);
261 request
->l2_table
->table
= qed_alloc_table(s
);
263 read_l2_table_cb
= gencb_alloc(sizeof(*read_l2_table_cb
), cb
, opaque
);
264 read_l2_table_cb
->s
= s
;
265 read_l2_table_cb
->l2_offset
= offset
;
266 read_l2_table_cb
->request
= request
;
268 BLKDBG_EVENT(s
->bs
->file
, BLKDBG_L2_LOAD
);
269 qed_read_table(s
, offset
, request
->l2_table
->table
,
270 qed_read_l2_table_cb
, read_l2_table_cb
);
273 int qed_read_l2_table_sync(BDRVQEDState
*s
, QEDRequest
*request
, uint64_t offset
)
275 int ret
= -EINPROGRESS
;
277 qed_read_l2_table(s
, request
, offset
, qed_sync_cb
, &ret
);
278 while (ret
== -EINPROGRESS
) {
285 void qed_write_l2_table(BDRVQEDState
*s
, QEDRequest
*request
,
286 unsigned int index
, unsigned int n
, bool flush
,
287 BlockDriverCompletionFunc
*cb
, void *opaque
)
289 BLKDBG_EVENT(s
->bs
->file
, BLKDBG_L2_UPDATE
);
290 qed_write_table(s
, request
->l2_table
->offset
,
291 request
->l2_table
->table
, index
, n
, flush
, cb
, opaque
);
294 int qed_write_l2_table_sync(BDRVQEDState
*s
, QEDRequest
*request
,
295 unsigned int index
, unsigned int n
, bool flush
)
297 int ret
= -EINPROGRESS
;
299 qed_write_l2_table(s
, request
, index
, n
, flush
, qed_sync_cb
, &ret
);
300 while (ret
== -EINPROGRESS
) {