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 allocate_cluster(BlockDriverState
*bs
, int64_t sector_num
)
108 BDRVParallelsState
*s
= bs
->opaque
;
109 uint32_t idx
, offset
;
113 idx
= sector_num
/ s
->tracks
;
114 offset
= sector_num
% s
->tracks
;
116 if (idx
>= s
->bat_size
) {
119 if (s
->bat_bitmap
[idx
] != 0) {
120 return bat2sect(s
, idx
) + offset
;
123 pos
= bdrv_getlength(bs
->file
) >> BDRV_SECTOR_BITS
;
124 if (s
->has_truncate
) {
125 ret
= bdrv_truncate(bs
->file
, (pos
+ s
->tracks
) << BDRV_SECTOR_BITS
);
127 ret
= bdrv_write_zeroes(bs
->file
, pos
, s
->tracks
, 0);
133 s
->bat_bitmap
[idx
] = cpu_to_le32(pos
/ s
->off_multiplier
);
134 ret
= bdrv_pwrite(bs
->file
,
135 sizeof(ParallelsHeader
) + idx
* sizeof(s
->bat_bitmap
[idx
]),
136 s
->bat_bitmap
+ idx
, sizeof(s
->bat_bitmap
[idx
]));
138 s
->bat_bitmap
[idx
] = 0;
141 return bat2sect(s
, idx
) + offset
;
144 static int64_t coroutine_fn
parallels_co_get_block_status(BlockDriverState
*bs
,
145 int64_t sector_num
, int nb_sectors
, int *pnum
)
147 BDRVParallelsState
*s
= bs
->opaque
;
150 qemu_co_mutex_lock(&s
->lock
);
151 offset
= seek_to_sector(s
, sector_num
);
152 qemu_co_mutex_unlock(&s
->lock
);
154 *pnum
= cluster_remainder(s
, sector_num
, nb_sectors
);
160 return (offset
<< BDRV_SECTOR_BITS
) |
161 BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
;
164 static coroutine_fn
int parallels_co_writev(BlockDriverState
*bs
,
165 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
)
167 BDRVParallelsState
*s
= bs
->opaque
;
168 uint64_t bytes_done
= 0;
169 QEMUIOVector hd_qiov
;
172 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
174 while (nb_sectors
> 0) {
178 qemu_co_mutex_lock(&s
->lock
);
179 position
= allocate_cluster(bs
, sector_num
);
180 qemu_co_mutex_unlock(&s
->lock
);
186 n
= cluster_remainder(s
, sector_num
, nb_sectors
);
187 nbytes
= n
<< BDRV_SECTOR_BITS
;
189 qemu_iovec_reset(&hd_qiov
);
190 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
, nbytes
);
192 ret
= bdrv_co_writev(bs
->file
, position
, n
, &hd_qiov
);
199 bytes_done
+= nbytes
;
202 qemu_iovec_destroy(&hd_qiov
);
206 static coroutine_fn
int parallels_co_readv(BlockDriverState
*bs
,
207 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
)
209 BDRVParallelsState
*s
= bs
->opaque
;
210 uint64_t bytes_done
= 0;
211 QEMUIOVector hd_qiov
;
214 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
216 while (nb_sectors
> 0) {
220 qemu_co_mutex_lock(&s
->lock
);
221 position
= seek_to_sector(s
, sector_num
);
222 qemu_co_mutex_unlock(&s
->lock
);
224 n
= cluster_remainder(s
, sector_num
, nb_sectors
);
225 nbytes
= n
<< BDRV_SECTOR_BITS
;
228 qemu_iovec_memset(qiov
, bytes_done
, 0, nbytes
);
230 qemu_iovec_reset(&hd_qiov
);
231 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
, nbytes
);
233 ret
= bdrv_co_readv(bs
->file
, position
, n
, &hd_qiov
);
241 bytes_done
+= nbytes
;
244 qemu_iovec_destroy(&hd_qiov
);
249 static int parallels_check(BlockDriverState
*bs
, BdrvCheckResult
*res
,
252 BDRVParallelsState
*s
= bs
->opaque
;
253 int64_t size
, prev_off
, high_off
;
256 bool flush_bat
= false;
257 int cluster_size
= s
->tracks
<< BDRV_SECTOR_BITS
;
259 size
= bdrv_getlength(bs
->file
);
265 if (s
->header_unclean
) {
266 fprintf(stderr
, "%s image was not closed correctly\n",
267 fix
& BDRV_FIX_ERRORS
? "Repairing" : "ERROR");
269 if (fix
& BDRV_FIX_ERRORS
) {
270 /* parallels_close will do the job right */
271 res
->corruptions_fixed
++;
272 s
->header_unclean
= false;
276 res
->bfi
.total_clusters
= s
->bat_size
;
277 res
->bfi
.compressed_clusters
= 0; /* compression is not supported */
281 for (i
= 0; i
< s
->bat_size
; i
++) {
282 int64_t off
= bat2sect(s
, i
) << BDRV_SECTOR_BITS
;
288 /* cluster outside the image */
290 fprintf(stderr
, "%s cluster %u is outside image\n",
291 fix
& BDRV_FIX_ERRORS
? "Repairing" : "ERROR", i
);
293 if (fix
& BDRV_FIX_ERRORS
) {
295 s
->bat_bitmap
[i
] = 0;
296 res
->corruptions_fixed
++;
302 res
->bfi
.allocated_clusters
++;
303 if (off
> high_off
) {
307 if (prev_off
!= 0 && (prev_off
+ cluster_size
) != off
) {
308 res
->bfi
.fragmented_clusters
++;
314 ret
= bdrv_pwrite_sync(bs
->file
, 0, s
->header
, s
->header_size
);
321 res
->image_end_offset
= high_off
+ cluster_size
;
322 if (size
> res
->image_end_offset
) {
324 count
= DIV_ROUND_UP(size
- res
->image_end_offset
, cluster_size
);
325 fprintf(stderr
, "%s space leaked at the end of the image %" PRId64
"\n",
326 fix
& BDRV_FIX_LEAKS
? "Repairing" : "ERROR",
327 size
- res
->image_end_offset
);
329 if (fix
& BDRV_FIX_LEAKS
) {
330 ret
= bdrv_truncate(bs
->file
, res
->image_end_offset
);
335 res
->leaks_fixed
+= count
;
343 static int parallels_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
345 int64_t total_size
, cl_size
;
346 uint8_t tmp
[BDRV_SECTOR_SIZE
];
347 Error
*local_err
= NULL
;
348 BlockDriverState
*file
;
349 uint32_t bat_entries
, bat_sectors
;
350 ParallelsHeader header
;
353 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
355 cl_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_CLUSTER_SIZE
,
356 DEFAULT_CLUSTER_SIZE
), BDRV_SECTOR_SIZE
);
358 ret
= bdrv_create_file(filename
, opts
, &local_err
);
360 error_propagate(errp
, local_err
);
365 ret
= bdrv_open(&file
, filename
, NULL
, NULL
,
366 BDRV_O_RDWR
| BDRV_O_PROTOCOL
, NULL
, &local_err
);
368 error_propagate(errp
, local_err
);
371 ret
= bdrv_truncate(file
, 0);
376 bat_entries
= DIV_ROUND_UP(total_size
, cl_size
);
377 bat_sectors
= DIV_ROUND_UP(bat_entries
* sizeof(uint32_t) +
378 sizeof(ParallelsHeader
), cl_size
);
379 bat_sectors
= (bat_sectors
* cl_size
) >> BDRV_SECTOR_BITS
;
381 memset(&header
, 0, sizeof(header
));
382 memcpy(header
.magic
, HEADER_MAGIC2
, sizeof(header
.magic
));
383 header
.version
= cpu_to_le32(HEADER_VERSION
);
384 /* don't care much about geometry, it is not used on image level */
385 header
.heads
= cpu_to_le32(16);
386 header
.cylinders
= cpu_to_le32(total_size
/ BDRV_SECTOR_SIZE
/ 16 / 32);
387 header
.tracks
= cpu_to_le32(cl_size
>> BDRV_SECTOR_BITS
);
388 header
.bat_entries
= cpu_to_le32(bat_entries
);
389 header
.nb_sectors
= cpu_to_le64(DIV_ROUND_UP(total_size
, BDRV_SECTOR_SIZE
));
390 header
.data_off
= cpu_to_le32(bat_sectors
);
392 /* write all the data */
393 memset(tmp
, 0, sizeof(tmp
));
394 memcpy(tmp
, &header
, sizeof(header
));
396 ret
= bdrv_pwrite(file
, 0, tmp
, BDRV_SECTOR_SIZE
);
400 ret
= bdrv_write_zeroes(file
, 1, bat_sectors
- 1, 0);
411 error_setg_errno(errp
, -ret
, "Failed to create Parallels image");
416 static int parallels_probe(const uint8_t *buf
, int buf_size
,
417 const char *filename
)
419 const ParallelsHeader
*ph
= (const void *)buf
;
421 if (buf_size
< sizeof(ParallelsHeader
)) {
425 if ((!memcmp(ph
->magic
, HEADER_MAGIC
, 16) ||
426 !memcmp(ph
->magic
, HEADER_MAGIC2
, 16)) &&
427 (le32_to_cpu(ph
->version
) == HEADER_VERSION
)) {
434 static int parallels_update_header(BlockDriverState
*bs
)
436 BDRVParallelsState
*s
= bs
->opaque
;
437 unsigned size
= MAX(bdrv_opt_mem_align(bs
->file
), sizeof(ParallelsHeader
));
439 if (size
> s
->header_size
) {
440 size
= s
->header_size
;
442 return bdrv_pwrite_sync(bs
->file
, 0, s
->header
, size
);
445 static int parallels_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
448 BDRVParallelsState
*s
= bs
->opaque
;
452 ret
= bdrv_pread(bs
->file
, 0, &ph
, sizeof(ph
));
457 bs
->total_sectors
= le64_to_cpu(ph
.nb_sectors
);
459 if (le32_to_cpu(ph
.version
) != HEADER_VERSION
) {
462 if (!memcmp(ph
.magic
, HEADER_MAGIC
, 16)) {
463 s
->off_multiplier
= 1;
464 bs
->total_sectors
= 0xffffffff & bs
->total_sectors
;
465 } else if (!memcmp(ph
.magic
, HEADER_MAGIC2
, 16)) {
466 s
->off_multiplier
= le32_to_cpu(ph
.tracks
);
471 s
->tracks
= le32_to_cpu(ph
.tracks
);
472 if (s
->tracks
== 0) {
473 error_setg(errp
, "Invalid image: Zero sectors per track");
477 if (s
->tracks
> INT32_MAX
/513) {
478 error_setg(errp
, "Invalid image: Too big cluster");
483 s
->bat_size
= le32_to_cpu(ph
.bat_entries
);
484 if (s
->bat_size
> INT_MAX
/ sizeof(uint32_t)) {
485 error_setg(errp
, "Catalog too large");
490 size
= sizeof(ParallelsHeader
) + sizeof(uint32_t) * s
->bat_size
;
491 s
->header_size
= ROUND_UP(size
, bdrv_opt_mem_align(bs
->file
));
492 s
->header
= qemu_try_blockalign(bs
->file
, s
->header_size
);
493 if (s
->header
== NULL
) {
497 if (le32_to_cpu(ph
.data_off
) < s
->header_size
) {
498 /* there is not enough unused space to fit to block align between BAT
499 and actual data. We can't avoid read-modify-write... */
500 s
->header_size
= size
;
503 ret
= bdrv_pread(bs
->file
, 0, s
->header
, s
->header_size
);
507 s
->bat_bitmap
= (uint32_t *)(s
->header
+ 1);
509 s
->has_truncate
= bdrv_has_zero_init(bs
->file
) &&
510 bdrv_truncate(bs
->file
, bdrv_getlength(bs
->file
)) == 0;
512 if (le32_to_cpu(ph
.inuse
) == HEADER_INUSE_MAGIC
) {
513 /* Image was not closed correctly. The check is mandatory */
514 s
->header_unclean
= true;
515 if ((flags
& BDRV_O_RDWR
) && !(flags
& BDRV_O_CHECK
)) {
516 error_setg(errp
, "parallels: Image was not closed correctly; "
517 "cannot be opened read/write");
523 if (flags
& BDRV_O_RDWR
) {
524 s
->header
->inuse
= cpu_to_le32(HEADER_INUSE_MAGIC
);
525 ret
= parallels_update_header(bs
);
531 qemu_co_mutex_init(&s
->lock
);
535 error_setg(errp
, "Image not in Parallels format");
538 qemu_vfree(s
->header
);
543 static void parallels_close(BlockDriverState
*bs
)
545 BDRVParallelsState
*s
= bs
->opaque
;
547 if (bs
->open_flags
& BDRV_O_RDWR
) {
548 s
->header
->inuse
= 0;
549 parallels_update_header(bs
);
552 qemu_vfree(s
->header
);
555 static QemuOptsList parallels_create_opts
= {
556 .name
= "parallels-create-opts",
557 .head
= QTAILQ_HEAD_INITIALIZER(parallels_create_opts
.head
),
560 .name
= BLOCK_OPT_SIZE
,
561 .type
= QEMU_OPT_SIZE
,
562 .help
= "Virtual disk size",
565 .name
= BLOCK_OPT_CLUSTER_SIZE
,
566 .type
= QEMU_OPT_SIZE
,
567 .help
= "Parallels image cluster size",
568 .def_value_str
= stringify(DEFAULT_CLUSTER_SIZE
),
570 { /* end of list */ }
574 static BlockDriver bdrv_parallels
= {
575 .format_name
= "parallels",
576 .instance_size
= sizeof(BDRVParallelsState
),
577 .bdrv_probe
= parallels_probe
,
578 .bdrv_open
= parallels_open
,
579 .bdrv_close
= parallels_close
,
580 .bdrv_co_get_block_status
= parallels_co_get_block_status
,
581 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
582 .bdrv_co_readv
= parallels_co_readv
,
583 .bdrv_co_writev
= parallels_co_writev
,
585 .bdrv_create
= parallels_create
,
586 .bdrv_check
= parallels_check
,
587 .create_opts
= ¶llels_create_opts
,
590 static void bdrv_parallels_init(void)
592 bdrv_register(&bdrv_parallels
);
595 block_init(bdrv_parallels_init
);