2 * Block driver for Parallels disk image format
4 * Copyright (c) 2007 Alex Beregszaszi
7 * This code was originally based on comparing different disk images created
8 * by Parallels. Currently it is based on opened OpenVZ sources
10 * http://git.openvz.org/?p=ploop;a=summary
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:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
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
30 #include "qemu-common.h"
31 #include "block/block_int.h"
32 #include "qemu/module.h"
33 #include "qemu/bitmap.h"
34 #include "qapi/util.h"
36 /**************************************************************/
38 #define HEADER_MAGIC "WithoutFreeSpace"
39 #define HEADER_MAGIC2 "WithouFreSpacExt"
40 #define HEADER_VERSION 2
41 #define HEADER_INUSE_MAGIC (0x746F6E59)
43 #define DEFAULT_CLUSTER_SIZE 1048576 /* 1 MiB */
46 // always little-endian
47 typedef struct ParallelsHeader {
48 char magic[16]; // "WithoutFreeSpace"
58 } QEMU_PACKED ParallelsHeader;
61 typedef enum ParallelsPreallocMode {
62 PRL_PREALLOC_MODE_FALLOCATE = 0,
63 PRL_PREALLOC_MODE_TRUNCATE = 1,
64 PRL_PREALLOC_MODE_MAX = 2,
65 } ParallelsPreallocMode;
67 static const char *prealloc_mode_lookup[] = {
74 typedef struct BDRVParallelsState {
75 /** Locking is conservative, the lock protects
76 * - image file extending (truncate, fallocate)
77 * - any access to block allocation table
81 ParallelsHeader *header;
85 unsigned long *bat_dirty_bmap;
86 unsigned int bat_dirty_block;
89 unsigned int bat_size;
92 uint64_t prealloc_size;
93 ParallelsPreallocMode prealloc_mode;
97 unsigned int off_multiplier;
101 #define PARALLELS_OPT_PREALLOC_MODE "prealloc-mode"
102 #define PARALLELS_OPT_PREALLOC_SIZE "prealloc-size"
104 static QemuOptsList parallels_runtime_opts = {
106 .head = QTAILQ_HEAD_INITIALIZER(parallels_runtime_opts.head),
109 .name = PARALLELS_OPT_PREALLOC_SIZE,
110 .type = QEMU_OPT_SIZE,
111 .help = "Preallocation size on image expansion",
112 .def_value_str = "128MiB",
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",
121 { /* end of list */ },
126 static int64_t bat2sect(BDRVParallelsState *s, uint32_t idx)
128 return (uint64_t)le32_to_cpu(s->bat_bitmap[idx]) * s->off_multiplier;
131 static uint32_t bat_entry_off(uint32_t idx)
133 return sizeof(ParallelsHeader) + sizeof(uint32_t) * idx;
136 static int64_t seek_to_sector(BDRVParallelsState *s, int64_t sector_num)
138 uint32_t index, offset;
140 index = sector_num / s->tracks;
141 offset = sector_num % s->tracks;
144 if ((index >= s->bat_size) || (s->bat_bitmap[index] == 0)) {
147 return bat2sect(s, index) + offset;
150 static int cluster_remainder(BDRVParallelsState *s, int64_t sector_num,
153 int ret = s->tracks - sector_num % s->tracks;
154 return MIN(nb_sectors, ret);
157 static int64_t block_status(BDRVParallelsState *s, int64_t sector_num,
158 int nb_sectors, int *pnum)
160 int64_t start_off = -2, prev_end_off = -2;
163 while (nb_sectors > 0 || start_off == -2) {
164 int64_t offset = seek_to_sector(s, sector_num);
167 if (start_off == -2) {
169 prev_end_off = offset;
170 } else if (offset != prev_end_off) {
174 to_end = cluster_remainder(s, sector_num, nb_sectors);
175 nb_sectors -= to_end;
176 sector_num += to_end;
180 prev_end_off += to_end;
186 static int64_t allocate_clusters(BlockDriverState *bs, int64_t sector_num,
187 int nb_sectors, int *pnum)
189 BDRVParallelsState *s = bs->opaque;
190 uint32_t idx, to_allocate, i;
193 pos = block_status(s, sector_num, nb_sectors, pnum);
198 idx = sector_num / s->tracks;
199 if (idx >= s->bat_size) {
203 to_allocate = (sector_num + *pnum + s->tracks - 1) / s->tracks - idx;
204 space = to_allocate * s->tracks;
205 if (s->data_end + space > bdrv_getlength(bs->file->bs) >> BDRV_SECTOR_BITS) {
207 space += s->prealloc_size;
208 if (s->prealloc_mode == PRL_PREALLOC_MODE_FALLOCATE) {
209 ret = bdrv_write_zeroes(bs->file->bs, s->data_end, space, 0);
211 ret = bdrv_truncate(bs->file->bs,
212 (s->data_end + space) << BDRV_SECTOR_BITS);
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);
226 return bat2sect(s, idx) + sector_num % s->tracks;
230 static coroutine_fn int parallels_co_flush_to_os(BlockDriverState *bs)
232 BDRVParallelsState *s = bs->opaque;
233 unsigned long size = DIV_ROUND_UP(s->header_size, s->bat_dirty_block);
236 qemu_co_mutex_lock(&s->lock);
238 bit = find_first_bit(s->bat_dirty_bmap, size);
240 uint32_t off = bit * s->bat_dirty_block;
241 uint32_t to_write = s->bat_dirty_block;
244 if (off + to_write > s->header_size) {
245 to_write = s->header_size - off;
247 ret = bdrv_pwrite(bs->file->bs, off, (uint8_t *)s->header + off,
250 qemu_co_mutex_unlock(&s->lock);
253 bit = find_next_bit(s->bat_dirty_bmap, size, bit + 1);
255 bitmap_zero(s->bat_dirty_bmap, size);
257 qemu_co_mutex_unlock(&s->lock);
262 static int64_t coroutine_fn parallels_co_get_block_status(BlockDriverState *bs,
263 int64_t sector_num, int nb_sectors, int *pnum)
265 BDRVParallelsState *s = bs->opaque;
268 qemu_co_mutex_lock(&s->lock);
269 offset = block_status(s, sector_num, nb_sectors, pnum);
270 qemu_co_mutex_unlock(&s->lock);
276 return (offset << BDRV_SECTOR_BITS) |
277 BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
280 static coroutine_fn int parallels_co_writev(BlockDriverState *bs,
281 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
283 BDRVParallelsState *s = bs->opaque;
284 uint64_t bytes_done = 0;
285 QEMUIOVector hd_qiov;
288 qemu_iovec_init(&hd_qiov, qiov->niov);
290 while (nb_sectors > 0) {
294 qemu_co_mutex_lock(&s->lock);
295 position = allocate_clusters(bs, sector_num, nb_sectors, &n);
296 qemu_co_mutex_unlock(&s->lock);
302 nbytes = n << BDRV_SECTOR_BITS;
304 qemu_iovec_reset(&hd_qiov);
305 qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes);
307 ret = bdrv_co_writev(bs->file->bs, position, n, &hd_qiov);
314 bytes_done += nbytes;
317 qemu_iovec_destroy(&hd_qiov);
321 static coroutine_fn int parallels_co_readv(BlockDriverState *bs,
322 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
324 BDRVParallelsState *s = bs->opaque;
325 uint64_t bytes_done = 0;
326 QEMUIOVector hd_qiov;
329 qemu_iovec_init(&hd_qiov, qiov->niov);
331 while (nb_sectors > 0) {
335 qemu_co_mutex_lock(&s->lock);
336 position = block_status(s, sector_num, nb_sectors, &n);
337 qemu_co_mutex_unlock(&s->lock);
339 nbytes = n << BDRV_SECTOR_BITS;
342 qemu_iovec_memset(qiov, bytes_done, 0, nbytes);
344 qemu_iovec_reset(&hd_qiov);
345 qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes);
347 ret = bdrv_co_readv(bs->file->bs, position, n, &hd_qiov);
355 bytes_done += nbytes;
358 qemu_iovec_destroy(&hd_qiov);
363 static int parallels_check(BlockDriverState *bs, BdrvCheckResult *res,
366 BDRVParallelsState *s = bs->opaque;
367 int64_t size, prev_off, high_off;
370 bool flush_bat = false;
371 int cluster_size = s->tracks << BDRV_SECTOR_BITS;
373 size = bdrv_getlength(bs->file->bs);
379 if (s->header_unclean) {
380 fprintf(stderr, "%s image was not closed correctly\n",
381 fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR");
383 if (fix & BDRV_FIX_ERRORS) {
384 /* parallels_close will do the job right */
385 res->corruptions_fixed++;
386 s->header_unclean = false;
390 res->bfi.total_clusters = s->bat_size;
391 res->bfi.compressed_clusters = 0; /* compression is not supported */
395 for (i = 0; i < s->bat_size; i++) {
396 int64_t off = bat2sect(s, i) << BDRV_SECTOR_BITS;
402 /* cluster outside the image */
404 fprintf(stderr, "%s cluster %u is outside image\n",
405 fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR", i);
407 if (fix & BDRV_FIX_ERRORS) {
409 s->bat_bitmap[i] = 0;
410 res->corruptions_fixed++;
416 res->bfi.allocated_clusters++;
417 if (off > high_off) {
421 if (prev_off != 0 && (prev_off + cluster_size) != off) {
422 res->bfi.fragmented_clusters++;
428 ret = bdrv_pwrite_sync(bs->file->bs, 0, s->header, s->header_size);
435 res->image_end_offset = high_off + cluster_size;
436 if (size > res->image_end_offset) {
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);
443 if (fix & BDRV_FIX_LEAKS) {
444 ret = bdrv_truncate(bs->file->bs, res->image_end_offset);
449 res->leaks_fixed += count;
457 static int parallels_create(const char *filename, QemuOpts *opts, Error **errp)
459 int64_t total_size, cl_size;
460 uint8_t tmp[BDRV_SECTOR_SIZE];
461 Error *local_err = NULL;
462 BlockDriverState *file;
463 uint32_t bat_entries, bat_sectors;
464 ParallelsHeader header;
467 total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
469 cl_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE,
470 DEFAULT_CLUSTER_SIZE), BDRV_SECTOR_SIZE);
472 ret = bdrv_create_file(filename, opts, &local_err);
474 error_propagate(errp, local_err);
479 ret = bdrv_open(&file, filename, NULL, NULL,
480 BDRV_O_RDWR | BDRV_O_PROTOCOL, &local_err);
482 error_propagate(errp, local_err);
485 ret = bdrv_truncate(file, 0);
490 bat_entries = DIV_ROUND_UP(total_size, cl_size);
491 bat_sectors = DIV_ROUND_UP(bat_entry_off(bat_entries), cl_size);
492 bat_sectors = (bat_sectors * cl_size) >> BDRV_SECTOR_BITS;
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);
501 header.bat_entries = cpu_to_le32(bat_entries);
502 header.nb_sectors = cpu_to_le64(DIV_ROUND_UP(total_size, BDRV_SECTOR_SIZE));
503 header.data_off = cpu_to_le32(bat_sectors);
505 /* write all the data */
506 memset(tmp, 0, sizeof(tmp));
507 memcpy(tmp, &header, sizeof(header));
509 ret = bdrv_pwrite(file, 0, tmp, BDRV_SECTOR_SIZE);
513 ret = bdrv_write_zeroes(file, 1, bat_sectors - 1, 0);
524 error_setg_errno(errp, -ret, "Failed to create Parallels image");
529 static int parallels_probe(const uint8_t *buf, int buf_size,
530 const char *filename)
532 const ParallelsHeader *ph = (const void *)buf;
534 if (buf_size < sizeof(ParallelsHeader)) {
538 if ((!memcmp(ph->magic, HEADER_MAGIC, 16) ||
539 !memcmp(ph->magic, HEADER_MAGIC2, 16)) &&
540 (le32_to_cpu(ph->version) == HEADER_VERSION)) {
547 static int parallels_update_header(BlockDriverState *bs)
549 BDRVParallelsState *s = bs->opaque;
550 unsigned size = MAX(bdrv_opt_mem_align(bs->file->bs),
551 sizeof(ParallelsHeader));
553 if (size > s->header_size) {
554 size = s->header_size;
556 return bdrv_pwrite_sync(bs->file->bs, 0, s->header, size);
559 static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
562 BDRVParallelsState *s = bs->opaque;
565 QemuOpts *opts = NULL;
566 Error *local_err = NULL;
569 ret = bdrv_pread(bs->file->bs, 0, &ph, sizeof(ph));
574 bs->total_sectors = le64_to_cpu(ph.nb_sectors);
576 if (le32_to_cpu(ph.version) != HEADER_VERSION) {
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);
588 s->tracks = le32_to_cpu(ph.tracks);
589 if (s->tracks == 0) {
590 error_setg(errp, "Invalid image: Zero sectors per track");
594 if (s->tracks > INT32_MAX/513) {
595 error_setg(errp, "Invalid image: Too big cluster");
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");
607 size = bat_entry_off(s->bat_size);
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);
610 if (s->header == NULL) {
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);
618 if (s->data_end < s->header_size) {
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;
624 ret = bdrv_pread(bs->file->bs, 0, s->header, s->header_size);
628 s->bat_bitmap = (uint32_t *)(s->header + 1);
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;
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");
648 opts = qemu_opts_create(¶llels_runtime_opts, NULL, 0, &local_err);
649 if (local_err != NULL) {
653 qemu_opts_absorb_qdict(opts, options, &local_err);
654 if (local_err != NULL) {
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);
665 if (local_err != NULL) {
668 if (!bdrv_has_zero_init(bs->file->bs) ||
669 bdrv_truncate(bs->file->bs, bdrv_getlength(bs->file->bs)) != 0) {
670 s->prealloc_mode = PRL_PREALLOC_MODE_FALLOCATE;
673 if (flags & BDRV_O_RDWR) {
674 s->header->inuse = cpu_to_le32(HEADER_INUSE_MAGIC);
675 ret = parallels_update_header(bs);
681 s->bat_dirty_block = 4 * getpagesize();
683 bitmap_new(DIV_ROUND_UP(s->header_size, s->bat_dirty_block));
685 qemu_co_mutex_init(&s->lock);
689 error_setg(errp, "Image not in Parallels format");
692 qemu_vfree(s->header);
696 error_propagate(errp, local_err);
702 static void parallels_close(BlockDriverState *bs)
704 BDRVParallelsState *s = bs->opaque;
706 if (bs->open_flags & BDRV_O_RDWR) {
707 s->header->inuse = 0;
708 parallels_update_header(bs);
711 if (bs->open_flags & BDRV_O_RDWR) {
712 bdrv_truncate(bs->file->bs, s->data_end << BDRV_SECTOR_BITS);
715 g_free(s->bat_dirty_bmap);
716 qemu_vfree(s->header);
719 static QemuOptsList parallels_create_opts = {
720 .name = "parallels-create-opts",
721 .head = QTAILQ_HEAD_INITIALIZER(parallels_create_opts.head),
724 .name = BLOCK_OPT_SIZE,
725 .type = QEMU_OPT_SIZE,
726 .help = "Virtual disk size",
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),
734 { /* end of list */ }
738 static BlockDriver bdrv_parallels = {
739 .format_name = "parallels",
740 .instance_size = sizeof(BDRVParallelsState),
741 .bdrv_probe = parallels_probe,
742 .bdrv_open = parallels_open,
743 .bdrv_close = parallels_close,
744 .bdrv_co_get_block_status = parallels_co_get_block_status,
745 .bdrv_has_zero_init = bdrv_has_zero_init_1,
746 .bdrv_co_flush_to_os = parallels_co_flush_to_os,
747 .bdrv_co_readv = parallels_co_readv,
748 .bdrv_co_writev = parallels_co_writev,
750 .bdrv_create = parallels_create,
751 .bdrv_check = parallels_check,
752 .create_opts = ¶llels_create_opts,
755 static void bdrv_parallels_init(void)
757 bdrv_register(&bdrv_parallels);
760 block_init(bdrv_parallels_init);