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.
15 #include "qemu/osdep.h"
17 #include "qemu/sockets.h" /* for EINPROGRESS on Windows */
19 #include "qemu/bswap.h"
20 #include "qemu/memalign.h"
22 /* Called with table_lock held. */
23 static int coroutine_fn
qed_read_table(BDRVQEDState
*s
, uint64_t offset
,
26 unsigned int bytes
= s
->header
.cluster_size
* s
->header
.table_size
;
31 trace_qed_read_table(s
, offset
, table
);
33 qemu_co_mutex_unlock(&s
->table_lock
);
34 ret
= bdrv_co_pread(s
->bs
->file
, offset
, bytes
, table
->offsets
, 0);
35 qemu_co_mutex_lock(&s
->table_lock
);
40 /* Byteswap offsets */
41 noffsets
= bytes
/ sizeof(uint64_t);
42 for (i
= 0; i
< noffsets
; i
++) {
43 table
->offsets
[i
] = le64_to_cpu(table
->offsets
[i
]);
49 trace_qed_read_table_cb(s
, table
, ret
);
54 * Write out an updated part or all of a table
57 * @offset: Offset of table in image file, in bytes
59 * @index: Index of first element
60 * @n: Number of elements
61 * @flush: Whether or not to sync to disk
63 * Called with table_lock held.
65 static int coroutine_fn
qed_write_table(BDRVQEDState
*s
, uint64_t offset
,
66 QEDTable
*table
, unsigned int index
,
67 unsigned int n
, bool flush
)
69 unsigned int sector_mask
= BDRV_SECTOR_SIZE
/ sizeof(uint64_t) - 1;
70 unsigned int start
, end
, i
;
75 trace_qed_write_table(s
, offset
, table
, index
, n
);
77 /* Calculate indices of the first and one after last elements */
78 start
= index
& ~sector_mask
;
79 end
= (index
+ n
+ sector_mask
) & ~sector_mask
;
81 len_bytes
= (end
- start
) * sizeof(uint64_t);
83 new_table
= qemu_blockalign(s
->bs
, len_bytes
);
86 for (i
= start
; i
< end
; i
++) {
87 uint64_t le_offset
= cpu_to_le64(table
->offsets
[i
]);
88 new_table
->offsets
[i
- start
] = le_offset
;
91 /* Adjust for offset into table */
92 offset
+= start
* sizeof(uint64_t);
94 qemu_co_mutex_unlock(&s
->table_lock
);
95 ret
= bdrv_co_pwrite(s
->bs
->file
, offset
, len_bytes
, new_table
->offsets
, 0);
96 qemu_co_mutex_lock(&s
->table_lock
);
97 trace_qed_write_table_cb(s
, table
, flush
, ret
);
103 ret
= bdrv_flush(s
->bs
);
111 qemu_vfree(new_table
);
115 int coroutine_fn
qed_read_l1_table_sync(BDRVQEDState
*s
)
117 return qed_read_table(s
, s
->header
.l1_table_offset
, s
->l1_table
);
120 /* Called with table_lock held. */
121 int coroutine_fn
qed_write_l1_table(BDRVQEDState
*s
, unsigned int index
,
124 BLKDBG_EVENT(s
->bs
->file
, BLKDBG_L1_UPDATE
);
125 return qed_write_table(s
, s
->header
.l1_table_offset
,
126 s
->l1_table
, index
, n
, false);
129 int coroutine_fn
qed_write_l1_table_sync(BDRVQEDState
*s
, unsigned int index
,
132 return qed_write_l1_table(s
, index
, n
);
135 /* Called with table_lock held. */
136 int coroutine_fn
qed_read_l2_table(BDRVQEDState
*s
, QEDRequest
*request
,
141 qed_unref_l2_cache_entry(request
->l2_table
);
143 /* Check for cached L2 entry */
144 request
->l2_table
= qed_find_l2_cache_entry(&s
->l2_cache
, offset
);
145 if (request
->l2_table
) {
149 request
->l2_table
= qed_alloc_l2_cache_entry(&s
->l2_cache
);
150 request
->l2_table
->table
= qed_alloc_table(s
);
152 BLKDBG_EVENT(s
->bs
->file
, BLKDBG_L2_LOAD
);
153 ret
= qed_read_table(s
, offset
, request
->l2_table
->table
);
156 /* can't trust loaded L2 table anymore */
157 qed_unref_l2_cache_entry(request
->l2_table
);
158 request
->l2_table
= NULL
;
160 request
->l2_table
->offset
= offset
;
162 qed_commit_l2_cache_entry(&s
->l2_cache
, request
->l2_table
);
164 /* This is guaranteed to succeed because we just committed the entry
167 request
->l2_table
= qed_find_l2_cache_entry(&s
->l2_cache
, offset
);
168 assert(request
->l2_table
!= NULL
);
174 int coroutine_fn
qed_read_l2_table_sync(BDRVQEDState
*s
, QEDRequest
*request
,
177 return qed_read_l2_table(s
, request
, offset
);
180 /* Called with table_lock held. */
181 int coroutine_fn
qed_write_l2_table(BDRVQEDState
*s
, QEDRequest
*request
,
182 unsigned int index
, unsigned int n
,
185 BLKDBG_EVENT(s
->bs
->file
, BLKDBG_L2_UPDATE
);
186 return qed_write_table(s
, request
->l2_table
->offset
,
187 request
->l2_table
->table
, index
, n
, flush
);
190 int coroutine_fn
qed_write_l2_table_sync(BDRVQEDState
*s
, QEDRequest
*request
,
191 unsigned int index
, unsigned int n
,
194 return qed_write_l2_table(s
, request
, index
, n
, flush
);