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