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 "qapi/error.h"
32 #include "qemu-common.h"
33 #include "block/block_int.h"
34 #include "sysemu/block-backend.h"
35 #include "qemu/module.h"
36 #include "qemu/bitmap.h"
37 #include "qapi/util.h"
39 /**************************************************************/
41 #define HEADER_MAGIC "WithoutFreeSpace"
42 #define HEADER_MAGIC2 "WithouFreSpacExt"
43 #define HEADER_VERSION 2
44 #define HEADER_INUSE_MAGIC (0x746F6E59)
46 #define DEFAULT_CLUSTER_SIZE 1048576 /* 1 MiB */
49 // always little-endian
50 typedef struct ParallelsHeader
{
51 char magic
[16]; // "WithoutFreeSpace"
61 } QEMU_PACKED ParallelsHeader
;
64 typedef enum ParallelsPreallocMode
{
65 PRL_PREALLOC_MODE_FALLOCATE
= 0,
66 PRL_PREALLOC_MODE_TRUNCATE
= 1,
67 PRL_PREALLOC_MODE__MAX
= 2,
68 } ParallelsPreallocMode
;
70 static const char *prealloc_mode_lookup
[] = {
77 typedef struct BDRVParallelsState
{
78 /** Locking is conservative, the lock protects
79 * - image file extending (truncate, fallocate)
80 * - any access to block allocation table
84 ParallelsHeader
*header
;
88 unsigned long *bat_dirty_bmap
;
89 unsigned int bat_dirty_block
;
92 unsigned int bat_size
;
95 uint64_t prealloc_size
;
96 ParallelsPreallocMode prealloc_mode
;
100 unsigned int off_multiplier
;
101 } BDRVParallelsState
;
104 #define PARALLELS_OPT_PREALLOC_MODE "prealloc-mode"
105 #define PARALLELS_OPT_PREALLOC_SIZE "prealloc-size"
107 static QemuOptsList parallels_runtime_opts
= {
109 .head
= QTAILQ_HEAD_INITIALIZER(parallels_runtime_opts
.head
),
112 .name
= PARALLELS_OPT_PREALLOC_SIZE
,
113 .type
= QEMU_OPT_SIZE
,
114 .help
= "Preallocation size on image expansion",
115 .def_value_str
= "128MiB",
118 .name
= PARALLELS_OPT_PREALLOC_MODE
,
119 .type
= QEMU_OPT_STRING
,
120 .help
= "Preallocation mode on image expansion "
121 "(allowed values: falloc, truncate)",
122 .def_value_str
= "falloc",
124 { /* end of list */ },
129 static int64_t bat2sect(BDRVParallelsState
*s
, uint32_t idx
)
131 return (uint64_t)le32_to_cpu(s
->bat_bitmap
[idx
]) * s
->off_multiplier
;
134 static uint32_t bat_entry_off(uint32_t idx
)
136 return sizeof(ParallelsHeader
) + sizeof(uint32_t) * idx
;
139 static int64_t seek_to_sector(BDRVParallelsState
*s
, int64_t sector_num
)
141 uint32_t index
, offset
;
143 index
= sector_num
/ s
->tracks
;
144 offset
= sector_num
% s
->tracks
;
147 if ((index
>= s
->bat_size
) || (s
->bat_bitmap
[index
] == 0)) {
150 return bat2sect(s
, index
) + offset
;
153 static int cluster_remainder(BDRVParallelsState
*s
, int64_t sector_num
,
156 int ret
= s
->tracks
- sector_num
% s
->tracks
;
157 return MIN(nb_sectors
, ret
);
160 static int64_t block_status(BDRVParallelsState
*s
, int64_t sector_num
,
161 int nb_sectors
, int *pnum
)
163 int64_t start_off
= -2, prev_end_off
= -2;
166 while (nb_sectors
> 0 || start_off
== -2) {
167 int64_t offset
= seek_to_sector(s
, sector_num
);
170 if (start_off
== -2) {
172 prev_end_off
= offset
;
173 } else if (offset
!= prev_end_off
) {
177 to_end
= cluster_remainder(s
, sector_num
, nb_sectors
);
178 nb_sectors
-= to_end
;
179 sector_num
+= to_end
;
183 prev_end_off
+= to_end
;
189 static int64_t allocate_clusters(BlockDriverState
*bs
, int64_t sector_num
,
190 int nb_sectors
, int *pnum
)
192 BDRVParallelsState
*s
= bs
->opaque
;
193 uint32_t idx
, to_allocate
, i
;
196 pos
= block_status(s
, sector_num
, nb_sectors
, pnum
);
201 idx
= sector_num
/ s
->tracks
;
202 if (idx
>= s
->bat_size
) {
206 to_allocate
= (sector_num
+ *pnum
+ s
->tracks
- 1) / s
->tracks
- idx
;
207 space
= to_allocate
* s
->tracks
;
208 if (s
->data_end
+ space
> bdrv_getlength(bs
->file
->bs
) >> BDRV_SECTOR_BITS
) {
210 space
+= s
->prealloc_size
;
211 if (s
->prealloc_mode
== PRL_PREALLOC_MODE_FALLOCATE
) {
212 ret
= bdrv_write_zeroes(bs
->file
->bs
, s
->data_end
, space
, 0);
214 ret
= bdrv_truncate(bs
->file
->bs
,
215 (s
->data_end
+ space
) << BDRV_SECTOR_BITS
);
222 for (i
= 0; i
< to_allocate
; i
++) {
223 s
->bat_bitmap
[idx
+ i
] = cpu_to_le32(s
->data_end
/ s
->off_multiplier
);
224 s
->data_end
+= s
->tracks
;
225 bitmap_set(s
->bat_dirty_bmap
,
226 bat_entry_off(idx
+ i
) / s
->bat_dirty_block
, 1);
229 return bat2sect(s
, idx
) + sector_num
% s
->tracks
;
233 static coroutine_fn
int parallels_co_flush_to_os(BlockDriverState
*bs
)
235 BDRVParallelsState
*s
= bs
->opaque
;
236 unsigned long size
= DIV_ROUND_UP(s
->header_size
, s
->bat_dirty_block
);
239 qemu_co_mutex_lock(&s
->lock
);
241 bit
= find_first_bit(s
->bat_dirty_bmap
, size
);
243 uint32_t off
= bit
* s
->bat_dirty_block
;
244 uint32_t to_write
= s
->bat_dirty_block
;
247 if (off
+ to_write
> s
->header_size
) {
248 to_write
= s
->header_size
- off
;
250 ret
= bdrv_pwrite(bs
->file
->bs
, off
, (uint8_t *)s
->header
+ off
,
253 qemu_co_mutex_unlock(&s
->lock
);
256 bit
= find_next_bit(s
->bat_dirty_bmap
, size
, bit
+ 1);
258 bitmap_zero(s
->bat_dirty_bmap
, size
);
260 qemu_co_mutex_unlock(&s
->lock
);
265 static int64_t coroutine_fn
parallels_co_get_block_status(BlockDriverState
*bs
,
266 int64_t sector_num
, int nb_sectors
, int *pnum
, BlockDriverState
**file
)
268 BDRVParallelsState
*s
= bs
->opaque
;
271 qemu_co_mutex_lock(&s
->lock
);
272 offset
= block_status(s
, sector_num
, nb_sectors
, pnum
);
273 qemu_co_mutex_unlock(&s
->lock
);
279 *file
= bs
->file
->bs
;
280 return (offset
<< BDRV_SECTOR_BITS
) |
281 BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
;
284 static coroutine_fn
int parallels_co_writev(BlockDriverState
*bs
,
285 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
)
287 BDRVParallelsState
*s
= bs
->opaque
;
288 uint64_t bytes_done
= 0;
289 QEMUIOVector hd_qiov
;
292 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
294 while (nb_sectors
> 0) {
298 qemu_co_mutex_lock(&s
->lock
);
299 position
= allocate_clusters(bs
, sector_num
, nb_sectors
, &n
);
300 qemu_co_mutex_unlock(&s
->lock
);
306 nbytes
= n
<< BDRV_SECTOR_BITS
;
308 qemu_iovec_reset(&hd_qiov
);
309 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
, nbytes
);
311 ret
= bdrv_co_writev(bs
->file
->bs
, position
, n
, &hd_qiov
);
318 bytes_done
+= nbytes
;
321 qemu_iovec_destroy(&hd_qiov
);
325 static coroutine_fn
int parallels_co_readv(BlockDriverState
*bs
,
326 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
)
328 BDRVParallelsState
*s
= bs
->opaque
;
329 uint64_t bytes_done
= 0;
330 QEMUIOVector hd_qiov
;
333 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
335 while (nb_sectors
> 0) {
339 qemu_co_mutex_lock(&s
->lock
);
340 position
= block_status(s
, sector_num
, nb_sectors
, &n
);
341 qemu_co_mutex_unlock(&s
->lock
);
343 nbytes
= n
<< BDRV_SECTOR_BITS
;
346 qemu_iovec_memset(qiov
, bytes_done
, 0, nbytes
);
348 qemu_iovec_reset(&hd_qiov
);
349 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
, nbytes
);
351 ret
= bdrv_co_readv(bs
->file
->bs
, position
, n
, &hd_qiov
);
359 bytes_done
+= nbytes
;
362 qemu_iovec_destroy(&hd_qiov
);
367 static int parallels_check(BlockDriverState
*bs
, BdrvCheckResult
*res
,
370 BDRVParallelsState
*s
= bs
->opaque
;
371 int64_t size
, prev_off
, high_off
;
374 bool flush_bat
= false;
375 int cluster_size
= s
->tracks
<< BDRV_SECTOR_BITS
;
377 size
= bdrv_getlength(bs
->file
->bs
);
383 if (s
->header_unclean
) {
384 fprintf(stderr
, "%s image was not closed correctly\n",
385 fix
& BDRV_FIX_ERRORS
? "Repairing" : "ERROR");
387 if (fix
& BDRV_FIX_ERRORS
) {
388 /* parallels_close will do the job right */
389 res
->corruptions_fixed
++;
390 s
->header_unclean
= false;
394 res
->bfi
.total_clusters
= s
->bat_size
;
395 res
->bfi
.compressed_clusters
= 0; /* compression is not supported */
399 for (i
= 0; i
< s
->bat_size
; i
++) {
400 int64_t off
= bat2sect(s
, i
) << BDRV_SECTOR_BITS
;
406 /* cluster outside the image */
408 fprintf(stderr
, "%s cluster %u is outside image\n",
409 fix
& BDRV_FIX_ERRORS
? "Repairing" : "ERROR", i
);
411 if (fix
& BDRV_FIX_ERRORS
) {
413 s
->bat_bitmap
[i
] = 0;
414 res
->corruptions_fixed
++;
420 res
->bfi
.allocated_clusters
++;
421 if (off
> high_off
) {
425 if (prev_off
!= 0 && (prev_off
+ cluster_size
) != off
) {
426 res
->bfi
.fragmented_clusters
++;
432 ret
= bdrv_pwrite_sync(bs
->file
->bs
, 0, s
->header
, s
->header_size
);
439 res
->image_end_offset
= high_off
+ cluster_size
;
440 if (size
> res
->image_end_offset
) {
442 count
= DIV_ROUND_UP(size
- res
->image_end_offset
, cluster_size
);
443 fprintf(stderr
, "%s space leaked at the end of the image %" PRId64
"\n",
444 fix
& BDRV_FIX_LEAKS
? "Repairing" : "ERROR",
445 size
- res
->image_end_offset
);
447 if (fix
& BDRV_FIX_LEAKS
) {
448 ret
= bdrv_truncate(bs
->file
->bs
, res
->image_end_offset
);
453 res
->leaks_fixed
+= count
;
461 static int parallels_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
463 int64_t total_size
, cl_size
;
464 uint8_t tmp
[BDRV_SECTOR_SIZE
];
465 Error
*local_err
= NULL
;
467 uint32_t bat_entries
, bat_sectors
;
468 ParallelsHeader header
;
471 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
473 cl_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_CLUSTER_SIZE
,
474 DEFAULT_CLUSTER_SIZE
), BDRV_SECTOR_SIZE
);
476 ret
= bdrv_create_file(filename
, opts
, &local_err
);
478 error_propagate(errp
, local_err
);
482 file
= blk_new_open(filename
, NULL
, NULL
,
483 BDRV_O_RDWR
| BDRV_O_PROTOCOL
, &local_err
);
485 error_propagate(errp
, local_err
);
489 blk_set_allow_write_beyond_eof(file
, true);
491 ret
= blk_truncate(file
, 0);
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
;
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
);
511 /* write all the data */
512 memset(tmp
, 0, sizeof(tmp
));
513 memcpy(tmp
, &header
, sizeof(header
));
515 ret
= blk_pwrite(file
, 0, tmp
, BDRV_SECTOR_SIZE
);
519 ret
= blk_write_zeroes(file
, 1, bat_sectors
- 1, 0);
530 error_setg_errno(errp
, -ret
, "Failed to create Parallels image");
535 static int parallels_probe(const uint8_t *buf
, int buf_size
,
536 const char *filename
)
538 const ParallelsHeader
*ph
= (const void *)buf
;
540 if (buf_size
< sizeof(ParallelsHeader
)) {
544 if ((!memcmp(ph
->magic
, HEADER_MAGIC
, 16) ||
545 !memcmp(ph
->magic
, HEADER_MAGIC2
, 16)) &&
546 (le32_to_cpu(ph
->version
) == HEADER_VERSION
)) {
553 static int parallels_update_header(BlockDriverState
*bs
)
555 BDRVParallelsState
*s
= bs
->opaque
;
556 unsigned size
= MAX(bdrv_opt_mem_align(bs
->file
->bs
),
557 sizeof(ParallelsHeader
));
559 if (size
> s
->header_size
) {
560 size
= s
->header_size
;
562 return bdrv_pwrite_sync(bs
->file
->bs
, 0, s
->header
, size
);
565 static int parallels_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
568 BDRVParallelsState
*s
= bs
->opaque
;
571 QemuOpts
*opts
= NULL
;
572 Error
*local_err
= NULL
;
575 ret
= bdrv_pread(bs
->file
->bs
, 0, &ph
, sizeof(ph
));
580 bs
->total_sectors
= le64_to_cpu(ph
.nb_sectors
);
582 if (le32_to_cpu(ph
.version
) != HEADER_VERSION
) {
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
);
594 s
->tracks
= le32_to_cpu(ph
.tracks
);
595 if (s
->tracks
== 0) {
596 error_setg(errp
, "Invalid image: Zero sectors per track");
600 if (s
->tracks
> INT32_MAX
/513) {
601 error_setg(errp
, "Invalid image: Too big cluster");
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");
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
) {
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
);
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
;
630 ret
= bdrv_pread(bs
->file
->bs
, 0, s
->header
, s
->header_size
);
634 s
->bat_bitmap
= (uint32_t *)(s
->header
+ 1);
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
;
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");
654 opts
= qemu_opts_create(¶llels_runtime_opts
, NULL
, 0, &local_err
);
655 if (local_err
!= NULL
) {
659 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
660 if (local_err
!= NULL
) {
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
);
671 if (local_err
!= NULL
) {
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
;
679 if (flags
& BDRV_O_RDWR
) {
680 s
->header
->inuse
= cpu_to_le32(HEADER_INUSE_MAGIC
);
681 ret
= parallels_update_header(bs
);
687 s
->bat_dirty_block
= 4 * getpagesize();
689 bitmap_new(DIV_ROUND_UP(s
->header_size
, s
->bat_dirty_block
));
691 qemu_co_mutex_init(&s
->lock
);
695 error_setg(errp
, "Image not in Parallels format");
698 qemu_vfree(s
->header
);
702 error_propagate(errp
, local_err
);
708 static void parallels_close(BlockDriverState
*bs
)
710 BDRVParallelsState
*s
= bs
->opaque
;
712 if (bs
->open_flags
& BDRV_O_RDWR
) {
713 s
->header
->inuse
= 0;
714 parallels_update_header(bs
);
717 if (bs
->open_flags
& BDRV_O_RDWR
) {
718 bdrv_truncate(bs
->file
->bs
, s
->data_end
<< BDRV_SECTOR_BITS
);
721 g_free(s
->bat_dirty_bmap
);
722 qemu_vfree(s
->header
);
725 static QemuOptsList parallels_create_opts
= {
726 .name
= "parallels-create-opts",
727 .head
= QTAILQ_HEAD_INITIALIZER(parallels_create_opts
.head
),
730 .name
= BLOCK_OPT_SIZE
,
731 .type
= QEMU_OPT_SIZE
,
732 .help
= "Virtual disk size",
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
),
740 { /* end of list */ }
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
,
756 .bdrv_create
= parallels_create
,
757 .bdrv_check
= parallels_check
,
758 .create_opts
= ¶llels_create_opts
,
761 static void bdrv_parallels_init(void)
763 bdrv_register(&bdrv_parallels
);
766 block_init(bdrv_parallels_init
);