]> git.proxmox.com Git - mirror_qemu.git/blob - block/parallels.c
migration: simplify blockers
[mirror_qemu.git] / block / parallels.c
1 /*
2 * Block driver for Parallels disk image format
3 *
4 * Copyright (c) 2007 Alex Beregszaszi
5 * Copyright (c) 2015 Denis V. Lunev <den@openvz.org>
6 *
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
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 */
30
31 #include "qemu/osdep.h"
32 #include "qemu/error-report.h"
33 #include "qapi/error.h"
34 #include "block/block_int.h"
35 #include "block/qdict.h"
36 #include "sysemu/block-backend.h"
37 #include "qemu/module.h"
38 #include "qemu/option.h"
39 #include "qapi/qmp/qdict.h"
40 #include "qapi/qobject-input-visitor.h"
41 #include "qapi/qapi-visit-block-core.h"
42 #include "qemu/bswap.h"
43 #include "qemu/bitmap.h"
44 #include "qemu/memalign.h"
45 #include "migration/blocker.h"
46 #include "parallels.h"
47
48 /**************************************************************/
49
50 #define HEADER_MAGIC "WithoutFreeSpace"
51 #define HEADER_MAGIC2 "WithouFreSpacExt"
52 #define HEADER_VERSION 2
53 #define HEADER_INUSE_MAGIC (0x746F6E59)
54 #define MAX_PARALLELS_IMAGE_FACTOR (1ull << 32)
55
56 static QEnumLookup prealloc_mode_lookup = {
57 .array = (const char *const[]) {
58 "falloc",
59 "truncate",
60 },
61 .size = PRL_PREALLOC_MODE__MAX
62 };
63
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",
75 .def_value_str = "128M",
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
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
107
108 static int64_t bat2sect(BDRVParallelsState *s, uint32_t idx)
109 {
110 return (uint64_t)le32_to_cpu(s->bat_bitmap[idx]) * s->off_multiplier;
111 }
112
113 static uint32_t bat_entry_off(uint32_t idx)
114 {
115 return sizeof(ParallelsHeader) + sizeof(uint32_t) * idx;
116 }
117
118 static int64_t seek_to_sector(BDRVParallelsState *s, int64_t sector_num)
119 {
120 uint32_t index, offset;
121
122 index = sector_num / s->tracks;
123 offset = sector_num % s->tracks;
124
125 /* not allocated */
126 if ((index >= s->bat_size) || (s->bat_bitmap[index] == 0)) {
127 return -1;
128 }
129 return bat2sect(s, index) + offset;
130 }
131
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
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
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
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
181 static int mark_used(BlockDriverState *bs, unsigned long *bitmap,
182 uint32_t bitmap_size, int64_t off, uint32_t count)
183 {
184 BDRVParallelsState *s = bs->opaque;
185 uint32_t cluster_index = host_cluster_index(s, off);
186 unsigned long next_used;
187 if (cluster_index + count > bitmap_size) {
188 return -E2BIG;
189 }
190 next_used = find_next_bit(bitmap, bitmap_size, cluster_index);
191 if (next_used < cluster_index + count) {
192 return -EBUSY;
193 }
194 bitmap_set(bitmap, cluster_index, count);
195 return 0;
196 }
197
198 /*
199 * Collect used bitmap. The image can contain errors, we should fill the
200 * bitmap anyway, as much as we can. This information will be used for
201 * error resolution.
202 */
203 static int parallels_fill_used_bitmap(BlockDriverState *bs)
204 {
205 BDRVParallelsState *s = bs->opaque;
206 int64_t payload_bytes;
207 uint32_t i;
208 int err = 0;
209
210 payload_bytes = bdrv_getlength(bs->file->bs);
211 if (payload_bytes < 0) {
212 return payload_bytes;
213 }
214 payload_bytes -= s->data_start * BDRV_SECTOR_SIZE;
215 if (payload_bytes < 0) {
216 return -EINVAL;
217 }
218
219 s->used_bmap_size = DIV_ROUND_UP(payload_bytes, s->cluster_size);
220 if (s->used_bmap_size == 0) {
221 return 0;
222 }
223 s->used_bmap = bitmap_try_new(s->used_bmap_size);
224 if (s->used_bmap == NULL) {
225 return -ENOMEM;
226 }
227
228 for (i = 0; i < s->bat_size; i++) {
229 int err2;
230 int64_t host_off = bat2sect(s, i) << BDRV_SECTOR_BITS;
231 if (host_off == 0) {
232 continue;
233 }
234
235 err2 = mark_used(bs, s->used_bmap, s->used_bmap_size, host_off, 1);
236 if (err2 < 0 && err == 0) {
237 err = err2;
238 }
239 }
240 return err;
241 }
242
243 static void parallels_free_used_bitmap(BlockDriverState *bs)
244 {
245 BDRVParallelsState *s = bs->opaque;
246 s->used_bmap_size = 0;
247 g_free(s->used_bmap);
248 }
249
250 static int64_t coroutine_fn GRAPH_RDLOCK
251 allocate_clusters(BlockDriverState *bs, int64_t sector_num,
252 int nb_sectors, int *pnum)
253 {
254 int ret = 0;
255 BDRVParallelsState *s = bs->opaque;
256 int64_t i, pos, idx, to_allocate, first_free, host_off;
257
258 pos = block_status(s, sector_num, nb_sectors, pnum);
259 if (pos > 0) {
260 return pos;
261 }
262
263 idx = sector_num / s->tracks;
264 to_allocate = DIV_ROUND_UP(sector_num + *pnum, s->tracks) - idx;
265
266 /*
267 * This function is called only by parallels_co_writev(), which will never
268 * pass a sector_num at or beyond the end of the image (because the block
269 * layer never passes such a sector_num to that function). Therefore, idx
270 * is always below s->bat_size.
271 * block_status() will limit *pnum so that sector_num + *pnum will not
272 * exceed the image end. Therefore, idx + to_allocate cannot exceed
273 * s->bat_size.
274 * Note that s->bat_size is an unsigned int, therefore idx + to_allocate
275 * will always fit into a uint32_t.
276 */
277 assert(idx < s->bat_size && idx + to_allocate <= s->bat_size);
278
279 first_free = find_first_zero_bit(s->used_bmap, s->used_bmap_size);
280 if (first_free == s->used_bmap_size) {
281 uint32_t new_usedsize;
282 int64_t bytes = to_allocate * s->cluster_size;
283 bytes += s->prealloc_size * BDRV_SECTOR_SIZE;
284
285 host_off = s->data_end * BDRV_SECTOR_SIZE;
286
287 /*
288 * We require the expanded size to read back as zero. If the
289 * user permitted truncation, we try that; but if it fails, we
290 * force the safer-but-slower fallocate.
291 */
292 if (s->prealloc_mode == PRL_PREALLOC_MODE_TRUNCATE) {
293 ret = bdrv_co_truncate(bs->file, host_off + bytes,
294 false, PREALLOC_MODE_OFF,
295 BDRV_REQ_ZERO_WRITE, NULL);
296 if (ret == -ENOTSUP) {
297 s->prealloc_mode = PRL_PREALLOC_MODE_FALLOCATE;
298 }
299 }
300 if (s->prealloc_mode == PRL_PREALLOC_MODE_FALLOCATE) {
301 ret = bdrv_co_pwrite_zeroes(bs->file, host_off, bytes, 0);
302 }
303 if (ret < 0) {
304 return ret;
305 }
306
307 new_usedsize = s->used_bmap_size + bytes / s->cluster_size;
308 s->used_bmap = bitmap_zero_extend(s->used_bmap, s->used_bmap_size,
309 new_usedsize);
310 s->used_bmap_size = new_usedsize;
311 } else {
312 int64_t next_used;
313 next_used = find_next_bit(s->used_bmap, s->used_bmap_size, first_free);
314
315 /* Not enough continuous clusters in the middle, adjust the size */
316 if (next_used - first_free < to_allocate) {
317 to_allocate = next_used - first_free;
318 *pnum = (idx + to_allocate) * s->tracks - sector_num;
319 }
320
321 host_off = s->data_start * BDRV_SECTOR_SIZE;
322 host_off += first_free * s->cluster_size;
323
324 /*
325 * No need to preallocate if we are using tail area from the above
326 * branch. In the other case we are likely re-using hole. Preallocate
327 * the space if required by the prealloc_mode.
328 */
329 if (s->prealloc_mode == PRL_PREALLOC_MODE_FALLOCATE &&
330 host_off < s->data_end * BDRV_SECTOR_SIZE) {
331 ret = bdrv_co_pwrite_zeroes(bs->file, host_off,
332 s->cluster_size * to_allocate, 0);
333 if (ret < 0) {
334 return ret;
335 }
336 }
337 }
338
339 /*
340 * Try to read from backing to fill empty clusters
341 * FIXME: 1. previous write_zeroes may be redundant
342 * 2. most of data we read from backing will be rewritten by
343 * parallels_co_writev. On aligned-to-cluster write we do not need
344 * this read at all.
345 * 3. it would be good to combine write of data from backing and new
346 * data into one write call.
347 */
348 if (bs->backing) {
349 int64_t nb_cow_sectors = to_allocate * s->tracks;
350 int64_t nb_cow_bytes = nb_cow_sectors << BDRV_SECTOR_BITS;
351 void *buf = qemu_blockalign(bs, nb_cow_bytes);
352
353 ret = bdrv_co_pread(bs->backing, idx * s->tracks * BDRV_SECTOR_SIZE,
354 nb_cow_bytes, buf, 0);
355 if (ret < 0) {
356 qemu_vfree(buf);
357 return ret;
358 }
359
360 ret = bdrv_co_pwrite(bs->file, s->data_end * BDRV_SECTOR_SIZE,
361 nb_cow_bytes, buf, 0);
362 qemu_vfree(buf);
363 if (ret < 0) {
364 return ret;
365 }
366 }
367
368 ret = mark_used(bs, s->used_bmap, s->used_bmap_size, host_off, to_allocate);
369 if (ret < 0) {
370 /* Image consistency is broken. Alarm! */
371 return ret;
372 }
373 for (i = 0; i < to_allocate; i++) {
374 parallels_set_bat_entry(s, idx + i,
375 host_off / BDRV_SECTOR_SIZE / s->off_multiplier);
376 host_off += s->cluster_size;
377 }
378 if (host_off > s->data_end * BDRV_SECTOR_SIZE) {
379 s->data_end = host_off / BDRV_SECTOR_SIZE;
380 }
381
382 return bat2sect(s, idx) + sector_num % s->tracks;
383 }
384
385
386 static int coroutine_fn GRAPH_RDLOCK
387 parallels_co_flush_to_os(BlockDriverState *bs)
388 {
389 BDRVParallelsState *s = bs->opaque;
390 unsigned long size = DIV_ROUND_UP(s->header_size, s->bat_dirty_block);
391 unsigned long bit;
392
393 qemu_co_mutex_lock(&s->lock);
394
395 bit = find_first_bit(s->bat_dirty_bmap, size);
396 while (bit < size) {
397 uint32_t off = bit * s->bat_dirty_block;
398 uint32_t to_write = s->bat_dirty_block;
399 int ret;
400
401 if (off + to_write > s->header_size) {
402 to_write = s->header_size - off;
403 }
404 ret = bdrv_co_pwrite(bs->file, off, to_write,
405 (uint8_t *)s->header + off, 0);
406 if (ret < 0) {
407 qemu_co_mutex_unlock(&s->lock);
408 return ret;
409 }
410 bit = find_next_bit(s->bat_dirty_bmap, size, bit + 1);
411 }
412 bitmap_zero(s->bat_dirty_bmap, size);
413
414 qemu_co_mutex_unlock(&s->lock);
415 return 0;
416 }
417
418
419 static int coroutine_fn parallels_co_block_status(BlockDriverState *bs,
420 bool want_zero,
421 int64_t offset,
422 int64_t bytes,
423 int64_t *pnum,
424 int64_t *map,
425 BlockDriverState **file)
426 {
427 BDRVParallelsState *s = bs->opaque;
428 int count;
429
430 assert(QEMU_IS_ALIGNED(offset | bytes, BDRV_SECTOR_SIZE));
431 qemu_co_mutex_lock(&s->lock);
432 offset = block_status(s, offset >> BDRV_SECTOR_BITS,
433 bytes >> BDRV_SECTOR_BITS, &count);
434 qemu_co_mutex_unlock(&s->lock);
435
436 *pnum = count * BDRV_SECTOR_SIZE;
437 if (offset < 0) {
438 return 0;
439 }
440
441 *map = offset * BDRV_SECTOR_SIZE;
442 *file = bs->file->bs;
443 return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
444 }
445
446 static int coroutine_fn GRAPH_RDLOCK
447 parallels_co_writev(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
448 QEMUIOVector *qiov, int flags)
449 {
450 BDRVParallelsState *s = bs->opaque;
451 uint64_t bytes_done = 0;
452 QEMUIOVector hd_qiov;
453 int ret = 0;
454
455 qemu_iovec_init(&hd_qiov, qiov->niov);
456
457 while (nb_sectors > 0) {
458 int64_t position;
459 int n, nbytes;
460
461 qemu_co_mutex_lock(&s->lock);
462 position = allocate_clusters(bs, sector_num, nb_sectors, &n);
463 qemu_co_mutex_unlock(&s->lock);
464 if (position < 0) {
465 ret = (int)position;
466 break;
467 }
468
469 nbytes = n << BDRV_SECTOR_BITS;
470
471 qemu_iovec_reset(&hd_qiov);
472 qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes);
473
474 ret = bdrv_co_pwritev(bs->file, position * BDRV_SECTOR_SIZE, nbytes,
475 &hd_qiov, 0);
476 if (ret < 0) {
477 break;
478 }
479
480 nb_sectors -= n;
481 sector_num += n;
482 bytes_done += nbytes;
483 }
484
485 qemu_iovec_destroy(&hd_qiov);
486 return ret;
487 }
488
489 static int coroutine_fn GRAPH_RDLOCK
490 parallels_co_readv(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
491 QEMUIOVector *qiov)
492 {
493 BDRVParallelsState *s = bs->opaque;
494 uint64_t bytes_done = 0;
495 QEMUIOVector hd_qiov;
496 int ret = 0;
497
498 qemu_iovec_init(&hd_qiov, qiov->niov);
499
500 while (nb_sectors > 0) {
501 int64_t position;
502 int n, nbytes;
503
504 qemu_co_mutex_lock(&s->lock);
505 position = block_status(s, sector_num, nb_sectors, &n);
506 qemu_co_mutex_unlock(&s->lock);
507
508 nbytes = n << BDRV_SECTOR_BITS;
509
510 qemu_iovec_reset(&hd_qiov);
511 qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes);
512
513 if (position < 0) {
514 if (bs->backing) {
515 ret = bdrv_co_preadv(bs->backing, sector_num * BDRV_SECTOR_SIZE,
516 nbytes, &hd_qiov, 0);
517 if (ret < 0) {
518 break;
519 }
520 } else {
521 qemu_iovec_memset(&hd_qiov, 0, 0, nbytes);
522 }
523 } else {
524 ret = bdrv_co_preadv(bs->file, position * BDRV_SECTOR_SIZE, nbytes,
525 &hd_qiov, 0);
526 if (ret < 0) {
527 break;
528 }
529 }
530
531 nb_sectors -= n;
532 sector_num += n;
533 bytes_done += nbytes;
534 }
535
536 qemu_iovec_destroy(&hd_qiov);
537 return ret;
538 }
539
540
541 static int coroutine_fn GRAPH_RDLOCK
542 parallels_co_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes)
543 {
544 int ret = 0;
545 uint32_t cluster, count;
546 BDRVParallelsState *s = bs->opaque;
547
548 /*
549 * The image does not support ZERO mark inside the BAT, which means that
550 * stale data could be exposed from the backing file.
551 */
552 if (bs->backing) {
553 return -ENOTSUP;
554 }
555
556 if (!QEMU_IS_ALIGNED(offset, s->cluster_size)) {
557 return -ENOTSUP;
558 } else if (!QEMU_IS_ALIGNED(bytes, s->cluster_size)) {
559 return -ENOTSUP;
560 }
561
562 cluster = offset / s->cluster_size;
563 count = bytes / s->cluster_size;
564
565 qemu_co_mutex_lock(&s->lock);
566 for (; count > 0; cluster++, count--) {
567 int64_t host_off = bat2sect(s, cluster) << BDRV_SECTOR_BITS;
568 if (host_off == 0) {
569 continue;
570 }
571
572 ret = bdrv_co_pdiscard(bs->file, host_off, s->cluster_size);
573 if (ret < 0) {
574 goto done;
575 }
576
577 parallels_set_bat_entry(s, cluster, 0);
578 bitmap_clear(s->used_bmap, host_cluster_index(s, host_off), 1);
579 }
580 done:
581 qemu_co_mutex_unlock(&s->lock);
582 return ret;
583 }
584
585 static int coroutine_fn GRAPH_RDLOCK
586 parallels_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes,
587 BdrvRequestFlags flags)
588 {
589 /*
590 * The zero flag is missed in the Parallels format specification. We can
591 * resort to discard if we have no backing file (this condition is checked
592 * inside parallels_co_pdiscard().
593 */
594 return parallels_co_pdiscard(bs, offset, bytes);
595 }
596
597
598 static void parallels_check_unclean(BlockDriverState *bs,
599 BdrvCheckResult *res,
600 BdrvCheckMode fix)
601 {
602 BDRVParallelsState *s = bs->opaque;
603
604 if (!s->header_unclean) {
605 return;
606 }
607
608 fprintf(stderr, "%s image was not closed correctly\n",
609 fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR");
610 res->corruptions++;
611 if (fix & BDRV_FIX_ERRORS) {
612 /* parallels_close will do the job right */
613 res->corruptions_fixed++;
614 s->header_unclean = false;
615 }
616 }
617
618 /*
619 * Returns true if data_off is correct, otherwise false. In both cases
620 * correct_offset is set to the proper value.
621 */
622 static bool parallels_test_data_off(BDRVParallelsState *s,
623 int64_t file_nb_sectors,
624 uint32_t *correct_offset)
625 {
626 uint32_t data_off, min_off;
627 bool old_magic;
628
629 /*
630 * There are two slightly different image formats: with "WithoutFreeSpace"
631 * or "WithouFreSpacExt" magic words. Call the first one as "old magic".
632 * In such images data_off field can be zero. In this case the offset is
633 * calculated as the end of BAT table plus some padding to ensure sector
634 * size alignment.
635 */
636 old_magic = !memcmp(s->header->magic, HEADER_MAGIC, 16);
637
638 min_off = DIV_ROUND_UP(bat_entry_off(s->bat_size), BDRV_SECTOR_SIZE);
639 if (!old_magic) {
640 min_off = ROUND_UP(min_off, s->cluster_size / BDRV_SECTOR_SIZE);
641 }
642
643 if (correct_offset) {
644 *correct_offset = min_off;
645 }
646
647 data_off = le32_to_cpu(s->header->data_off);
648 if (data_off == 0 && old_magic) {
649 return true;
650 }
651
652 if (data_off < min_off || data_off > file_nb_sectors) {
653 return false;
654 }
655
656 if (correct_offset) {
657 *correct_offset = data_off;
658 }
659
660 return true;
661 }
662
663 static int coroutine_fn GRAPH_RDLOCK
664 parallels_check_data_off(BlockDriverState *bs, BdrvCheckResult *res,
665 BdrvCheckMode fix)
666 {
667 BDRVParallelsState *s = bs->opaque;
668 int64_t file_size;
669 uint32_t data_off;
670
671 file_size = bdrv_co_nb_sectors(bs->file->bs);
672 if (file_size < 0) {
673 res->check_errors++;
674 return file_size;
675 }
676
677 if (parallels_test_data_off(s, file_size, &data_off)) {
678 return 0;
679 }
680
681 res->corruptions++;
682 if (fix & BDRV_FIX_ERRORS) {
683 int err;
684 s->header->data_off = cpu_to_le32(data_off);
685 s->data_start = data_off;
686
687 parallels_free_used_bitmap(bs);
688 err = parallels_fill_used_bitmap(bs);
689 if (err == -ENOMEM) {
690 res->check_errors++;
691 return err;
692 }
693
694 res->corruptions_fixed++;
695 }
696
697 fprintf(stderr, "%s data_off field has incorrect value\n",
698 fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR");
699
700 return 0;
701 }
702
703 static int coroutine_fn GRAPH_RDLOCK
704 parallels_check_outside_image(BlockDriverState *bs, BdrvCheckResult *res,
705 BdrvCheckMode fix)
706 {
707 BDRVParallelsState *s = bs->opaque;
708 uint32_t i;
709 int64_t off, high_off, size;
710
711 size = bdrv_co_getlength(bs->file->bs);
712 if (size < 0) {
713 res->check_errors++;
714 return size;
715 }
716
717 high_off = 0;
718 for (i = 0; i < s->bat_size; i++) {
719 off = bat2sect(s, i) << BDRV_SECTOR_BITS;
720 if (off + s->cluster_size > size) {
721 fprintf(stderr, "%s cluster %u is outside image\n",
722 fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR", i);
723 res->corruptions++;
724 if (fix & BDRV_FIX_ERRORS) {
725 parallels_set_bat_entry(s, i, 0);
726 res->corruptions_fixed++;
727 }
728 continue;
729 }
730 if (high_off < off) {
731 high_off = off;
732 }
733 }
734
735 if (high_off == 0) {
736 res->image_end_offset = s->data_end << BDRV_SECTOR_BITS;
737 } else {
738 res->image_end_offset = high_off + s->cluster_size;
739 s->data_end = res->image_end_offset >> BDRV_SECTOR_BITS;
740 }
741
742 return 0;
743 }
744
745 static int coroutine_fn GRAPH_RDLOCK
746 parallels_check_leak(BlockDriverState *bs, BdrvCheckResult *res,
747 BdrvCheckMode fix, bool explicit)
748 {
749 BDRVParallelsState *s = bs->opaque;
750 int64_t size;
751 int ret;
752
753 size = bdrv_co_getlength(bs->file->bs);
754 if (size < 0) {
755 res->check_errors++;
756 return size;
757 }
758
759 if (size > res->image_end_offset) {
760 int64_t count;
761 count = DIV_ROUND_UP(size - res->image_end_offset, s->cluster_size);
762 if (explicit) {
763 fprintf(stderr,
764 "%s space leaked at the end of the image %" PRId64 "\n",
765 fix & BDRV_FIX_LEAKS ? "Repairing" : "ERROR",
766 size - res->image_end_offset);
767 res->leaks += count;
768 }
769 if (fix & BDRV_FIX_LEAKS) {
770 Error *local_err = NULL;
771
772 /*
773 * In order to really repair the image, we must shrink it.
774 * That means we have to pass exact=true.
775 */
776 ret = bdrv_co_truncate(bs->file, res->image_end_offset, true,
777 PREALLOC_MODE_OFF, 0, &local_err);
778 if (ret < 0) {
779 error_report_err(local_err);
780 res->check_errors++;
781 return ret;
782 }
783 if (explicit) {
784 res->leaks_fixed += count;
785 }
786 }
787 }
788
789 return 0;
790 }
791
792 static int coroutine_fn GRAPH_RDLOCK
793 parallels_check_duplicate(BlockDriverState *bs, BdrvCheckResult *res,
794 BdrvCheckMode fix)
795 {
796 BDRVParallelsState *s = bs->opaque;
797 int64_t host_off, host_sector, guest_sector;
798 unsigned long *bitmap;
799 uint32_t i, bitmap_size, bat_entry;
800 int n, ret = 0;
801 uint64_t *buf = NULL;
802 bool fixed = false;
803
804 /*
805 * Create a bitmap of used clusters.
806 * If a bit is set, there is a BAT entry pointing to this cluster.
807 * Loop through the BAT entries, check bits relevant to an entry offset.
808 * If bit is set, this entry is duplicated. Otherwise set the bit.
809 *
810 * We shouldn't worry about newly allocated clusters outside the image
811 * because they are created higher then any existing cluster pointed by
812 * a BAT entry.
813 */
814 bitmap_size = host_cluster_index(s, res->image_end_offset);
815 if (bitmap_size == 0) {
816 return 0;
817 }
818 if (res->image_end_offset % s->cluster_size) {
819 /* A not aligned image end leads to a bitmap shorter by 1 */
820 bitmap_size++;
821 }
822
823 bitmap = bitmap_new(bitmap_size);
824
825 buf = qemu_blockalign(bs, s->cluster_size);
826
827 for (i = 0; i < s->bat_size; i++) {
828 host_off = bat2sect(s, i) << BDRV_SECTOR_BITS;
829 if (host_off == 0) {
830 continue;
831 }
832
833 ret = mark_used(bs, bitmap, bitmap_size, host_off, 1);
834 assert(ret != -E2BIG);
835 if (ret == 0) {
836 continue;
837 }
838
839 /* this cluster duplicates another one */
840 fprintf(stderr, "%s duplicate offset in BAT entry %u\n",
841 fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR", i);
842
843 res->corruptions++;
844
845 if (!(fix & BDRV_FIX_ERRORS)) {
846 continue;
847 }
848
849 /*
850 * Reset the entry and allocate a new cluster
851 * for the relevant guest offset. In this way we let
852 * the lower layer to place the new cluster properly.
853 * Copy the original cluster to the allocated one.
854 * But before save the old offset value for repairing
855 * if we have an error.
856 */
857 bat_entry = s->bat_bitmap[i];
858 parallels_set_bat_entry(s, i, 0);
859
860 ret = bdrv_co_pread(bs->file, host_off, s->cluster_size, buf, 0);
861 if (ret < 0) {
862 res->check_errors++;
863 goto out_repair_bat;
864 }
865
866 guest_sector = (i * (int64_t)s->cluster_size) >> BDRV_SECTOR_BITS;
867 host_sector = allocate_clusters(bs, guest_sector, s->tracks, &n);
868 if (host_sector < 0) {
869 res->check_errors++;
870 goto out_repair_bat;
871 }
872 host_off = host_sector << BDRV_SECTOR_BITS;
873
874 ret = bdrv_co_pwrite(bs->file, host_off, s->cluster_size, buf, 0);
875 if (ret < 0) {
876 res->check_errors++;
877 goto out_repair_bat;
878 }
879
880 if (host_off + s->cluster_size > res->image_end_offset) {
881 res->image_end_offset = host_off + s->cluster_size;
882 }
883
884 /*
885 * In the future allocate_cluster() will reuse holed offsets
886 * inside the image. Keep the used clusters bitmap content
887 * consistent for the new allocated clusters too.
888 *
889 * Note, clusters allocated outside the current image are not
890 * considered, and the bitmap size doesn't change. This specifically
891 * means that -E2BIG is OK.
892 */
893 ret = mark_used(bs, bitmap, bitmap_size, host_off, 1);
894 if (ret == -EBUSY) {
895 res->check_errors++;
896 goto out_repair_bat;
897 }
898
899 fixed = true;
900 res->corruptions_fixed++;
901
902 }
903
904 if (fixed) {
905 /*
906 * When new clusters are allocated, the file size increases by
907 * 128 Mb. We need to truncate the file to the right size. Let
908 * the leak fix code make its job without res changing.
909 */
910 ret = parallels_check_leak(bs, res, fix, false);
911 }
912
913 out_free:
914 g_free(buf);
915 g_free(bitmap);
916 return ret;
917 /*
918 * We can get here only from places where index and old_offset have
919 * meaningful values.
920 */
921 out_repair_bat:
922 s->bat_bitmap[i] = bat_entry;
923 goto out_free;
924 }
925
926 static void parallels_collect_statistics(BlockDriverState *bs,
927 BdrvCheckResult *res,
928 BdrvCheckMode fix)
929 {
930 BDRVParallelsState *s = bs->opaque;
931 int64_t off, prev_off;
932 uint32_t i;
933
934 res->bfi.total_clusters = s->bat_size;
935 res->bfi.compressed_clusters = 0; /* compression is not supported */
936
937 prev_off = 0;
938 for (i = 0; i < s->bat_size; i++) {
939 off = bat2sect(s, i) << BDRV_SECTOR_BITS;
940 /*
941 * If BDRV_FIX_ERRORS is not set, out-of-image BAT entries were not
942 * fixed. Skip not allocated and out-of-image BAT entries.
943 */
944 if (off == 0 || off + s->cluster_size > res->image_end_offset) {
945 prev_off = 0;
946 continue;
947 }
948
949 if (prev_off != 0 && (prev_off + s->cluster_size) != off) {
950 res->bfi.fragmented_clusters++;
951 }
952 prev_off = off;
953 res->bfi.allocated_clusters++;
954 }
955 }
956
957 static int coroutine_fn GRAPH_RDLOCK
958 parallels_co_check(BlockDriverState *bs, BdrvCheckResult *res,
959 BdrvCheckMode fix)
960 {
961 BDRVParallelsState *s = bs->opaque;
962 int ret;
963
964 WITH_QEMU_LOCK_GUARD(&s->lock) {
965 parallels_check_unclean(bs, res, fix);
966
967 ret = parallels_check_data_off(bs, res, fix);
968 if (ret < 0) {
969 return ret;
970 }
971
972 ret = parallels_check_outside_image(bs, res, fix);
973 if (ret < 0) {
974 return ret;
975 }
976
977 ret = parallels_check_leak(bs, res, fix, true);
978 if (ret < 0) {
979 return ret;
980 }
981
982 ret = parallels_check_duplicate(bs, res, fix);
983 if (ret < 0) {
984 return ret;
985 }
986
987 parallels_collect_statistics(bs, res, fix);
988 }
989
990 ret = bdrv_co_flush(bs);
991 if (ret < 0) {
992 res->check_errors++;
993 }
994
995 return ret;
996 }
997
998
999 static int coroutine_fn GRAPH_UNLOCKED
1000 parallels_co_create(BlockdevCreateOptions* opts, Error **errp)
1001 {
1002 BlockdevCreateOptionsParallels *parallels_opts;
1003 BlockDriverState *bs;
1004 BlockBackend *blk;
1005 int64_t total_size, cl_size;
1006 uint32_t bat_entries, bat_sectors;
1007 ParallelsHeader header;
1008 uint8_t tmp[BDRV_SECTOR_SIZE];
1009 int ret;
1010
1011 assert(opts->driver == BLOCKDEV_DRIVER_PARALLELS);
1012 parallels_opts = &opts->u.parallels;
1013
1014 /* Sanity checks */
1015 total_size = parallels_opts->size;
1016
1017 if (parallels_opts->has_cluster_size) {
1018 cl_size = parallels_opts->cluster_size;
1019 } else {
1020 cl_size = DEFAULT_CLUSTER_SIZE;
1021 }
1022
1023 /* XXX What is the real limit here? This is an insanely large maximum. */
1024 if (cl_size >= INT64_MAX / MAX_PARALLELS_IMAGE_FACTOR) {
1025 error_setg(errp, "Cluster size is too large");
1026 return -EINVAL;
1027 }
1028 if (total_size >= MAX_PARALLELS_IMAGE_FACTOR * cl_size) {
1029 error_setg(errp, "Image size is too large for this cluster size");
1030 return -E2BIG;
1031 }
1032
1033 if (!QEMU_IS_ALIGNED(total_size, BDRV_SECTOR_SIZE)) {
1034 error_setg(errp, "Image size must be a multiple of 512 bytes");
1035 return -EINVAL;
1036 }
1037
1038 if (!QEMU_IS_ALIGNED(cl_size, BDRV_SECTOR_SIZE)) {
1039 error_setg(errp, "Cluster size must be a multiple of 512 bytes");
1040 return -EINVAL;
1041 }
1042
1043 /* Create BlockBackend to write to the image */
1044 bs = bdrv_co_open_blockdev_ref(parallels_opts->file, errp);
1045 if (bs == NULL) {
1046 return -EIO;
1047 }
1048
1049 blk = blk_co_new_with_bs(bs, BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL,
1050 errp);
1051 if (!blk) {
1052 ret = -EPERM;
1053 goto out;
1054 }
1055 blk_set_allow_write_beyond_eof(blk, true);
1056
1057 /* Create image format */
1058 bat_entries = DIV_ROUND_UP(total_size, cl_size);
1059 bat_sectors = DIV_ROUND_UP(bat_entry_off(bat_entries), cl_size);
1060 bat_sectors = (bat_sectors * cl_size) >> BDRV_SECTOR_BITS;
1061
1062 memset(&header, 0, sizeof(header));
1063 memcpy(header.magic, HEADER_MAGIC2, sizeof(header.magic));
1064 header.version = cpu_to_le32(HEADER_VERSION);
1065 /* don't care much about geometry, it is not used on image level */
1066 header.heads = cpu_to_le32(HEADS_NUMBER);
1067 header.cylinders = cpu_to_le32(total_size / BDRV_SECTOR_SIZE
1068 / HEADS_NUMBER / SEC_IN_CYL);
1069 header.tracks = cpu_to_le32(cl_size >> BDRV_SECTOR_BITS);
1070 header.bat_entries = cpu_to_le32(bat_entries);
1071 header.nb_sectors = cpu_to_le64(DIV_ROUND_UP(total_size, BDRV_SECTOR_SIZE));
1072 header.data_off = cpu_to_le32(bat_sectors);
1073
1074 /* write all the data */
1075 memset(tmp, 0, sizeof(tmp));
1076 memcpy(tmp, &header, sizeof(header));
1077
1078 ret = blk_co_pwrite(blk, 0, BDRV_SECTOR_SIZE, tmp, 0);
1079 if (ret < 0) {
1080 goto exit;
1081 }
1082 ret = blk_co_pwrite_zeroes(blk, BDRV_SECTOR_SIZE,
1083 (bat_sectors - 1) << BDRV_SECTOR_BITS, 0);
1084 if (ret < 0) {
1085 goto exit;
1086 }
1087
1088 ret = 0;
1089 out:
1090 blk_co_unref(blk);
1091 bdrv_co_unref(bs);
1092 return ret;
1093
1094 exit:
1095 error_setg_errno(errp, -ret, "Failed to create Parallels image");
1096 goto out;
1097 }
1098
1099 static int coroutine_fn GRAPH_UNLOCKED
1100 parallels_co_create_opts(BlockDriver *drv, const char *filename,
1101 QemuOpts *opts, Error **errp)
1102 {
1103 BlockdevCreateOptions *create_options = NULL;
1104 BlockDriverState *bs = NULL;
1105 QDict *qdict;
1106 Visitor *v;
1107 int ret;
1108
1109 static const QDictRenames opt_renames[] = {
1110 { BLOCK_OPT_CLUSTER_SIZE, "cluster-size" },
1111 { NULL, NULL },
1112 };
1113
1114 /* Parse options and convert legacy syntax */
1115 qdict = qemu_opts_to_qdict_filtered(opts, NULL, &parallels_create_opts,
1116 true);
1117
1118 if (!qdict_rename_keys(qdict, opt_renames, errp)) {
1119 ret = -EINVAL;
1120 goto done;
1121 }
1122
1123 /* Create and open the file (protocol layer) */
1124 ret = bdrv_co_create_file(filename, opts, errp);
1125 if (ret < 0) {
1126 goto done;
1127 }
1128
1129 bs = bdrv_co_open(filename, NULL, NULL,
1130 BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp);
1131 if (bs == NULL) {
1132 ret = -EIO;
1133 goto done;
1134 }
1135
1136 /* Now get the QAPI type BlockdevCreateOptions */
1137 qdict_put_str(qdict, "driver", "parallels");
1138 qdict_put_str(qdict, "file", bs->node_name);
1139
1140 v = qobject_input_visitor_new_flat_confused(qdict, errp);
1141 if (!v) {
1142 ret = -EINVAL;
1143 goto done;
1144 }
1145
1146 visit_type_BlockdevCreateOptions(v, NULL, &create_options, errp);
1147 visit_free(v);
1148 if (!create_options) {
1149 ret = -EINVAL;
1150 goto done;
1151 }
1152
1153 /* Silently round up sizes */
1154 create_options->u.parallels.size =
1155 ROUND_UP(create_options->u.parallels.size, BDRV_SECTOR_SIZE);
1156 create_options->u.parallels.cluster_size =
1157 ROUND_UP(create_options->u.parallels.cluster_size, BDRV_SECTOR_SIZE);
1158
1159 /* Create the Parallels image (format layer) */
1160 ret = parallels_co_create(create_options, errp);
1161 if (ret < 0) {
1162 goto done;
1163 }
1164 ret = 0;
1165
1166 done:
1167 qobject_unref(qdict);
1168 bdrv_co_unref(bs);
1169 qapi_free_BlockdevCreateOptions(create_options);
1170 return ret;
1171 }
1172
1173
1174 static int parallels_probe(const uint8_t *buf, int buf_size,
1175 const char *filename)
1176 {
1177 const ParallelsHeader *ph = (const void *)buf;
1178
1179 if (buf_size < sizeof(ParallelsHeader)) {
1180 return 0;
1181 }
1182
1183 if ((!memcmp(ph->magic, HEADER_MAGIC, 16) ||
1184 !memcmp(ph->magic, HEADER_MAGIC2, 16)) &&
1185 (le32_to_cpu(ph->version) == HEADER_VERSION)) {
1186 return 100;
1187 }
1188
1189 return 0;
1190 }
1191
1192 static int parallels_update_header(BlockDriverState *bs)
1193 {
1194 BDRVParallelsState *s = bs->opaque;
1195 unsigned size = MAX(bdrv_opt_mem_align(bs->file->bs),
1196 sizeof(ParallelsHeader));
1197
1198 if (size > s->header_size) {
1199 size = s->header_size;
1200 }
1201 return bdrv_pwrite_sync(bs->file, 0, size, s->header, 0);
1202 }
1203
1204
1205 static int parallels_opts_prealloc(BlockDriverState *bs, QDict *options,
1206 Error **errp)
1207 {
1208 int err;
1209 char *buf;
1210 int64_t bytes;
1211 BDRVParallelsState *s = bs->opaque;
1212 Error *local_err = NULL;
1213 QemuOpts *opts = qemu_opts_create(&parallels_runtime_opts, NULL, 0, errp);
1214 if (!opts) {
1215 return -ENOMEM;
1216 }
1217
1218 err = -EINVAL;
1219 if (!qemu_opts_absorb_qdict(opts, options, errp)) {
1220 goto done;
1221 }
1222
1223 bytes = qemu_opt_get_size_del(opts, PARALLELS_OPT_PREALLOC_SIZE, 0);
1224 s->prealloc_size = bytes >> BDRV_SECTOR_BITS;
1225 buf = qemu_opt_get_del(opts, PARALLELS_OPT_PREALLOC_MODE);
1226 /* prealloc_mode can be downgraded later during allocate_clusters */
1227 s->prealloc_mode = qapi_enum_parse(&prealloc_mode_lookup, buf,
1228 PRL_PREALLOC_MODE_FALLOCATE,
1229 &local_err);
1230 g_free(buf);
1231 if (local_err != NULL) {
1232 error_propagate(errp, local_err);
1233 goto done;
1234 }
1235 err = 0;
1236
1237 done:
1238 qemu_opts_del(opts);
1239 return err;
1240 }
1241
1242 static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
1243 Error **errp)
1244 {
1245 BDRVParallelsState *s = bs->opaque;
1246 ParallelsHeader ph;
1247 int ret, size, i;
1248 int64_t file_nb_sectors, sector;
1249 uint32_t data_start;
1250 bool need_check = false;
1251
1252 ret = parallels_opts_prealloc(bs, options, errp);
1253 if (ret < 0) {
1254 return ret;
1255 }
1256
1257 ret = bdrv_open_file_child(NULL, options, "file", bs, errp);
1258 if (ret < 0) {
1259 return ret;
1260 }
1261
1262 file_nb_sectors = bdrv_nb_sectors(bs->file->bs);
1263 if (file_nb_sectors < 0) {
1264 return -EINVAL;
1265 }
1266
1267 ret = bdrv_pread(bs->file, 0, sizeof(ph), &ph, 0);
1268 if (ret < 0) {
1269 return ret;
1270 }
1271
1272 bs->total_sectors = le64_to_cpu(ph.nb_sectors);
1273
1274 if (le32_to_cpu(ph.version) != HEADER_VERSION) {
1275 goto fail_format;
1276 }
1277 if (!memcmp(ph.magic, HEADER_MAGIC, 16)) {
1278 s->off_multiplier = 1;
1279 bs->total_sectors = 0xffffffff & bs->total_sectors;
1280 } else if (!memcmp(ph.magic, HEADER_MAGIC2, 16)) {
1281 s->off_multiplier = le32_to_cpu(ph.tracks);
1282 } else {
1283 goto fail_format;
1284 }
1285
1286 s->tracks = le32_to_cpu(ph.tracks);
1287 if (s->tracks == 0) {
1288 error_setg(errp, "Invalid image: Zero sectors per track");
1289 return -EINVAL;
1290 }
1291 if (s->tracks > INT32_MAX/513) {
1292 error_setg(errp, "Invalid image: Too big cluster");
1293 return -EFBIG;
1294 }
1295 s->prealloc_size = MAX(s->tracks, s->prealloc_size);
1296 s->cluster_size = s->tracks << BDRV_SECTOR_BITS;
1297
1298 s->bat_size = le32_to_cpu(ph.bat_entries);
1299 if (s->bat_size > INT_MAX / sizeof(uint32_t)) {
1300 error_setg(errp, "Catalog too large");
1301 return -EFBIG;
1302 }
1303
1304 size = bat_entry_off(s->bat_size);
1305 s->header_size = ROUND_UP(size, bdrv_opt_mem_align(bs->file->bs));
1306 s->header = qemu_try_blockalign(bs->file->bs, s->header_size);
1307 if (s->header == NULL) {
1308 return -ENOMEM;
1309 }
1310
1311 ret = bdrv_pread(bs->file, 0, s->header_size, s->header, 0);
1312 if (ret < 0) {
1313 goto fail;
1314 }
1315 s->bat_bitmap = (uint32_t *)(s->header + 1);
1316
1317 if (le32_to_cpu(ph.inuse) == HEADER_INUSE_MAGIC) {
1318 need_check = s->header_unclean = true;
1319 }
1320
1321 {
1322 bool ok = parallels_test_data_off(s, file_nb_sectors, &data_start);
1323 need_check = need_check || !ok;
1324 }
1325
1326 s->data_start = data_start;
1327 s->data_end = s->data_start;
1328 if (s->data_end < (s->header_size >> BDRV_SECTOR_BITS)) {
1329 /*
1330 * There is not enough unused space to fit to block align between BAT
1331 * and actual data. We can't avoid read-modify-write...
1332 */
1333 s->header_size = size;
1334 }
1335
1336 if (ph.ext_off) {
1337 if (flags & BDRV_O_RDWR) {
1338 /*
1339 * It's unsafe to open image RW if there is an extension (as we
1340 * don't support it). But parallels driver in QEMU historically
1341 * ignores the extension, so print warning and don't care.
1342 */
1343 warn_report("Format Extension ignored in RW mode");
1344 } else {
1345 ret = parallels_read_format_extension(
1346 bs, le64_to_cpu(ph.ext_off) << BDRV_SECTOR_BITS, errp);
1347 if (ret < 0) {
1348 goto fail;
1349 }
1350 }
1351 }
1352
1353 if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_INACTIVE)) {
1354 s->header->inuse = cpu_to_le32(HEADER_INUSE_MAGIC);
1355 ret = parallels_update_header(bs);
1356 if (ret < 0) {
1357 goto fail;
1358 }
1359 }
1360
1361 s->bat_dirty_block = 4 * qemu_real_host_page_size();
1362 s->bat_dirty_bmap =
1363 bitmap_new(DIV_ROUND_UP(s->header_size, s->bat_dirty_block));
1364
1365 /* Disable migration until bdrv_activate method is added */
1366 bdrv_graph_rdlock_main_loop();
1367 error_setg(&s->migration_blocker, "The Parallels format used by node '%s' "
1368 "does not support live migration",
1369 bdrv_get_device_or_node_name(bs));
1370 bdrv_graph_rdunlock_main_loop();
1371
1372 ret = migrate_add_blocker(&s->migration_blocker, errp);
1373 if (ret < 0) {
1374 goto fail;
1375 }
1376 qemu_co_mutex_init(&s->lock);
1377
1378 for (i = 0; i < s->bat_size; i++) {
1379 sector = bat2sect(s, i);
1380 if (sector + s->tracks > s->data_end) {
1381 s->data_end = sector + s->tracks;
1382 }
1383 }
1384 need_check = need_check || s->data_end > file_nb_sectors;
1385
1386 if (!need_check) {
1387 ret = parallels_fill_used_bitmap(bs);
1388 if (ret == -ENOMEM) {
1389 goto fail;
1390 }
1391 need_check = need_check || ret < 0; /* These are correctable errors */
1392 }
1393
1394 /*
1395 * We don't repair the image here if it's opened for checks. Also we don't
1396 * want to change inactive images and can't change readonly images.
1397 */
1398 if ((flags & (BDRV_O_CHECK | BDRV_O_INACTIVE)) || !(flags & BDRV_O_RDWR)) {
1399 return 0;
1400 }
1401
1402 /* Repair the image if corruption was detected. */
1403 if (need_check) {
1404 BdrvCheckResult res;
1405 ret = bdrv_check(bs, &res, BDRV_FIX_ERRORS | BDRV_FIX_LEAKS);
1406 if (ret < 0) {
1407 error_setg_errno(errp, -ret, "Could not repair corrupted image");
1408 migrate_del_blocker(&s->migration_blocker);
1409 goto fail;
1410 }
1411 }
1412 return 0;
1413
1414 fail_format:
1415 error_setg(errp, "Image not in Parallels format");
1416 return -EINVAL;
1417
1418 fail:
1419 /*
1420 * "s" object was allocated by g_malloc0 so we can safely
1421 * try to free its fields even they were not allocated.
1422 */
1423 parallels_free_used_bitmap(bs);
1424
1425 g_free(s->bat_dirty_bmap);
1426 qemu_vfree(s->header);
1427 return ret;
1428 }
1429
1430
1431 static void parallels_close(BlockDriverState *bs)
1432 {
1433 BDRVParallelsState *s = bs->opaque;
1434
1435 if ((bs->open_flags & BDRV_O_RDWR) && !(bs->open_flags & BDRV_O_INACTIVE)) {
1436 s->header->inuse = 0;
1437 parallels_update_header(bs);
1438
1439 /* errors are ignored, so we might as well pass exact=true */
1440 bdrv_truncate(bs->file, s->data_end << BDRV_SECTOR_BITS, true,
1441 PREALLOC_MODE_OFF, 0, NULL);
1442 }
1443
1444 parallels_free_used_bitmap(bs);
1445
1446 g_free(s->bat_dirty_bmap);
1447 qemu_vfree(s->header);
1448
1449 migrate_del_blocker(&s->migration_blocker);
1450 }
1451
1452 static bool parallels_is_support_dirty_bitmaps(BlockDriverState *bs)
1453 {
1454 return 1;
1455 }
1456
1457 static BlockDriver bdrv_parallels = {
1458 .format_name = "parallels",
1459 .instance_size = sizeof(BDRVParallelsState),
1460 .create_opts = &parallels_create_opts,
1461 .is_format = true,
1462 .supports_backing = true,
1463
1464 .bdrv_has_zero_init = bdrv_has_zero_init_1,
1465 .bdrv_supports_persistent_dirty_bitmap = parallels_is_support_dirty_bitmaps,
1466
1467 .bdrv_probe = parallels_probe,
1468 .bdrv_open = parallels_open,
1469 .bdrv_close = parallels_close,
1470 .bdrv_child_perm = bdrv_default_perms,
1471 .bdrv_co_block_status = parallels_co_block_status,
1472 .bdrv_co_flush_to_os = parallels_co_flush_to_os,
1473 .bdrv_co_readv = parallels_co_readv,
1474 .bdrv_co_writev = parallels_co_writev,
1475 .bdrv_co_create = parallels_co_create,
1476 .bdrv_co_create_opts = parallels_co_create_opts,
1477 .bdrv_co_check = parallels_co_check,
1478 .bdrv_co_pdiscard = parallels_co_pdiscard,
1479 .bdrv_co_pwrite_zeroes = parallels_co_pwrite_zeroes,
1480 };
1481
1482 static void bdrv_parallels_init(void)
1483 {
1484 bdrv_register(&bdrv_parallels);
1485 }
1486
1487 block_init(bdrv_parallels_init);