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"
21 static int qed_read_table(BDRVQEDState
*s
, uint64_t offset
, QEDTable
*table
)
28 .iov_base
= table
->offsets
,
29 .iov_len
= s
->header
.cluster_size
* s
->header
.table_size
,
31 qemu_iovec_init_external(&qiov
, &iov
, 1);
33 trace_qed_read_table(s
, offset
, table
);
35 ret
= bdrv_preadv(s
->bs
->file
, offset
, &qiov
);
40 /* Byteswap offsets */
42 noffsets
= qiov
.size
/ sizeof(uint64_t);
43 for (i
= 0; i
< noffsets
; i
++) {
44 table
->offsets
[i
] = le64_to_cpu(table
->offsets
[i
]);
51 trace_qed_read_table_cb(s
, table
, ret
);
56 * Write out an updated part or all of a table
59 * @offset: Offset of table in image file, in bytes
61 * @index: Index of first element
62 * @n: Number of elements
63 * @flush: Whether or not to sync to disk
65 static int qed_write_table(BDRVQEDState
*s
, uint64_t offset
, QEDTable
*table
,
66 unsigned int index
, unsigned int n
, bool flush
)
68 unsigned int sector_mask
= BDRV_SECTOR_SIZE
/ sizeof(uint64_t) - 1;
69 unsigned int start
, end
, i
;
76 trace_qed_write_table(s
, offset
, table
, index
, n
);
78 /* Calculate indices of the first and one after last elements */
79 start
= index
& ~sector_mask
;
80 end
= (index
+ n
+ sector_mask
) & ~sector_mask
;
82 len_bytes
= (end
- start
) * sizeof(uint64_t);
84 new_table
= qemu_blockalign(s
->bs
, len_bytes
);
85 iov
= (struct iovec
) {
86 .iov_base
= new_table
->offsets
,
89 qemu_iovec_init_external(&qiov
, &iov
, 1);
92 for (i
= start
; i
< end
; i
++) {
93 uint64_t le_offset
= cpu_to_le64(table
->offsets
[i
]);
94 new_table
->offsets
[i
- start
] = le_offset
;
97 /* Adjust for offset into table */
98 offset
+= start
* sizeof(uint64_t);
100 ret
= bdrv_pwritev(s
->bs
->file
, offset
, &qiov
);
101 trace_qed_write_table_cb(s
, table
, flush
, ret
);
108 ret
= bdrv_flush(s
->bs
);
117 qemu_vfree(new_table
);
121 int qed_read_l1_table_sync(BDRVQEDState
*s
)
123 return qed_read_table(s
, s
->header
.l1_table_offset
, s
->l1_table
);
126 int qed_write_l1_table(BDRVQEDState
*s
, unsigned int index
, unsigned int n
)
128 BLKDBG_EVENT(s
->bs
->file
, BLKDBG_L1_UPDATE
);
129 return qed_write_table(s
, s
->header
.l1_table_offset
,
130 s
->l1_table
, index
, n
, false);
133 int qed_write_l1_table_sync(BDRVQEDState
*s
, unsigned int index
,
136 return qed_write_l1_table(s
, index
, n
);
139 int qed_read_l2_table(BDRVQEDState
*s
, QEDRequest
*request
, uint64_t offset
)
143 qed_unref_l2_cache_entry(request
->l2_table
);
145 /* Check for cached L2 entry */
146 request
->l2_table
= qed_find_l2_cache_entry(&s
->l2_cache
, offset
);
147 if (request
->l2_table
) {
151 request
->l2_table
= qed_alloc_l2_cache_entry(&s
->l2_cache
);
152 request
->l2_table
->table
= qed_alloc_table(s
);
154 BLKDBG_EVENT(s
->bs
->file
, BLKDBG_L2_LOAD
);
155 ret
= qed_read_table(s
, offset
, request
->l2_table
->table
);
159 /* can't trust loaded L2 table anymore */
160 qed_unref_l2_cache_entry(request
->l2_table
);
161 request
->l2_table
= NULL
;
163 request
->l2_table
->offset
= offset
;
165 qed_commit_l2_cache_entry(&s
->l2_cache
, request
->l2_table
);
167 /* This is guaranteed to succeed because we just committed the entry
170 request
->l2_table
= qed_find_l2_cache_entry(&s
->l2_cache
, offset
);
171 assert(request
->l2_table
!= NULL
);
178 int qed_read_l2_table_sync(BDRVQEDState
*s
, QEDRequest
*request
, uint64_t offset
)
180 return qed_read_l2_table(s
, request
, offset
);
183 int qed_write_l2_table(BDRVQEDState
*s
, QEDRequest
*request
,
184 unsigned int index
, unsigned int n
, bool flush
)
186 BLKDBG_EVENT(s
->bs
->file
, BLKDBG_L2_UPDATE
);
187 return qed_write_table(s
, request
->l2_table
->offset
,
188 request
->l2_table
->table
, index
, n
, flush
);
191 int qed_write_l2_table_sync(BDRVQEDState
*s
, QEDRequest
*request
,
192 unsigned int index
, unsigned int n
, bool flush
)
194 return qed_write_l2_table(s
, request
, index
, n
, flush
);