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