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/bswap.h"
37 #include "qemu/bitmap.h"
38 #include "qapi/util.h"
40 /**************************************************************/
42 #define HEADER_MAGIC "WithoutFreeSpace"
43 #define HEADER_MAGIC2 "WithouFreSpacExt"
44 #define HEADER_VERSION 2
45 #define HEADER_INUSE_MAGIC (0x746F6E59)
47 #define DEFAULT_CLUSTER_SIZE 1048576 /* 1 MiB */
50 // always little-endian
51 typedef struct ParallelsHeader
{
52 char magic
[16]; // "WithoutFreeSpace"
62 } QEMU_PACKED ParallelsHeader
;
65 typedef enum ParallelsPreallocMode
{
66 PRL_PREALLOC_MODE_FALLOCATE
= 0,
67 PRL_PREALLOC_MODE_TRUNCATE
= 1,
68 PRL_PREALLOC_MODE__MAX
= 2,
69 } ParallelsPreallocMode
;
71 static const char *prealloc_mode_lookup
[] = {
78 typedef struct BDRVParallelsState
{
79 /** Locking is conservative, the lock protects
80 * - image file extending (truncate, fallocate)
81 * - any access to block allocation table
85 ParallelsHeader
*header
;
89 unsigned long *bat_dirty_bmap
;
90 unsigned int bat_dirty_block
;
93 unsigned int bat_size
;
96 uint64_t prealloc_size
;
97 ParallelsPreallocMode prealloc_mode
;
101 unsigned int off_multiplier
;
102 } BDRVParallelsState
;
105 #define PARALLELS_OPT_PREALLOC_MODE "prealloc-mode"
106 #define PARALLELS_OPT_PREALLOC_SIZE "prealloc-size"
108 static QemuOptsList parallels_runtime_opts
= {
110 .head
= QTAILQ_HEAD_INITIALIZER(parallels_runtime_opts
.head
),
113 .name
= PARALLELS_OPT_PREALLOC_SIZE
,
114 .type
= QEMU_OPT_SIZE
,
115 .help
= "Preallocation size on image expansion",
116 .def_value_str
= "128MiB",
119 .name
= PARALLELS_OPT_PREALLOC_MODE
,
120 .type
= QEMU_OPT_STRING
,
121 .help
= "Preallocation mode on image expansion "
122 "(allowed values: falloc, truncate)",
123 .def_value_str
= "falloc",
125 { /* end of list */ },
130 static int64_t bat2sect(BDRVParallelsState
*s
, uint32_t idx
)
132 return (uint64_t)le32_to_cpu(s
->bat_bitmap
[idx
]) * s
->off_multiplier
;
135 static uint32_t bat_entry_off(uint32_t idx
)
137 return sizeof(ParallelsHeader
) + sizeof(uint32_t) * idx
;
140 static int64_t seek_to_sector(BDRVParallelsState
*s
, int64_t sector_num
)
142 uint32_t index
, offset
;
144 index
= sector_num
/ s
->tracks
;
145 offset
= sector_num
% s
->tracks
;
148 if ((index
>= s
->bat_size
) || (s
->bat_bitmap
[index
] == 0)) {
151 return bat2sect(s
, index
) + offset
;
154 static int cluster_remainder(BDRVParallelsState
*s
, int64_t sector_num
,
157 int ret
= s
->tracks
- sector_num
% s
->tracks
;
158 return MIN(nb_sectors
, ret
);
161 static int64_t block_status(BDRVParallelsState
*s
, int64_t sector_num
,
162 int nb_sectors
, int *pnum
)
164 int64_t start_off
= -2, prev_end_off
= -2;
167 while (nb_sectors
> 0 || start_off
== -2) {
168 int64_t offset
= seek_to_sector(s
, sector_num
);
171 if (start_off
== -2) {
173 prev_end_off
= offset
;
174 } else if (offset
!= prev_end_off
) {
178 to_end
= cluster_remainder(s
, sector_num
, nb_sectors
);
179 nb_sectors
-= to_end
;
180 sector_num
+= to_end
;
184 prev_end_off
+= to_end
;
190 static int64_t allocate_clusters(BlockDriverState
*bs
, int64_t sector_num
,
191 int nb_sectors
, int *pnum
)
193 BDRVParallelsState
*s
= bs
->opaque
;
194 uint32_t idx
, to_allocate
, i
;
197 pos
= block_status(s
, sector_num
, nb_sectors
, pnum
);
202 idx
= sector_num
/ s
->tracks
;
203 if (idx
>= s
->bat_size
) {
207 to_allocate
= (sector_num
+ *pnum
+ s
->tracks
- 1) / s
->tracks
- idx
;
208 space
= to_allocate
* s
->tracks
;
209 if (s
->data_end
+ space
> bdrv_getlength(bs
->file
->bs
) >> BDRV_SECTOR_BITS
) {
211 space
+= s
->prealloc_size
;
212 if (s
->prealloc_mode
== PRL_PREALLOC_MODE_FALLOCATE
) {
213 ret
= bdrv_write_zeroes(bs
->file
->bs
, s
->data_end
, space
, 0);
215 ret
= bdrv_truncate(bs
->file
->bs
,
216 (s
->data_end
+ space
) << BDRV_SECTOR_BITS
);
223 for (i
= 0; i
< to_allocate
; i
++) {
224 s
->bat_bitmap
[idx
+ i
] = cpu_to_le32(s
->data_end
/ s
->off_multiplier
);
225 s
->data_end
+= s
->tracks
;
226 bitmap_set(s
->bat_dirty_bmap
,
227 bat_entry_off(idx
+ i
) / s
->bat_dirty_block
, 1);
230 return bat2sect(s
, idx
) + sector_num
% s
->tracks
;
234 static coroutine_fn
int parallels_co_flush_to_os(BlockDriverState
*bs
)
236 BDRVParallelsState
*s
= bs
->opaque
;
237 unsigned long size
= DIV_ROUND_UP(s
->header_size
, s
->bat_dirty_block
);
240 qemu_co_mutex_lock(&s
->lock
);
242 bit
= find_first_bit(s
->bat_dirty_bmap
, size
);
244 uint32_t off
= bit
* s
->bat_dirty_block
;
245 uint32_t to_write
= s
->bat_dirty_block
;
248 if (off
+ to_write
> s
->header_size
) {
249 to_write
= s
->header_size
- off
;
251 ret
= bdrv_pwrite(bs
->file
->bs
, off
, (uint8_t *)s
->header
+ off
,
254 qemu_co_mutex_unlock(&s
->lock
);
257 bit
= find_next_bit(s
->bat_dirty_bmap
, size
, bit
+ 1);
259 bitmap_zero(s
->bat_dirty_bmap
, size
);
261 qemu_co_mutex_unlock(&s
->lock
);
266 static int64_t coroutine_fn
parallels_co_get_block_status(BlockDriverState
*bs
,
267 int64_t sector_num
, int nb_sectors
, int *pnum
, BlockDriverState
**file
)
269 BDRVParallelsState
*s
= bs
->opaque
;
272 qemu_co_mutex_lock(&s
->lock
);
273 offset
= block_status(s
, sector_num
, nb_sectors
, pnum
);
274 qemu_co_mutex_unlock(&s
->lock
);
280 *file
= bs
->file
->bs
;
281 return (offset
<< BDRV_SECTOR_BITS
) |
282 BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
;
285 static coroutine_fn
int parallels_co_writev(BlockDriverState
*bs
,
286 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
)
288 BDRVParallelsState
*s
= bs
->opaque
;
289 uint64_t bytes_done
= 0;
290 QEMUIOVector hd_qiov
;
293 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
295 while (nb_sectors
> 0) {
299 qemu_co_mutex_lock(&s
->lock
);
300 position
= allocate_clusters(bs
, sector_num
, nb_sectors
, &n
);
301 qemu_co_mutex_unlock(&s
->lock
);
307 nbytes
= n
<< BDRV_SECTOR_BITS
;
309 qemu_iovec_reset(&hd_qiov
);
310 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
, nbytes
);
312 ret
= bdrv_co_writev(bs
->file
->bs
, position
, n
, &hd_qiov
);
319 bytes_done
+= nbytes
;
322 qemu_iovec_destroy(&hd_qiov
);
326 static coroutine_fn
int parallels_co_readv(BlockDriverState
*bs
,
327 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
)
329 BDRVParallelsState
*s
= bs
->opaque
;
330 uint64_t bytes_done
= 0;
331 QEMUIOVector hd_qiov
;
334 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
336 while (nb_sectors
> 0) {
340 qemu_co_mutex_lock(&s
->lock
);
341 position
= block_status(s
, sector_num
, nb_sectors
, &n
);
342 qemu_co_mutex_unlock(&s
->lock
);
344 nbytes
= n
<< BDRV_SECTOR_BITS
;
347 qemu_iovec_memset(qiov
, bytes_done
, 0, nbytes
);
349 qemu_iovec_reset(&hd_qiov
);
350 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
, nbytes
);
352 ret
= bdrv_co_readv(bs
->file
->bs
, position
, n
, &hd_qiov
);
360 bytes_done
+= nbytes
;
363 qemu_iovec_destroy(&hd_qiov
);
368 static int parallels_check(BlockDriverState
*bs
, BdrvCheckResult
*res
,
371 BDRVParallelsState
*s
= bs
->opaque
;
372 int64_t size
, prev_off
, high_off
;
375 bool flush_bat
= false;
376 int cluster_size
= s
->tracks
<< BDRV_SECTOR_BITS
;
378 size
= bdrv_getlength(bs
->file
->bs
);
384 if (s
->header_unclean
) {
385 fprintf(stderr
, "%s image was not closed correctly\n",
386 fix
& BDRV_FIX_ERRORS
? "Repairing" : "ERROR");
388 if (fix
& BDRV_FIX_ERRORS
) {
389 /* parallels_close will do the job right */
390 res
->corruptions_fixed
++;
391 s
->header_unclean
= false;
395 res
->bfi
.total_clusters
= s
->bat_size
;
396 res
->bfi
.compressed_clusters
= 0; /* compression is not supported */
400 for (i
= 0; i
< s
->bat_size
; i
++) {
401 int64_t off
= bat2sect(s
, i
) << BDRV_SECTOR_BITS
;
407 /* cluster outside the image */
409 fprintf(stderr
, "%s cluster %u is outside image\n",
410 fix
& BDRV_FIX_ERRORS
? "Repairing" : "ERROR", i
);
412 if (fix
& BDRV_FIX_ERRORS
) {
414 s
->bat_bitmap
[i
] = 0;
415 res
->corruptions_fixed
++;
421 res
->bfi
.allocated_clusters
++;
422 if (off
> high_off
) {
426 if (prev_off
!= 0 && (prev_off
+ cluster_size
) != off
) {
427 res
->bfi
.fragmented_clusters
++;
433 ret
= bdrv_pwrite_sync(bs
->file
->bs
, 0, s
->header
, s
->header_size
);
440 res
->image_end_offset
= high_off
+ cluster_size
;
441 if (size
> res
->image_end_offset
) {
443 count
= DIV_ROUND_UP(size
- res
->image_end_offset
, cluster_size
);
444 fprintf(stderr
, "%s space leaked at the end of the image %" PRId64
"\n",
445 fix
& BDRV_FIX_LEAKS
? "Repairing" : "ERROR",
446 size
- res
->image_end_offset
);
448 if (fix
& BDRV_FIX_LEAKS
) {
449 ret
= bdrv_truncate(bs
->file
->bs
, res
->image_end_offset
);
454 res
->leaks_fixed
+= count
;
462 static int parallels_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
464 int64_t total_size
, cl_size
;
465 uint8_t tmp
[BDRV_SECTOR_SIZE
];
466 Error
*local_err
= NULL
;
468 uint32_t bat_entries
, bat_sectors
;
469 ParallelsHeader header
;
472 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
474 cl_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_CLUSTER_SIZE
,
475 DEFAULT_CLUSTER_SIZE
), BDRV_SECTOR_SIZE
);
477 ret
= bdrv_create_file(filename
, opts
, &local_err
);
479 error_propagate(errp
, local_err
);
483 file
= blk_new_open(filename
, NULL
, NULL
,
484 BDRV_O_RDWR
| BDRV_O_PROTOCOL
, &local_err
);
486 error_propagate(errp
, local_err
);
490 blk_set_allow_write_beyond_eof(file
, true);
492 ret
= blk_truncate(file
, 0);
497 bat_entries
= DIV_ROUND_UP(total_size
, cl_size
);
498 bat_sectors
= DIV_ROUND_UP(bat_entry_off(bat_entries
), cl_size
);
499 bat_sectors
= (bat_sectors
* cl_size
) >> BDRV_SECTOR_BITS
;
501 memset(&header
, 0, sizeof(header
));
502 memcpy(header
.magic
, HEADER_MAGIC2
, sizeof(header
.magic
));
503 header
.version
= cpu_to_le32(HEADER_VERSION
);
504 /* don't care much about geometry, it is not used on image level */
505 header
.heads
= cpu_to_le32(16);
506 header
.cylinders
= cpu_to_le32(total_size
/ BDRV_SECTOR_SIZE
/ 16 / 32);
507 header
.tracks
= cpu_to_le32(cl_size
>> BDRV_SECTOR_BITS
);
508 header
.bat_entries
= cpu_to_le32(bat_entries
);
509 header
.nb_sectors
= cpu_to_le64(DIV_ROUND_UP(total_size
, BDRV_SECTOR_SIZE
));
510 header
.data_off
= cpu_to_le32(bat_sectors
);
512 /* write all the data */
513 memset(tmp
, 0, sizeof(tmp
));
514 memcpy(tmp
, &header
, sizeof(header
));
516 ret
= blk_pwrite(file
, 0, tmp
, BDRV_SECTOR_SIZE
, 0);
520 ret
= blk_write_zeroes(file
, BDRV_SECTOR_SIZE
,
521 (bat_sectors
- 1) << BDRV_SECTOR_BITS
, 0);
532 error_setg_errno(errp
, -ret
, "Failed to create Parallels image");
537 static int parallels_probe(const uint8_t *buf
, int buf_size
,
538 const char *filename
)
540 const ParallelsHeader
*ph
= (const void *)buf
;
542 if (buf_size
< sizeof(ParallelsHeader
)) {
546 if ((!memcmp(ph
->magic
, HEADER_MAGIC
, 16) ||
547 !memcmp(ph
->magic
, HEADER_MAGIC2
, 16)) &&
548 (le32_to_cpu(ph
->version
) == HEADER_VERSION
)) {
555 static int parallels_update_header(BlockDriverState
*bs
)
557 BDRVParallelsState
*s
= bs
->opaque
;
558 unsigned size
= MAX(bdrv_opt_mem_align(bs
->file
->bs
),
559 sizeof(ParallelsHeader
));
561 if (size
> s
->header_size
) {
562 size
= s
->header_size
;
564 return bdrv_pwrite_sync(bs
->file
->bs
, 0, s
->header
, size
);
567 static int parallels_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
570 BDRVParallelsState
*s
= bs
->opaque
;
573 QemuOpts
*opts
= NULL
;
574 Error
*local_err
= NULL
;
577 ret
= bdrv_pread(bs
->file
->bs
, 0, &ph
, sizeof(ph
));
582 bs
->total_sectors
= le64_to_cpu(ph
.nb_sectors
);
584 if (le32_to_cpu(ph
.version
) != HEADER_VERSION
) {
587 if (!memcmp(ph
.magic
, HEADER_MAGIC
, 16)) {
588 s
->off_multiplier
= 1;
589 bs
->total_sectors
= 0xffffffff & bs
->total_sectors
;
590 } else if (!memcmp(ph
.magic
, HEADER_MAGIC2
, 16)) {
591 s
->off_multiplier
= le32_to_cpu(ph
.tracks
);
596 s
->tracks
= le32_to_cpu(ph
.tracks
);
597 if (s
->tracks
== 0) {
598 error_setg(errp
, "Invalid image: Zero sectors per track");
602 if (s
->tracks
> INT32_MAX
/513) {
603 error_setg(errp
, "Invalid image: Too big cluster");
608 s
->bat_size
= le32_to_cpu(ph
.bat_entries
);
609 if (s
->bat_size
> INT_MAX
/ sizeof(uint32_t)) {
610 error_setg(errp
, "Catalog too large");
615 size
= bat_entry_off(s
->bat_size
);
616 s
->header_size
= ROUND_UP(size
, bdrv_opt_mem_align(bs
->file
->bs
));
617 s
->header
= qemu_try_blockalign(bs
->file
->bs
, s
->header_size
);
618 if (s
->header
== NULL
) {
622 s
->data_end
= le32_to_cpu(ph
.data_off
);
623 if (s
->data_end
== 0) {
624 s
->data_end
= ROUND_UP(bat_entry_off(s
->bat_size
), BDRV_SECTOR_SIZE
);
626 if (s
->data_end
< s
->header_size
) {
627 /* there is not enough unused space to fit to block align between BAT
628 and actual data. We can't avoid read-modify-write... */
629 s
->header_size
= size
;
632 ret
= bdrv_pread(bs
->file
->bs
, 0, s
->header
, s
->header_size
);
636 s
->bat_bitmap
= (uint32_t *)(s
->header
+ 1);
638 for (i
= 0; i
< s
->bat_size
; i
++) {
639 int64_t off
= bat2sect(s
, i
);
640 if (off
>= s
->data_end
) {
641 s
->data_end
= off
+ s
->tracks
;
645 if (le32_to_cpu(ph
.inuse
) == HEADER_INUSE_MAGIC
) {
646 /* Image was not closed correctly. The check is mandatory */
647 s
->header_unclean
= true;
648 if ((flags
& BDRV_O_RDWR
) && !(flags
& BDRV_O_CHECK
)) {
649 error_setg(errp
, "parallels: Image was not closed correctly; "
650 "cannot be opened read/write");
656 opts
= qemu_opts_create(¶llels_runtime_opts
, NULL
, 0, &local_err
);
657 if (local_err
!= NULL
) {
661 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
662 if (local_err
!= NULL
) {
667 qemu_opt_get_size_del(opts
, PARALLELS_OPT_PREALLOC_SIZE
, 0);
668 s
->prealloc_size
= MAX(s
->tracks
, s
->prealloc_size
>> BDRV_SECTOR_BITS
);
669 buf
= qemu_opt_get_del(opts
, PARALLELS_OPT_PREALLOC_MODE
);
670 s
->prealloc_mode
= qapi_enum_parse(prealloc_mode_lookup
, buf
,
671 PRL_PREALLOC_MODE__MAX
, PRL_PREALLOC_MODE_FALLOCATE
, &local_err
);
673 if (local_err
!= NULL
) {
676 if (!bdrv_has_zero_init(bs
->file
->bs
) ||
677 bdrv_truncate(bs
->file
->bs
, bdrv_getlength(bs
->file
->bs
)) != 0) {
678 s
->prealloc_mode
= PRL_PREALLOC_MODE_FALLOCATE
;
681 if (flags
& BDRV_O_RDWR
) {
682 s
->header
->inuse
= cpu_to_le32(HEADER_INUSE_MAGIC
);
683 ret
= parallels_update_header(bs
);
689 s
->bat_dirty_block
= 4 * getpagesize();
691 bitmap_new(DIV_ROUND_UP(s
->header_size
, s
->bat_dirty_block
));
693 qemu_co_mutex_init(&s
->lock
);
697 error_setg(errp
, "Image not in Parallels format");
700 qemu_vfree(s
->header
);
704 error_propagate(errp
, local_err
);
710 static void parallels_close(BlockDriverState
*bs
)
712 BDRVParallelsState
*s
= bs
->opaque
;
714 if (bs
->open_flags
& BDRV_O_RDWR
) {
715 s
->header
->inuse
= 0;
716 parallels_update_header(bs
);
719 if (bs
->open_flags
& BDRV_O_RDWR
) {
720 bdrv_truncate(bs
->file
->bs
, s
->data_end
<< BDRV_SECTOR_BITS
);
723 g_free(s
->bat_dirty_bmap
);
724 qemu_vfree(s
->header
);
727 static QemuOptsList parallels_create_opts
= {
728 .name
= "parallels-create-opts",
729 .head
= QTAILQ_HEAD_INITIALIZER(parallels_create_opts
.head
),
732 .name
= BLOCK_OPT_SIZE
,
733 .type
= QEMU_OPT_SIZE
,
734 .help
= "Virtual disk size",
737 .name
= BLOCK_OPT_CLUSTER_SIZE
,
738 .type
= QEMU_OPT_SIZE
,
739 .help
= "Parallels image cluster size",
740 .def_value_str
= stringify(DEFAULT_CLUSTER_SIZE
),
742 { /* end of list */ }
746 static BlockDriver bdrv_parallels
= {
747 .format_name
= "parallels",
748 .instance_size
= sizeof(BDRVParallelsState
),
749 .bdrv_probe
= parallels_probe
,
750 .bdrv_open
= parallels_open
,
751 .bdrv_close
= parallels_close
,
752 .bdrv_co_get_block_status
= parallels_co_get_block_status
,
753 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
754 .bdrv_co_flush_to_os
= parallels_co_flush_to_os
,
755 .bdrv_co_readv
= parallels_co_readv
,
756 .bdrv_co_writev
= parallels_co_writev
,
758 .bdrv_create
= parallels_create
,
759 .bdrv_check
= parallels_check
,
760 .create_opts
= ¶llels_create_opts
,
763 static void bdrv_parallels_init(void)
765 bdrv_register(&bdrv_parallels
);
768 block_init(bdrv_parallels_init
);