root / block / qcow2-cluster.c @ 067d01de
History | View | Annotate | Download (24.2 kB)
1 | 45aba42f | Kevin Wolf | /*
|
---|---|---|---|
2 | 45aba42f | Kevin Wolf | * Block driver for the QCOW version 2 format
|
3 | 45aba42f | Kevin Wolf | *
|
4 | 45aba42f | Kevin Wolf | * Copyright (c) 2004-2006 Fabrice Bellard
|
5 | 45aba42f | Kevin Wolf | *
|
6 | 45aba42f | Kevin Wolf | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 45aba42f | Kevin Wolf | * of this software and associated documentation files (the "Software"), to deal
|
8 | 45aba42f | Kevin Wolf | * in the Software without restriction, including without limitation the rights
|
9 | 45aba42f | Kevin Wolf | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 45aba42f | Kevin Wolf | * copies of the Software, and to permit persons to whom the Software is
|
11 | 45aba42f | Kevin Wolf | * furnished to do so, subject to the following conditions:
|
12 | 45aba42f | Kevin Wolf | *
|
13 | 45aba42f | Kevin Wolf | * The above copyright notice and this permission notice shall be included in
|
14 | 45aba42f | Kevin Wolf | * all copies or substantial portions of the Software.
|
15 | 45aba42f | Kevin Wolf | *
|
16 | 45aba42f | Kevin Wolf | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 45aba42f | Kevin Wolf | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 45aba42f | Kevin Wolf | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 45aba42f | Kevin Wolf | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 45aba42f | Kevin Wolf | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 45aba42f | Kevin Wolf | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 45aba42f | Kevin Wolf | * THE SOFTWARE.
|
23 | 45aba42f | Kevin Wolf | */
|
24 | 45aba42f | Kevin Wolf | |
25 | 45aba42f | Kevin Wolf | #include <zlib.h> |
26 | 45aba42f | Kevin Wolf | |
27 | 45aba42f | Kevin Wolf | #include "qemu-common.h" |
28 | 45aba42f | Kevin Wolf | #include "block_int.h" |
29 | 45aba42f | Kevin Wolf | #include "block/qcow2.h" |
30 | 45aba42f | Kevin Wolf | |
31 | ed6ccf0f | Kevin Wolf | int qcow2_grow_l1_table(BlockDriverState *bs, int min_size) |
32 | 45aba42f | Kevin Wolf | { |
33 | 45aba42f | Kevin Wolf | BDRVQcowState *s = bs->opaque; |
34 | 45aba42f | Kevin Wolf | int new_l1_size, new_l1_size2, ret, i;
|
35 | 45aba42f | Kevin Wolf | uint64_t *new_l1_table; |
36 | 45aba42f | Kevin Wolf | uint64_t new_l1_table_offset; |
37 | 45aba42f | Kevin Wolf | uint8_t data[12];
|
38 | 45aba42f | Kevin Wolf | |
39 | 45aba42f | Kevin Wolf | new_l1_size = s->l1_size; |
40 | 45aba42f | Kevin Wolf | if (min_size <= new_l1_size)
|
41 | 45aba42f | Kevin Wolf | return 0; |
42 | 45aba42f | Kevin Wolf | while (min_size > new_l1_size) {
|
43 | 45aba42f | Kevin Wolf | new_l1_size = (new_l1_size * 3 + 1) / 2; |
44 | 45aba42f | Kevin Wolf | } |
45 | 45aba42f | Kevin Wolf | #ifdef DEBUG_ALLOC2
|
46 | 45aba42f | Kevin Wolf | printf("grow l1_table from %d to %d\n", s->l1_size, new_l1_size);
|
47 | 45aba42f | Kevin Wolf | #endif
|
48 | 45aba42f | Kevin Wolf | |
49 | 45aba42f | Kevin Wolf | new_l1_size2 = sizeof(uint64_t) * new_l1_size;
|
50 | 3f6a3ee5 | Kevin Wolf | new_l1_table = qemu_mallocz(align_offset(new_l1_size2, 512));
|
51 | 45aba42f | Kevin Wolf | memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t));
|
52 | 45aba42f | Kevin Wolf | |
53 | 45aba42f | Kevin Wolf | /* write new table (align to cluster) */
|
54 | ed6ccf0f | Kevin Wolf | new_l1_table_offset = qcow2_alloc_clusters(bs, new_l1_size2); |
55 | 45aba42f | Kevin Wolf | |
56 | 45aba42f | Kevin Wolf | for(i = 0; i < s->l1_size; i++) |
57 | 45aba42f | Kevin Wolf | new_l1_table[i] = cpu_to_be64(new_l1_table[i]); |
58 | 45aba42f | Kevin Wolf | ret = bdrv_pwrite(s->hd, new_l1_table_offset, new_l1_table, new_l1_size2); |
59 | 45aba42f | Kevin Wolf | if (ret != new_l1_size2)
|
60 | 45aba42f | Kevin Wolf | goto fail;
|
61 | 45aba42f | Kevin Wolf | for(i = 0; i < s->l1_size; i++) |
62 | 45aba42f | Kevin Wolf | new_l1_table[i] = be64_to_cpu(new_l1_table[i]); |
63 | 45aba42f | Kevin Wolf | |
64 | 45aba42f | Kevin Wolf | /* set new table */
|
65 | 45aba42f | Kevin Wolf | cpu_to_be32w((uint32_t*)data, new_l1_size); |
66 | 45aba42f | Kevin Wolf | cpu_to_be64w((uint64_t*)(data + 4), new_l1_table_offset);
|
67 | 45aba42f | Kevin Wolf | if (bdrv_pwrite(s->hd, offsetof(QCowHeader, l1_size), data,
|
68 | 45aba42f | Kevin Wolf | sizeof(data)) != sizeof(data)) |
69 | 45aba42f | Kevin Wolf | goto fail;
|
70 | 45aba42f | Kevin Wolf | qemu_free(s->l1_table); |
71 | ed6ccf0f | Kevin Wolf | qcow2_free_clusters(bs, s->l1_table_offset, s->l1_size * sizeof(uint64_t));
|
72 | 45aba42f | Kevin Wolf | s->l1_table_offset = new_l1_table_offset; |
73 | 45aba42f | Kevin Wolf | s->l1_table = new_l1_table; |
74 | 45aba42f | Kevin Wolf | s->l1_size = new_l1_size; |
75 | 45aba42f | Kevin Wolf | return 0; |
76 | 45aba42f | Kevin Wolf | fail:
|
77 | 45aba42f | Kevin Wolf | qemu_free(s->l1_table); |
78 | 45aba42f | Kevin Wolf | return -EIO;
|
79 | 45aba42f | Kevin Wolf | } |
80 | 45aba42f | Kevin Wolf | |
81 | ed6ccf0f | Kevin Wolf | void qcow2_l2_cache_reset(BlockDriverState *bs)
|
82 | 45aba42f | Kevin Wolf | { |
83 | 45aba42f | Kevin Wolf | BDRVQcowState *s = bs->opaque; |
84 | 45aba42f | Kevin Wolf | |
85 | 45aba42f | Kevin Wolf | memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t)); |
86 | 45aba42f | Kevin Wolf | memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t)); |
87 | 45aba42f | Kevin Wolf | memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t)); |
88 | 45aba42f | Kevin Wolf | } |
89 | 45aba42f | Kevin Wolf | |
90 | 45aba42f | Kevin Wolf | static inline int l2_cache_new_entry(BlockDriverState *bs) |
91 | 45aba42f | Kevin Wolf | { |
92 | 45aba42f | Kevin Wolf | BDRVQcowState *s = bs->opaque; |
93 | 45aba42f | Kevin Wolf | uint32_t min_count; |
94 | 45aba42f | Kevin Wolf | int min_index, i;
|
95 | 45aba42f | Kevin Wolf | |
96 | 45aba42f | Kevin Wolf | /* find a new entry in the least used one */
|
97 | 45aba42f | Kevin Wolf | min_index = 0;
|
98 | 45aba42f | Kevin Wolf | min_count = 0xffffffff;
|
99 | 45aba42f | Kevin Wolf | for(i = 0; i < L2_CACHE_SIZE; i++) { |
100 | 45aba42f | Kevin Wolf | if (s->l2_cache_counts[i] < min_count) {
|
101 | 45aba42f | Kevin Wolf | min_count = s->l2_cache_counts[i]; |
102 | 45aba42f | Kevin Wolf | min_index = i; |
103 | 45aba42f | Kevin Wolf | } |
104 | 45aba42f | Kevin Wolf | } |
105 | 45aba42f | Kevin Wolf | return min_index;
|
106 | 45aba42f | Kevin Wolf | } |
107 | 45aba42f | Kevin Wolf | |
108 | 45aba42f | Kevin Wolf | /*
|
109 | 45aba42f | Kevin Wolf | * seek_l2_table
|
110 | 45aba42f | Kevin Wolf | *
|
111 | 45aba42f | Kevin Wolf | * seek l2_offset in the l2_cache table
|
112 | 45aba42f | Kevin Wolf | * if not found, return NULL,
|
113 | 45aba42f | Kevin Wolf | * if found,
|
114 | 45aba42f | Kevin Wolf | * increments the l2 cache hit count of the entry,
|
115 | 45aba42f | Kevin Wolf | * if counter overflow, divide by two all counters
|
116 | 45aba42f | Kevin Wolf | * return the pointer to the l2 cache entry
|
117 | 45aba42f | Kevin Wolf | *
|
118 | 45aba42f | Kevin Wolf | */
|
119 | 45aba42f | Kevin Wolf | |
120 | 45aba42f | Kevin Wolf | static uint64_t *seek_l2_table(BDRVQcowState *s, uint64_t l2_offset)
|
121 | 45aba42f | Kevin Wolf | { |
122 | 45aba42f | Kevin Wolf | int i, j;
|
123 | 45aba42f | Kevin Wolf | |
124 | 45aba42f | Kevin Wolf | for(i = 0; i < L2_CACHE_SIZE; i++) { |
125 | 45aba42f | Kevin Wolf | if (l2_offset == s->l2_cache_offsets[i]) {
|
126 | 45aba42f | Kevin Wolf | /* increment the hit count */
|
127 | 45aba42f | Kevin Wolf | if (++s->l2_cache_counts[i] == 0xffffffff) { |
128 | 45aba42f | Kevin Wolf | for(j = 0; j < L2_CACHE_SIZE; j++) { |
129 | 45aba42f | Kevin Wolf | s->l2_cache_counts[j] >>= 1;
|
130 | 45aba42f | Kevin Wolf | } |
131 | 45aba42f | Kevin Wolf | } |
132 | 45aba42f | Kevin Wolf | return s->l2_cache + (i << s->l2_bits);
|
133 | 45aba42f | Kevin Wolf | } |
134 | 45aba42f | Kevin Wolf | } |
135 | 45aba42f | Kevin Wolf | return NULL; |
136 | 45aba42f | Kevin Wolf | } |
137 | 45aba42f | Kevin Wolf | |
138 | 45aba42f | Kevin Wolf | /*
|
139 | 45aba42f | Kevin Wolf | * l2_load
|
140 | 45aba42f | Kevin Wolf | *
|
141 | 45aba42f | Kevin Wolf | * Loads a L2 table into memory. If the table is in the cache, the cache
|
142 | 45aba42f | Kevin Wolf | * is used; otherwise the L2 table is loaded from the image file.
|
143 | 45aba42f | Kevin Wolf | *
|
144 | 45aba42f | Kevin Wolf | * Returns a pointer to the L2 table on success, or NULL if the read from
|
145 | 45aba42f | Kevin Wolf | * the image file failed.
|
146 | 45aba42f | Kevin Wolf | */
|
147 | 45aba42f | Kevin Wolf | |
148 | 45aba42f | Kevin Wolf | static uint64_t *l2_load(BlockDriverState *bs, uint64_t l2_offset)
|
149 | 45aba42f | Kevin Wolf | { |
150 | 45aba42f | Kevin Wolf | BDRVQcowState *s = bs->opaque; |
151 | 45aba42f | Kevin Wolf | int min_index;
|
152 | 45aba42f | Kevin Wolf | uint64_t *l2_table; |
153 | 45aba42f | Kevin Wolf | |
154 | 45aba42f | Kevin Wolf | /* seek if the table for the given offset is in the cache */
|
155 | 45aba42f | Kevin Wolf | |
156 | 45aba42f | Kevin Wolf | l2_table = seek_l2_table(s, l2_offset); |
157 | 45aba42f | Kevin Wolf | if (l2_table != NULL) |
158 | 45aba42f | Kevin Wolf | return l2_table;
|
159 | 45aba42f | Kevin Wolf | |
160 | 45aba42f | Kevin Wolf | /* not found: load a new entry in the least used one */
|
161 | 45aba42f | Kevin Wolf | |
162 | 45aba42f | Kevin Wolf | min_index = l2_cache_new_entry(bs); |
163 | 45aba42f | Kevin Wolf | l2_table = s->l2_cache + (min_index << s->l2_bits); |
164 | 45aba42f | Kevin Wolf | if (bdrv_pread(s->hd, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) != |
165 | 45aba42f | Kevin Wolf | s->l2_size * sizeof(uint64_t))
|
166 | 45aba42f | Kevin Wolf | return NULL; |
167 | 45aba42f | Kevin Wolf | s->l2_cache_offsets[min_index] = l2_offset; |
168 | 45aba42f | Kevin Wolf | s->l2_cache_counts[min_index] = 1;
|
169 | 45aba42f | Kevin Wolf | |
170 | 45aba42f | Kevin Wolf | return l2_table;
|
171 | 45aba42f | Kevin Wolf | } |
172 | 45aba42f | Kevin Wolf | |
173 | 45aba42f | Kevin Wolf | /*
|
174 | 6583e3c7 | Kevin Wolf | * Writes one sector of the L1 table to the disk (can't update single entries
|
175 | 6583e3c7 | Kevin Wolf | * and we really don't want bdrv_pread to perform a read-modify-write)
|
176 | 6583e3c7 | Kevin Wolf | */
|
177 | 6583e3c7 | Kevin Wolf | #define L1_ENTRIES_PER_SECTOR (512 / 8) |
178 | 6583e3c7 | Kevin Wolf | static int write_l1_entry(BDRVQcowState *s, int l1_index) |
179 | 6583e3c7 | Kevin Wolf | { |
180 | 6583e3c7 | Kevin Wolf | uint64_t buf[L1_ENTRIES_PER_SECTOR]; |
181 | 6583e3c7 | Kevin Wolf | int l1_start_index;
|
182 | 6583e3c7 | Kevin Wolf | int i;
|
183 | 6583e3c7 | Kevin Wolf | |
184 | 6583e3c7 | Kevin Wolf | l1_start_index = l1_index & ~(L1_ENTRIES_PER_SECTOR - 1);
|
185 | 6583e3c7 | Kevin Wolf | for (i = 0; i < L1_ENTRIES_PER_SECTOR; i++) { |
186 | 6583e3c7 | Kevin Wolf | buf[i] = cpu_to_be64(s->l1_table[l1_start_index + i]); |
187 | 6583e3c7 | Kevin Wolf | } |
188 | 6583e3c7 | Kevin Wolf | |
189 | 6583e3c7 | Kevin Wolf | if (bdrv_pwrite(s->hd, s->l1_table_offset + 8 * l1_start_index, |
190 | 6583e3c7 | Kevin Wolf | buf, sizeof(buf)) != sizeof(buf)) |
191 | 6583e3c7 | Kevin Wolf | { |
192 | 6583e3c7 | Kevin Wolf | return -1; |
193 | 6583e3c7 | Kevin Wolf | } |
194 | 6583e3c7 | Kevin Wolf | |
195 | 6583e3c7 | Kevin Wolf | return 0; |
196 | 6583e3c7 | Kevin Wolf | } |
197 | 6583e3c7 | Kevin Wolf | |
198 | 6583e3c7 | Kevin Wolf | /*
|
199 | 45aba42f | Kevin Wolf | * l2_allocate
|
200 | 45aba42f | Kevin Wolf | *
|
201 | 45aba42f | Kevin Wolf | * Allocate a new l2 entry in the file. If l1_index points to an already
|
202 | 45aba42f | Kevin Wolf | * used entry in the L2 table (i.e. we are doing a copy on write for the L2
|
203 | 45aba42f | Kevin Wolf | * table) copy the contents of the old L2 table into the newly allocated one.
|
204 | 45aba42f | Kevin Wolf | * Otherwise the new table is initialized with zeros.
|
205 | 45aba42f | Kevin Wolf | *
|
206 | 45aba42f | Kevin Wolf | */
|
207 | 45aba42f | Kevin Wolf | |
208 | 45aba42f | Kevin Wolf | static uint64_t *l2_allocate(BlockDriverState *bs, int l1_index) |
209 | 45aba42f | Kevin Wolf | { |
210 | 45aba42f | Kevin Wolf | BDRVQcowState *s = bs->opaque; |
211 | 45aba42f | Kevin Wolf | int min_index;
|
212 | 6583e3c7 | Kevin Wolf | uint64_t old_l2_offset; |
213 | 45aba42f | Kevin Wolf | uint64_t *l2_table, l2_offset; |
214 | 45aba42f | Kevin Wolf | |
215 | 45aba42f | Kevin Wolf | old_l2_offset = s->l1_table[l1_index]; |
216 | 45aba42f | Kevin Wolf | |
217 | 45aba42f | Kevin Wolf | /* allocate a new l2 entry */
|
218 | 45aba42f | Kevin Wolf | |
219 | ed6ccf0f | Kevin Wolf | l2_offset = qcow2_alloc_clusters(bs, s->l2_size * sizeof(uint64_t));
|
220 | 45aba42f | Kevin Wolf | |
221 | 45aba42f | Kevin Wolf | /* update the L1 entry */
|
222 | 45aba42f | Kevin Wolf | |
223 | 45aba42f | Kevin Wolf | s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED; |
224 | 6583e3c7 | Kevin Wolf | if (write_l1_entry(s, l1_index) < 0) { |
225 | 45aba42f | Kevin Wolf | return NULL; |
226 | 6583e3c7 | Kevin Wolf | } |
227 | 45aba42f | Kevin Wolf | |
228 | 45aba42f | Kevin Wolf | /* allocate a new entry in the l2 cache */
|
229 | 45aba42f | Kevin Wolf | |
230 | 45aba42f | Kevin Wolf | min_index = l2_cache_new_entry(bs); |
231 | 45aba42f | Kevin Wolf | l2_table = s->l2_cache + (min_index << s->l2_bits); |
232 | 45aba42f | Kevin Wolf | |
233 | 45aba42f | Kevin Wolf | if (old_l2_offset == 0) { |
234 | 45aba42f | Kevin Wolf | /* if there was no old l2 table, clear the new table */
|
235 | 45aba42f | Kevin Wolf | memset(l2_table, 0, s->l2_size * sizeof(uint64_t)); |
236 | 45aba42f | Kevin Wolf | } else {
|
237 | 45aba42f | Kevin Wolf | /* if there was an old l2 table, read it from the disk */
|
238 | 45aba42f | Kevin Wolf | if (bdrv_pread(s->hd, old_l2_offset,
|
239 | 45aba42f | Kevin Wolf | l2_table, s->l2_size * sizeof(uint64_t)) !=
|
240 | 45aba42f | Kevin Wolf | s->l2_size * sizeof(uint64_t))
|
241 | 45aba42f | Kevin Wolf | return NULL; |
242 | 45aba42f | Kevin Wolf | } |
243 | 45aba42f | Kevin Wolf | /* write the l2 table to the file */
|
244 | 45aba42f | Kevin Wolf | if (bdrv_pwrite(s->hd, l2_offset,
|
245 | 45aba42f | Kevin Wolf | l2_table, s->l2_size * sizeof(uint64_t)) !=
|
246 | 45aba42f | Kevin Wolf | s->l2_size * sizeof(uint64_t))
|
247 | 45aba42f | Kevin Wolf | return NULL; |
248 | 45aba42f | Kevin Wolf | |
249 | 45aba42f | Kevin Wolf | /* update the l2 cache entry */
|
250 | 45aba42f | Kevin Wolf | |
251 | 45aba42f | Kevin Wolf | s->l2_cache_offsets[min_index] = l2_offset; |
252 | 45aba42f | Kevin Wolf | s->l2_cache_counts[min_index] = 1;
|
253 | 45aba42f | Kevin Wolf | |
254 | 45aba42f | Kevin Wolf | return l2_table;
|
255 | 45aba42f | Kevin Wolf | } |
256 | 45aba42f | Kevin Wolf | |
257 | 45aba42f | Kevin Wolf | static int count_contiguous_clusters(uint64_t nb_clusters, int cluster_size, |
258 | 45aba42f | Kevin Wolf | uint64_t *l2_table, uint64_t start, uint64_t mask) |
259 | 45aba42f | Kevin Wolf | { |
260 | 45aba42f | Kevin Wolf | int i;
|
261 | 45aba42f | Kevin Wolf | uint64_t offset = be64_to_cpu(l2_table[0]) & ~mask;
|
262 | 45aba42f | Kevin Wolf | |
263 | 45aba42f | Kevin Wolf | if (!offset)
|
264 | 45aba42f | Kevin Wolf | return 0; |
265 | 45aba42f | Kevin Wolf | |
266 | 45aba42f | Kevin Wolf | for (i = start; i < start + nb_clusters; i++)
|
267 | 45aba42f | Kevin Wolf | if (offset + i * cluster_size != (be64_to_cpu(l2_table[i]) & ~mask))
|
268 | 45aba42f | Kevin Wolf | break;
|
269 | 45aba42f | Kevin Wolf | |
270 | 45aba42f | Kevin Wolf | return (i - start);
|
271 | 45aba42f | Kevin Wolf | } |
272 | 45aba42f | Kevin Wolf | |
273 | 45aba42f | Kevin Wolf | static int count_contiguous_free_clusters(uint64_t nb_clusters, uint64_t *l2_table) |
274 | 45aba42f | Kevin Wolf | { |
275 | 45aba42f | Kevin Wolf | int i = 0; |
276 | 45aba42f | Kevin Wolf | |
277 | 45aba42f | Kevin Wolf | while(nb_clusters-- && l2_table[i] == 0) |
278 | 45aba42f | Kevin Wolf | i++; |
279 | 45aba42f | Kevin Wolf | |
280 | 45aba42f | Kevin Wolf | return i;
|
281 | 45aba42f | Kevin Wolf | } |
282 | 45aba42f | Kevin Wolf | |
283 | 45aba42f | Kevin Wolf | /* The crypt function is compatible with the linux cryptoloop
|
284 | 45aba42f | Kevin Wolf | algorithm for < 4 GB images. NOTE: out_buf == in_buf is
|
285 | 45aba42f | Kevin Wolf | supported */
|
286 | ed6ccf0f | Kevin Wolf | void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
|
287 | ed6ccf0f | Kevin Wolf | uint8_t *out_buf, const uint8_t *in_buf,
|
288 | ed6ccf0f | Kevin Wolf | int nb_sectors, int enc, |
289 | ed6ccf0f | Kevin Wolf | const AES_KEY *key)
|
290 | 45aba42f | Kevin Wolf | { |
291 | 45aba42f | Kevin Wolf | union {
|
292 | 45aba42f | Kevin Wolf | uint64_t ll[2];
|
293 | 45aba42f | Kevin Wolf | uint8_t b[16];
|
294 | 45aba42f | Kevin Wolf | } ivec; |
295 | 45aba42f | Kevin Wolf | int i;
|
296 | 45aba42f | Kevin Wolf | |
297 | 45aba42f | Kevin Wolf | for(i = 0; i < nb_sectors; i++) { |
298 | 45aba42f | Kevin Wolf | ivec.ll[0] = cpu_to_le64(sector_num);
|
299 | 45aba42f | Kevin Wolf | ivec.ll[1] = 0; |
300 | 45aba42f | Kevin Wolf | AES_cbc_encrypt(in_buf, out_buf, 512, key,
|
301 | 45aba42f | Kevin Wolf | ivec.b, enc); |
302 | 45aba42f | Kevin Wolf | sector_num++; |
303 | 45aba42f | Kevin Wolf | in_buf += 512;
|
304 | 45aba42f | Kevin Wolf | out_buf += 512;
|
305 | 45aba42f | Kevin Wolf | } |
306 | 45aba42f | Kevin Wolf | } |
307 | 45aba42f | Kevin Wolf | |
308 | 45aba42f | Kevin Wolf | |
309 | 45aba42f | Kevin Wolf | static int qcow_read(BlockDriverState *bs, int64_t sector_num, |
310 | 45aba42f | Kevin Wolf | uint8_t *buf, int nb_sectors)
|
311 | 45aba42f | Kevin Wolf | { |
312 | 45aba42f | Kevin Wolf | BDRVQcowState *s = bs->opaque; |
313 | 45aba42f | Kevin Wolf | int ret, index_in_cluster, n, n1;
|
314 | 45aba42f | Kevin Wolf | uint64_t cluster_offset; |
315 | 45aba42f | Kevin Wolf | |
316 | 45aba42f | Kevin Wolf | while (nb_sectors > 0) { |
317 | 45aba42f | Kevin Wolf | n = nb_sectors; |
318 | ed6ccf0f | Kevin Wolf | cluster_offset = qcow2_get_cluster_offset(bs, sector_num << 9, &n);
|
319 | 45aba42f | Kevin Wolf | index_in_cluster = sector_num & (s->cluster_sectors - 1);
|
320 | 45aba42f | Kevin Wolf | if (!cluster_offset) {
|
321 | 45aba42f | Kevin Wolf | if (bs->backing_hd) {
|
322 | 45aba42f | Kevin Wolf | /* read from the base image */
|
323 | ed6ccf0f | Kevin Wolf | n1 = qcow2_backing_read1(bs->backing_hd, sector_num, buf, n); |
324 | 45aba42f | Kevin Wolf | if (n1 > 0) { |
325 | 45aba42f | Kevin Wolf | ret = bdrv_read(bs->backing_hd, sector_num, buf, n1); |
326 | 45aba42f | Kevin Wolf | if (ret < 0) |
327 | 45aba42f | Kevin Wolf | return -1; |
328 | 45aba42f | Kevin Wolf | } |
329 | 45aba42f | Kevin Wolf | } else {
|
330 | 45aba42f | Kevin Wolf | memset(buf, 0, 512 * n); |
331 | 45aba42f | Kevin Wolf | } |
332 | 45aba42f | Kevin Wolf | } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) { |
333 | ed6ccf0f | Kevin Wolf | if (qcow2_decompress_cluster(s, cluster_offset) < 0) |
334 | 45aba42f | Kevin Wolf | return -1; |
335 | 45aba42f | Kevin Wolf | memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n); |
336 | 45aba42f | Kevin Wolf | } else {
|
337 | 45aba42f | Kevin Wolf | ret = bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512); |
338 | 45aba42f | Kevin Wolf | if (ret != n * 512) |
339 | 45aba42f | Kevin Wolf | return -1; |
340 | 45aba42f | Kevin Wolf | if (s->crypt_method) {
|
341 | ed6ccf0f | Kevin Wolf | qcow2_encrypt_sectors(s, sector_num, buf, buf, n, 0,
|
342 | 45aba42f | Kevin Wolf | &s->aes_decrypt_key); |
343 | 45aba42f | Kevin Wolf | } |
344 | 45aba42f | Kevin Wolf | } |
345 | 45aba42f | Kevin Wolf | nb_sectors -= n; |
346 | 45aba42f | Kevin Wolf | sector_num += n; |
347 | 45aba42f | Kevin Wolf | buf += n * 512;
|
348 | 45aba42f | Kevin Wolf | } |
349 | 45aba42f | Kevin Wolf | return 0; |
350 | 45aba42f | Kevin Wolf | } |
351 | 45aba42f | Kevin Wolf | |
352 | 45aba42f | Kevin Wolf | static int copy_sectors(BlockDriverState *bs, uint64_t start_sect, |
353 | 45aba42f | Kevin Wolf | uint64_t cluster_offset, int n_start, int n_end) |
354 | 45aba42f | Kevin Wolf | { |
355 | 45aba42f | Kevin Wolf | BDRVQcowState *s = bs->opaque; |
356 | 45aba42f | Kevin Wolf | int n, ret;
|
357 | 45aba42f | Kevin Wolf | |
358 | 45aba42f | Kevin Wolf | n = n_end - n_start; |
359 | 45aba42f | Kevin Wolf | if (n <= 0) |
360 | 45aba42f | Kevin Wolf | return 0; |
361 | 45aba42f | Kevin Wolf | ret = qcow_read(bs, start_sect + n_start, s->cluster_data, n); |
362 | 45aba42f | Kevin Wolf | if (ret < 0) |
363 | 45aba42f | Kevin Wolf | return ret;
|
364 | 45aba42f | Kevin Wolf | if (s->crypt_method) {
|
365 | ed6ccf0f | Kevin Wolf | qcow2_encrypt_sectors(s, start_sect + n_start, |
366 | 45aba42f | Kevin Wolf | s->cluster_data, |
367 | 45aba42f | Kevin Wolf | s->cluster_data, n, 1,
|
368 | 45aba42f | Kevin Wolf | &s->aes_encrypt_key); |
369 | 45aba42f | Kevin Wolf | } |
370 | 45aba42f | Kevin Wolf | ret = bdrv_write(s->hd, (cluster_offset >> 9) + n_start,
|
371 | 45aba42f | Kevin Wolf | s->cluster_data, n); |
372 | 45aba42f | Kevin Wolf | if (ret < 0) |
373 | 45aba42f | Kevin Wolf | return ret;
|
374 | 45aba42f | Kevin Wolf | return 0; |
375 | 45aba42f | Kevin Wolf | } |
376 | 45aba42f | Kevin Wolf | |
377 | 45aba42f | Kevin Wolf | |
378 | 45aba42f | Kevin Wolf | /*
|
379 | 45aba42f | Kevin Wolf | * get_cluster_offset
|
380 | 45aba42f | Kevin Wolf | *
|
381 | 45aba42f | Kevin Wolf | * For a given offset of the disk image, return cluster offset in
|
382 | 45aba42f | Kevin Wolf | * qcow2 file.
|
383 | 45aba42f | Kevin Wolf | *
|
384 | 45aba42f | Kevin Wolf | * on entry, *num is the number of contiguous clusters we'd like to
|
385 | 45aba42f | Kevin Wolf | * access following offset.
|
386 | 45aba42f | Kevin Wolf | *
|
387 | 45aba42f | Kevin Wolf | * on exit, *num is the number of contiguous clusters we can read.
|
388 | 45aba42f | Kevin Wolf | *
|
389 | 45aba42f | Kevin Wolf | * Return 1, if the offset is found
|
390 | 45aba42f | Kevin Wolf | * Return 0, otherwise.
|
391 | 45aba42f | Kevin Wolf | *
|
392 | 45aba42f | Kevin Wolf | */
|
393 | 45aba42f | Kevin Wolf | |
394 | ed6ccf0f | Kevin Wolf | uint64_t qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset, |
395 | ed6ccf0f | Kevin Wolf | int *num)
|
396 | 45aba42f | Kevin Wolf | { |
397 | 45aba42f | Kevin Wolf | BDRVQcowState *s = bs->opaque; |
398 | 45aba42f | Kevin Wolf | int l1_index, l2_index;
|
399 | 45aba42f | Kevin Wolf | uint64_t l2_offset, *l2_table, cluster_offset; |
400 | 45aba42f | Kevin Wolf | int l1_bits, c;
|
401 | 45aba42f | Kevin Wolf | int index_in_cluster, nb_available, nb_needed, nb_clusters;
|
402 | 45aba42f | Kevin Wolf | |
403 | 45aba42f | Kevin Wolf | index_in_cluster = (offset >> 9) & (s->cluster_sectors - 1); |
404 | 45aba42f | Kevin Wolf | nb_needed = *num + index_in_cluster; |
405 | 45aba42f | Kevin Wolf | |
406 | 45aba42f | Kevin Wolf | l1_bits = s->l2_bits + s->cluster_bits; |
407 | 45aba42f | Kevin Wolf | |
408 | 45aba42f | Kevin Wolf | /* compute how many bytes there are between the offset and
|
409 | 45aba42f | Kevin Wolf | * the end of the l1 entry
|
410 | 45aba42f | Kevin Wolf | */
|
411 | 45aba42f | Kevin Wolf | |
412 | 45aba42f | Kevin Wolf | nb_available = (1 << l1_bits) - (offset & ((1 << l1_bits) - 1)); |
413 | 45aba42f | Kevin Wolf | |
414 | 45aba42f | Kevin Wolf | /* compute the number of available sectors */
|
415 | 45aba42f | Kevin Wolf | |
416 | 45aba42f | Kevin Wolf | nb_available = (nb_available >> 9) + index_in_cluster;
|
417 | 45aba42f | Kevin Wolf | |
418 | 45aba42f | Kevin Wolf | if (nb_needed > nb_available) {
|
419 | 45aba42f | Kevin Wolf | nb_needed = nb_available; |
420 | 45aba42f | Kevin Wolf | } |
421 | 45aba42f | Kevin Wolf | |
422 | 45aba42f | Kevin Wolf | cluster_offset = 0;
|
423 | 45aba42f | Kevin Wolf | |
424 | 45aba42f | Kevin Wolf | /* seek the the l2 offset in the l1 table */
|
425 | 45aba42f | Kevin Wolf | |
426 | 45aba42f | Kevin Wolf | l1_index = offset >> l1_bits; |
427 | 45aba42f | Kevin Wolf | if (l1_index >= s->l1_size)
|
428 | 45aba42f | Kevin Wolf | goto out;
|
429 | 45aba42f | Kevin Wolf | |
430 | 45aba42f | Kevin Wolf | l2_offset = s->l1_table[l1_index]; |
431 | 45aba42f | Kevin Wolf | |
432 | 45aba42f | Kevin Wolf | /* seek the l2 table of the given l2 offset */
|
433 | 45aba42f | Kevin Wolf | |
434 | 45aba42f | Kevin Wolf | if (!l2_offset)
|
435 | 45aba42f | Kevin Wolf | goto out;
|
436 | 45aba42f | Kevin Wolf | |
437 | 45aba42f | Kevin Wolf | /* load the l2 table in memory */
|
438 | 45aba42f | Kevin Wolf | |
439 | 45aba42f | Kevin Wolf | l2_offset &= ~QCOW_OFLAG_COPIED; |
440 | 45aba42f | Kevin Wolf | l2_table = l2_load(bs, l2_offset); |
441 | 45aba42f | Kevin Wolf | if (l2_table == NULL) |
442 | 45aba42f | Kevin Wolf | return 0; |
443 | 45aba42f | Kevin Wolf | |
444 | 45aba42f | Kevin Wolf | /* find the cluster offset for the given disk offset */
|
445 | 45aba42f | Kevin Wolf | |
446 | 45aba42f | Kevin Wolf | l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
|
447 | 45aba42f | Kevin Wolf | cluster_offset = be64_to_cpu(l2_table[l2_index]); |
448 | 45aba42f | Kevin Wolf | nb_clusters = size_to_clusters(s, nb_needed << 9);
|
449 | 45aba42f | Kevin Wolf | |
450 | 45aba42f | Kevin Wolf | if (!cluster_offset) {
|
451 | 45aba42f | Kevin Wolf | /* how many empty clusters ? */
|
452 | 45aba42f | Kevin Wolf | c = count_contiguous_free_clusters(nb_clusters, &l2_table[l2_index]); |
453 | 45aba42f | Kevin Wolf | } else {
|
454 | 45aba42f | Kevin Wolf | /* how many allocated clusters ? */
|
455 | 45aba42f | Kevin Wolf | c = count_contiguous_clusters(nb_clusters, s->cluster_size, |
456 | 45aba42f | Kevin Wolf | &l2_table[l2_index], 0, QCOW_OFLAG_COPIED);
|
457 | 45aba42f | Kevin Wolf | } |
458 | 45aba42f | Kevin Wolf | |
459 | 45aba42f | Kevin Wolf | nb_available = (c * s->cluster_sectors); |
460 | 45aba42f | Kevin Wolf | out:
|
461 | 45aba42f | Kevin Wolf | if (nb_available > nb_needed)
|
462 | 45aba42f | Kevin Wolf | nb_available = nb_needed; |
463 | 45aba42f | Kevin Wolf | |
464 | 45aba42f | Kevin Wolf | *num = nb_available - index_in_cluster; |
465 | 45aba42f | Kevin Wolf | |
466 | 45aba42f | Kevin Wolf | return cluster_offset & ~QCOW_OFLAG_COPIED;
|
467 | 45aba42f | Kevin Wolf | } |
468 | 45aba42f | Kevin Wolf | |
469 | 45aba42f | Kevin Wolf | /*
|
470 | 45aba42f | Kevin Wolf | * get_cluster_table
|
471 | 45aba42f | Kevin Wolf | *
|
472 | 45aba42f | Kevin Wolf | * for a given disk offset, load (and allocate if needed)
|
473 | 45aba42f | Kevin Wolf | * the l2 table.
|
474 | 45aba42f | Kevin Wolf | *
|
475 | 45aba42f | Kevin Wolf | * the l2 table offset in the qcow2 file and the cluster index
|
476 | 45aba42f | Kevin Wolf | * in the l2 table are given to the caller.
|
477 | 45aba42f | Kevin Wolf | *
|
478 | 45aba42f | Kevin Wolf | */
|
479 | 45aba42f | Kevin Wolf | |
480 | 45aba42f | Kevin Wolf | static int get_cluster_table(BlockDriverState *bs, uint64_t offset, |
481 | 45aba42f | Kevin Wolf | uint64_t **new_l2_table, |
482 | 45aba42f | Kevin Wolf | uint64_t *new_l2_offset, |
483 | 45aba42f | Kevin Wolf | int *new_l2_index)
|
484 | 45aba42f | Kevin Wolf | { |
485 | 45aba42f | Kevin Wolf | BDRVQcowState *s = bs->opaque; |
486 | 45aba42f | Kevin Wolf | int l1_index, l2_index, ret;
|
487 | 45aba42f | Kevin Wolf | uint64_t l2_offset, *l2_table; |
488 | 45aba42f | Kevin Wolf | |
489 | 45aba42f | Kevin Wolf | /* seek the the l2 offset in the l1 table */
|
490 | 45aba42f | Kevin Wolf | |
491 | 45aba42f | Kevin Wolf | l1_index = offset >> (s->l2_bits + s->cluster_bits); |
492 | 45aba42f | Kevin Wolf | if (l1_index >= s->l1_size) {
|
493 | ed6ccf0f | Kevin Wolf | ret = qcow2_grow_l1_table(bs, l1_index + 1);
|
494 | 45aba42f | Kevin Wolf | if (ret < 0) |
495 | 45aba42f | Kevin Wolf | return 0; |
496 | 45aba42f | Kevin Wolf | } |
497 | 45aba42f | Kevin Wolf | l2_offset = s->l1_table[l1_index]; |
498 | 45aba42f | Kevin Wolf | |
499 | 45aba42f | Kevin Wolf | /* seek the l2 table of the given l2 offset */
|
500 | 45aba42f | Kevin Wolf | |
501 | 45aba42f | Kevin Wolf | if (l2_offset & QCOW_OFLAG_COPIED) {
|
502 | 45aba42f | Kevin Wolf | /* load the l2 table in memory */
|
503 | 45aba42f | Kevin Wolf | l2_offset &= ~QCOW_OFLAG_COPIED; |
504 | 45aba42f | Kevin Wolf | l2_table = l2_load(bs, l2_offset); |
505 | 45aba42f | Kevin Wolf | if (l2_table == NULL) |
506 | 45aba42f | Kevin Wolf | return 0; |
507 | 45aba42f | Kevin Wolf | } else {
|
508 | 45aba42f | Kevin Wolf | if (l2_offset)
|
509 | ed6ccf0f | Kevin Wolf | qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t));
|
510 | 45aba42f | Kevin Wolf | l2_table = l2_allocate(bs, l1_index); |
511 | 45aba42f | Kevin Wolf | if (l2_table == NULL) |
512 | 45aba42f | Kevin Wolf | return 0; |
513 | 45aba42f | Kevin Wolf | l2_offset = s->l1_table[l1_index] & ~QCOW_OFLAG_COPIED; |
514 | 45aba42f | Kevin Wolf | } |
515 | 45aba42f | Kevin Wolf | |
516 | 45aba42f | Kevin Wolf | /* find the cluster offset for the given disk offset */
|
517 | 45aba42f | Kevin Wolf | |
518 | 45aba42f | Kevin Wolf | l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
|
519 | 45aba42f | Kevin Wolf | |
520 | 45aba42f | Kevin Wolf | *new_l2_table = l2_table; |
521 | 45aba42f | Kevin Wolf | *new_l2_offset = l2_offset; |
522 | 45aba42f | Kevin Wolf | *new_l2_index = l2_index; |
523 | 45aba42f | Kevin Wolf | |
524 | 45aba42f | Kevin Wolf | return 1; |
525 | 45aba42f | Kevin Wolf | } |
526 | 45aba42f | Kevin Wolf | |
527 | 45aba42f | Kevin Wolf | /*
|
528 | 45aba42f | Kevin Wolf | * alloc_compressed_cluster_offset
|
529 | 45aba42f | Kevin Wolf | *
|
530 | 45aba42f | Kevin Wolf | * For a given offset of the disk image, return cluster offset in
|
531 | 45aba42f | Kevin Wolf | * qcow2 file.
|
532 | 45aba42f | Kevin Wolf | *
|
533 | 45aba42f | Kevin Wolf | * If the offset is not found, allocate a new compressed cluster.
|
534 | 45aba42f | Kevin Wolf | *
|
535 | 45aba42f | Kevin Wolf | * Return the cluster offset if successful,
|
536 | 45aba42f | Kevin Wolf | * Return 0, otherwise.
|
537 | 45aba42f | Kevin Wolf | *
|
538 | 45aba42f | Kevin Wolf | */
|
539 | 45aba42f | Kevin Wolf | |
540 | ed6ccf0f | Kevin Wolf | uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs, |
541 | ed6ccf0f | Kevin Wolf | uint64_t offset, |
542 | ed6ccf0f | Kevin Wolf | int compressed_size)
|
543 | 45aba42f | Kevin Wolf | { |
544 | 45aba42f | Kevin Wolf | BDRVQcowState *s = bs->opaque; |
545 | 45aba42f | Kevin Wolf | int l2_index, ret;
|
546 | 45aba42f | Kevin Wolf | uint64_t l2_offset, *l2_table, cluster_offset; |
547 | 45aba42f | Kevin Wolf | int nb_csectors;
|
548 | 45aba42f | Kevin Wolf | |
549 | 45aba42f | Kevin Wolf | ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index); |
550 | 45aba42f | Kevin Wolf | if (ret == 0) |
551 | 45aba42f | Kevin Wolf | return 0; |
552 | 45aba42f | Kevin Wolf | |
553 | 45aba42f | Kevin Wolf | cluster_offset = be64_to_cpu(l2_table[l2_index]); |
554 | 45aba42f | Kevin Wolf | if (cluster_offset & QCOW_OFLAG_COPIED)
|
555 | 45aba42f | Kevin Wolf | return cluster_offset & ~QCOW_OFLAG_COPIED;
|
556 | 45aba42f | Kevin Wolf | |
557 | 45aba42f | Kevin Wolf | if (cluster_offset)
|
558 | ed6ccf0f | Kevin Wolf | qcow2_free_any_clusters(bs, cluster_offset, 1);
|
559 | 45aba42f | Kevin Wolf | |
560 | ed6ccf0f | Kevin Wolf | cluster_offset = qcow2_alloc_bytes(bs, compressed_size); |
561 | 45aba42f | Kevin Wolf | nb_csectors = ((cluster_offset + compressed_size - 1) >> 9) - |
562 | 45aba42f | Kevin Wolf | (cluster_offset >> 9);
|
563 | 45aba42f | Kevin Wolf | |
564 | 45aba42f | Kevin Wolf | cluster_offset |= QCOW_OFLAG_COMPRESSED | |
565 | 45aba42f | Kevin Wolf | ((uint64_t)nb_csectors << s->csize_shift); |
566 | 45aba42f | Kevin Wolf | |
567 | 45aba42f | Kevin Wolf | /* update L2 table */
|
568 | 45aba42f | Kevin Wolf | |
569 | 45aba42f | Kevin Wolf | /* compressed clusters never have the copied flag */
|
570 | 45aba42f | Kevin Wolf | |
571 | 45aba42f | Kevin Wolf | l2_table[l2_index] = cpu_to_be64(cluster_offset); |
572 | 45aba42f | Kevin Wolf | if (bdrv_pwrite(s->hd,
|
573 | 45aba42f | Kevin Wolf | l2_offset + l2_index * sizeof(uint64_t),
|
574 | 45aba42f | Kevin Wolf | l2_table + l2_index, |
575 | 45aba42f | Kevin Wolf | sizeof(uint64_t)) != sizeof(uint64_t)) |
576 | 45aba42f | Kevin Wolf | return 0; |
577 | 45aba42f | Kevin Wolf | |
578 | 45aba42f | Kevin Wolf | return cluster_offset;
|
579 | 45aba42f | Kevin Wolf | } |
580 | 45aba42f | Kevin Wolf | |
581 | 4c1612d9 | Kevin Wolf | /*
|
582 | 4c1612d9 | Kevin Wolf | * Write L2 table updates to disk, writing whole sectors to avoid a
|
583 | 4c1612d9 | Kevin Wolf | * read-modify-write in bdrv_pwrite
|
584 | 4c1612d9 | Kevin Wolf | */
|
585 | 4c1612d9 | Kevin Wolf | #define L2_ENTRIES_PER_SECTOR (512 / 8) |
586 | 4c1612d9 | Kevin Wolf | static int write_l2_entries(BDRVQcowState *s, uint64_t *l2_table, |
587 | 4c1612d9 | Kevin Wolf | uint64_t l2_offset, int l2_index, int num) |
588 | 4c1612d9 | Kevin Wolf | { |
589 | 4c1612d9 | Kevin Wolf | int l2_start_index = l2_index & ~(L1_ENTRIES_PER_SECTOR - 1); |
590 | 4c1612d9 | Kevin Wolf | int start_offset = (8 * l2_index) & ~511; |
591 | 4c1612d9 | Kevin Wolf | int end_offset = (8 * (l2_index + num) + 511) & ~511; |
592 | 4c1612d9 | Kevin Wolf | size_t len = end_offset - start_offset; |
593 | 4c1612d9 | Kevin Wolf | |
594 | 4c1612d9 | Kevin Wolf | if (bdrv_pwrite(s->hd, l2_offset + start_offset, &l2_table[l2_start_index],
|
595 | 4c1612d9 | Kevin Wolf | len) != len) |
596 | 4c1612d9 | Kevin Wolf | { |
597 | 4c1612d9 | Kevin Wolf | return -1; |
598 | 4c1612d9 | Kevin Wolf | } |
599 | 4c1612d9 | Kevin Wolf | |
600 | 4c1612d9 | Kevin Wolf | return 0; |
601 | 4c1612d9 | Kevin Wolf | } |
602 | 4c1612d9 | Kevin Wolf | |
603 | ed6ccf0f | Kevin Wolf | int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, uint64_t cluster_offset,
|
604 | 45aba42f | Kevin Wolf | QCowL2Meta *m) |
605 | 45aba42f | Kevin Wolf | { |
606 | 45aba42f | Kevin Wolf | BDRVQcowState *s = bs->opaque; |
607 | 45aba42f | Kevin Wolf | int i, j = 0, l2_index, ret; |
608 | 45aba42f | Kevin Wolf | uint64_t *old_cluster, start_sect, l2_offset, *l2_table; |
609 | 45aba42f | Kevin Wolf | |
610 | 45aba42f | Kevin Wolf | if (m->nb_clusters == 0) |
611 | 45aba42f | Kevin Wolf | return 0; |
612 | 45aba42f | Kevin Wolf | |
613 | 45aba42f | Kevin Wolf | old_cluster = qemu_malloc(m->nb_clusters * sizeof(uint64_t));
|
614 | 45aba42f | Kevin Wolf | |
615 | 45aba42f | Kevin Wolf | /* copy content of unmodified sectors */
|
616 | 45aba42f | Kevin Wolf | start_sect = (m->offset & ~(s->cluster_size - 1)) >> 9; |
617 | 45aba42f | Kevin Wolf | if (m->n_start) {
|
618 | 45aba42f | Kevin Wolf | ret = copy_sectors(bs, start_sect, cluster_offset, 0, m->n_start);
|
619 | 45aba42f | Kevin Wolf | if (ret < 0) |
620 | 45aba42f | Kevin Wolf | goto err;
|
621 | 45aba42f | Kevin Wolf | } |
622 | 45aba42f | Kevin Wolf | |
623 | 45aba42f | Kevin Wolf | if (m->nb_available & (s->cluster_sectors - 1)) { |
624 | 45aba42f | Kevin Wolf | uint64_t end = m->nb_available & ~(uint64_t)(s->cluster_sectors - 1);
|
625 | 45aba42f | Kevin Wolf | ret = copy_sectors(bs, start_sect + end, cluster_offset + (end << 9),
|
626 | 45aba42f | Kevin Wolf | m->nb_available - end, s->cluster_sectors); |
627 | 45aba42f | Kevin Wolf | if (ret < 0) |
628 | 45aba42f | Kevin Wolf | goto err;
|
629 | 45aba42f | Kevin Wolf | } |
630 | 45aba42f | Kevin Wolf | |
631 | 45aba42f | Kevin Wolf | ret = -EIO; |
632 | 45aba42f | Kevin Wolf | /* update L2 table */
|
633 | 45aba42f | Kevin Wolf | if (!get_cluster_table(bs, m->offset, &l2_table, &l2_offset, &l2_index))
|
634 | 45aba42f | Kevin Wolf | goto err;
|
635 | 45aba42f | Kevin Wolf | |
636 | 45aba42f | Kevin Wolf | for (i = 0; i < m->nb_clusters; i++) { |
637 | 45aba42f | Kevin Wolf | /* if two concurrent writes happen to the same unallocated cluster
|
638 | 45aba42f | Kevin Wolf | * each write allocates separate cluster and writes data concurrently.
|
639 | 45aba42f | Kevin Wolf | * The first one to complete updates l2 table with pointer to its
|
640 | 45aba42f | Kevin Wolf | * cluster the second one has to do RMW (which is done above by
|
641 | 45aba42f | Kevin Wolf | * copy_sectors()), update l2 table with its cluster pointer and free
|
642 | 45aba42f | Kevin Wolf | * old cluster. This is what this loop does */
|
643 | 45aba42f | Kevin Wolf | if(l2_table[l2_index + i] != 0) |
644 | 45aba42f | Kevin Wolf | old_cluster[j++] = l2_table[l2_index + i]; |
645 | 45aba42f | Kevin Wolf | |
646 | 45aba42f | Kevin Wolf | l2_table[l2_index + i] = cpu_to_be64((cluster_offset + |
647 | 45aba42f | Kevin Wolf | (i << s->cluster_bits)) | QCOW_OFLAG_COPIED); |
648 | 45aba42f | Kevin Wolf | } |
649 | 45aba42f | Kevin Wolf | |
650 | 4c1612d9 | Kevin Wolf | if (write_l2_entries(s, l2_table, l2_offset, l2_index, m->nb_clusters) < 0) { |
651 | 4c1612d9 | Kevin Wolf | ret = -1;
|
652 | 45aba42f | Kevin Wolf | goto err;
|
653 | 4c1612d9 | Kevin Wolf | } |
654 | 45aba42f | Kevin Wolf | |
655 | 45aba42f | Kevin Wolf | for (i = 0; i < j; i++) |
656 | ed6ccf0f | Kevin Wolf | qcow2_free_any_clusters(bs, |
657 | ed6ccf0f | Kevin Wolf | be64_to_cpu(old_cluster[i]) & ~QCOW_OFLAG_COPIED, 1);
|
658 | 45aba42f | Kevin Wolf | |
659 | 45aba42f | Kevin Wolf | ret = 0;
|
660 | 45aba42f | Kevin Wolf | err:
|
661 | 45aba42f | Kevin Wolf | qemu_free(old_cluster); |
662 | 45aba42f | Kevin Wolf | return ret;
|
663 | 45aba42f | Kevin Wolf | } |
664 | 45aba42f | Kevin Wolf | |
665 | 45aba42f | Kevin Wolf | /*
|
666 | 45aba42f | Kevin Wolf | * alloc_cluster_offset
|
667 | 45aba42f | Kevin Wolf | *
|
668 | 45aba42f | Kevin Wolf | * For a given offset of the disk image, return cluster offset in
|
669 | 45aba42f | Kevin Wolf | * qcow2 file.
|
670 | 45aba42f | Kevin Wolf | *
|
671 | 45aba42f | Kevin Wolf | * If the offset is not found, allocate a new cluster.
|
672 | 45aba42f | Kevin Wolf | *
|
673 | 45aba42f | Kevin Wolf | * Return the cluster offset if successful,
|
674 | 45aba42f | Kevin Wolf | * Return 0, otherwise.
|
675 | 45aba42f | Kevin Wolf | *
|
676 | 45aba42f | Kevin Wolf | */
|
677 | 45aba42f | Kevin Wolf | |
678 | ed6ccf0f | Kevin Wolf | uint64_t qcow2_alloc_cluster_offset(BlockDriverState *bs, |
679 | ed6ccf0f | Kevin Wolf | uint64_t offset, |
680 | ed6ccf0f | Kevin Wolf | int n_start, int n_end, |
681 | ed6ccf0f | Kevin Wolf | int *num, QCowL2Meta *m)
|
682 | 45aba42f | Kevin Wolf | { |
683 | 45aba42f | Kevin Wolf | BDRVQcowState *s = bs->opaque; |
684 | 45aba42f | Kevin Wolf | int l2_index, ret;
|
685 | 45aba42f | Kevin Wolf | uint64_t l2_offset, *l2_table, cluster_offset; |
686 | 45aba42f | Kevin Wolf | int nb_clusters, i = 0; |
687 | f214978a | Kevin Wolf | QCowL2Meta *old_alloc; |
688 | 45aba42f | Kevin Wolf | |
689 | 45aba42f | Kevin Wolf | ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index); |
690 | 45aba42f | Kevin Wolf | if (ret == 0) |
691 | 45aba42f | Kevin Wolf | return 0; |
692 | 45aba42f | Kevin Wolf | |
693 | 45aba42f | Kevin Wolf | nb_clusters = size_to_clusters(s, n_end << 9);
|
694 | 45aba42f | Kevin Wolf | |
695 | 45aba42f | Kevin Wolf | nb_clusters = MIN(nb_clusters, s->l2_size - l2_index); |
696 | 45aba42f | Kevin Wolf | |
697 | 45aba42f | Kevin Wolf | cluster_offset = be64_to_cpu(l2_table[l2_index]); |
698 | 45aba42f | Kevin Wolf | |
699 | 45aba42f | Kevin Wolf | /* We keep all QCOW_OFLAG_COPIED clusters */
|
700 | 45aba42f | Kevin Wolf | |
701 | 45aba42f | Kevin Wolf | if (cluster_offset & QCOW_OFLAG_COPIED) {
|
702 | 45aba42f | Kevin Wolf | nb_clusters = count_contiguous_clusters(nb_clusters, s->cluster_size, |
703 | 45aba42f | Kevin Wolf | &l2_table[l2_index], 0, 0); |
704 | 45aba42f | Kevin Wolf | |
705 | 45aba42f | Kevin Wolf | cluster_offset &= ~QCOW_OFLAG_COPIED; |
706 | 45aba42f | Kevin Wolf | m->nb_clusters = 0;
|
707 | 45aba42f | Kevin Wolf | |
708 | 45aba42f | Kevin Wolf | goto out;
|
709 | 45aba42f | Kevin Wolf | } |
710 | 45aba42f | Kevin Wolf | |
711 | 45aba42f | Kevin Wolf | /* for the moment, multiple compressed clusters are not managed */
|
712 | 45aba42f | Kevin Wolf | |
713 | 45aba42f | Kevin Wolf | if (cluster_offset & QCOW_OFLAG_COMPRESSED)
|
714 | 45aba42f | Kevin Wolf | nb_clusters = 1;
|
715 | 45aba42f | Kevin Wolf | |
716 | 45aba42f | Kevin Wolf | /* how many available clusters ? */
|
717 | 45aba42f | Kevin Wolf | |
718 | 45aba42f | Kevin Wolf | while (i < nb_clusters) {
|
719 | 45aba42f | Kevin Wolf | i += count_contiguous_clusters(nb_clusters - i, s->cluster_size, |
720 | 45aba42f | Kevin Wolf | &l2_table[l2_index], i, 0);
|
721 | 45aba42f | Kevin Wolf | |
722 | 45aba42f | Kevin Wolf | if(be64_to_cpu(l2_table[l2_index + i]))
|
723 | 45aba42f | Kevin Wolf | break;
|
724 | 45aba42f | Kevin Wolf | |
725 | 45aba42f | Kevin Wolf | i += count_contiguous_free_clusters(nb_clusters - i, |
726 | 45aba42f | Kevin Wolf | &l2_table[l2_index + i]); |
727 | 45aba42f | Kevin Wolf | |
728 | 45aba42f | Kevin Wolf | cluster_offset = be64_to_cpu(l2_table[l2_index + i]); |
729 | 45aba42f | Kevin Wolf | |
730 | 45aba42f | Kevin Wolf | if ((cluster_offset & QCOW_OFLAG_COPIED) ||
|
731 | 45aba42f | Kevin Wolf | (cluster_offset & QCOW_OFLAG_COMPRESSED)) |
732 | 45aba42f | Kevin Wolf | break;
|
733 | 45aba42f | Kevin Wolf | } |
734 | 45aba42f | Kevin Wolf | nb_clusters = i; |
735 | 45aba42f | Kevin Wolf | |
736 | f214978a | Kevin Wolf | /*
|
737 | f214978a | Kevin Wolf | * Check if there already is an AIO write request in flight which allocates
|
738 | f214978a | Kevin Wolf | * the same cluster. In this case we need to wait until the previous
|
739 | f214978a | Kevin Wolf | * request has completed and updated the L2 table accordingly.
|
740 | f214978a | Kevin Wolf | */
|
741 | 72cf2d4f | Blue Swirl | QLIST_FOREACH(old_alloc, &s->cluster_allocs, next_in_flight) { |
742 | f214978a | Kevin Wolf | |
743 | f214978a | Kevin Wolf | uint64_t end_offset = offset + nb_clusters * s->cluster_size; |
744 | f214978a | Kevin Wolf | uint64_t old_offset = old_alloc->offset; |
745 | f214978a | Kevin Wolf | uint64_t old_end_offset = old_alloc->offset + |
746 | f214978a | Kevin Wolf | old_alloc->nb_clusters * s->cluster_size; |
747 | f214978a | Kevin Wolf | |
748 | f214978a | Kevin Wolf | if (end_offset < old_offset || offset > old_end_offset) {
|
749 | f214978a | Kevin Wolf | /* No intersection */
|
750 | f214978a | Kevin Wolf | } else {
|
751 | f214978a | Kevin Wolf | if (offset < old_offset) {
|
752 | f214978a | Kevin Wolf | /* Stop at the start of a running allocation */
|
753 | f214978a | Kevin Wolf | nb_clusters = (old_offset - offset) >> s->cluster_bits; |
754 | f214978a | Kevin Wolf | } else {
|
755 | f214978a | Kevin Wolf | nb_clusters = 0;
|
756 | f214978a | Kevin Wolf | } |
757 | f214978a | Kevin Wolf | |
758 | f214978a | Kevin Wolf | if (nb_clusters == 0) { |
759 | f214978a | Kevin Wolf | /* Set dependency and wait for a callback */
|
760 | f214978a | Kevin Wolf | m->depends_on = old_alloc; |
761 | f214978a | Kevin Wolf | m->nb_clusters = 0;
|
762 | f214978a | Kevin Wolf | *num = 0;
|
763 | f214978a | Kevin Wolf | return 0; |
764 | f214978a | Kevin Wolf | } |
765 | f214978a | Kevin Wolf | } |
766 | f214978a | Kevin Wolf | } |
767 | f214978a | Kevin Wolf | |
768 | f214978a | Kevin Wolf | if (!nb_clusters) {
|
769 | f214978a | Kevin Wolf | abort(); |
770 | f214978a | Kevin Wolf | } |
771 | f214978a | Kevin Wolf | |
772 | 72cf2d4f | Blue Swirl | QLIST_INSERT_HEAD(&s->cluster_allocs, m, next_in_flight); |
773 | f214978a | Kevin Wolf | |
774 | 45aba42f | Kevin Wolf | /* allocate a new cluster */
|
775 | 45aba42f | Kevin Wolf | |
776 | ed6ccf0f | Kevin Wolf | cluster_offset = qcow2_alloc_clusters(bs, nb_clusters * s->cluster_size); |
777 | 45aba42f | Kevin Wolf | |
778 | 45aba42f | Kevin Wolf | /* save info needed for meta data update */
|
779 | 45aba42f | Kevin Wolf | m->offset = offset; |
780 | 45aba42f | Kevin Wolf | m->n_start = n_start; |
781 | 45aba42f | Kevin Wolf | m->nb_clusters = nb_clusters; |
782 | 45aba42f | Kevin Wolf | |
783 | 45aba42f | Kevin Wolf | out:
|
784 | 45aba42f | Kevin Wolf | m->nb_available = MIN(nb_clusters << (s->cluster_bits - 9), n_end);
|
785 | 45aba42f | Kevin Wolf | |
786 | 45aba42f | Kevin Wolf | *num = m->nb_available - n_start; |
787 | 45aba42f | Kevin Wolf | |
788 | 45aba42f | Kevin Wolf | return cluster_offset;
|
789 | 45aba42f | Kevin Wolf | } |
790 | 45aba42f | Kevin Wolf | |
791 | 45aba42f | Kevin Wolf | static int decompress_buffer(uint8_t *out_buf, int out_buf_size, |
792 | 45aba42f | Kevin Wolf | const uint8_t *buf, int buf_size) |
793 | 45aba42f | Kevin Wolf | { |
794 | 45aba42f | Kevin Wolf | z_stream strm1, *strm = &strm1; |
795 | 45aba42f | Kevin Wolf | int ret, out_len;
|
796 | 45aba42f | Kevin Wolf | |
797 | 45aba42f | Kevin Wolf | memset(strm, 0, sizeof(*strm)); |
798 | 45aba42f | Kevin Wolf | |
799 | 45aba42f | Kevin Wolf | strm->next_in = (uint8_t *)buf; |
800 | 45aba42f | Kevin Wolf | strm->avail_in = buf_size; |
801 | 45aba42f | Kevin Wolf | strm->next_out = out_buf; |
802 | 45aba42f | Kevin Wolf | strm->avail_out = out_buf_size; |
803 | 45aba42f | Kevin Wolf | |
804 | 45aba42f | Kevin Wolf | ret = inflateInit2(strm, -12);
|
805 | 45aba42f | Kevin Wolf | if (ret != Z_OK)
|
806 | 45aba42f | Kevin Wolf | return -1; |
807 | 45aba42f | Kevin Wolf | ret = inflate(strm, Z_FINISH); |
808 | 45aba42f | Kevin Wolf | out_len = strm->next_out - out_buf; |
809 | 45aba42f | Kevin Wolf | if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
|
810 | 45aba42f | Kevin Wolf | out_len != out_buf_size) { |
811 | 45aba42f | Kevin Wolf | inflateEnd(strm); |
812 | 45aba42f | Kevin Wolf | return -1; |
813 | 45aba42f | Kevin Wolf | } |
814 | 45aba42f | Kevin Wolf | inflateEnd(strm); |
815 | 45aba42f | Kevin Wolf | return 0; |
816 | 45aba42f | Kevin Wolf | } |
817 | 45aba42f | Kevin Wolf | |
818 | ed6ccf0f | Kevin Wolf | int qcow2_decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset)
|
819 | 45aba42f | Kevin Wolf | { |
820 | 45aba42f | Kevin Wolf | int ret, csize, nb_csectors, sector_offset;
|
821 | 45aba42f | Kevin Wolf | uint64_t coffset; |
822 | 45aba42f | Kevin Wolf | |
823 | 45aba42f | Kevin Wolf | coffset = cluster_offset & s->cluster_offset_mask; |
824 | 45aba42f | Kevin Wolf | if (s->cluster_cache_offset != coffset) {
|
825 | 45aba42f | Kevin Wolf | nb_csectors = ((cluster_offset >> s->csize_shift) & s->csize_mask) + 1;
|
826 | 45aba42f | Kevin Wolf | sector_offset = coffset & 511;
|
827 | 45aba42f | Kevin Wolf | csize = nb_csectors * 512 - sector_offset;
|
828 | 45aba42f | Kevin Wolf | ret = bdrv_read(s->hd, coffset >> 9, s->cluster_data, nb_csectors);
|
829 | 45aba42f | Kevin Wolf | if (ret < 0) { |
830 | 45aba42f | Kevin Wolf | return -1; |
831 | 45aba42f | Kevin Wolf | } |
832 | 45aba42f | Kevin Wolf | if (decompress_buffer(s->cluster_cache, s->cluster_size,
|
833 | 45aba42f | Kevin Wolf | s->cluster_data + sector_offset, csize) < 0) {
|
834 | 45aba42f | Kevin Wolf | return -1; |
835 | 45aba42f | Kevin Wolf | } |
836 | 45aba42f | Kevin Wolf | s->cluster_cache_offset = coffset; |
837 | 45aba42f | Kevin Wolf | } |
838 | 45aba42f | Kevin Wolf | return 0; |
839 | 45aba42f | Kevin Wolf | } |