]> git.proxmox.com Git - mirror_qemu.git/blame - block/parallels.c
include/qemu/osdep.h: Don't include qapi/error.h
[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 */
80c71a24 30#include "qemu/osdep.h"
da34e65c 31#include "qapi/error.h"
faf07963 32#include "qemu-common.h"
737e150e 33#include "block/block_int.h"
8942764f 34#include "sysemu/block-backend.h"
1de7afc9 35#include "qemu/module.h"
0d31c7c2 36#include "qemu/bitmap.h"
d6179011 37#include "qapi/util.h"
6ada7453
TS
38
39/**************************************************************/
40
41#define HEADER_MAGIC "WithoutFreeSpace"
d25d5980 42#define HEADER_MAGIC2 "WithouFreSpacExt"
6ada7453 43#define HEADER_VERSION 2
6dd6b9f1 44#define HEADER_INUSE_MAGIC (0x746F6E59)
6ada7453 45
74cf6c50
DL
46#define DEFAULT_CLUSTER_SIZE 1048576 /* 1 MiB */
47
48
6ada7453 49// always little-endian
07898904 50typedef struct ParallelsHeader {
6ada7453
TS
51 char magic[16]; // "WithoutFreeSpace"
52 uint32_t version;
53 uint32_t heads;
54 uint32_t cylinders;
55 uint32_t tracks;
369f7de9 56 uint32_t bat_entries;
8c27d54f
DL
57 uint64_t nb_sectors;
58 uint32_t inuse;
59 uint32_t data_off;
60 char padding[12];
07898904 61} QEMU_PACKED ParallelsHeader;
6ada7453 62
d6179011
DL
63
64typedef enum ParallelsPreallocMode {
65 PRL_PREALLOC_MODE_FALLOCATE = 0,
66 PRL_PREALLOC_MODE_TRUNCATE = 1,
7fb1cf16 67 PRL_PREALLOC_MODE__MAX = 2,
d6179011
DL
68} ParallelsPreallocMode;
69
70static const char *prealloc_mode_lookup[] = {
71 "falloc",
72 "truncate",
73 NULL,
74};
75
76
6ada7453 77typedef struct BDRVParallelsState {
481fb9cf
DL
78 /** Locking is conservative, the lock protects
79 * - image file extending (truncate, fallocate)
80 * - any access to block allocation table
81 */
848c66e8 82 CoMutex lock;
6ada7453 83
9eae9cca
DL
84 ParallelsHeader *header;
85 uint32_t header_size;
6dd6b9f1
DL
86 bool header_unclean;
87
0d31c7c2
DL
88 unsigned long *bat_dirty_bmap;
89 unsigned int bat_dirty_block;
90
369f7de9
DL
91 uint32_t *bat_bitmap;
92 unsigned int bat_size;
6ada7453 93
19f5dc15 94 int64_t data_end;
d6179011
DL
95 uint64_t prealloc_size;
96 ParallelsPreallocMode prealloc_mode;
97
9302e863 98 unsigned int tracks;
d25d5980
DL
99
100 unsigned int off_multiplier;
6ada7453
TS
101} BDRVParallelsState;
102
555cc9d9 103
d6179011
DL
104#define PARALLELS_OPT_PREALLOC_MODE "prealloc-mode"
105#define PARALLELS_OPT_PREALLOC_SIZE "prealloc-size"
106
107static QemuOptsList parallels_runtime_opts = {
108 .name = "parallels",
109 .head = QTAILQ_HEAD_INITIALIZER(parallels_runtime_opts.head),
110 .desc = {
111 {
112 .name = PARALLELS_OPT_PREALLOC_SIZE,
113 .type = QEMU_OPT_SIZE,
114 .help = "Preallocation size on image expansion",
115 .def_value_str = "128MiB",
116 },
117 {
118 .name = PARALLELS_OPT_PREALLOC_MODE,
119 .type = QEMU_OPT_STRING,
120 .help = "Preallocation mode on image expansion "
121 "(allowed values: falloc, truncate)",
122 .def_value_str = "falloc",
123 },
124 { /* end of list */ },
125 },
126};
127
128
555cc9d9
DL
129static int64_t bat2sect(BDRVParallelsState *s, uint32_t idx)
130{
dd97cdc0 131 return (uint64_t)le32_to_cpu(s->bat_bitmap[idx]) * s->off_multiplier;
555cc9d9
DL
132}
133
2d68e22e
DL
134static uint32_t bat_entry_off(uint32_t idx)
135{
136 return sizeof(ParallelsHeader) + sizeof(uint32_t) * idx;
137}
138
29442569 139static int64_t seek_to_sector(BDRVParallelsState *s, int64_t sector_num)
6ada7453 140{
c34d2451 141 uint32_t index, offset;
6ada7453
TS
142
143 index = sector_num / s->tracks;
144 offset = sector_num % s->tracks;
145
9d8b88f6 146 /* not allocated */
369f7de9 147 if ((index >= s->bat_size) || (s->bat_bitmap[index] == 0)) {
f08e2f84 148 return -1;
369f7de9 149 }
555cc9d9 150 return bat2sect(s, index) + offset;
6ada7453
TS
151}
152
9de9da17
RK
153static int cluster_remainder(BDRVParallelsState *s, int64_t sector_num,
154 int nb_sectors)
155{
156 int ret = s->tracks - sector_num % s->tracks;
157 return MIN(nb_sectors, ret);
158}
159
6953d920
DL
160static int64_t block_status(BDRVParallelsState *s, int64_t sector_num,
161 int nb_sectors, int *pnum)
162{
163 int64_t start_off = -2, prev_end_off = -2;
164
165 *pnum = 0;
166 while (nb_sectors > 0 || start_off == -2) {
167 int64_t offset = seek_to_sector(s, sector_num);
168 int to_end;
169
170 if (start_off == -2) {
171 start_off = offset;
172 prev_end_off = offset;
173 } else if (offset != prev_end_off) {
174 break;
175 }
176
177 to_end = cluster_remainder(s, sector_num, nb_sectors);
178 nb_sectors -= to_end;
179 sector_num += to_end;
180 *pnum += to_end;
181
182 if (offset > 0) {
183 prev_end_off += to_end;
184 }
185 }
186 return start_off;
187}
188
ddd2ef2c
DL
189static int64_t allocate_clusters(BlockDriverState *bs, int64_t sector_num,
190 int nb_sectors, int *pnum)
5a41e1fa
DL
191{
192 BDRVParallelsState *s = bs->opaque;
ddd2ef2c
DL
193 uint32_t idx, to_allocate, i;
194 int64_t pos, space;
5a41e1fa 195
ddd2ef2c
DL
196 pos = block_status(s, sector_num, nb_sectors, pnum);
197 if (pos > 0) {
198 return pos;
199 }
5a41e1fa 200
ddd2ef2c 201 idx = sector_num / s->tracks;
369f7de9 202 if (idx >= s->bat_size) {
5a41e1fa
DL
203 return -EINVAL;
204 }
5a41e1fa 205
ddd2ef2c
DL
206 to_allocate = (sector_num + *pnum + s->tracks - 1) / s->tracks - idx;
207 space = to_allocate * s->tracks;
9a4f4c31 208 if (s->data_end + space > bdrv_getlength(bs->file->bs) >> BDRV_SECTOR_BITS) {
19f5dc15 209 int ret;
ddd2ef2c 210 space += s->prealloc_size;
19f5dc15 211 if (s->prealloc_mode == PRL_PREALLOC_MODE_FALLOCATE) {
9a4f4c31 212 ret = bdrv_write_zeroes(bs->file->bs, s->data_end, space, 0);
19f5dc15 213 } else {
9a4f4c31 214 ret = bdrv_truncate(bs->file->bs,
ddd2ef2c 215 (s->data_end + space) << BDRV_SECTOR_BITS);
19f5dc15
DL
216 }
217 if (ret < 0) {
218 return ret;
219 }
5a41e1fa
DL
220 }
221
ddd2ef2c
DL
222 for (i = 0; i < to_allocate; i++) {
223 s->bat_bitmap[idx + i] = cpu_to_le32(s->data_end / s->off_multiplier);
224 s->data_end += s->tracks;
225 bitmap_set(s->bat_dirty_bmap,
c9f6856d 226 bat_entry_off(idx + i) / s->bat_dirty_block, 1);
ddd2ef2c 227 }
0d31c7c2 228
ddd2ef2c 229 return bat2sect(s, idx) + sector_num % s->tracks;
5a41e1fa
DL
230}
231
0d31c7c2
DL
232
233static coroutine_fn int parallels_co_flush_to_os(BlockDriverState *bs)
234{
235 BDRVParallelsState *s = bs->opaque;
236 unsigned long size = DIV_ROUND_UP(s->header_size, s->bat_dirty_block);
237 unsigned long bit;
238
239 qemu_co_mutex_lock(&s->lock);
240
241 bit = find_first_bit(s->bat_dirty_bmap, size);
242 while (bit < size) {
243 uint32_t off = bit * s->bat_dirty_block;
244 uint32_t to_write = s->bat_dirty_block;
245 int ret;
246
247 if (off + to_write > s->header_size) {
248 to_write = s->header_size - off;
249 }
9a4f4c31
KW
250 ret = bdrv_pwrite(bs->file->bs, off, (uint8_t *)s->header + off,
251 to_write);
0d31c7c2
DL
252 if (ret < 0) {
253 qemu_co_mutex_unlock(&s->lock);
254 return ret;
255 }
256 bit = find_next_bit(s->bat_dirty_bmap, size, bit + 1);
257 }
258 bitmap_zero(s->bat_dirty_bmap, size);
259
260 qemu_co_mutex_unlock(&s->lock);
261 return 0;
262}
263
264
dd3bed16 265static int64_t coroutine_fn parallels_co_get_block_status(BlockDriverState *bs,
67a0fd2a 266 int64_t sector_num, int nb_sectors, int *pnum, BlockDriverState **file)
dd3bed16
RK
267{
268 BDRVParallelsState *s = bs->opaque;
269 int64_t offset;
270
271 qemu_co_mutex_lock(&s->lock);
6953d920 272 offset = block_status(s, sector_num, nb_sectors, pnum);
dd3bed16
RK
273 qemu_co_mutex_unlock(&s->lock);
274
dd3bed16
RK
275 if (offset < 0) {
276 return 0;
277 }
278
ddf4987d 279 *file = bs->file->bs;
dd3bed16
RK
280 return (offset << BDRV_SECTOR_BITS) |
281 BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
282}
283
5a41e1fa
DL
284static coroutine_fn int parallels_co_writev(BlockDriverState *bs,
285 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
286{
287 BDRVParallelsState *s = bs->opaque;
288 uint64_t bytes_done = 0;
289 QEMUIOVector hd_qiov;
290 int ret = 0;
291
292 qemu_iovec_init(&hd_qiov, qiov->niov);
293
294 while (nb_sectors > 0) {
295 int64_t position;
296 int n, nbytes;
297
298 qemu_co_mutex_lock(&s->lock);
ddd2ef2c 299 position = allocate_clusters(bs, sector_num, nb_sectors, &n);
5a41e1fa
DL
300 qemu_co_mutex_unlock(&s->lock);
301 if (position < 0) {
302 ret = (int)position;
303 break;
304 }
305
5a41e1fa
DL
306 nbytes = n << BDRV_SECTOR_BITS;
307
308 qemu_iovec_reset(&hd_qiov);
309 qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes);
310
9a4f4c31 311 ret = bdrv_co_writev(bs->file->bs, position, n, &hd_qiov);
5a41e1fa
DL
312 if (ret < 0) {
313 break;
314 }
315
316 nb_sectors -= n;
317 sector_num += n;
318 bytes_done += nbytes;
319 }
320
321 qemu_iovec_destroy(&hd_qiov);
322 return ret;
323}
324
481fb9cf
DL
325static coroutine_fn int parallels_co_readv(BlockDriverState *bs,
326 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
6ada7453 327{
29442569 328 BDRVParallelsState *s = bs->opaque;
481fb9cf
DL
329 uint64_t bytes_done = 0;
330 QEMUIOVector hd_qiov;
331 int ret = 0;
332
333 qemu_iovec_init(&hd_qiov, qiov->niov);
29442569 334
6ada7453 335 while (nb_sectors > 0) {
481fb9cf
DL
336 int64_t position;
337 int n, nbytes;
338
339 qemu_co_mutex_lock(&s->lock);
6953d920 340 position = block_status(s, sector_num, nb_sectors, &n);
481fb9cf
DL
341 qemu_co_mutex_unlock(&s->lock);
342
481fb9cf
DL
343 nbytes = n << BDRV_SECTOR_BITS;
344
345 if (position < 0) {
346 qemu_iovec_memset(qiov, bytes_done, 0, nbytes);
347 } else {
348 qemu_iovec_reset(&hd_qiov);
349 qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes);
350
9a4f4c31 351 ret = bdrv_co_readv(bs->file->bs, position, n, &hd_qiov);
29442569 352 if (ret < 0) {
481fb9cf 353 break;
29442569 354 }
9d8b88f6 355 }
481fb9cf 356
9de9da17
RK
357 nb_sectors -= n;
358 sector_num += n;
481fb9cf 359 bytes_done += nbytes;
6ada7453 360 }
6ada7453 361
481fb9cf 362 qemu_iovec_destroy(&hd_qiov);
2914caa0
PB
363 return ret;
364}
365
49ad6467
DL
366
367static int parallels_check(BlockDriverState *bs, BdrvCheckResult *res,
368 BdrvCheckMode fix)
369{
370 BDRVParallelsState *s = bs->opaque;
371 int64_t size, prev_off, high_off;
372 int ret;
373 uint32_t i;
374 bool flush_bat = false;
375 int cluster_size = s->tracks << BDRV_SECTOR_BITS;
376
9a4f4c31 377 size = bdrv_getlength(bs->file->bs);
49ad6467
DL
378 if (size < 0) {
379 res->check_errors++;
380 return size;
381 }
382
6dd6b9f1
DL
383 if (s->header_unclean) {
384 fprintf(stderr, "%s image was not closed correctly\n",
385 fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR");
386 res->corruptions++;
387 if (fix & BDRV_FIX_ERRORS) {
388 /* parallels_close will do the job right */
389 res->corruptions_fixed++;
390 s->header_unclean = false;
391 }
392 }
393
49ad6467
DL
394 res->bfi.total_clusters = s->bat_size;
395 res->bfi.compressed_clusters = 0; /* compression is not supported */
396
397 high_off = 0;
398 prev_off = 0;
399 for (i = 0; i < s->bat_size; i++) {
400 int64_t off = bat2sect(s, i) << BDRV_SECTOR_BITS;
401 if (off == 0) {
402 prev_off = 0;
403 continue;
404 }
405
406 /* cluster outside the image */
407 if (off > size) {
408 fprintf(stderr, "%s cluster %u is outside image\n",
409 fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR", i);
410 res->corruptions++;
411 if (fix & BDRV_FIX_ERRORS) {
412 prev_off = 0;
413 s->bat_bitmap[i] = 0;
414 res->corruptions_fixed++;
415 flush_bat = true;
416 continue;
417 }
418 }
419
420 res->bfi.allocated_clusters++;
421 if (off > high_off) {
422 high_off = off;
423 }
424
425 if (prev_off != 0 && (prev_off + cluster_size) != off) {
426 res->bfi.fragmented_clusters++;
427 }
428 prev_off = off;
429 }
430
431 if (flush_bat) {
9a4f4c31 432 ret = bdrv_pwrite_sync(bs->file->bs, 0, s->header, s->header_size);
49ad6467
DL
433 if (ret < 0) {
434 res->check_errors++;
435 return ret;
436 }
437 }
438
439 res->image_end_offset = high_off + cluster_size;
440 if (size > res->image_end_offset) {
441 int64_t count;
442 count = DIV_ROUND_UP(size - res->image_end_offset, cluster_size);
443 fprintf(stderr, "%s space leaked at the end of the image %" PRId64 "\n",
444 fix & BDRV_FIX_LEAKS ? "Repairing" : "ERROR",
445 size - res->image_end_offset);
446 res->leaks += count;
447 if (fix & BDRV_FIX_LEAKS) {
9a4f4c31 448 ret = bdrv_truncate(bs->file->bs, res->image_end_offset);
49ad6467
DL
449 if (ret < 0) {
450 res->check_errors++;
451 return ret;
452 }
453 res->leaks_fixed += count;
454 }
455 }
456
457 return 0;
458}
459
460
74cf6c50
DL
461static int parallels_create(const char *filename, QemuOpts *opts, Error **errp)
462{
463 int64_t total_size, cl_size;
464 uint8_t tmp[BDRV_SECTOR_SIZE];
465 Error *local_err = NULL;
8942764f 466 BlockBackend *file;
369f7de9 467 uint32_t bat_entries, bat_sectors;
74cf6c50
DL
468 ParallelsHeader header;
469 int ret;
470
471 total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
472 BDRV_SECTOR_SIZE);
473 cl_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE,
474 DEFAULT_CLUSTER_SIZE), BDRV_SECTOR_SIZE);
475
476 ret = bdrv_create_file(filename, opts, &local_err);
477 if (ret < 0) {
478 error_propagate(errp, local_err);
479 return ret;
480 }
481
efaa7c4e 482 file = blk_new_open(filename, NULL, NULL,
8942764f
KW
483 BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_PROTOCOL,
484 &local_err);
485 if (file == NULL) {
74cf6c50 486 error_propagate(errp, local_err);
8942764f 487 return -EIO;
74cf6c50 488 }
8942764f
KW
489
490 blk_set_allow_write_beyond_eof(file, true);
491
492 ret = blk_truncate(file, 0);
74cf6c50
DL
493 if (ret < 0) {
494 goto exit;
495 }
496
369f7de9 497 bat_entries = DIV_ROUND_UP(total_size, cl_size);
2d68e22e 498 bat_sectors = DIV_ROUND_UP(bat_entry_off(bat_entries), cl_size);
369f7de9 499 bat_sectors = (bat_sectors * cl_size) >> BDRV_SECTOR_BITS;
74cf6c50
DL
500
501 memset(&header, 0, sizeof(header));
502 memcpy(header.magic, HEADER_MAGIC2, sizeof(header.magic));
503 header.version = cpu_to_le32(HEADER_VERSION);
504 /* don't care much about geometry, it is not used on image level */
505 header.heads = cpu_to_le32(16);
506 header.cylinders = cpu_to_le32(total_size / BDRV_SECTOR_SIZE / 16 / 32);
507 header.tracks = cpu_to_le32(cl_size >> BDRV_SECTOR_BITS);
369f7de9 508 header.bat_entries = cpu_to_le32(bat_entries);
74cf6c50 509 header.nb_sectors = cpu_to_le64(DIV_ROUND_UP(total_size, BDRV_SECTOR_SIZE));
369f7de9 510 header.data_off = cpu_to_le32(bat_sectors);
74cf6c50
DL
511
512 /* write all the data */
513 memset(tmp, 0, sizeof(tmp));
514 memcpy(tmp, &header, sizeof(header));
515
8942764f 516 ret = blk_pwrite(file, 0, tmp, BDRV_SECTOR_SIZE);
74cf6c50
DL
517 if (ret < 0) {
518 goto exit;
519 }
8942764f 520 ret = blk_write_zeroes(file, 1, bat_sectors - 1, 0);
74cf6c50
DL
521 if (ret < 0) {
522 goto exit;
523 }
524 ret = 0;
525
526done:
8942764f 527 blk_unref(file);
74cf6c50
DL
528 return ret;
529
530exit:
531 error_setg_errno(errp, -ret, "Failed to create Parallels image");
532 goto done;
533}
534
23d6bd3b
DL
535
536static int parallels_probe(const uint8_t *buf, int buf_size,
537 const char *filename)
538{
539 const ParallelsHeader *ph = (const void *)buf;
540
541 if (buf_size < sizeof(ParallelsHeader)) {
542 return 0;
543 }
544
545 if ((!memcmp(ph->magic, HEADER_MAGIC, 16) ||
546 !memcmp(ph->magic, HEADER_MAGIC2, 16)) &&
547 (le32_to_cpu(ph->version) == HEADER_VERSION)) {
548 return 100;
549 }
550
551 return 0;
552}
553
6dd6b9f1
DL
554static int parallels_update_header(BlockDriverState *bs)
555{
556 BDRVParallelsState *s = bs->opaque;
9a4f4c31
KW
557 unsigned size = MAX(bdrv_opt_mem_align(bs->file->bs),
558 sizeof(ParallelsHeader));
6dd6b9f1
DL
559
560 if (size > s->header_size) {
561 size = s->header_size;
562 }
9a4f4c31 563 return bdrv_pwrite_sync(bs->file->bs, 0, s->header, size);
6dd6b9f1
DL
564}
565
23d6bd3b
DL
566static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
567 Error **errp)
568{
569 BDRVParallelsState *s = bs->opaque;
570 ParallelsHeader ph;
19f5dc15 571 int ret, size, i;
d6179011
DL
572 QemuOpts *opts = NULL;
573 Error *local_err = NULL;
574 char *buf;
23d6bd3b 575
9a4f4c31 576 ret = bdrv_pread(bs->file->bs, 0, &ph, sizeof(ph));
23d6bd3b
DL
577 if (ret < 0) {
578 goto fail;
579 }
580
581 bs->total_sectors = le64_to_cpu(ph.nb_sectors);
582
583 if (le32_to_cpu(ph.version) != HEADER_VERSION) {
584 goto fail_format;
585 }
586 if (!memcmp(ph.magic, HEADER_MAGIC, 16)) {
587 s->off_multiplier = 1;
588 bs->total_sectors = 0xffffffff & bs->total_sectors;
589 } else if (!memcmp(ph.magic, HEADER_MAGIC2, 16)) {
590 s->off_multiplier = le32_to_cpu(ph.tracks);
591 } else {
592 goto fail_format;
593 }
594
595 s->tracks = le32_to_cpu(ph.tracks);
596 if (s->tracks == 0) {
597 error_setg(errp, "Invalid image: Zero sectors per track");
598 ret = -EINVAL;
599 goto fail;
600 }
601 if (s->tracks > INT32_MAX/513) {
602 error_setg(errp, "Invalid image: Too big cluster");
603 ret = -EFBIG;
604 goto fail;
605 }
606
607 s->bat_size = le32_to_cpu(ph.bat_entries);
608 if (s->bat_size > INT_MAX / sizeof(uint32_t)) {
609 error_setg(errp, "Catalog too large");
610 ret = -EFBIG;
611 goto fail;
612 }
613
2d68e22e 614 size = bat_entry_off(s->bat_size);
9a4f4c31
KW
615 s->header_size = ROUND_UP(size, bdrv_opt_mem_align(bs->file->bs));
616 s->header = qemu_try_blockalign(bs->file->bs, s->header_size);
23d6bd3b
DL
617 if (s->header == NULL) {
618 ret = -ENOMEM;
619 goto fail;
620 }
19f5dc15
DL
621 s->data_end = le32_to_cpu(ph.data_off);
622 if (s->data_end == 0) {
623 s->data_end = ROUND_UP(bat_entry_off(s->bat_size), BDRV_SECTOR_SIZE);
624 }
625 if (s->data_end < s->header_size) {
23d6bd3b
DL
626 /* there is not enough unused space to fit to block align between BAT
627 and actual data. We can't avoid read-modify-write... */
628 s->header_size = size;
629 }
630
9a4f4c31 631 ret = bdrv_pread(bs->file->bs, 0, s->header, s->header_size);
23d6bd3b
DL
632 if (ret < 0) {
633 goto fail;
634 }
635 s->bat_bitmap = (uint32_t *)(s->header + 1);
636
19f5dc15
DL
637 for (i = 0; i < s->bat_size; i++) {
638 int64_t off = bat2sect(s, i);
639 if (off >= s->data_end) {
640 s->data_end = off + s->tracks;
641 }
642 }
643
6dd6b9f1
DL
644 if (le32_to_cpu(ph.inuse) == HEADER_INUSE_MAGIC) {
645 /* Image was not closed correctly. The check is mandatory */
646 s->header_unclean = true;
647 if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_CHECK)) {
648 error_setg(errp, "parallels: Image was not closed correctly; "
649 "cannot be opened read/write");
650 ret = -EACCES;
651 goto fail;
652 }
653 }
654
d6179011
DL
655 opts = qemu_opts_create(&parallels_runtime_opts, NULL, 0, &local_err);
656 if (local_err != NULL) {
657 goto fail_options;
658 }
659
660 qemu_opts_absorb_qdict(opts, options, &local_err);
661 if (local_err != NULL) {
662 goto fail_options;
663 }
664
665 s->prealloc_size =
666 qemu_opt_get_size_del(opts, PARALLELS_OPT_PREALLOC_SIZE, 0);
667 s->prealloc_size = MAX(s->tracks, s->prealloc_size >> BDRV_SECTOR_BITS);
668 buf = qemu_opt_get_del(opts, PARALLELS_OPT_PREALLOC_MODE);
669 s->prealloc_mode = qapi_enum_parse(prealloc_mode_lookup, buf,
7fb1cf16 670 PRL_PREALLOC_MODE__MAX, PRL_PREALLOC_MODE_FALLOCATE, &local_err);
d6179011
DL
671 g_free(buf);
672 if (local_err != NULL) {
673 goto fail_options;
674 }
9a4f4c31
KW
675 if (!bdrv_has_zero_init(bs->file->bs) ||
676 bdrv_truncate(bs->file->bs, bdrv_getlength(bs->file->bs)) != 0) {
d6179011
DL
677 s->prealloc_mode = PRL_PREALLOC_MODE_FALLOCATE;
678 }
679
6dd6b9f1
DL
680 if (flags & BDRV_O_RDWR) {
681 s->header->inuse = cpu_to_le32(HEADER_INUSE_MAGIC);
682 ret = parallels_update_header(bs);
683 if (ret < 0) {
684 goto fail;
685 }
686 }
687
0d31c7c2
DL
688 s->bat_dirty_block = 4 * getpagesize();
689 s->bat_dirty_bmap =
690 bitmap_new(DIV_ROUND_UP(s->header_size, s->bat_dirty_block));
691
23d6bd3b
DL
692 qemu_co_mutex_init(&s->lock);
693 return 0;
694
695fail_format:
696 error_setg(errp, "Image not in Parallels format");
697 ret = -EINVAL;
698fail:
699 qemu_vfree(s->header);
700 return ret;
d6179011
DL
701
702fail_options:
703 error_propagate(errp, local_err);
704 ret = -EINVAL;
705 goto fail;
23d6bd3b
DL
706}
707
708
6ada7453
TS
709static void parallels_close(BlockDriverState *bs)
710{
711 BDRVParallelsState *s = bs->opaque;
6dd6b9f1
DL
712
713 if (bs->open_flags & BDRV_O_RDWR) {
714 s->header->inuse = 0;
715 parallels_update_header(bs);
716 }
717
19f5dc15 718 if (bs->open_flags & BDRV_O_RDWR) {
9a4f4c31 719 bdrv_truncate(bs->file->bs, s->data_end << BDRV_SECTOR_BITS);
19f5dc15
DL
720 }
721
0d31c7c2 722 g_free(s->bat_dirty_bmap);
9eae9cca 723 qemu_vfree(s->header);
6ada7453
TS
724}
725
74cf6c50
DL
726static QemuOptsList parallels_create_opts = {
727 .name = "parallels-create-opts",
728 .head = QTAILQ_HEAD_INITIALIZER(parallels_create_opts.head),
729 .desc = {
730 {
731 .name = BLOCK_OPT_SIZE,
732 .type = QEMU_OPT_SIZE,
733 .help = "Virtual disk size",
734 },
735 {
736 .name = BLOCK_OPT_CLUSTER_SIZE,
737 .type = QEMU_OPT_SIZE,
738 .help = "Parallels image cluster size",
739 .def_value_str = stringify(DEFAULT_CLUSTER_SIZE),
740 },
741 { /* end of list */ }
742 }
743};
744
5efa9d5a 745static BlockDriver bdrv_parallels = {
e60f469c
AJ
746 .format_name = "parallels",
747 .instance_size = sizeof(BDRVParallelsState),
748 .bdrv_probe = parallels_probe,
1dec5a70 749 .bdrv_open = parallels_open,
e60f469c 750 .bdrv_close = parallels_close,
dd3bed16 751 .bdrv_co_get_block_status = parallels_co_get_block_status,
d0e61ce5 752 .bdrv_has_zero_init = bdrv_has_zero_init_1,
0d31c7c2 753 .bdrv_co_flush_to_os = parallels_co_flush_to_os,
481fb9cf 754 .bdrv_co_readv = parallels_co_readv,
5a41e1fa 755 .bdrv_co_writev = parallels_co_writev,
74cf6c50
DL
756
757 .bdrv_create = parallels_create,
49ad6467 758 .bdrv_check = parallels_check,
74cf6c50 759 .create_opts = &parallels_create_opts,
6ada7453 760};
5efa9d5a
AL
761
762static void bdrv_parallels_init(void)
763{
764 bdrv_register(&bdrv_parallels);
765}
766
767block_init(bdrv_parallels_init);