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