]>
Commit | Line | Data |
---|---|---|
6ada7453 TS |
1 | /* |
2 | * Block driver for Parallels disk image format | |
3 | * | |
4 | * Copyright (c) 2007 Alex Beregszaszi | |
cc5690f2 | 5 | * Copyright (c) 2015 Denis V. Lunev <den@openvz.org> |
6ada7453 | 6 | * |
cc5690f2 DL |
7 | * This code was originally based on comparing different disk images created |
8 | * by Parallels. Currently it is based on opened OpenVZ sources | |
9 | * available at | |
10 | * http://git.openvz.org/?p=ploop;a=summary | |
6ada7453 TS |
11 | * |
12 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
13 | * of this software and associated documentation files (the "Software"), to deal | |
14 | * in the Software without restriction, including without limitation the rights | |
15 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
16 | * copies of the Software, and to permit persons to whom the Software is | |
17 | * furnished to do so, subject to the following conditions: | |
18 | * | |
19 | * The above copyright notice and this permission notice shall be included in | |
20 | * all copies or substantial portions of the Software. | |
21 | * | |
22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
23 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
24 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
25 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
26 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
27 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
28 | * THE SOFTWARE. | |
29 | */ | |
922a01a0 | 30 | |
80c71a24 | 31 | #include "qemu/osdep.h" |
baefd977 | 32 | #include "qemu/error-report.h" |
da34e65c | 33 | #include "qapi/error.h" |
737e150e | 34 | #include "block/block_int.h" |
609f45ea | 35 | #include "block/qdict.h" |
8942764f | 36 | #include "sysemu/block-backend.h" |
1de7afc9 | 37 | #include "qemu/module.h" |
922a01a0 | 38 | #include "qemu/option.h" |
1511b490 KW |
39 | #include "qapi/qmp/qdict.h" |
40 | #include "qapi/qobject-input-visitor.h" | |
41 | #include "qapi/qapi-visit-block-core.h" | |
58369e22 | 42 | #include "qemu/bswap.h" |
0d31c7c2 | 43 | #include "qemu/bitmap.h" |
5df022cf | 44 | #include "qemu/memalign.h" |
1d0f37cf | 45 | #include "migration/blocker.h" |
90fe66f0 | 46 | #include "parallels.h" |
6ada7453 TS |
47 | |
48 | /**************************************************************/ | |
49 | ||
50 | #define HEADER_MAGIC "WithoutFreeSpace" | |
d25d5980 | 51 | #define HEADER_MAGIC2 "WithouFreSpacExt" |
6ada7453 | 52 | #define HEADER_VERSION 2 |
6dd6b9f1 | 53 | #define HEADER_INUSE_MAGIC (0x746F6E59) |
555a608c | 54 | #define MAX_PARALLELS_IMAGE_FACTOR (1ull << 32) |
6ada7453 | 55 | |
f7abe0ec MAL |
56 | static QEnumLookup prealloc_mode_lookup = { |
57 | .array = (const char *const[]) { | |
58 | "falloc", | |
59 | "truncate", | |
f7abe0ec MAL |
60 | }, |
61 | .size = PRL_PREALLOC_MODE__MAX | |
d6179011 DL |
62 | }; |
63 | ||
d6179011 DL |
64 | #define PARALLELS_OPT_PREALLOC_MODE "prealloc-mode" |
65 | #define PARALLELS_OPT_PREALLOC_SIZE "prealloc-size" | |
66 | ||
67 | static QemuOptsList parallels_runtime_opts = { | |
68 | .name = "parallels", | |
69 | .head = QTAILQ_HEAD_INITIALIZER(parallels_runtime_opts.head), | |
70 | .desc = { | |
71 | { | |
72 | .name = PARALLELS_OPT_PREALLOC_SIZE, | |
73 | .type = QEMU_OPT_SIZE, | |
74 | .help = "Preallocation size on image expansion", | |
ff5bbe56 | 75 | .def_value_str = "128M", |
d6179011 DL |
76 | }, |
77 | { | |
78 | .name = PARALLELS_OPT_PREALLOC_MODE, | |
79 | .type = QEMU_OPT_STRING, | |
80 | .help = "Preallocation mode on image expansion " | |
81 | "(allowed values: falloc, truncate)", | |
82 | .def_value_str = "falloc", | |
83 | }, | |
84 | { /* end of list */ }, | |
85 | }, | |
86 | }; | |
87 | ||
1511b490 KW |
88 | static QemuOptsList parallels_create_opts = { |
89 | .name = "parallels-create-opts", | |
90 | .head = QTAILQ_HEAD_INITIALIZER(parallels_create_opts.head), | |
91 | .desc = { | |
92 | { | |
93 | .name = BLOCK_OPT_SIZE, | |
94 | .type = QEMU_OPT_SIZE, | |
95 | .help = "Virtual disk size", | |
96 | }, | |
97 | { | |
98 | .name = BLOCK_OPT_CLUSTER_SIZE, | |
99 | .type = QEMU_OPT_SIZE, | |
100 | .help = "Parallels image cluster size", | |
101 | .def_value_str = stringify(DEFAULT_CLUSTER_SIZE), | |
102 | }, | |
103 | { /* end of list */ } | |
104 | } | |
105 | }; | |
106 | ||
d6179011 | 107 | |
555cc9d9 DL |
108 | static int64_t bat2sect(BDRVParallelsState *s, uint32_t idx) |
109 | { | |
dd97cdc0 | 110 | return (uint64_t)le32_to_cpu(s->bat_bitmap[idx]) * s->off_multiplier; |
555cc9d9 DL |
111 | } |
112 | ||
2d68e22e DL |
113 | static uint32_t bat_entry_off(uint32_t idx) |
114 | { | |
115 | return sizeof(ParallelsHeader) + sizeof(uint32_t) * idx; | |
116 | } | |
117 | ||
29442569 | 118 | static int64_t seek_to_sector(BDRVParallelsState *s, int64_t sector_num) |
6ada7453 | 119 | { |
c34d2451 | 120 | uint32_t index, offset; |
6ada7453 TS |
121 | |
122 | index = sector_num / s->tracks; | |
123 | offset = sector_num % s->tracks; | |
124 | ||
9d8b88f6 | 125 | /* not allocated */ |
369f7de9 | 126 | if ((index >= s->bat_size) || (s->bat_bitmap[index] == 0)) { |
f08e2f84 | 127 | return -1; |
369f7de9 | 128 | } |
555cc9d9 | 129 | return bat2sect(s, index) + offset; |
6ada7453 TS |
130 | } |
131 | ||
9de9da17 RK |
132 | static int cluster_remainder(BDRVParallelsState *s, int64_t sector_num, |
133 | int nb_sectors) | |
134 | { | |
135 | int ret = s->tracks - sector_num % s->tracks; | |
136 | return MIN(nb_sectors, ret); | |
137 | } | |
138 | ||
6bb8bc63 AI |
139 | static uint32_t host_cluster_index(BDRVParallelsState *s, int64_t off) |
140 | { | |
141 | off -= s->data_start << BDRV_SECTOR_BITS; | |
142 | return off / s->cluster_size; | |
143 | } | |
144 | ||
6953d920 DL |
145 | static int64_t block_status(BDRVParallelsState *s, int64_t sector_num, |
146 | int nb_sectors, int *pnum) | |
147 | { | |
148 | int64_t start_off = -2, prev_end_off = -2; | |
149 | ||
150 | *pnum = 0; | |
151 | while (nb_sectors > 0 || start_off == -2) { | |
152 | int64_t offset = seek_to_sector(s, sector_num); | |
153 | int to_end; | |
154 | ||
155 | if (start_off == -2) { | |
156 | start_off = offset; | |
157 | prev_end_off = offset; | |
158 | } else if (offset != prev_end_off) { | |
159 | break; | |
160 | } | |
161 | ||
162 | to_end = cluster_remainder(s, sector_num, nb_sectors); | |
163 | nb_sectors -= to_end; | |
164 | sector_num += to_end; | |
165 | *pnum += to_end; | |
166 | ||
167 | if (offset > 0) { | |
168 | prev_end_off += to_end; | |
169 | } | |
170 | } | |
171 | return start_off; | |
172 | } | |
173 | ||
b64b29b9 AI |
174 | static void parallels_set_bat_entry(BDRVParallelsState *s, |
175 | uint32_t index, uint32_t offset) | |
176 | { | |
177 | s->bat_bitmap[index] = cpu_to_le32(offset); | |
178 | bitmap_set(s->bat_dirty_bmap, bat_entry_off(index) / s->bat_dirty_block, 1); | |
179 | } | |
180 | ||
a398275e DL |
181 | static int mark_used(BlockDriverState *bs, |
182 | unsigned long *bitmap, uint32_t bitmap_size, int64_t off) | |
183 | { | |
184 | BDRVParallelsState *s = bs->opaque; | |
185 | uint32_t cluster_index = host_cluster_index(s, off); | |
186 | if (cluster_index >= bitmap_size) { | |
187 | return -E2BIG; | |
188 | } | |
189 | if (test_bit(cluster_index, bitmap)) { | |
190 | return -EBUSY; | |
191 | } | |
192 | bitmap_set(bitmap, cluster_index, 1); | |
193 | return 0; | |
194 | } | |
195 | ||
c2b8e315 KW |
196 | static int64_t coroutine_fn GRAPH_RDLOCK |
197 | allocate_clusters(BlockDriverState *bs, int64_t sector_num, | |
198 | int nb_sectors, int *pnum) | |
5a41e1fa | 199 | { |
bda4cdcb | 200 | int ret = 0; |
5a41e1fa | 201 | BDRVParallelsState *s = bs->opaque; |
d8b83e37 | 202 | int64_t pos, space, idx, to_allocate, i, len; |
5a41e1fa | 203 | |
ddd2ef2c DL |
204 | pos = block_status(s, sector_num, nb_sectors, pnum); |
205 | if (pos > 0) { | |
206 | return pos; | |
207 | } | |
5a41e1fa | 208 | |
ddd2ef2c | 209 | idx = sector_num / s->tracks; |
969401fe | 210 | to_allocate = DIV_ROUND_UP(sector_num + *pnum, s->tracks) - idx; |
86d1bd70 | 211 | |
a338dcbb AI |
212 | /* |
213 | * This function is called only by parallels_co_writev(), which will never | |
86d1bd70 HR |
214 | * pass a sector_num at or beyond the end of the image (because the block |
215 | * layer never passes such a sector_num to that function). Therefore, idx | |
216 | * is always below s->bat_size. | |
217 | * block_status() will limit *pnum so that sector_num + *pnum will not | |
218 | * exceed the image end. Therefore, idx + to_allocate cannot exceed | |
219 | * s->bat_size. | |
220 | * Note that s->bat_size is an unsigned int, therefore idx + to_allocate | |
a338dcbb AI |
221 | * will always fit into a uint32_t. |
222 | */ | |
86d1bd70 HR |
223 | assert(idx < s->bat_size && idx + to_allocate <= s->bat_size); |
224 | ||
ddd2ef2c | 225 | space = to_allocate * s->tracks; |
0af02bd1 | 226 | len = bdrv_co_getlength(bs->file->bs); |
d8b83e37 DL |
227 | if (len < 0) { |
228 | return len; | |
229 | } | |
230 | if (s->data_end + space > (len >> BDRV_SECTOR_BITS)) { | |
ddd2ef2c | 231 | space += s->prealloc_size; |
bda4cdcb EB |
232 | /* |
233 | * We require the expanded size to read back as zero. If the | |
234 | * user permitted truncation, we try that; but if it fails, we | |
235 | * force the safer-but-slower fallocate. | |
236 | */ | |
237 | if (s->prealloc_mode == PRL_PREALLOC_MODE_TRUNCATE) { | |
50688be0 AF |
238 | ret = bdrv_co_truncate(bs->file, |
239 | (s->data_end + space) << BDRV_SECTOR_BITS, | |
240 | false, PREALLOC_MODE_OFF, | |
241 | BDRV_REQ_ZERO_WRITE, NULL); | |
bda4cdcb EB |
242 | if (ret == -ENOTSUP) { |
243 | s->prealloc_mode = PRL_PREALLOC_MODE_FALLOCATE; | |
244 | } | |
245 | } | |
19f5dc15 | 246 | if (s->prealloc_mode == PRL_PREALLOC_MODE_FALLOCATE) { |
50688be0 AF |
247 | ret = bdrv_co_pwrite_zeroes(bs->file, |
248 | s->data_end << BDRV_SECTOR_BITS, | |
249 | space << BDRV_SECTOR_BITS, 0); | |
19f5dc15 DL |
250 | } |
251 | if (ret < 0) { | |
252 | return ret; | |
253 | } | |
5a41e1fa DL |
254 | } |
255 | ||
a338dcbb AI |
256 | /* |
257 | * Try to read from backing to fill empty clusters | |
bcbb3866 EK |
258 | * FIXME: 1. previous write_zeroes may be redundant |
259 | * 2. most of data we read from backing will be rewritten by | |
260 | * parallels_co_writev. On aligned-to-cluster write we do not need | |
261 | * this read at all. | |
262 | * 3. it would be good to combine write of data from backing and new | |
a338dcbb AI |
263 | * data into one write call. |
264 | */ | |
bcbb3866 EK |
265 | if (bs->backing) { |
266 | int64_t nb_cow_sectors = to_allocate * s->tracks; | |
267 | int64_t nb_cow_bytes = nb_cow_sectors << BDRV_SECTOR_BITS; | |
a4072543 | 268 | void *buf = qemu_blockalign(bs, nb_cow_bytes); |
bcbb3866 | 269 | |
a4072543 VSO |
270 | ret = bdrv_co_pread(bs->backing, idx * s->tracks * BDRV_SECTOR_SIZE, |
271 | nb_cow_bytes, buf, 0); | |
bcbb3866 | 272 | if (ret < 0) { |
a4072543 | 273 | qemu_vfree(buf); |
bcbb3866 EK |
274 | return ret; |
275 | } | |
276 | ||
eba088f9 HR |
277 | ret = bdrv_co_pwrite(bs->file, s->data_end * BDRV_SECTOR_SIZE, |
278 | nb_cow_bytes, buf, 0); | |
a4072543 | 279 | qemu_vfree(buf); |
bcbb3866 EK |
280 | if (ret < 0) { |
281 | return ret; | |
282 | } | |
283 | } | |
284 | ||
ddd2ef2c | 285 | for (i = 0; i < to_allocate; i++) { |
b64b29b9 | 286 | parallels_set_bat_entry(s, idx + i, s->data_end / s->off_multiplier); |
ddd2ef2c | 287 | s->data_end += s->tracks; |
ddd2ef2c | 288 | } |
0d31c7c2 | 289 | |
ddd2ef2c | 290 | return bat2sect(s, idx) + sector_num % s->tracks; |
5a41e1fa DL |
291 | } |
292 | ||
0d31c7c2 | 293 | |
b9b10c35 KW |
294 | static int coroutine_fn GRAPH_RDLOCK |
295 | parallels_co_flush_to_os(BlockDriverState *bs) | |
0d31c7c2 DL |
296 | { |
297 | BDRVParallelsState *s = bs->opaque; | |
298 | unsigned long size = DIV_ROUND_UP(s->header_size, s->bat_dirty_block); | |
299 | unsigned long bit; | |
300 | ||
301 | qemu_co_mutex_lock(&s->lock); | |
302 | ||
303 | bit = find_first_bit(s->bat_dirty_bmap, size); | |
304 | while (bit < size) { | |
305 | uint32_t off = bit * s->bat_dirty_block; | |
306 | uint32_t to_write = s->bat_dirty_block; | |
307 | int ret; | |
308 | ||
309 | if (off + to_write > s->header_size) { | |
310 | to_write = s->header_size - off; | |
311 | } | |
50688be0 AF |
312 | ret = bdrv_co_pwrite(bs->file, off, to_write, |
313 | (uint8_t *)s->header + off, 0); | |
0d31c7c2 DL |
314 | if (ret < 0) { |
315 | qemu_co_mutex_unlock(&s->lock); | |
316 | return ret; | |
317 | } | |
318 | bit = find_next_bit(s->bat_dirty_bmap, size, bit + 1); | |
319 | } | |
320 | bitmap_zero(s->bat_dirty_bmap, size); | |
321 | ||
322 | qemu_co_mutex_unlock(&s->lock); | |
323 | return 0; | |
324 | } | |
325 | ||
326 | ||
8e0cf59d EB |
327 | static int coroutine_fn parallels_co_block_status(BlockDriverState *bs, |
328 | bool want_zero, | |
329 | int64_t offset, | |
330 | int64_t bytes, | |
331 | int64_t *pnum, | |
332 | int64_t *map, | |
333 | BlockDriverState **file) | |
dd3bed16 RK |
334 | { |
335 | BDRVParallelsState *s = bs->opaque; | |
8e0cf59d | 336 | int count; |
dd3bed16 | 337 | |
8e0cf59d | 338 | assert(QEMU_IS_ALIGNED(offset | bytes, BDRV_SECTOR_SIZE)); |
dd3bed16 | 339 | qemu_co_mutex_lock(&s->lock); |
8e0cf59d EB |
340 | offset = block_status(s, offset >> BDRV_SECTOR_BITS, |
341 | bytes >> BDRV_SECTOR_BITS, &count); | |
dd3bed16 RK |
342 | qemu_co_mutex_unlock(&s->lock); |
343 | ||
8e0cf59d | 344 | *pnum = count * BDRV_SECTOR_SIZE; |
dd3bed16 RK |
345 | if (offset < 0) { |
346 | return 0; | |
347 | } | |
348 | ||
8e0cf59d | 349 | *map = offset * BDRV_SECTOR_SIZE; |
ddf4987d | 350 | *file = bs->file->bs; |
8e0cf59d | 351 | return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID; |
dd3bed16 RK |
352 | } |
353 | ||
7b1fb72e KW |
354 | static int coroutine_fn GRAPH_RDLOCK |
355 | parallels_co_writev(BlockDriverState *bs, int64_t sector_num, int nb_sectors, | |
356 | QEMUIOVector *qiov, int flags) | |
5a41e1fa DL |
357 | { |
358 | BDRVParallelsState *s = bs->opaque; | |
359 | uint64_t bytes_done = 0; | |
360 | QEMUIOVector hd_qiov; | |
361 | int ret = 0; | |
362 | ||
363 | qemu_iovec_init(&hd_qiov, qiov->niov); | |
364 | ||
365 | while (nb_sectors > 0) { | |
366 | int64_t position; | |
367 | int n, nbytes; | |
368 | ||
369 | qemu_co_mutex_lock(&s->lock); | |
ddd2ef2c | 370 | position = allocate_clusters(bs, sector_num, nb_sectors, &n); |
5a41e1fa DL |
371 | qemu_co_mutex_unlock(&s->lock); |
372 | if (position < 0) { | |
373 | ret = (int)position; | |
374 | break; | |
375 | } | |
376 | ||
5a41e1fa DL |
377 | nbytes = n << BDRV_SECTOR_BITS; |
378 | ||
379 | qemu_iovec_reset(&hd_qiov); | |
380 | qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes); | |
381 | ||
d08c2a24 EB |
382 | ret = bdrv_co_pwritev(bs->file, position * BDRV_SECTOR_SIZE, nbytes, |
383 | &hd_qiov, 0); | |
5a41e1fa DL |
384 | if (ret < 0) { |
385 | break; | |
386 | } | |
387 | ||
388 | nb_sectors -= n; | |
389 | sector_num += n; | |
390 | bytes_done += nbytes; | |
391 | } | |
392 | ||
393 | qemu_iovec_destroy(&hd_qiov); | |
394 | return ret; | |
395 | } | |
396 | ||
b9b10c35 KW |
397 | static int coroutine_fn GRAPH_RDLOCK |
398 | parallels_co_readv(BlockDriverState *bs, int64_t sector_num, int nb_sectors, | |
399 | QEMUIOVector *qiov) | |
6ada7453 | 400 | { |
29442569 | 401 | BDRVParallelsState *s = bs->opaque; |
481fb9cf DL |
402 | uint64_t bytes_done = 0; |
403 | QEMUIOVector hd_qiov; | |
404 | int ret = 0; | |
405 | ||
406 | qemu_iovec_init(&hd_qiov, qiov->niov); | |
29442569 | 407 | |
6ada7453 | 408 | while (nb_sectors > 0) { |
481fb9cf DL |
409 | int64_t position; |
410 | int n, nbytes; | |
411 | ||
412 | qemu_co_mutex_lock(&s->lock); | |
6953d920 | 413 | position = block_status(s, sector_num, nb_sectors, &n); |
481fb9cf DL |
414 | qemu_co_mutex_unlock(&s->lock); |
415 | ||
481fb9cf DL |
416 | nbytes = n << BDRV_SECTOR_BITS; |
417 | ||
bcbb3866 EK |
418 | qemu_iovec_reset(&hd_qiov); |
419 | qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes); | |
420 | ||
481fb9cf | 421 | if (position < 0) { |
bcbb3866 | 422 | if (bs->backing) { |
d08c2a24 EB |
423 | ret = bdrv_co_preadv(bs->backing, sector_num * BDRV_SECTOR_SIZE, |
424 | nbytes, &hd_qiov, 0); | |
bcbb3866 EK |
425 | if (ret < 0) { |
426 | break; | |
427 | } | |
428 | } else { | |
429 | qemu_iovec_memset(&hd_qiov, 0, 0, nbytes); | |
430 | } | |
481fb9cf | 431 | } else { |
d08c2a24 EB |
432 | ret = bdrv_co_preadv(bs->file, position * BDRV_SECTOR_SIZE, nbytes, |
433 | &hd_qiov, 0); | |
29442569 | 434 | if (ret < 0) { |
481fb9cf | 435 | break; |
29442569 | 436 | } |
9d8b88f6 | 437 | } |
481fb9cf | 438 | |
9de9da17 RK |
439 | nb_sectors -= n; |
440 | sector_num += n; | |
481fb9cf | 441 | bytes_done += nbytes; |
6ada7453 | 442 | } |
6ada7453 | 443 | |
481fb9cf | 444 | qemu_iovec_destroy(&hd_qiov); |
2914caa0 PB |
445 | return ret; |
446 | } | |
447 | ||
96de69c7 AI |
448 | static void parallels_check_unclean(BlockDriverState *bs, |
449 | BdrvCheckResult *res, | |
450 | BdrvCheckMode fix) | |
451 | { | |
452 | BDRVParallelsState *s = bs->opaque; | |
453 | ||
454 | if (!s->header_unclean) { | |
455 | return; | |
456 | } | |
457 | ||
458 | fprintf(stderr, "%s image was not closed correctly\n", | |
459 | fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR"); | |
460 | res->corruptions++; | |
461 | if (fix & BDRV_FIX_ERRORS) { | |
462 | /* parallels_close will do the job right */ | |
463 | res->corruptions_fixed++; | |
464 | s->header_unclean = false; | |
465 | } | |
466 | } | |
49ad6467 | 467 | |
8cd19203 AI |
468 | /* |
469 | * Returns true if data_off is correct, otherwise false. In both cases | |
470 | * correct_offset is set to the proper value. | |
471 | */ | |
472 | static bool parallels_test_data_off(BDRVParallelsState *s, | |
473 | int64_t file_nb_sectors, | |
474 | uint32_t *correct_offset) | |
475 | { | |
476 | uint32_t data_off, min_off; | |
477 | bool old_magic; | |
478 | ||
479 | /* | |
480 | * There are two slightly different image formats: with "WithoutFreeSpace" | |
481 | * or "WithouFreSpacExt" magic words. Call the first one as "old magic". | |
482 | * In such images data_off field can be zero. In this case the offset is | |
483 | * calculated as the end of BAT table plus some padding to ensure sector | |
484 | * size alignment. | |
485 | */ | |
486 | old_magic = !memcmp(s->header->magic, HEADER_MAGIC, 16); | |
487 | ||
488 | min_off = DIV_ROUND_UP(bat_entry_off(s->bat_size), BDRV_SECTOR_SIZE); | |
489 | if (!old_magic) { | |
490 | min_off = ROUND_UP(min_off, s->cluster_size / BDRV_SECTOR_SIZE); | |
491 | } | |
492 | ||
493 | if (correct_offset) { | |
494 | *correct_offset = min_off; | |
495 | } | |
496 | ||
497 | data_off = le32_to_cpu(s->header->data_off); | |
498 | if (data_off == 0 && old_magic) { | |
499 | return true; | |
500 | } | |
501 | ||
502 | if (data_off < min_off || data_off > file_nb_sectors) { | |
503 | return false; | |
504 | } | |
505 | ||
506 | if (correct_offset) { | |
507 | *correct_offset = data_off; | |
508 | } | |
509 | ||
510 | return true; | |
511 | } | |
512 | ||
513 | static int coroutine_fn GRAPH_RDLOCK | |
514 | parallels_check_data_off(BlockDriverState *bs, BdrvCheckResult *res, | |
515 | BdrvCheckMode fix) | |
516 | { | |
517 | BDRVParallelsState *s = bs->opaque; | |
518 | int64_t file_size; | |
519 | uint32_t data_off; | |
520 | ||
521 | file_size = bdrv_co_nb_sectors(bs->file->bs); | |
522 | if (file_size < 0) { | |
523 | res->check_errors++; | |
524 | return file_size; | |
525 | } | |
526 | ||
527 | if (parallels_test_data_off(s, file_size, &data_off)) { | |
528 | return 0; | |
529 | } | |
530 | ||
531 | res->corruptions++; | |
532 | if (fix & BDRV_FIX_ERRORS) { | |
533 | s->header->data_off = cpu_to_le32(data_off); | |
534 | res->corruptions_fixed++; | |
535 | } | |
536 | ||
537 | fprintf(stderr, "%s data_off field has incorrect value\n", | |
538 | fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR"); | |
539 | ||
540 | return 0; | |
541 | } | |
542 | ||
6d416e56 AI |
543 | static int coroutine_fn GRAPH_RDLOCK |
544 | parallels_check_outside_image(BlockDriverState *bs, BdrvCheckResult *res, | |
545 | BdrvCheckMode fix) | |
546 | { | |
547 | BDRVParallelsState *s = bs->opaque; | |
548 | uint32_t i; | |
549 | int64_t off, high_off, size; | |
550 | ||
0af02bd1 | 551 | size = bdrv_co_getlength(bs->file->bs); |
6d416e56 AI |
552 | if (size < 0) { |
553 | res->check_errors++; | |
554 | return size; | |
555 | } | |
556 | ||
557 | high_off = 0; | |
558 | for (i = 0; i < s->bat_size; i++) { | |
559 | off = bat2sect(s, i) << BDRV_SECTOR_BITS; | |
029136f2 | 560 | if (off + s->cluster_size > size) { |
6d416e56 AI |
561 | fprintf(stderr, "%s cluster %u is outside image\n", |
562 | fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR", i); | |
563 | res->corruptions++; | |
564 | if (fix & BDRV_FIX_ERRORS) { | |
565 | parallels_set_bat_entry(s, i, 0); | |
566 | res->corruptions_fixed++; | |
567 | } | |
568 | continue; | |
569 | } | |
570 | if (high_off < off) { | |
571 | high_off = off; | |
572 | } | |
573 | } | |
574 | ||
575 | if (high_off == 0) { | |
576 | res->image_end_offset = s->data_end << BDRV_SECTOR_BITS; | |
577 | } else { | |
578 | res->image_end_offset = high_off + s->cluster_size; | |
579 | s->data_end = res->image_end_offset >> BDRV_SECTOR_BITS; | |
580 | } | |
581 | ||
582 | return 0; | |
583 | } | |
584 | ||
c2b8e315 | 585 | static int coroutine_fn GRAPH_RDLOCK |
09a21edf | 586 | parallels_check_leak(BlockDriverState *bs, BdrvCheckResult *res, |
728e1017 | 587 | BdrvCheckMode fix, bool explicit) |
49ad6467 DL |
588 | { |
589 | BDRVParallelsState *s = bs->opaque; | |
09a21edf | 590 | int64_t size; |
6d416e56 | 591 | int ret; |
49ad6467 | 592 | |
7c5f86d5 | 593 | size = bdrv_co_getlength(bs->file->bs); |
49ad6467 DL |
594 | if (size < 0) { |
595 | res->check_errors++; | |
596 | return size; | |
597 | } | |
598 | ||
09a21edf AI |
599 | if (size > res->image_end_offset) { |
600 | int64_t count; | |
601 | count = DIV_ROUND_UP(size - res->image_end_offset, s->cluster_size); | |
728e1017 AI |
602 | if (explicit) { |
603 | fprintf(stderr, | |
604 | "%s space leaked at the end of the image %" PRId64 "\n", | |
605 | fix & BDRV_FIX_LEAKS ? "Repairing" : "ERROR", | |
606 | size - res->image_end_offset); | |
607 | res->leaks += count; | |
608 | } | |
09a21edf AI |
609 | if (fix & BDRV_FIX_LEAKS) { |
610 | Error *local_err = NULL; | |
611 | ||
612 | /* | |
613 | * In order to really repair the image, we must shrink it. | |
614 | * That means we have to pass exact=true. | |
615 | */ | |
616 | ret = bdrv_co_truncate(bs->file, res->image_end_offset, true, | |
617 | PREALLOC_MODE_OFF, 0, &local_err); | |
618 | if (ret < 0) { | |
619 | error_report_err(local_err); | |
620 | res->check_errors++; | |
621 | return ret; | |
622 | } | |
728e1017 AI |
623 | if (explicit) { |
624 | res->leaks_fixed += count; | |
625 | } | |
09a21edf AI |
626 | } |
627 | } | |
628 | ||
629 | return 0; | |
630 | } | |
631 | ||
6bb8bc63 AI |
632 | static int coroutine_fn GRAPH_RDLOCK |
633 | parallels_check_duplicate(BlockDriverState *bs, BdrvCheckResult *res, | |
634 | BdrvCheckMode fix) | |
635 | { | |
636 | BDRVParallelsState *s = bs->opaque; | |
637 | int64_t host_off, host_sector, guest_sector; | |
638 | unsigned long *bitmap; | |
a398275e | 639 | uint32_t i, bitmap_size, bat_entry; |
6bb8bc63 AI |
640 | int n, ret = 0; |
641 | uint64_t *buf = NULL; | |
642 | bool fixed = false; | |
643 | ||
644 | /* | |
645 | * Create a bitmap of used clusters. | |
646 | * If a bit is set, there is a BAT entry pointing to this cluster. | |
647 | * Loop through the BAT entries, check bits relevant to an entry offset. | |
648 | * If bit is set, this entry is duplicated. Otherwise set the bit. | |
649 | * | |
650 | * We shouldn't worry about newly allocated clusters outside the image | |
651 | * because they are created higher then any existing cluster pointed by | |
652 | * a BAT entry. | |
653 | */ | |
654 | bitmap_size = host_cluster_index(s, res->image_end_offset); | |
655 | if (bitmap_size == 0) { | |
656 | return 0; | |
657 | } | |
658 | if (res->image_end_offset % s->cluster_size) { | |
659 | /* A not aligned image end leads to a bitmap shorter by 1 */ | |
660 | bitmap_size++; | |
661 | } | |
662 | ||
663 | bitmap = bitmap_new(bitmap_size); | |
664 | ||
665 | buf = qemu_blockalign(bs, s->cluster_size); | |
666 | ||
667 | for (i = 0; i < s->bat_size; i++) { | |
668 | host_off = bat2sect(s, i) << BDRV_SECTOR_BITS; | |
669 | if (host_off == 0) { | |
670 | continue; | |
671 | } | |
672 | ||
a398275e DL |
673 | ret = mark_used(bs, bitmap, bitmap_size, host_off); |
674 | assert(ret != -E2BIG); | |
675 | if (ret == 0) { | |
6bb8bc63 AI |
676 | continue; |
677 | } | |
678 | ||
679 | /* this cluster duplicates another one */ | |
680 | fprintf(stderr, "%s duplicate offset in BAT entry %u\n", | |
681 | fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR", i); | |
682 | ||
683 | res->corruptions++; | |
684 | ||
685 | if (!(fix & BDRV_FIX_ERRORS)) { | |
686 | continue; | |
687 | } | |
688 | ||
689 | /* | |
690 | * Reset the entry and allocate a new cluster | |
691 | * for the relevant guest offset. In this way we let | |
692 | * the lower layer to place the new cluster properly. | |
693 | * Copy the original cluster to the allocated one. | |
694 | * But before save the old offset value for repairing | |
695 | * if we have an error. | |
696 | */ | |
697 | bat_entry = s->bat_bitmap[i]; | |
698 | parallels_set_bat_entry(s, i, 0); | |
699 | ||
700 | ret = bdrv_co_pread(bs->file, host_off, s->cluster_size, buf, 0); | |
701 | if (ret < 0) { | |
702 | res->check_errors++; | |
703 | goto out_repair_bat; | |
704 | } | |
705 | ||
706 | guest_sector = (i * (int64_t)s->cluster_size) >> BDRV_SECTOR_BITS; | |
707 | host_sector = allocate_clusters(bs, guest_sector, s->tracks, &n); | |
708 | if (host_sector < 0) { | |
709 | res->check_errors++; | |
710 | goto out_repair_bat; | |
711 | } | |
712 | host_off = host_sector << BDRV_SECTOR_BITS; | |
713 | ||
714 | ret = bdrv_co_pwrite(bs->file, host_off, s->cluster_size, buf, 0); | |
715 | if (ret < 0) { | |
716 | res->check_errors++; | |
717 | goto out_repair_bat; | |
718 | } | |
719 | ||
720 | if (host_off + s->cluster_size > res->image_end_offset) { | |
721 | res->image_end_offset = host_off + s->cluster_size; | |
722 | } | |
723 | ||
724 | /* | |
725 | * In the future allocate_cluster() will reuse holed offsets | |
726 | * inside the image. Keep the used clusters bitmap content | |
727 | * consistent for the new allocated clusters too. | |
728 | * | |
729 | * Note, clusters allocated outside the current image are not | |
a398275e DL |
730 | * considered, and the bitmap size doesn't change. This specifically |
731 | * means that -E2BIG is OK. | |
6bb8bc63 | 732 | */ |
a398275e DL |
733 | ret = mark_used(bs, bitmap, bitmap_size, host_off); |
734 | if (ret == -EBUSY) { | |
735 | res->check_errors++; | |
736 | goto out_repair_bat; | |
6bb8bc63 AI |
737 | } |
738 | ||
739 | fixed = true; | |
740 | res->corruptions_fixed++; | |
741 | ||
742 | } | |
743 | ||
744 | if (fixed) { | |
745 | /* | |
746 | * When new clusters are allocated, the file size increases by | |
747 | * 128 Mb. We need to truncate the file to the right size. Let | |
748 | * the leak fix code make its job without res changing. | |
749 | */ | |
750 | ret = parallels_check_leak(bs, res, fix, false); | |
751 | } | |
752 | ||
753 | out_free: | |
754 | g_free(buf); | |
755 | g_free(bitmap); | |
756 | return ret; | |
757 | /* | |
758 | * We can get here only from places where index and old_offset have | |
759 | * meaningful values. | |
760 | */ | |
761 | out_repair_bat: | |
762 | s->bat_bitmap[i] = bat_entry; | |
763 | goto out_free; | |
764 | } | |
765 | ||
7e259e25 AI |
766 | static void parallels_collect_statistics(BlockDriverState *bs, |
767 | BdrvCheckResult *res, | |
768 | BdrvCheckMode fix) | |
09a21edf AI |
769 | { |
770 | BDRVParallelsState *s = bs->opaque; | |
7e259e25 | 771 | int64_t off, prev_off; |
09a21edf AI |
772 | uint32_t i; |
773 | ||
49ad6467 DL |
774 | res->bfi.total_clusters = s->bat_size; |
775 | res->bfi.compressed_clusters = 0; /* compression is not supported */ | |
776 | ||
49ad6467 DL |
777 | prev_off = 0; |
778 | for (i = 0; i < s->bat_size; i++) { | |
7e259e25 | 779 | off = bat2sect(s, i) << BDRV_SECTOR_BITS; |
9616f7a6 AI |
780 | /* |
781 | * If BDRV_FIX_ERRORS is not set, out-of-image BAT entries were not | |
782 | * fixed. Skip not allocated and out-of-image BAT entries. | |
783 | */ | |
784 | if (off == 0 || off + s->cluster_size > res->image_end_offset) { | |
49ad6467 DL |
785 | prev_off = 0; |
786 | continue; | |
787 | } | |
788 | ||
e0b5207f | 789 | if (prev_off != 0 && (prev_off + s->cluster_size) != off) { |
49ad6467 DL |
790 | res->bfi.fragmented_clusters++; |
791 | } | |
792 | prev_off = off; | |
7e259e25 | 793 | res->bfi.allocated_clusters++; |
49ad6467 | 794 | } |
7e259e25 AI |
795 | } |
796 | ||
797 | static int coroutine_fn GRAPH_RDLOCK | |
798 | parallels_co_check(BlockDriverState *bs, BdrvCheckResult *res, | |
799 | BdrvCheckMode fix) | |
800 | { | |
801 | BDRVParallelsState *s = bs->opaque; | |
802 | int ret; | |
803 | ||
c0fc051d AI |
804 | WITH_QEMU_LOCK_GUARD(&s->lock) { |
805 | parallels_check_unclean(bs, res, fix); | |
7e259e25 | 806 | |
8cd19203 AI |
807 | ret = parallels_check_data_off(bs, res, fix); |
808 | if (ret < 0) { | |
809 | return ret; | |
810 | } | |
811 | ||
c0fc051d AI |
812 | ret = parallels_check_outside_image(bs, res, fix); |
813 | if (ret < 0) { | |
814 | return ret; | |
815 | } | |
7e259e25 | 816 | |
728e1017 | 817 | ret = parallels_check_leak(bs, res, fix, true); |
c0fc051d AI |
818 | if (ret < 0) { |
819 | return ret; | |
820 | } | |
7e259e25 | 821 | |
6bb8bc63 AI |
822 | ret = parallels_check_duplicate(bs, res, fix); |
823 | if (ret < 0) { | |
824 | return ret; | |
825 | } | |
826 | ||
c0fc051d | 827 | parallels_collect_statistics(bs, res, fix); |
7e259e25 AI |
828 | } |
829 | ||
c0fc051d AI |
830 | ret = bdrv_co_flush(bs); |
831 | if (ret < 0) { | |
832 | res->check_errors++; | |
3569cb7b AI |
833 | } |
834 | ||
2fd61638 | 835 | return ret; |
49ad6467 DL |
836 | } |
837 | ||
838 | ||
4db7ba3b KW |
839 | static int coroutine_fn GRAPH_UNLOCKED |
840 | parallels_co_create(BlockdevCreateOptions* opts, Error **errp) | |
74cf6c50 | 841 | { |
1511b490 KW |
842 | BlockdevCreateOptionsParallels *parallels_opts; |
843 | BlockDriverState *bs; | |
844 | BlockBackend *blk; | |
74cf6c50 | 845 | int64_t total_size, cl_size; |
369f7de9 | 846 | uint32_t bat_entries, bat_sectors; |
74cf6c50 | 847 | ParallelsHeader header; |
1511b490 | 848 | uint8_t tmp[BDRV_SECTOR_SIZE]; |
74cf6c50 DL |
849 | int ret; |
850 | ||
1511b490 KW |
851 | assert(opts->driver == BLOCKDEV_DRIVER_PARALLELS); |
852 | parallels_opts = &opts->u.parallels; | |
853 | ||
854 | /* Sanity checks */ | |
855 | total_size = parallels_opts->size; | |
856 | ||
857 | if (parallels_opts->has_cluster_size) { | |
858 | cl_size = parallels_opts->cluster_size; | |
859 | } else { | |
860 | cl_size = DEFAULT_CLUSTER_SIZE; | |
861 | } | |
862 | ||
2332d825 KW |
863 | /* XXX What is the real limit here? This is an insanely large maximum. */ |
864 | if (cl_size >= INT64_MAX / MAX_PARALLELS_IMAGE_FACTOR) { | |
865 | error_setg(errp, "Cluster size is too large"); | |
866 | return -EINVAL; | |
867 | } | |
555a608c | 868 | if (total_size >= MAX_PARALLELS_IMAGE_FACTOR * cl_size) { |
1511b490 | 869 | error_setg(errp, "Image size is too large for this cluster size"); |
555a608c KK |
870 | return -E2BIG; |
871 | } | |
74cf6c50 | 872 | |
1511b490 KW |
873 | if (!QEMU_IS_ALIGNED(total_size, BDRV_SECTOR_SIZE)) { |
874 | error_setg(errp, "Image size must be a multiple of 512 bytes"); | |
875 | return -EINVAL; | |
74cf6c50 DL |
876 | } |
877 | ||
1511b490 KW |
878 | if (!QEMU_IS_ALIGNED(cl_size, BDRV_SECTOR_SIZE)) { |
879 | error_setg(errp, "Cluster size must be a multiple of 512 bytes"); | |
880 | return -EINVAL; | |
881 | } | |
882 | ||
883 | /* Create BlockBackend to write to the image */ | |
48a4e92d | 884 | bs = bdrv_co_open_blockdev_ref(parallels_opts->file, errp); |
1511b490 | 885 | if (bs == NULL) { |
8942764f | 886 | return -EIO; |
74cf6c50 | 887 | } |
8942764f | 888 | |
48a4e92d KW |
889 | blk = blk_co_new_with_bs(bs, BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL, |
890 | errp); | |
a3aeeab5 EB |
891 | if (!blk) { |
892 | ret = -EPERM; | |
1511b490 KW |
893 | goto out; |
894 | } | |
895 | blk_set_allow_write_beyond_eof(blk, true); | |
8942764f | 896 | |
1511b490 | 897 | /* Create image format */ |
369f7de9 | 898 | bat_entries = DIV_ROUND_UP(total_size, cl_size); |
2d68e22e | 899 | bat_sectors = DIV_ROUND_UP(bat_entry_off(bat_entries), cl_size); |
369f7de9 | 900 | bat_sectors = (bat_sectors * cl_size) >> BDRV_SECTOR_BITS; |
74cf6c50 DL |
901 | |
902 | memset(&header, 0, sizeof(header)); | |
903 | memcpy(header.magic, HEADER_MAGIC2, sizeof(header.magic)); | |
904 | header.version = cpu_to_le32(HEADER_VERSION); | |
905 | /* don't care much about geometry, it is not used on image level */ | |
908b1c84 KK |
906 | header.heads = cpu_to_le32(HEADS_NUMBER); |
907 | header.cylinders = cpu_to_le32(total_size / BDRV_SECTOR_SIZE | |
908 | / HEADS_NUMBER / SEC_IN_CYL); | |
74cf6c50 | 909 | header.tracks = cpu_to_le32(cl_size >> BDRV_SECTOR_BITS); |
369f7de9 | 910 | header.bat_entries = cpu_to_le32(bat_entries); |
74cf6c50 | 911 | header.nb_sectors = cpu_to_le64(DIV_ROUND_UP(total_size, BDRV_SECTOR_SIZE)); |
369f7de9 | 912 | header.data_off = cpu_to_le32(bat_sectors); |
74cf6c50 DL |
913 | |
914 | /* write all the data */ | |
915 | memset(tmp, 0, sizeof(tmp)); | |
916 | memcpy(tmp, &header, sizeof(header)); | |
917 | ||
50688be0 | 918 | ret = blk_co_pwrite(blk, 0, BDRV_SECTOR_SIZE, tmp, 0); |
74cf6c50 DL |
919 | if (ret < 0) { |
920 | goto exit; | |
921 | } | |
50688be0 AF |
922 | ret = blk_co_pwrite_zeroes(blk, BDRV_SECTOR_SIZE, |
923 | (bat_sectors - 1) << BDRV_SECTOR_BITS, 0); | |
74cf6c50 DL |
924 | if (ret < 0) { |
925 | goto exit; | |
926 | } | |
74cf6c50 | 927 | |
1511b490 KW |
928 | ret = 0; |
929 | out: | |
b2ab5f54 KW |
930 | blk_co_unref(blk); |
931 | bdrv_co_unref(bs); | |
74cf6c50 DL |
932 | return ret; |
933 | ||
934 | exit: | |
935 | error_setg_errno(errp, -ret, "Failed to create Parallels image"); | |
1511b490 KW |
936 | goto out; |
937 | } | |
938 | ||
4db7ba3b | 939 | static int coroutine_fn GRAPH_UNLOCKED |
4ec8df01 KW |
940 | parallels_co_create_opts(BlockDriver *drv, const char *filename, |
941 | QemuOpts *opts, Error **errp) | |
1511b490 KW |
942 | { |
943 | BlockdevCreateOptions *create_options = NULL; | |
1511b490 | 944 | BlockDriverState *bs = NULL; |
92adf9db | 945 | QDict *qdict; |
1511b490 KW |
946 | Visitor *v; |
947 | int ret; | |
948 | ||
949 | static const QDictRenames opt_renames[] = { | |
950 | { BLOCK_OPT_CLUSTER_SIZE, "cluster-size" }, | |
951 | { NULL, NULL }, | |
952 | }; | |
953 | ||
954 | /* Parse options and convert legacy syntax */ | |
955 | qdict = qemu_opts_to_qdict_filtered(opts, NULL, ¶llels_create_opts, | |
956 | true); | |
957 | ||
958 | if (!qdict_rename_keys(qdict, opt_renames, errp)) { | |
959 | ret = -EINVAL; | |
960 | goto done; | |
961 | } | |
962 | ||
963 | /* Create and open the file (protocol layer) */ | |
2475a0d0 | 964 | ret = bdrv_co_create_file(filename, opts, errp); |
1511b490 | 965 | if (ret < 0) { |
1511b490 KW |
966 | goto done; |
967 | } | |
968 | ||
48a4e92d KW |
969 | bs = bdrv_co_open(filename, NULL, NULL, |
970 | BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp); | |
1511b490 KW |
971 | if (bs == NULL) { |
972 | ret = -EIO; | |
973 | goto done; | |
974 | } | |
975 | ||
976 | /* Now get the QAPI type BlockdevCreateOptions */ | |
977 | qdict_put_str(qdict, "driver", "parallels"); | |
978 | qdict_put_str(qdict, "file", bs->node_name); | |
979 | ||
af91062e MA |
980 | v = qobject_input_visitor_new_flat_confused(qdict, errp); |
981 | if (!v) { | |
1511b490 KW |
982 | ret = -EINVAL; |
983 | goto done; | |
984 | } | |
985 | ||
b11a093c | 986 | visit_type_BlockdevCreateOptions(v, NULL, &create_options, errp); |
1511b490 | 987 | visit_free(v); |
b11a093c | 988 | if (!create_options) { |
1511b490 KW |
989 | ret = -EINVAL; |
990 | goto done; | |
991 | } | |
992 | ||
993 | /* Silently round up sizes */ | |
994 | create_options->u.parallels.size = | |
995 | ROUND_UP(create_options->u.parallels.size, BDRV_SECTOR_SIZE); | |
996 | create_options->u.parallels.cluster_size = | |
997 | ROUND_UP(create_options->u.parallels.cluster_size, BDRV_SECTOR_SIZE); | |
998 | ||
999 | /* Create the Parallels image (format layer) */ | |
1000 | ret = parallels_co_create(create_options, errp); | |
1001 | if (ret < 0) { | |
1002 | goto done; | |
1003 | } | |
1004 | ret = 0; | |
1005 | ||
1006 | done: | |
cb3e7f08 | 1007 | qobject_unref(qdict); |
b2ab5f54 | 1008 | bdrv_co_unref(bs); |
1511b490 KW |
1009 | qapi_free_BlockdevCreateOptions(create_options); |
1010 | return ret; | |
74cf6c50 DL |
1011 | } |
1012 | ||
23d6bd3b DL |
1013 | |
1014 | static int parallels_probe(const uint8_t *buf, int buf_size, | |
1015 | const char *filename) | |
1016 | { | |
1017 | const ParallelsHeader *ph = (const void *)buf; | |
1018 | ||
1019 | if (buf_size < sizeof(ParallelsHeader)) { | |
1020 | return 0; | |
1021 | } | |
1022 | ||
1023 | if ((!memcmp(ph->magic, HEADER_MAGIC, 16) || | |
1024 | !memcmp(ph->magic, HEADER_MAGIC2, 16)) && | |
1025 | (le32_to_cpu(ph->version) == HEADER_VERSION)) { | |
1026 | return 100; | |
1027 | } | |
1028 | ||
1029 | return 0; | |
1030 | } | |
1031 | ||
6dd6b9f1 DL |
1032 | static int parallels_update_header(BlockDriverState *bs) |
1033 | { | |
1034 | BDRVParallelsState *s = bs->opaque; | |
9a4f4c31 KW |
1035 | unsigned size = MAX(bdrv_opt_mem_align(bs->file->bs), |
1036 | sizeof(ParallelsHeader)); | |
6dd6b9f1 DL |
1037 | |
1038 | if (size > s->header_size) { | |
1039 | size = s->header_size; | |
1040 | } | |
32cc71de | 1041 | return bdrv_pwrite_sync(bs->file, 0, size, s->header, 0); |
6dd6b9f1 DL |
1042 | } |
1043 | ||
93361b7e DL |
1044 | |
1045 | static int parallels_opts_prealloc(BlockDriverState *bs, QDict *options, | |
1046 | Error **errp) | |
1047 | { | |
1048 | int err; | |
1049 | char *buf; | |
1050 | int64_t bytes; | |
1051 | BDRVParallelsState *s = bs->opaque; | |
1052 | Error *local_err = NULL; | |
1053 | QemuOpts *opts = qemu_opts_create(¶llels_runtime_opts, NULL, 0, errp); | |
1054 | if (!opts) { | |
1055 | return -ENOMEM; | |
1056 | } | |
1057 | ||
1058 | err = -EINVAL; | |
1059 | if (!qemu_opts_absorb_qdict(opts, options, errp)) { | |
1060 | goto done; | |
1061 | } | |
1062 | ||
1063 | bytes = qemu_opt_get_size_del(opts, PARALLELS_OPT_PREALLOC_SIZE, 0); | |
1064 | s->prealloc_size = bytes >> BDRV_SECTOR_BITS; | |
1065 | buf = qemu_opt_get_del(opts, PARALLELS_OPT_PREALLOC_MODE); | |
1066 | /* prealloc_mode can be downgraded later during allocate_clusters */ | |
1067 | s->prealloc_mode = qapi_enum_parse(&prealloc_mode_lookup, buf, | |
1068 | PRL_PREALLOC_MODE_FALLOCATE, | |
1069 | &local_err); | |
1070 | g_free(buf); | |
1071 | if (local_err != NULL) { | |
1072 | error_propagate(errp, local_err); | |
1073 | goto done; | |
1074 | } | |
1075 | err = 0; | |
1076 | ||
1077 | done: | |
1078 | qemu_opts_del(opts); | |
1079 | return err; | |
1080 | } | |
1081 | ||
23d6bd3b DL |
1082 | static int parallels_open(BlockDriverState *bs, QDict *options, int flags, |
1083 | Error **errp) | |
1084 | { | |
1085 | BDRVParallelsState *s = bs->opaque; | |
1086 | ParallelsHeader ph; | |
19f5dc15 | 1087 | int ret, size, i; |
cfce1091 | 1088 | int64_t file_nb_sectors, sector; |
c89d4362 | 1089 | uint32_t data_start; |
8f5f5326 | 1090 | bool need_check = false; |
23d6bd3b | 1091 | |
93361b7e DL |
1092 | ret = parallels_opts_prealloc(bs, options, errp); |
1093 | if (ret < 0) { | |
1094 | return ret; | |
1095 | } | |
1096 | ||
83930780 VSO |
1097 | ret = bdrv_open_file_child(NULL, options, "file", bs, errp); |
1098 | if (ret < 0) { | |
1099 | return ret; | |
4e4bf5c4 KW |
1100 | } |
1101 | ||
f5e715db AI |
1102 | file_nb_sectors = bdrv_nb_sectors(bs->file->bs); |
1103 | if (file_nb_sectors < 0) { | |
1104 | return -EINVAL; | |
1105 | } | |
1106 | ||
32cc71de | 1107 | ret = bdrv_pread(bs->file, 0, sizeof(ph), &ph, 0); |
23d6bd3b | 1108 | if (ret < 0) { |
e17b9d08 | 1109 | return ret; |
23d6bd3b DL |
1110 | } |
1111 | ||
1112 | bs->total_sectors = le64_to_cpu(ph.nb_sectors); | |
1113 | ||
1114 | if (le32_to_cpu(ph.version) != HEADER_VERSION) { | |
1115 | goto fail_format; | |
1116 | } | |
1117 | if (!memcmp(ph.magic, HEADER_MAGIC, 16)) { | |
1118 | s->off_multiplier = 1; | |
1119 | bs->total_sectors = 0xffffffff & bs->total_sectors; | |
1120 | } else if (!memcmp(ph.magic, HEADER_MAGIC2, 16)) { | |
1121 | s->off_multiplier = le32_to_cpu(ph.tracks); | |
1122 | } else { | |
1123 | goto fail_format; | |
1124 | } | |
1125 | ||
1126 | s->tracks = le32_to_cpu(ph.tracks); | |
1127 | if (s->tracks == 0) { | |
1128 | error_setg(errp, "Invalid image: Zero sectors per track"); | |
e17b9d08 | 1129 | return -EINVAL; |
23d6bd3b DL |
1130 | } |
1131 | if (s->tracks > INT32_MAX/513) { | |
1132 | error_setg(errp, "Invalid image: Too big cluster"); | |
e17b9d08 | 1133 | return -EFBIG; |
23d6bd3b | 1134 | } |
93361b7e | 1135 | s->prealloc_size = MAX(s->tracks, s->prealloc_size); |
e0b5207f | 1136 | s->cluster_size = s->tracks << BDRV_SECTOR_BITS; |
23d6bd3b DL |
1137 | |
1138 | s->bat_size = le32_to_cpu(ph.bat_entries); | |
1139 | if (s->bat_size > INT_MAX / sizeof(uint32_t)) { | |
1140 | error_setg(errp, "Catalog too large"); | |
e17b9d08 | 1141 | return -EFBIG; |
23d6bd3b DL |
1142 | } |
1143 | ||
2d68e22e | 1144 | size = bat_entry_off(s->bat_size); |
9a4f4c31 KW |
1145 | s->header_size = ROUND_UP(size, bdrv_opt_mem_align(bs->file->bs)); |
1146 | s->header = qemu_try_blockalign(bs->file->bs, s->header_size); | |
23d6bd3b | 1147 | if (s->header == NULL) { |
e17b9d08 | 1148 | return -ENOMEM; |
23d6bd3b | 1149 | } |
23d6bd3b | 1150 | |
32cc71de | 1151 | ret = bdrv_pread(bs->file, 0, s->header_size, s->header, 0); |
23d6bd3b DL |
1152 | if (ret < 0) { |
1153 | goto fail; | |
1154 | } | |
1155 | s->bat_bitmap = (uint32_t *)(s->header + 1); | |
1156 | ||
6dd6b9f1 | 1157 | if (le32_to_cpu(ph.inuse) == HEADER_INUSE_MAGIC) { |
8f5f5326 DL |
1158 | need_check = s->header_unclean = true; |
1159 | } | |
1160 | ||
1161 | { | |
1162 | bool ok = parallels_test_data_off(s, file_nb_sectors, &data_start); | |
1163 | need_check = need_check || !ok; | |
6dd6b9f1 DL |
1164 | } |
1165 | ||
c89d4362 AI |
1166 | s->data_start = data_start; |
1167 | s->data_end = s->data_start; | |
1168 | if (s->data_end < (s->header_size >> BDRV_SECTOR_BITS)) { | |
1169 | /* | |
1170 | * There is not enough unused space to fit to block align between BAT | |
1171 | * and actual data. We can't avoid read-modify-write... | |
1172 | */ | |
1173 | s->header_size = size; | |
1174 | } | |
1175 | ||
baefd977 VSO |
1176 | if (ph.ext_off) { |
1177 | if (flags & BDRV_O_RDWR) { | |
1178 | /* | |
1179 | * It's unsafe to open image RW if there is an extension (as we | |
1180 | * don't support it). But parallels driver in QEMU historically | |
1181 | * ignores the extension, so print warning and don't care. | |
1182 | */ | |
1183 | warn_report("Format Extension ignored in RW mode"); | |
1184 | } else { | |
1185 | ret = parallels_read_format_extension( | |
1186 | bs, le64_to_cpu(ph.ext_off) << BDRV_SECTOR_BITS, errp); | |
1187 | if (ret < 0) { | |
1188 | goto fail; | |
1189 | } | |
1190 | } | |
1191 | } | |
1192 | ||
6c7d390b | 1193 | if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_INACTIVE)) { |
6dd6b9f1 DL |
1194 | s->header->inuse = cpu_to_le32(HEADER_INUSE_MAGIC); |
1195 | ret = parallels_update_header(bs); | |
1196 | if (ret < 0) { | |
1197 | goto fail; | |
1198 | } | |
1199 | } | |
1200 | ||
8e3b0cbb | 1201 | s->bat_dirty_block = 4 * qemu_real_host_page_size(); |
0d31c7c2 DL |
1202 | s->bat_dirty_bmap = |
1203 | bitmap_new(DIV_ROUND_UP(s->header_size, s->bat_dirty_block)); | |
1204 | ||
a94750d9 | 1205 | /* Disable migration until bdrv_activate method is added */ |
1d0f37cf JC |
1206 | error_setg(&s->migration_blocker, "The Parallels format used by node '%s' " |
1207 | "does not support live migration", | |
1208 | bdrv_get_device_or_node_name(bs)); | |
386f6c07 MA |
1209 | ret = migrate_add_blocker(s->migration_blocker, errp); |
1210 | if (ret < 0) { | |
cfce1091 | 1211 | error_setg(errp, "Migration blocker error"); |
1d0f37cf JC |
1212 | goto fail; |
1213 | } | |
23d6bd3b | 1214 | qemu_co_mutex_init(&s->lock); |
cfce1091 AI |
1215 | |
1216 | for (i = 0; i < s->bat_size; i++) { | |
1217 | sector = bat2sect(s, i); | |
1218 | if (sector + s->tracks > s->data_end) { | |
1219 | s->data_end = sector + s->tracks; | |
1220 | } | |
1221 | } | |
8f5f5326 | 1222 | need_check = need_check || s->data_end > file_nb_sectors; |
cfce1091 AI |
1223 | |
1224 | /* | |
1225 | * We don't repair the image here if it's opened for checks. Also we don't | |
1226 | * want to change inactive images and can't change readonly images. | |
1227 | */ | |
1228 | if ((flags & (BDRV_O_CHECK | BDRV_O_INACTIVE)) || !(flags & BDRV_O_RDWR)) { | |
1229 | return 0; | |
1230 | } | |
1231 | ||
8f5f5326 DL |
1232 | /* Repair the image if corruption was detected. */ |
1233 | if (need_check) { | |
cfce1091 AI |
1234 | BdrvCheckResult res; |
1235 | ret = bdrv_check(bs, &res, BDRV_FIX_ERRORS | BDRV_FIX_LEAKS); | |
1236 | if (ret < 0) { | |
1237 | error_setg_errno(errp, -ret, "Could not repair corrupted image"); | |
1238 | migrate_del_blocker(s->migration_blocker); | |
1239 | goto fail; | |
1240 | } | |
1241 | } | |
23d6bd3b DL |
1242 | return 0; |
1243 | ||
1244 | fail_format: | |
1245 | error_setg(errp, "Image not in Parallels format"); | |
9c398781 DL |
1246 | return -EINVAL; |
1247 | ||
23d6bd3b | 1248 | fail: |
cfce1091 AI |
1249 | /* |
1250 | * "s" object was allocated by g_malloc0 so we can safely | |
1251 | * try to free its fields even they were not allocated. | |
1252 | */ | |
1253 | error_free(s->migration_blocker); | |
1254 | g_free(s->bat_dirty_bmap); | |
23d6bd3b DL |
1255 | qemu_vfree(s->header); |
1256 | return ret; | |
1257 | } | |
1258 | ||
1259 | ||
6ada7453 TS |
1260 | static void parallels_close(BlockDriverState *bs) |
1261 | { | |
1262 | BDRVParallelsState *s = bs->opaque; | |
6dd6b9f1 | 1263 | |
6c7d390b | 1264 | if ((bs->open_flags & BDRV_O_RDWR) && !(bs->open_flags & BDRV_O_INACTIVE)) { |
6dd6b9f1 DL |
1265 | s->header->inuse = 0; |
1266 | parallels_update_header(bs); | |
e8d04f92 HR |
1267 | |
1268 | /* errors are ignored, so we might as well pass exact=true */ | |
1269 | bdrv_truncate(bs->file, s->data_end << BDRV_SECTOR_BITS, true, | |
7b8e4857 | 1270 | PREALLOC_MODE_OFF, 0, NULL); |
19f5dc15 DL |
1271 | } |
1272 | ||
0d31c7c2 | 1273 | g_free(s->bat_dirty_bmap); |
9eae9cca | 1274 | qemu_vfree(s->header); |
1d0f37cf JC |
1275 | |
1276 | migrate_del_blocker(s->migration_blocker); | |
1277 | error_free(s->migration_blocker); | |
6ada7453 TS |
1278 | } |
1279 | ||
73f3e136 DL |
1280 | static bool parallels_is_support_dirty_bitmaps(BlockDriverState *bs) |
1281 | { | |
1282 | return 1; | |
1283 | } | |
1284 | ||
5efa9d5a | 1285 | static BlockDriver bdrv_parallels = { |
bb16991f DL |
1286 | .format_name = "parallels", |
1287 | .instance_size = sizeof(BDRVParallelsState), | |
1288 | .create_opts = ¶llels_create_opts, | |
1289 | .is_format = true, | |
1290 | .supports_backing = true, | |
1291 | ||
1292 | .bdrv_has_zero_init = bdrv_has_zero_init_1, | |
73f3e136 | 1293 | .bdrv_supports_persistent_dirty_bitmap = parallels_is_support_dirty_bitmaps, |
bb16991f DL |
1294 | |
1295 | .bdrv_probe = parallels_probe, | |
1296 | .bdrv_open = parallels_open, | |
1297 | .bdrv_close = parallels_close, | |
1298 | .bdrv_child_perm = bdrv_default_perms, | |
1299 | .bdrv_co_block_status = parallels_co_block_status, | |
1300 | .bdrv_co_flush_to_os = parallels_co_flush_to_os, | |
1301 | .bdrv_co_readv = parallels_co_readv, | |
1302 | .bdrv_co_writev = parallels_co_writev, | |
1303 | .bdrv_co_create = parallels_co_create, | |
1304 | .bdrv_co_create_opts = parallels_co_create_opts, | |
1305 | .bdrv_co_check = parallels_co_check, | |
6ada7453 | 1306 | }; |
5efa9d5a AL |
1307 | |
1308 | static void bdrv_parallels_init(void) | |
1309 | { | |
1310 | bdrv_register(&bdrv_parallels); | |
1311 | } | |
1312 | ||
1313 | block_init(bdrv_parallels_init); |