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