2 * Block driver for Parallels disk image format
4 * Copyright (c) 2007 Alex Beregszaszi
5 * Copyright (c) 2015 Denis V. Lunev <den@openvz.org>
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/osdep.h"
31 #include "qemu-common.h"
32 #include "block/block_int.h"
33 #include "qemu/module.h"
34 #include "qemu/bitmap.h"
35 #include "qapi/util.h"
37 /**************************************************************/
39 #define HEADER_MAGIC "WithoutFreeSpace"
40 #define HEADER_MAGIC2 "WithouFreSpacExt"
41 #define HEADER_VERSION 2
42 #define HEADER_INUSE_MAGIC (0x746F6E59)
44 #define DEFAULT_CLUSTER_SIZE 1048576 /* 1 MiB */
47 // always little-endian
48 typedef struct ParallelsHeader
{
49 char magic
[16]; // "WithoutFreeSpace"
59 } QEMU_PACKED ParallelsHeader
;
62 typedef enum ParallelsPreallocMode
{
63 PRL_PREALLOC_MODE_FALLOCATE
= 0,
64 PRL_PREALLOC_MODE_TRUNCATE
= 1,
65 PRL_PREALLOC_MODE__MAX
= 2,
66 } ParallelsPreallocMode
;
68 static const char *prealloc_mode_lookup
[] = {
75 typedef struct BDRVParallelsState
{
76 /** Locking is conservative, the lock protects
77 * - image file extending (truncate, fallocate)
78 * - any access to block allocation table
82 ParallelsHeader
*header
;
86 unsigned long *bat_dirty_bmap
;
87 unsigned int bat_dirty_block
;
90 unsigned int bat_size
;
93 uint64_t prealloc_size
;
94 ParallelsPreallocMode prealloc_mode
;
98 unsigned int off_multiplier
;
102 #define PARALLELS_OPT_PREALLOC_MODE "prealloc-mode"
103 #define PARALLELS_OPT_PREALLOC_SIZE "prealloc-size"
105 static QemuOptsList parallels_runtime_opts
= {
107 .head
= QTAILQ_HEAD_INITIALIZER(parallels_runtime_opts
.head
),
110 .name
= PARALLELS_OPT_PREALLOC_SIZE
,
111 .type
= QEMU_OPT_SIZE
,
112 .help
= "Preallocation size on image expansion",
113 .def_value_str
= "128MiB",
116 .name
= PARALLELS_OPT_PREALLOC_MODE
,
117 .type
= QEMU_OPT_STRING
,
118 .help
= "Preallocation mode on image expansion "
119 "(allowed values: falloc, truncate)",
120 .def_value_str
= "falloc",
122 { /* end of list */ },
127 static int64_t bat2sect(BDRVParallelsState
*s
, uint32_t idx
)
129 return (uint64_t)le32_to_cpu(s
->bat_bitmap
[idx
]) * s
->off_multiplier
;
132 static uint32_t bat_entry_off(uint32_t idx
)
134 return sizeof(ParallelsHeader
) + sizeof(uint32_t) * idx
;
137 static int64_t seek_to_sector(BDRVParallelsState
*s
, int64_t sector_num
)
139 uint32_t index
, offset
;
141 index
= sector_num
/ s
->tracks
;
142 offset
= sector_num
% s
->tracks
;
145 if ((index
>= s
->bat_size
) || (s
->bat_bitmap
[index
] == 0)) {
148 return bat2sect(s
, index
) + offset
;
151 static int cluster_remainder(BDRVParallelsState
*s
, int64_t sector_num
,
154 int ret
= s
->tracks
- sector_num
% s
->tracks
;
155 return MIN(nb_sectors
, ret
);
158 static int64_t block_status(BDRVParallelsState
*s
, int64_t sector_num
,
159 int nb_sectors
, int *pnum
)
161 int64_t start_off
= -2, prev_end_off
= -2;
164 while (nb_sectors
> 0 || start_off
== -2) {
165 int64_t offset
= seek_to_sector(s
, sector_num
);
168 if (start_off
== -2) {
170 prev_end_off
= offset
;
171 } else if (offset
!= prev_end_off
) {
175 to_end
= cluster_remainder(s
, sector_num
, nb_sectors
);
176 nb_sectors
-= to_end
;
177 sector_num
+= to_end
;
181 prev_end_off
+= to_end
;
187 static int64_t allocate_clusters(BlockDriverState
*bs
, int64_t sector_num
,
188 int nb_sectors
, int *pnum
)
190 BDRVParallelsState
*s
= bs
->opaque
;
191 uint32_t idx
, to_allocate
, i
;
194 pos
= block_status(s
, sector_num
, nb_sectors
, pnum
);
199 idx
= sector_num
/ s
->tracks
;
200 if (idx
>= s
->bat_size
) {
204 to_allocate
= (sector_num
+ *pnum
+ s
->tracks
- 1) / s
->tracks
- idx
;
205 space
= to_allocate
* s
->tracks
;
206 if (s
->data_end
+ space
> bdrv_getlength(bs
->file
->bs
) >> BDRV_SECTOR_BITS
) {
208 space
+= s
->prealloc_size
;
209 if (s
->prealloc_mode
== PRL_PREALLOC_MODE_FALLOCATE
) {
210 ret
= bdrv_write_zeroes(bs
->file
->bs
, s
->data_end
, space
, 0);
212 ret
= bdrv_truncate(bs
->file
->bs
,
213 (s
->data_end
+ space
) << BDRV_SECTOR_BITS
);
220 for (i
= 0; i
< to_allocate
; i
++) {
221 s
->bat_bitmap
[idx
+ i
] = cpu_to_le32(s
->data_end
/ s
->off_multiplier
);
222 s
->data_end
+= s
->tracks
;
223 bitmap_set(s
->bat_dirty_bmap
,
224 bat_entry_off(idx
+ i
) / s
->bat_dirty_block
, 1);
227 return bat2sect(s
, idx
) + sector_num
% s
->tracks
;
231 static coroutine_fn
int parallels_co_flush_to_os(BlockDriverState
*bs
)
233 BDRVParallelsState
*s
= bs
->opaque
;
234 unsigned long size
= DIV_ROUND_UP(s
->header_size
, s
->bat_dirty_block
);
237 qemu_co_mutex_lock(&s
->lock
);
239 bit
= find_first_bit(s
->bat_dirty_bmap
, size
);
241 uint32_t off
= bit
* s
->bat_dirty_block
;
242 uint32_t to_write
= s
->bat_dirty_block
;
245 if (off
+ to_write
> s
->header_size
) {
246 to_write
= s
->header_size
- off
;
248 ret
= bdrv_pwrite(bs
->file
->bs
, off
, (uint8_t *)s
->header
+ off
,
251 qemu_co_mutex_unlock(&s
->lock
);
254 bit
= find_next_bit(s
->bat_dirty_bmap
, size
, bit
+ 1);
256 bitmap_zero(s
->bat_dirty_bmap
, size
);
258 qemu_co_mutex_unlock(&s
->lock
);
263 static int64_t coroutine_fn
parallels_co_get_block_status(BlockDriverState
*bs
,
264 int64_t sector_num
, int nb_sectors
, int *pnum
)
266 BDRVParallelsState
*s
= bs
->opaque
;
269 qemu_co_mutex_lock(&s
->lock
);
270 offset
= block_status(s
, sector_num
, nb_sectors
, pnum
);
271 qemu_co_mutex_unlock(&s
->lock
);
277 return (offset
<< BDRV_SECTOR_BITS
) |
278 BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
;
281 static coroutine_fn
int parallels_co_writev(BlockDriverState
*bs
,
282 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
)
284 BDRVParallelsState
*s
= bs
->opaque
;
285 uint64_t bytes_done
= 0;
286 QEMUIOVector hd_qiov
;
289 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
291 while (nb_sectors
> 0) {
295 qemu_co_mutex_lock(&s
->lock
);
296 position
= allocate_clusters(bs
, sector_num
, nb_sectors
, &n
);
297 qemu_co_mutex_unlock(&s
->lock
);
303 nbytes
= n
<< BDRV_SECTOR_BITS
;
305 qemu_iovec_reset(&hd_qiov
);
306 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
, nbytes
);
308 ret
= bdrv_co_writev(bs
->file
->bs
, position
, n
, &hd_qiov
);
315 bytes_done
+= nbytes
;
318 qemu_iovec_destroy(&hd_qiov
);
322 static coroutine_fn
int parallels_co_readv(BlockDriverState
*bs
,
323 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
)
325 BDRVParallelsState
*s
= bs
->opaque
;
326 uint64_t bytes_done
= 0;
327 QEMUIOVector hd_qiov
;
330 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
332 while (nb_sectors
> 0) {
336 qemu_co_mutex_lock(&s
->lock
);
337 position
= block_status(s
, sector_num
, nb_sectors
, &n
);
338 qemu_co_mutex_unlock(&s
->lock
);
340 nbytes
= n
<< BDRV_SECTOR_BITS
;
343 qemu_iovec_memset(qiov
, bytes_done
, 0, nbytes
);
345 qemu_iovec_reset(&hd_qiov
);
346 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
, nbytes
);
348 ret
= bdrv_co_readv(bs
->file
->bs
, position
, n
, &hd_qiov
);
356 bytes_done
+= nbytes
;
359 qemu_iovec_destroy(&hd_qiov
);
364 static int parallels_check(BlockDriverState
*bs
, BdrvCheckResult
*res
,
367 BDRVParallelsState
*s
= bs
->opaque
;
368 int64_t size
, prev_off
, high_off
;
371 bool flush_bat
= false;
372 int cluster_size
= s
->tracks
<< BDRV_SECTOR_BITS
;
374 size
= bdrv_getlength(bs
->file
->bs
);
380 if (s
->header_unclean
) {
381 fprintf(stderr
, "%s image was not closed correctly\n",
382 fix
& BDRV_FIX_ERRORS
? "Repairing" : "ERROR");
384 if (fix
& BDRV_FIX_ERRORS
) {
385 /* parallels_close will do the job right */
386 res
->corruptions_fixed
++;
387 s
->header_unclean
= false;
391 res
->bfi
.total_clusters
= s
->bat_size
;
392 res
->bfi
.compressed_clusters
= 0; /* compression is not supported */
396 for (i
= 0; i
< s
->bat_size
; i
++) {
397 int64_t off
= bat2sect(s
, i
) << BDRV_SECTOR_BITS
;
403 /* cluster outside the image */
405 fprintf(stderr
, "%s cluster %u is outside image\n",
406 fix
& BDRV_FIX_ERRORS
? "Repairing" : "ERROR", i
);
408 if (fix
& BDRV_FIX_ERRORS
) {
410 s
->bat_bitmap
[i
] = 0;
411 res
->corruptions_fixed
++;
417 res
->bfi
.allocated_clusters
++;
418 if (off
> high_off
) {
422 if (prev_off
!= 0 && (prev_off
+ cluster_size
) != off
) {
423 res
->bfi
.fragmented_clusters
++;
429 ret
= bdrv_pwrite_sync(bs
->file
->bs
, 0, s
->header
, s
->header_size
);
436 res
->image_end_offset
= high_off
+ cluster_size
;
437 if (size
> res
->image_end_offset
) {
439 count
= DIV_ROUND_UP(size
- res
->image_end_offset
, cluster_size
);
440 fprintf(stderr
, "%s space leaked at the end of the image %" PRId64
"\n",
441 fix
& BDRV_FIX_LEAKS
? "Repairing" : "ERROR",
442 size
- res
->image_end_offset
);
444 if (fix
& BDRV_FIX_LEAKS
) {
445 ret
= bdrv_truncate(bs
->file
->bs
, res
->image_end_offset
);
450 res
->leaks_fixed
+= count
;
458 static int parallels_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
460 int64_t total_size
, cl_size
;
461 uint8_t tmp
[BDRV_SECTOR_SIZE
];
462 Error
*local_err
= NULL
;
463 BlockDriverState
*file
;
464 uint32_t bat_entries
, bat_sectors
;
465 ParallelsHeader header
;
468 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
470 cl_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_CLUSTER_SIZE
,
471 DEFAULT_CLUSTER_SIZE
), BDRV_SECTOR_SIZE
);
473 ret
= bdrv_create_file(filename
, opts
, &local_err
);
475 error_propagate(errp
, local_err
);
480 ret
= bdrv_open(&file
, filename
, NULL
, NULL
,
481 BDRV_O_RDWR
| BDRV_O_PROTOCOL
, &local_err
);
483 error_propagate(errp
, local_err
);
486 ret
= bdrv_truncate(file
, 0);
491 bat_entries
= DIV_ROUND_UP(total_size
, cl_size
);
492 bat_sectors
= DIV_ROUND_UP(bat_entry_off(bat_entries
), cl_size
);
493 bat_sectors
= (bat_sectors
* cl_size
) >> BDRV_SECTOR_BITS
;
495 memset(&header
, 0, sizeof(header
));
496 memcpy(header
.magic
, HEADER_MAGIC2
, sizeof(header
.magic
));
497 header
.version
= cpu_to_le32(HEADER_VERSION
);
498 /* don't care much about geometry, it is not used on image level */
499 header
.heads
= cpu_to_le32(16);
500 header
.cylinders
= cpu_to_le32(total_size
/ BDRV_SECTOR_SIZE
/ 16 / 32);
501 header
.tracks
= cpu_to_le32(cl_size
>> BDRV_SECTOR_BITS
);
502 header
.bat_entries
= cpu_to_le32(bat_entries
);
503 header
.nb_sectors
= cpu_to_le64(DIV_ROUND_UP(total_size
, BDRV_SECTOR_SIZE
));
504 header
.data_off
= cpu_to_le32(bat_sectors
);
506 /* write all the data */
507 memset(tmp
, 0, sizeof(tmp
));
508 memcpy(tmp
, &header
, sizeof(header
));
510 ret
= bdrv_pwrite(file
, 0, tmp
, BDRV_SECTOR_SIZE
);
514 ret
= bdrv_write_zeroes(file
, 1, bat_sectors
- 1, 0);
525 error_setg_errno(errp
, -ret
, "Failed to create Parallels image");
530 static int parallels_probe(const uint8_t *buf
, int buf_size
,
531 const char *filename
)
533 const ParallelsHeader
*ph
= (const void *)buf
;
535 if (buf_size
< sizeof(ParallelsHeader
)) {
539 if ((!memcmp(ph
->magic
, HEADER_MAGIC
, 16) ||
540 !memcmp(ph
->magic
, HEADER_MAGIC2
, 16)) &&
541 (le32_to_cpu(ph
->version
) == HEADER_VERSION
)) {
548 static int parallels_update_header(BlockDriverState
*bs
)
550 BDRVParallelsState
*s
= bs
->opaque
;
551 unsigned size
= MAX(bdrv_opt_mem_align(bs
->file
->bs
),
552 sizeof(ParallelsHeader
));
554 if (size
> s
->header_size
) {
555 size
= s
->header_size
;
557 return bdrv_pwrite_sync(bs
->file
->bs
, 0, s
->header
, size
);
560 static int parallels_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
563 BDRVParallelsState
*s
= bs
->opaque
;
566 QemuOpts
*opts
= NULL
;
567 Error
*local_err
= NULL
;
570 ret
= bdrv_pread(bs
->file
->bs
, 0, &ph
, sizeof(ph
));
575 bs
->total_sectors
= le64_to_cpu(ph
.nb_sectors
);
577 if (le32_to_cpu(ph
.version
) != HEADER_VERSION
) {
580 if (!memcmp(ph
.magic
, HEADER_MAGIC
, 16)) {
581 s
->off_multiplier
= 1;
582 bs
->total_sectors
= 0xffffffff & bs
->total_sectors
;
583 } else if (!memcmp(ph
.magic
, HEADER_MAGIC2
, 16)) {
584 s
->off_multiplier
= le32_to_cpu(ph
.tracks
);
589 s
->tracks
= le32_to_cpu(ph
.tracks
);
590 if (s
->tracks
== 0) {
591 error_setg(errp
, "Invalid image: Zero sectors per track");
595 if (s
->tracks
> INT32_MAX
/513) {
596 error_setg(errp
, "Invalid image: Too big cluster");
601 s
->bat_size
= le32_to_cpu(ph
.bat_entries
);
602 if (s
->bat_size
> INT_MAX
/ sizeof(uint32_t)) {
603 error_setg(errp
, "Catalog too large");
608 size
= bat_entry_off(s
->bat_size
);
609 s
->header_size
= ROUND_UP(size
, bdrv_opt_mem_align(bs
->file
->bs
));
610 s
->header
= qemu_try_blockalign(bs
->file
->bs
, s
->header_size
);
611 if (s
->header
== NULL
) {
615 s
->data_end
= le32_to_cpu(ph
.data_off
);
616 if (s
->data_end
== 0) {
617 s
->data_end
= ROUND_UP(bat_entry_off(s
->bat_size
), BDRV_SECTOR_SIZE
);
619 if (s
->data_end
< s
->header_size
) {
620 /* there is not enough unused space to fit to block align between BAT
621 and actual data. We can't avoid read-modify-write... */
622 s
->header_size
= size
;
625 ret
= bdrv_pread(bs
->file
->bs
, 0, s
->header
, s
->header_size
);
629 s
->bat_bitmap
= (uint32_t *)(s
->header
+ 1);
631 for (i
= 0; i
< s
->bat_size
; i
++) {
632 int64_t off
= bat2sect(s
, i
);
633 if (off
>= s
->data_end
) {
634 s
->data_end
= off
+ s
->tracks
;
638 if (le32_to_cpu(ph
.inuse
) == HEADER_INUSE_MAGIC
) {
639 /* Image was not closed correctly. The check is mandatory */
640 s
->header_unclean
= true;
641 if ((flags
& BDRV_O_RDWR
) && !(flags
& BDRV_O_CHECK
)) {
642 error_setg(errp
, "parallels: Image was not closed correctly; "
643 "cannot be opened read/write");
649 opts
= qemu_opts_create(¶llels_runtime_opts
, NULL
, 0, &local_err
);
650 if (local_err
!= NULL
) {
654 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
655 if (local_err
!= NULL
) {
660 qemu_opt_get_size_del(opts
, PARALLELS_OPT_PREALLOC_SIZE
, 0);
661 s
->prealloc_size
= MAX(s
->tracks
, s
->prealloc_size
>> BDRV_SECTOR_BITS
);
662 buf
= qemu_opt_get_del(opts
, PARALLELS_OPT_PREALLOC_MODE
);
663 s
->prealloc_mode
= qapi_enum_parse(prealloc_mode_lookup
, buf
,
664 PRL_PREALLOC_MODE__MAX
, PRL_PREALLOC_MODE_FALLOCATE
, &local_err
);
666 if (local_err
!= NULL
) {
669 if (!bdrv_has_zero_init(bs
->file
->bs
) ||
670 bdrv_truncate(bs
->file
->bs
, bdrv_getlength(bs
->file
->bs
)) != 0) {
671 s
->prealloc_mode
= PRL_PREALLOC_MODE_FALLOCATE
;
674 if (flags
& BDRV_O_RDWR
) {
675 s
->header
->inuse
= cpu_to_le32(HEADER_INUSE_MAGIC
);
676 ret
= parallels_update_header(bs
);
682 s
->bat_dirty_block
= 4 * getpagesize();
684 bitmap_new(DIV_ROUND_UP(s
->header_size
, s
->bat_dirty_block
));
686 qemu_co_mutex_init(&s
->lock
);
690 error_setg(errp
, "Image not in Parallels format");
693 qemu_vfree(s
->header
);
697 error_propagate(errp
, local_err
);
703 static void parallels_close(BlockDriverState
*bs
)
705 BDRVParallelsState
*s
= bs
->opaque
;
707 if (bs
->open_flags
& BDRV_O_RDWR
) {
708 s
->header
->inuse
= 0;
709 parallels_update_header(bs
);
712 if (bs
->open_flags
& BDRV_O_RDWR
) {
713 bdrv_truncate(bs
->file
->bs
, s
->data_end
<< BDRV_SECTOR_BITS
);
716 g_free(s
->bat_dirty_bmap
);
717 qemu_vfree(s
->header
);
720 static QemuOptsList parallels_create_opts
= {
721 .name
= "parallels-create-opts",
722 .head
= QTAILQ_HEAD_INITIALIZER(parallels_create_opts
.head
),
725 .name
= BLOCK_OPT_SIZE
,
726 .type
= QEMU_OPT_SIZE
,
727 .help
= "Virtual disk size",
730 .name
= BLOCK_OPT_CLUSTER_SIZE
,
731 .type
= QEMU_OPT_SIZE
,
732 .help
= "Parallels image cluster size",
733 .def_value_str
= stringify(DEFAULT_CLUSTER_SIZE
),
735 { /* end of list */ }
739 static BlockDriver bdrv_parallels
= {
740 .format_name
= "parallels",
741 .instance_size
= sizeof(BDRVParallelsState
),
742 .bdrv_probe
= parallels_probe
,
743 .bdrv_open
= parallels_open
,
744 .bdrv_close
= parallels_close
,
745 .bdrv_co_get_block_status
= parallels_co_get_block_status
,
746 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
747 .bdrv_co_flush_to_os
= parallels_co_flush_to_os
,
748 .bdrv_co_readv
= parallels_co_readv
,
749 .bdrv_co_writev
= parallels_co_writev
,
751 .bdrv_create
= parallels_create
,
752 .bdrv_check
= parallels_check
,
753 .create_opts
= ¶llels_create_opts
,
756 static void bdrv_parallels_init(void)
758 bdrv_register(&bdrv_parallels
);
761 block_init(bdrv_parallels_init
);