]>
Commit | Line | Data |
---|---|---|
298800ca SH |
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 | ||
80c71a24 | 15 | #include "qemu/osdep.h" |
298800ca | 16 | #include "trace.h" |
1de7afc9 | 17 | #include "qemu/sockets.h" /* for EINPROGRESS on Windows */ |
298800ca | 18 | #include "qed.h" |
58369e22 | 19 | #include "qemu/bswap.h" |
5df022cf | 20 | #include "qemu/memalign.h" |
298800ca | 21 | |
2fd61638 | 22 | /* Called with table_lock held. */ |
54277a2a VSO |
23 | static int coroutine_fn qed_read_table(BDRVQEDState *s, uint64_t offset, |
24 | QEDTable *table) | |
11273076 | 25 | { |
696e8cb2 VSO |
26 | unsigned int bytes = s->header.cluster_size * s->header.table_size; |
27 | ||
11273076 KW |
28 | int noffsets; |
29 | int i, ret; | |
298800ca | 30 | |
11273076 KW |
31 | trace_qed_read_table(s, offset, table); |
32 | ||
2fd61638 | 33 | qemu_co_mutex_unlock(&s->table_lock); |
696e8cb2 | 34 | ret = bdrv_co_pread(s->bs->file, offset, bytes, table->offsets, 0); |
2fd61638 | 35 | qemu_co_mutex_lock(&s->table_lock); |
11273076 | 36 | if (ret < 0) { |
298800ca SH |
37 | goto out; |
38 | } | |
39 | ||
40 | /* Byteswap offsets */ | |
696e8cb2 | 41 | noffsets = bytes / sizeof(uint64_t); |
298800ca SH |
42 | for (i = 0; i < noffsets; i++) { |
43 | table->offsets[i] = le64_to_cpu(table->offsets[i]); | |
44 | } | |
45 | ||
11273076 | 46 | ret = 0; |
298800ca SH |
47 | out: |
48 | /* Completion */ | |
11273076 | 49 | trace_qed_read_table_cb(s, table, ret); |
f6513529 | 50 | return ret; |
298800ca SH |
51 | } |
52 | ||
298800ca SH |
53 | /** |
54 | * Write out an updated part or all of a table | |
55 | * | |
56 | * @s: QED state | |
57 | * @offset: Offset of table in image file, in bytes | |
58 | * @table: Table | |
59 | * @index: Index of first element | |
60 | * @n: Number of elements | |
61 | * @flush: Whether or not to sync to disk | |
1f01e50b | 62 | * |
2fd61638 | 63 | * Called with table_lock held. |
298800ca | 64 | */ |
54277a2a VSO |
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) | |
298800ca | 68 | { |
298800ca SH |
69 | unsigned int sector_mask = BDRV_SECTOR_SIZE / sizeof(uint64_t) - 1; |
70 | unsigned int start, end, i; | |
602b57fb | 71 | QEDTable *new_table; |
298800ca | 72 | size_t len_bytes; |
602b57fb | 73 | int ret; |
298800ca SH |
74 | |
75 | trace_qed_write_table(s, offset, table, index, n); | |
76 | ||
77 | /* Calculate indices of the first and one after last elements */ | |
78 | start = index & ~sector_mask; | |
79 | end = (index + n + sector_mask) & ~sector_mask; | |
80 | ||
81 | len_bytes = (end - start) * sizeof(uint64_t); | |
82 | ||
602b57fb | 83 | new_table = qemu_blockalign(s->bs, len_bytes); |
298800ca SH |
84 | |
85 | /* Byteswap table */ | |
86 | for (i = start; i < end; i++) { | |
87 | uint64_t le_offset = cpu_to_le64(table->offsets[i]); | |
602b57fb | 88 | new_table->offsets[i - start] = le_offset; |
298800ca SH |
89 | } |
90 | ||
91 | /* Adjust for offset into table */ | |
92 | offset += start * sizeof(uint64_t); | |
93 | ||
2fd61638 | 94 | qemu_co_mutex_unlock(&s->table_lock); |
696e8cb2 | 95 | ret = bdrv_co_pwrite(s->bs->file, offset, len_bytes, new_table->offsets, 0); |
2fd61638 | 96 | qemu_co_mutex_lock(&s->table_lock); |
602b57fb KW |
97 | trace_qed_write_table_cb(s, table, flush, ret); |
98 | if (ret < 0) { | |
99 | goto out; | |
100 | } | |
101 | ||
102 | if (flush) { | |
602b57fb | 103 | ret = bdrv_flush(s->bs); |
602b57fb KW |
104 | if (ret < 0) { |
105 | goto out; | |
106 | } | |
107 | } | |
108 | ||
109 | ret = 0; | |
110 | out: | |
111 | qemu_vfree(new_table); | |
453e53e2 | 112 | return ret; |
298800ca SH |
113 | } |
114 | ||
54277a2a | 115 | int coroutine_fn qed_read_l1_table_sync(BDRVQEDState *s) |
298800ca | 116 | { |
f6513529 | 117 | return qed_read_table(s, s->header.l1_table_offset, s->l1_table); |
298800ca SH |
118 | } |
119 | ||
2fd61638 | 120 | /* Called with table_lock held. */ |
54277a2a VSO |
121 | int coroutine_fn qed_write_l1_table(BDRVQEDState *s, unsigned int index, |
122 | unsigned int n) | |
298800ca SH |
123 | { |
124 | BLKDBG_EVENT(s->bs->file, BLKDBG_L1_UPDATE); | |
453e53e2 KW |
125 | return qed_write_table(s, s->header.l1_table_offset, |
126 | s->l1_table, index, n, false); | |
298800ca SH |
127 | } |
128 | ||
54277a2a VSO |
129 | int coroutine_fn qed_write_l1_table_sync(BDRVQEDState *s, unsigned int index, |
130 | unsigned int n) | |
298800ca | 131 | { |
453e53e2 | 132 | return qed_write_l1_table(s, index, n); |
298800ca SH |
133 | } |
134 | ||
2fd61638 | 135 | /* Called with table_lock held. */ |
54277a2a VSO |
136 | int coroutine_fn qed_read_l2_table(BDRVQEDState *s, QEDRequest *request, |
137 | uint64_t offset) | |
298800ca | 138 | { |
f6513529 | 139 | int ret; |
298800ca SH |
140 | |
141 | qed_unref_l2_cache_entry(request->l2_table); | |
142 | ||
143 | /* Check for cached L2 entry */ | |
144 | request->l2_table = qed_find_l2_cache_entry(&s->l2_cache, offset); | |
145 | if (request->l2_table) { | |
a8165d2d | 146 | return 0; |
298800ca SH |
147 | } |
148 | ||
149 | request->l2_table = qed_alloc_l2_cache_entry(&s->l2_cache); | |
150 | request->l2_table->table = qed_alloc_table(s); | |
151 | ||
298800ca | 152 | BLKDBG_EVENT(s->bs->file, BLKDBG_L2_LOAD); |
f6513529 KW |
153 | ret = qed_read_table(s, offset, request->l2_table->table); |
154 | ||
f6513529 KW |
155 | if (ret) { |
156 | /* can't trust loaded L2 table anymore */ | |
157 | qed_unref_l2_cache_entry(request->l2_table); | |
158 | request->l2_table = NULL; | |
159 | } else { | |
160 | request->l2_table->offset = offset; | |
161 | ||
162 | qed_commit_l2_cache_entry(&s->l2_cache, request->l2_table); | |
163 | ||
164 | /* This is guaranteed to succeed because we just committed the entry | |
165 | * to the cache. | |
166 | */ | |
167 | request->l2_table = qed_find_l2_cache_entry(&s->l2_cache, offset); | |
168 | assert(request->l2_table != NULL); | |
169 | } | |
f6513529 | 170 | |
a8165d2d | 171 | return ret; |
298800ca SH |
172 | } |
173 | ||
54277a2a VSO |
174 | int coroutine_fn qed_read_l2_table_sync(BDRVQEDState *s, QEDRequest *request, |
175 | uint64_t offset) | |
298800ca | 176 | { |
a8165d2d | 177 | return qed_read_l2_table(s, request, offset); |
298800ca SH |
178 | } |
179 | ||
2fd61638 | 180 | /* Called with table_lock held. */ |
54277a2a VSO |
181 | int coroutine_fn qed_write_l2_table(BDRVQEDState *s, QEDRequest *request, |
182 | unsigned int index, unsigned int n, | |
183 | bool flush) | |
298800ca SH |
184 | { |
185 | BLKDBG_EVENT(s->bs->file, BLKDBG_L2_UPDATE); | |
453e53e2 KW |
186 | return qed_write_table(s, request->l2_table->offset, |
187 | request->l2_table->table, index, n, flush); | |
298800ca SH |
188 | } |
189 | ||
54277a2a VSO |
190 | int coroutine_fn qed_write_l2_table_sync(BDRVQEDState *s, QEDRequest *request, |
191 | unsigned int index, unsigned int n, | |
192 | bool flush) | |
298800ca | 193 | { |
453e53e2 | 194 | return qed_write_l2_table(s, request, index, n, flush); |
298800ca | 195 | } |