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-common.h"
31 #include "block/block_int.h"
32 #include "qemu/module.h"
34 /**************************************************************/
36 #define HEADER_MAGIC "WithoutFreeSpace"
37 #define HEADER_MAGIC2 "WithouFreSpacExt"
38 #define HEADER_VERSION 2
39 #define HEADER_INUSE_MAGIC (0x746F6E59)
41 #define DEFAULT_CLUSTER_SIZE 1048576 /* 1 MiB */
44 // always little-endian
45 typedef struct ParallelsHeader
{
46 char magic
[16]; // "WithoutFreeSpace"
56 } QEMU_PACKED ParallelsHeader
;
58 typedef struct BDRVParallelsState
{
59 /** Locking is conservative, the lock protects
60 * - image file extending (truncate, fallocate)
61 * - any access to block allocation table
65 ParallelsHeader
*header
;
70 unsigned int bat_size
;
74 unsigned int off_multiplier
;
80 static int64_t bat2sect(BDRVParallelsState
*s
, uint32_t idx
)
82 return (uint64_t)le32_to_cpu(s
->bat_bitmap
[idx
]) * s
->off_multiplier
;
85 static int64_t seek_to_sector(BDRVParallelsState
*s
, int64_t sector_num
)
87 uint32_t index
, offset
;
89 index
= sector_num
/ s
->tracks
;
90 offset
= sector_num
% s
->tracks
;
93 if ((index
>= s
->bat_size
) || (s
->bat_bitmap
[index
] == 0)) {
96 return bat2sect(s
, index
) + offset
;
99 static int cluster_remainder(BDRVParallelsState
*s
, int64_t sector_num
,
102 int ret
= s
->tracks
- sector_num
% s
->tracks
;
103 return MIN(nb_sectors
, ret
);
106 static int64_t block_status(BDRVParallelsState
*s
, int64_t sector_num
,
107 int nb_sectors
, int *pnum
)
109 int64_t start_off
= -2, prev_end_off
= -2;
112 while (nb_sectors
> 0 || start_off
== -2) {
113 int64_t offset
= seek_to_sector(s
, sector_num
);
116 if (start_off
== -2) {
118 prev_end_off
= offset
;
119 } else if (offset
!= prev_end_off
) {
123 to_end
= cluster_remainder(s
, sector_num
, nb_sectors
);
124 nb_sectors
-= to_end
;
125 sector_num
+= to_end
;
129 prev_end_off
+= to_end
;
135 static int64_t allocate_cluster(BlockDriverState
*bs
, int64_t sector_num
)
137 BDRVParallelsState
*s
= bs
->opaque
;
138 uint32_t idx
, offset
;
142 idx
= sector_num
/ s
->tracks
;
143 offset
= sector_num
% s
->tracks
;
145 if (idx
>= s
->bat_size
) {
148 if (s
->bat_bitmap
[idx
] != 0) {
149 return bat2sect(s
, idx
) + offset
;
152 pos
= bdrv_getlength(bs
->file
) >> BDRV_SECTOR_BITS
;
153 if (s
->has_truncate
) {
154 ret
= bdrv_truncate(bs
->file
, (pos
+ s
->tracks
) << BDRV_SECTOR_BITS
);
156 ret
= bdrv_write_zeroes(bs
->file
, pos
, s
->tracks
, 0);
162 s
->bat_bitmap
[idx
] = cpu_to_le32(pos
/ s
->off_multiplier
);
163 ret
= bdrv_pwrite(bs
->file
,
164 sizeof(ParallelsHeader
) + idx
* sizeof(s
->bat_bitmap
[idx
]),
165 s
->bat_bitmap
+ idx
, sizeof(s
->bat_bitmap
[idx
]));
167 s
->bat_bitmap
[idx
] = 0;
170 return bat2sect(s
, idx
) + offset
;
173 static int64_t coroutine_fn
parallels_co_get_block_status(BlockDriverState
*bs
,
174 int64_t sector_num
, int nb_sectors
, int *pnum
)
176 BDRVParallelsState
*s
= bs
->opaque
;
179 qemu_co_mutex_lock(&s
->lock
);
180 offset
= block_status(s
, sector_num
, nb_sectors
, pnum
);
181 qemu_co_mutex_unlock(&s
->lock
);
187 return (offset
<< BDRV_SECTOR_BITS
) |
188 BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
;
191 static coroutine_fn
int parallels_co_writev(BlockDriverState
*bs
,
192 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
)
194 BDRVParallelsState
*s
= bs
->opaque
;
195 uint64_t bytes_done
= 0;
196 QEMUIOVector hd_qiov
;
199 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
201 while (nb_sectors
> 0) {
205 qemu_co_mutex_lock(&s
->lock
);
206 position
= allocate_cluster(bs
, sector_num
);
207 qemu_co_mutex_unlock(&s
->lock
);
213 n
= cluster_remainder(s
, sector_num
, nb_sectors
);
214 nbytes
= n
<< BDRV_SECTOR_BITS
;
216 qemu_iovec_reset(&hd_qiov
);
217 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
, nbytes
);
219 ret
= bdrv_co_writev(bs
->file
, position
, n
, &hd_qiov
);
226 bytes_done
+= nbytes
;
229 qemu_iovec_destroy(&hd_qiov
);
233 static coroutine_fn
int parallels_co_readv(BlockDriverState
*bs
,
234 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
)
236 BDRVParallelsState
*s
= bs
->opaque
;
237 uint64_t bytes_done
= 0;
238 QEMUIOVector hd_qiov
;
241 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
243 while (nb_sectors
> 0) {
247 qemu_co_mutex_lock(&s
->lock
);
248 position
= block_status(s
, sector_num
, nb_sectors
, &n
);
249 qemu_co_mutex_unlock(&s
->lock
);
251 nbytes
= n
<< BDRV_SECTOR_BITS
;
254 qemu_iovec_memset(qiov
, bytes_done
, 0, nbytes
);
256 qemu_iovec_reset(&hd_qiov
);
257 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
, nbytes
);
259 ret
= bdrv_co_readv(bs
->file
, position
, n
, &hd_qiov
);
267 bytes_done
+= nbytes
;
270 qemu_iovec_destroy(&hd_qiov
);
275 static int parallels_check(BlockDriverState
*bs
, BdrvCheckResult
*res
,
278 BDRVParallelsState
*s
= bs
->opaque
;
279 int64_t size
, prev_off
, high_off
;
282 bool flush_bat
= false;
283 int cluster_size
= s
->tracks
<< BDRV_SECTOR_BITS
;
285 size
= bdrv_getlength(bs
->file
);
291 if (s
->header_unclean
) {
292 fprintf(stderr
, "%s image was not closed correctly\n",
293 fix
& BDRV_FIX_ERRORS
? "Repairing" : "ERROR");
295 if (fix
& BDRV_FIX_ERRORS
) {
296 /* parallels_close will do the job right */
297 res
->corruptions_fixed
++;
298 s
->header_unclean
= false;
302 res
->bfi
.total_clusters
= s
->bat_size
;
303 res
->bfi
.compressed_clusters
= 0; /* compression is not supported */
307 for (i
= 0; i
< s
->bat_size
; i
++) {
308 int64_t off
= bat2sect(s
, i
) << BDRV_SECTOR_BITS
;
314 /* cluster outside the image */
316 fprintf(stderr
, "%s cluster %u is outside image\n",
317 fix
& BDRV_FIX_ERRORS
? "Repairing" : "ERROR", i
);
319 if (fix
& BDRV_FIX_ERRORS
) {
321 s
->bat_bitmap
[i
] = 0;
322 res
->corruptions_fixed
++;
328 res
->bfi
.allocated_clusters
++;
329 if (off
> high_off
) {
333 if (prev_off
!= 0 && (prev_off
+ cluster_size
) != off
) {
334 res
->bfi
.fragmented_clusters
++;
340 ret
= bdrv_pwrite_sync(bs
->file
, 0, s
->header
, s
->header_size
);
347 res
->image_end_offset
= high_off
+ cluster_size
;
348 if (size
> res
->image_end_offset
) {
350 count
= DIV_ROUND_UP(size
- res
->image_end_offset
, cluster_size
);
351 fprintf(stderr
, "%s space leaked at the end of the image %" PRId64
"\n",
352 fix
& BDRV_FIX_LEAKS
? "Repairing" : "ERROR",
353 size
- res
->image_end_offset
);
355 if (fix
& BDRV_FIX_LEAKS
) {
356 ret
= bdrv_truncate(bs
->file
, res
->image_end_offset
);
361 res
->leaks_fixed
+= count
;
369 static int parallels_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
371 int64_t total_size
, cl_size
;
372 uint8_t tmp
[BDRV_SECTOR_SIZE
];
373 Error
*local_err
= NULL
;
374 BlockDriverState
*file
;
375 uint32_t bat_entries
, bat_sectors
;
376 ParallelsHeader header
;
379 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
381 cl_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_CLUSTER_SIZE
,
382 DEFAULT_CLUSTER_SIZE
), BDRV_SECTOR_SIZE
);
384 ret
= bdrv_create_file(filename
, opts
, &local_err
);
386 error_propagate(errp
, local_err
);
391 ret
= bdrv_open(&file
, filename
, NULL
, NULL
,
392 BDRV_O_RDWR
| BDRV_O_PROTOCOL
, NULL
, &local_err
);
394 error_propagate(errp
, local_err
);
397 ret
= bdrv_truncate(file
, 0);
402 bat_entries
= DIV_ROUND_UP(total_size
, cl_size
);
403 bat_sectors
= DIV_ROUND_UP(bat_entries
* sizeof(uint32_t) +
404 sizeof(ParallelsHeader
), cl_size
);
405 bat_sectors
= (bat_sectors
* cl_size
) >> BDRV_SECTOR_BITS
;
407 memset(&header
, 0, sizeof(header
));
408 memcpy(header
.magic
, HEADER_MAGIC2
, sizeof(header
.magic
));
409 header
.version
= cpu_to_le32(HEADER_VERSION
);
410 /* don't care much about geometry, it is not used on image level */
411 header
.heads
= cpu_to_le32(16);
412 header
.cylinders
= cpu_to_le32(total_size
/ BDRV_SECTOR_SIZE
/ 16 / 32);
413 header
.tracks
= cpu_to_le32(cl_size
>> BDRV_SECTOR_BITS
);
414 header
.bat_entries
= cpu_to_le32(bat_entries
);
415 header
.nb_sectors
= cpu_to_le64(DIV_ROUND_UP(total_size
, BDRV_SECTOR_SIZE
));
416 header
.data_off
= cpu_to_le32(bat_sectors
);
418 /* write all the data */
419 memset(tmp
, 0, sizeof(tmp
));
420 memcpy(tmp
, &header
, sizeof(header
));
422 ret
= bdrv_pwrite(file
, 0, tmp
, BDRV_SECTOR_SIZE
);
426 ret
= bdrv_write_zeroes(file
, 1, bat_sectors
- 1, 0);
437 error_setg_errno(errp
, -ret
, "Failed to create Parallels image");
442 static int parallels_probe(const uint8_t *buf
, int buf_size
,
443 const char *filename
)
445 const ParallelsHeader
*ph
= (const void *)buf
;
447 if (buf_size
< sizeof(ParallelsHeader
)) {
451 if ((!memcmp(ph
->magic
, HEADER_MAGIC
, 16) ||
452 !memcmp(ph
->magic
, HEADER_MAGIC2
, 16)) &&
453 (le32_to_cpu(ph
->version
) == HEADER_VERSION
)) {
460 static int parallels_update_header(BlockDriverState
*bs
)
462 BDRVParallelsState
*s
= bs
->opaque
;
463 unsigned size
= MAX(bdrv_opt_mem_align(bs
->file
), sizeof(ParallelsHeader
));
465 if (size
> s
->header_size
) {
466 size
= s
->header_size
;
468 return bdrv_pwrite_sync(bs
->file
, 0, s
->header
, size
);
471 static int parallels_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
474 BDRVParallelsState
*s
= bs
->opaque
;
478 ret
= bdrv_pread(bs
->file
, 0, &ph
, sizeof(ph
));
483 bs
->total_sectors
= le64_to_cpu(ph
.nb_sectors
);
485 if (le32_to_cpu(ph
.version
) != HEADER_VERSION
) {
488 if (!memcmp(ph
.magic
, HEADER_MAGIC
, 16)) {
489 s
->off_multiplier
= 1;
490 bs
->total_sectors
= 0xffffffff & bs
->total_sectors
;
491 } else if (!memcmp(ph
.magic
, HEADER_MAGIC2
, 16)) {
492 s
->off_multiplier
= le32_to_cpu(ph
.tracks
);
497 s
->tracks
= le32_to_cpu(ph
.tracks
);
498 if (s
->tracks
== 0) {
499 error_setg(errp
, "Invalid image: Zero sectors per track");
503 if (s
->tracks
> INT32_MAX
/513) {
504 error_setg(errp
, "Invalid image: Too big cluster");
509 s
->bat_size
= le32_to_cpu(ph
.bat_entries
);
510 if (s
->bat_size
> INT_MAX
/ sizeof(uint32_t)) {
511 error_setg(errp
, "Catalog too large");
516 size
= sizeof(ParallelsHeader
) + sizeof(uint32_t) * s
->bat_size
;
517 s
->header_size
= ROUND_UP(size
, bdrv_opt_mem_align(bs
->file
));
518 s
->header
= qemu_try_blockalign(bs
->file
, s
->header_size
);
519 if (s
->header
== NULL
) {
523 if (le32_to_cpu(ph
.data_off
) < s
->header_size
) {
524 /* there is not enough unused space to fit to block align between BAT
525 and actual data. We can't avoid read-modify-write... */
526 s
->header_size
= size
;
529 ret
= bdrv_pread(bs
->file
, 0, s
->header
, s
->header_size
);
533 s
->bat_bitmap
= (uint32_t *)(s
->header
+ 1);
535 s
->has_truncate
= bdrv_has_zero_init(bs
->file
) &&
536 bdrv_truncate(bs
->file
, bdrv_getlength(bs
->file
)) == 0;
538 if (le32_to_cpu(ph
.inuse
) == HEADER_INUSE_MAGIC
) {
539 /* Image was not closed correctly. The check is mandatory */
540 s
->header_unclean
= true;
541 if ((flags
& BDRV_O_RDWR
) && !(flags
& BDRV_O_CHECK
)) {
542 error_setg(errp
, "parallels: Image was not closed correctly; "
543 "cannot be opened read/write");
549 if (flags
& BDRV_O_RDWR
) {
550 s
->header
->inuse
= cpu_to_le32(HEADER_INUSE_MAGIC
);
551 ret
= parallels_update_header(bs
);
557 qemu_co_mutex_init(&s
->lock
);
561 error_setg(errp
, "Image not in Parallels format");
564 qemu_vfree(s
->header
);
569 static void parallels_close(BlockDriverState
*bs
)
571 BDRVParallelsState
*s
= bs
->opaque
;
573 if (bs
->open_flags
& BDRV_O_RDWR
) {
574 s
->header
->inuse
= 0;
575 parallels_update_header(bs
);
578 qemu_vfree(s
->header
);
581 static QemuOptsList parallels_create_opts
= {
582 .name
= "parallels-create-opts",
583 .head
= QTAILQ_HEAD_INITIALIZER(parallels_create_opts
.head
),
586 .name
= BLOCK_OPT_SIZE
,
587 .type
= QEMU_OPT_SIZE
,
588 .help
= "Virtual disk size",
591 .name
= BLOCK_OPT_CLUSTER_SIZE
,
592 .type
= QEMU_OPT_SIZE
,
593 .help
= "Parallels image cluster size",
594 .def_value_str
= stringify(DEFAULT_CLUSTER_SIZE
),
596 { /* end of list */ }
600 static BlockDriver bdrv_parallels
= {
601 .format_name
= "parallels",
602 .instance_size
= sizeof(BDRVParallelsState
),
603 .bdrv_probe
= parallels_probe
,
604 .bdrv_open
= parallels_open
,
605 .bdrv_close
= parallels_close
,
606 .bdrv_co_get_block_status
= parallels_co_get_block_status
,
607 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
608 .bdrv_co_readv
= parallels_co_readv
,
609 .bdrv_co_writev
= parallels_co_writev
,
611 .bdrv_create
= parallels_create
,
612 .bdrv_check
= parallels_check
,
613 .create_opts
= ¶llels_create_opts
,
616 static void bdrv_parallels_init(void)
618 bdrv_register(&bdrv_parallels
);
621 block_init(bdrv_parallels_init
);