parallels: load bitmap extension
Subsystems (2)
PATCH 2/5 parallels.txt: fix bitmap L1 table description
In reply to Vladimir Sementsov-OgievskiyActually L1 table entry offset is in 512 bytes sectors. Fix the spec.
--- docs/interop/parallels.txt | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
docs/interop/parallels.txt +9 -6
@@ -209,15 +209,14 @@ of its data area are: The number of entries in the L1 table of the bitmap. variable: l1_table (8 * l1_size bytes) - L1 offset table (in bytes) A dirty bitmap is stored using a one-level structure for the mapping to host -clusters - an L1 table. +clusters - an L1 table. Each L1 table entry is a 64bit integer described +below: -Given an offset in bytes into the bitmap data, the offset in bytes into the -image file can be obtained as follows: +Given an offset in bytes into the bitmap data, corresponding L1 entry is - offset = l1_table[offset / cluster_size] + (offset % cluster_size) + l1_table[offset / cluster_size] If an L1 table entry is 0, the corresponding cluster of the bitmap is assumed to be zero.
@@ -225,4 +224,8 @@ to be zero. If an L1 table entry is 1, the corresponding cluster of the bitmap is assumed to have all bits set. -If an L1 table entry is not 0 or 1, it allocates a cluster from the data area. +If an L1 table entry is not 0 or 1, it contains corresponding cluster offset +(in 512b sectors). Given an offset in bytes into the bitmap data the offset in +bytes into the image file can be obtained as follows: + + offset = l1_table[offset / cluster_size] * 512 + (offset % cluster_size)
Signature · 1 line
2.29.2
PATCH 3/5 parallels: support bitmap extension for read-only mode
In reply to Vladimir Sementsov-Ogievskiy--- block/parallels.h | 6 +- block/parallels-ext.c | 286 ++++++++++++++++++++++++++++++++++++++++++ block/parallels.c | 18 +++ block/meson.build | 3 +- 4 files changed, 311 insertions(+), 2 deletions(-) create mode 100644 block/parallels-ext.c
block/parallels.h +5 -1
@@ -48,7 +48,8 @@ typedef struct ParallelsHeader {uint64_t nb_sectors;uint32_t inuse;uint32_t data_off; -char padding[12 ]; +uint32_t flags; +uint64_t ext_off; }QEMU_PACKED ParallelsHeader; typedef enum ParallelsPreallocMode {
@@ -84,4 +85,7 @@ typedef struct BDRVParallelsState { Error *migration_blocker; } BDRVParallelsState; +int parallels_read_format_extension (BlockDriverState *bs, +int64_t ext_off,Error **errp); +#endif
block/parallels-ext.c new file +286 -0
286 lines not on this page · raw message
block/parallels.c +18 -0
@@ -29,6 +29,7 @@*/ #include "qemu/osdep.h" +#include "qemu/error-report.h" #include "qapi/error.h" #include "block/block_int.h" #include "block/qdict.h"
@@ -843,6 +844,23 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags, goto fail_options; } +if (ph.ext_off) { +if (flags &BDRV_O_RDWR ) { +/* +* It's unsafe to open image RW if there is an extension (as we +* don't support it). But parallels driver in QEMU historically +* ignores the extension, so print warning and don't care. +*/ +warn_report ("Format Extension ignored in RW mode" ); + }else { + ret =parallels_read_format_extension ( + bs,le64_to_cpu (ph.ext_off) <<BDRV_SECTOR_BITS , errp); +if (ret <0 ) { + goto fail; + } + } + } +if ((flags &BDRV_O_RDWR ) && !(flags &BDRV_O_INACTIVE )) { s->header->inuse =cpu_to_le32 (HEADER_INUSE_MAGIC ); ret =parallels_update_header (bs);
block/meson.build +2 -1
@@ -57,7 +57,8 @@ block_ss.add(when: 'CONFIG_QED', if_true: files( 'qed-table.c', 'qed.c', )) -block_ss.add(when: [libxml2, 'CONFIG_PARALLELS'], if_true: files('parallels.c')) +block_ss.add(when: [libxml2, 'CONFIG_PARALLELS'], + if_true: files('parallels.c', 'parallels-ext.c')) block_ss.add(when: 'CONFIG_WIN32', if_true: files('file-win32.c', 'win32-aio.c')) block_ss.add(when: 'CONFIG_POSIX', if_true: [files('file-posix.c'), coref, iokit]) block_ss.add(when: libiscsi, if_true: files('iscsi-opts.c'))
Signature · 1 line
2.29.2
PATCH 5/5 iotests: add parallels-read-bitmap test
In reply to Vladimir Sementsov-OgievskiyTest support for reading bitmap from parallels image format. parallels-with-bitmap.bz2 is generated on Virtuozzo by parallels-with-bitmap.sh
--- .../sample_images/parallels-with-bitmap.bz2 | Bin 0 -> 203 bytes .../sample_images/parallels-with-bitmap.sh | 33 ++++++++++ .../qemu-iotests/tests/parallels-read-bitmap | 57 ++++++++++++++++++ .../tests/parallels-read-bitmap.out | 6 ++ 4 files changed, 96 insertions(+) create mode 100644 tests/qemu-iotests/sample_images/parallels-with-bitmap.bz2 create mode 100755 tests/qemu-iotests/sample_images/parallels-with-bitmap.sh create mode 100755 tests/qemu-iotests/tests/parallels-read-bitmap create mode 100644 tests/qemu-iotests/tests/parallels-read-bitmap.out
tests/qemu-iotests/sample_images/parallels-with-bitmap.bz2 new file binary +0 -0
tests/qemu-iotests/sample_images/parallels-with-bitmap.sh new file +33 -0
@@ -0,0 +1,33 @@ +#!/bin/bash + +CT=parallels-with-bitmap-ct +DIR=$PWD/parallels-with-bitmap-dir +IMG=$DIR/root.hds +XML=$DIR/DiskDescriptor.xml +TARGET=parallels-with-bitmap.bz2 + +rm -rf $DIR + +prlctl create $CT --vmtype ct +prlctl set $CT --device-add hdd --image $DIR --recreate --size 2G + +# cleanup the image +qemu-img create -f parallels $IMG 64G + +# create bitmap +prlctl backup $CT + +prlctl set $CT --device-del hdd1 +prlctl destroy $CT + +dev=$(ploop mount $XML | sed -n 's/^Adding delta dev=\(\/dev\/ploop[0-9]\+\).*/\1/p') +dd if=/dev/zero of=$dev bs=64K seek=5 count=2 oflag=direct +dd if=/dev/zero of=$dev bs=64K seek=30 count=1 oflag=direct +dd if=/dev/zero of=$dev bs=64K seek=10 count=3 oflag=direct +ploop umount $XML # bitmap name will be in the output + +bzip2 -z $IMG + +mv $IMG.bz2 $TARGET + +rm -rf $DIR
tests/qemu-iotests/tests/parallels-read-bitmap new file +57 -0
57 lines not on this page · raw message
tests/qemu-iotests/tests/parallels-read-bitmap.out new file +6 -0
@@ -0,0 +1,6 @@ +Start NBD server +dirty clusters (cluster size is 64K): +5-6 +10-12 +30 +Kill NBD server
Signature · 1 line
2.29.2
cover parallels: load bitmap extension
Hi all!
Suddenly we need to load bitmaps from parallels image in our product. So here is a feature.
Vladimir Sementsov-Ogievskiy (5):
qcow2-bitmap: make bytes_covered_by_bitmap_cluster() public parallels.txt: fix bitmap L1 table description
parallels: support bitmap extension for read-only mode iotests.py: add unarchive_sample_image() helper
iotests: add parallels-read-bitmap test
docs/interop/parallels.txt | 15 +- block/parallels.h | 6 +- include/block/dirty-bitmap.h | 2 + block/dirty-bitmap.c | 13 + block/parallels-ext.c | 286 ++++++++++++++++++ block/parallels.c | 18 ++ block/qcow2-bitmap.c | 16 +- block/meson.build | 3 +- tests/qemu-iotests/iotests.py | 10 + .../sample_images/parallels-with-bitmap.bz2 | Bin 0 -> 203 bytes .../sample_images/parallels-with-bitmap.sh | 33 ++ .../qemu-iotests/tests/parallels-read-bitmap | 57 ++++ .../tests/parallels-read-bitmap.out | 6 + 13 files changed, 443 insertions(+), 22 deletions(-) create mode 100644 block/parallels-ext.c create mode 100644 tests/qemu-iotests/sample_images/parallels-with-bitmap.bz2 create mode 100755 tests/qemu-iotests/sample_images/parallels-with-bitmap.sh create mode 100755 tests/qemu-iotests/tests/parallels-read-bitmap create mode 100644 tests/qemu-iotests/tests/parallels-read-bitmap.out
Signature · 1 line
2.29.2
PATCH 4/5 iotests.py: add unarchive_sample_image() helper
--- tests/qemu-iotests/iotests.py | 10 ++++++++++ 1 file changed, 10 insertions(+)
tests/qemu-iotests/iotests.py +10 -0
@@ -17,6 +17,7 @@ # import atexit +import bz2 from collections import OrderedDict import faulthandler import io
@@ -24,6 +25,7 @@ import json import logging import os import re +import shutil import signal import struct import subprocess
@@ -96,6 +98,14 @@ luks_default_secret_object = 'secret,id=keysec0,data=' + \ os.environ.get('IMGKEYSECRET', '') luks_default_key_secret_opt = 'key-secret=keysec0' +sample_img_dir = os.environ['SAMPLE_IMG_DIR'] + + +def unarchive_sample_image(sample, fname): + sample_fname = os.path.join(sample_img_dir, sample + '.bz2') + with bz2.open(sample_fname) as f_in, open(fname, 'wb') as f_out: + shutil.copyfileobj(f_in, f_out) + def qemu_tool_pipe_and_status(tool: str, args: Sequence[str], connect_stderr: bool = True) -> Tuple[str, int]:
Signature · 1 line
2.29.2
PATCH 1/5 qcow2-bitmap: make bytes_covered_by_bitmap_cluster() public
In reply to Vladimir Sementsov-OgievskiyRename bytes_covered_by_bitmap_cluster() to bdrv_dirty_bitmap_serialization_coverage() and make it public. It is needed as we are going to make load_bitmap_data() public in the next commit.
--- include/block/dirty-bitmap.h | 2 ++ block/dirty-bitmap.c | 13 +++++++++++++ block/qcow2-bitmap.c | 16 ++-------------- 3 files changed, 17 insertions(+), 14 deletions(-)
include/block/dirty-bitmap.h +2 -0
@@ -57,6 +57,8 @@ void bdrv_dirty_iter_free(BdrvDirtyBitmapIter *iter);uint64_t bdrv_dirty_bitmap_serialization_size (const BdrvDirtyBitmap *bitmap,uint64_t offset,uint64_t bytes);uint64_t bdrv_dirty_bitmap_serialization_align (const BdrvDirtyBitmap *bitmap); +uint64_t bdrv_dirty_bitmap_serialization_coverage (int serialized_chunk_size, +const BdrvDirtyBitmap *bitmap);void bdrv_dirty_bitmap_serialize_part (const BdrvDirtyBitmap *bitmap,uint8_t *buf,uint64_t offset,uint64_t bytes);
block/dirty-bitmap.c +13 -0
@@ -726,6 +726,19 @@ uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap *bitmap)return hbitmap_serialization_align (bitmap->bitmap); } +/* Return the disk size covered by a chunk of serialized bitmap data. */ +uint64_t bdrv_dirty_bitmap_serialization_coverage (int serialized_chunk_size, +const BdrvDirtyBitmap *bitmap) +{ +uint64_t granularity =bdrv_dirty_bitmap_granularity (bitmap); +uint64_t limit = granularity * (serialized_chunk_size <<3 ); + +assert (QEMU_IS_ALIGNED (limit, +bdrv_dirty_bitmap_serialization_align (bitmap))); +return limit; +} + +void bdrv_dirty_bitmap_serialize_part (const BdrvDirtyBitmap *bitmap,uint8_t *buf,uint64_t offset,uint64_t bytes)
block/qcow2-bitmap.c +2 -14
@@ -278,18 +278,6 @@ static int free_bitmap_clusters(BlockDriverState *bs, Qcow2BitmapTable *tb)return 0 ; } -/* Return the disk size covered by a single qcow2 cluster of bitmap data. */ -static uint64_t bytes_covered_by_bitmap_cluster (const BDRVQcow2State *s, -const BdrvDirtyBitmap *bitmap) -{ -uint64_t granularity =bdrv_dirty_bitmap_granularity (bitmap); -uint64_t limit = granularity * (s->cluster_size <<3 ); - -assert (QEMU_IS_ALIGNED (limit, -bdrv_dirty_bitmap_serialization_align (bitmap))); -return limit; -} -/* load_bitmap_data * @bitmap_table entries must satisfy specification constraints. * @bitmap must be cleared */
@@ -312,7 +300,7 @@ static int load_bitmap_data(BlockDriverState *bs, } buf =g_malloc (s->cluster_size); - limit =bytes_covered_by_bitmap_cluster (s, bitmap); + limit =bdrv_dirty_bitmap_serialization_coverage (s->cluster_size, bitmap);for (i =0 , offset =0 ; i < tab_size; ++i, offset += limit) {uint64_t count =MIN (bm_size - offset, limit);uint64_t entry = bitmap_table[i];
@@ -1303,7 +1291,7 @@ static uint64_t *store_bitmap_data(BlockDriverState *bs, } buf =g_malloc (s->cluster_size); - limit =bytes_covered_by_bitmap_cluster (s, bitmap); + limit =bdrv_dirty_bitmap_serialization_coverage (s->cluster_size, bitmap);assert (DIV_ROUND_UP (bm_size, limit) == tb_size); offset =0 ;
Signature · 1 line
2.29.2
Re: [PATCH 1/5] qcow2-bitmap: make bytes_covered_by_bitmap_cluster() public
Quoted 5 lines · Vladimir Sementsov-Ogievskiy
Rename
bytes_covered_by_bitmap_cluster()tobdrv_dirty_bitmap_serialization_coverage()and make it public. It is needed as we are going to makeload_bitmap_data()public in the next commit.
Quoted 7 lines
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- include/block/dirty-bitmap.h | 2 ++ block/dirty-bitmap.c | 13 +++++++++++++ block/qcow2-bitmap.c | 16 ++-------------- 3 files changed, 17 insertions(+), 14 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
Re: [PATCH 1/5] qcow2-bitmap: make bytes_covered_by_bitmap_cluster() public
In reply to Vladimir Sementsov-OgievskiyQuoted 4 lines · Vladimir Sementsov-Ogievskiy
Rename
bytes_covered_by_bitmap_cluster()tobdrv_dirty_bitmap_serialization_coverage()and make it public. It is needed as we are going to makeload_bitmap_data()public in the next commit.
Not we are not. So the last sentence should be changed to
It is needed as we are going to share it with bitmap loading in parallels format.
I started from sharing load_bitmap_data() function, but than dropped this idea.
Quoted 87 lines
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- include/block/dirty-bitmap.h | 2 ++ block/dirty-bitmap.c | 13 +++++++++++++ block/qcow2-bitmap.c | 16 ++-------------- 3 files changed, 17 insertions(+), 14 deletions(-)include/block/dirty-bitmap.h +2
-0@@ -57,6 +57,8 @@ void bdrv_dirty_iter_free(BdrvDirtyBitmapIter *iter);uint64_t bdrv_dirty_bitmap_serialization_size (const BdrvDirtyBitmap *bitmap,uint64_t offset,uint64_t bytes);uint64_t bdrv_dirty_bitmap_serialization_align (const BdrvDirtyBitmap *bitmap); +uint64_t bdrv_dirty_bitmap_serialization_coverage (int serialized_chunk_size, +const BdrvDirtyBitmap *bitmap);void bdrv_dirty_bitmap_serialize_part (const BdrvDirtyBitmap *bitmap,uint8_t *buf,uint64_t offset,uint64_t bytes);block/dirty-bitmap.c +13
-0@@ -726,6 +726,19 @@ uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap *bitmap)return hbitmap_serialization_align (bitmap->bitmap); } +/* Return the disk size covered by a chunk of serialized bitmap data. */ +uint64_t bdrv_dirty_bitmap_serialization_coverage (int serialized_chunk_size, +const BdrvDirtyBitmap *bitmap) +{ +uint64_t granularity =bdrv_dirty_bitmap_granularity (bitmap); +uint64_t limit = granularity * (serialized_chunk_size <<3 ); + +assert (QEMU_IS_ALIGNED (limit, +bdrv_dirty_bitmap_serialization_align (bitmap))); +return limit; +} + +void bdrv_dirty_bitmap_serialize_part (const BdrvDirtyBitmap *bitmap,uint8_t *buf,uint64_t offset,uint64_t bytes)block/qcow2-bitmap.c +2
-14@@ -278,18 +278,6 @@ static int free_bitmap_clusters(BlockDriverState *bs, Qcow2BitmapTable *tb)return 0 ; } -/* Return the disk size covered by a single qcow2 cluster of bitmap data. */ -static uint64_t bytes_covered_by_bitmap_cluster (const BDRVQcow2State *s, -const BdrvDirtyBitmap *bitmap) -{ -uint64_t granularity =bdrv_dirty_bitmap_granularity (bitmap); -uint64_t limit = granularity * (s->cluster_size <<3 ); - -assert (QEMU_IS_ALIGNED (limit, -bdrv_dirty_bitmap_serialization_align (bitmap))); -return limit; -} -/* load_bitmap_data * @bitmap_table entries must satisfy specification constraints. * @bitmap must be cleared */ @@ -312,7 +300,7 @@ static int load_bitmap_data(BlockDriverState *bs, } buf =g_malloc (s->cluster_size); - limit =bytes_covered_by_bitmap_cluster (s, bitmap); + limit =bdrv_dirty_bitmap_serialization_coverage (s->cluster_size, bitmap);for (i =0 , offset =0 ; i < tab_size; ++i, offset += limit) {uint64_t count =MIN (bm_size - offset, limit);uint64_t entry = bitmap_table[i];@@ -1303,7 +1291,7 @@ static uint64_t *store_bitmap_data(BlockDriverState *bs, } buf =g_malloc (s->cluster_size); - limit =bytes_covered_by_bitmap_cluster (s, bitmap); + limit =bdrv_dirty_bitmap_serialization_coverage (s->cluster_size, bitmap);assert (DIV_ROUND_UP (bm_size, limit) == tb_size); offset =0 ;
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 1/5] qcow2-bitmap: make bytes_covered_by_bitmap_cluster() public
In reply to Vladimir Sementsov-OgievskiyQuoted 82 lines · Vladimir Sementsov-Ogievskiy
Rename
bytes_covered_by_bitmap_cluster()tobdrv_dirty_bitmap_serialization_coverage()and make it public. It is needed as we are going to makeload_bitmap_data()public in the next commit.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- include/block/dirty-bitmap.h | 2 ++ block/dirty-bitmap.c | 13 +++++++++++++ block/qcow2-bitmap.c | 16 ++-------------- 3 files changed, 17 insertions(+), 14 deletions(-)include/block/dirty-bitmap.h +2
-0@@ -57,6 +57,8 @@ void bdrv_dirty_iter_free(BdrvDirtyBitmapIter *iter);uint64_t bdrv_dirty_bitmap_serialization_size (const BdrvDirtyBitmap *bitmap,uint64_t offset,uint64_t bytes);uint64_t bdrv_dirty_bitmap_serialization_align (const BdrvDirtyBitmap *bitmap); +uint64_t bdrv_dirty_bitmap_serialization_coverage (int serialized_chunk_size, +const BdrvDirtyBitmap *bitmap);void bdrv_dirty_bitmap_serialize_part (const BdrvDirtyBitmap *bitmap,uint8_t *buf,uint64_t offset,uint64_t bytes);block/dirty-bitmap.c +13
-0@@ -726,6 +726,19 @@ uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap *bitmap)return hbitmap_serialization_align (bitmap->bitmap); } +/* Return the disk size covered by a chunk of serialized bitmap data. */ +uint64_t bdrv_dirty_bitmap_serialization_coverage (int serialized_chunk_size, +const BdrvDirtyBitmap *bitmap) +{ +uint64_t granularity =bdrv_dirty_bitmap_granularity (bitmap); +uint64_t limit = granularity * (serialized_chunk_size <<3 ); + +assert (QEMU_IS_ALIGNED (limit, +bdrv_dirty_bitmap_serialization_align (bitmap))); +return limit; +} + +void bdrv_dirty_bitmap_serialize_part (const BdrvDirtyBitmap *bitmap,uint8_t *buf,uint64_t offset,uint64_t bytes)block/qcow2-bitmap.c +1
-13@@ -278,18 +278,6 @@ static int free_bitmap_clusters(BlockDriverState *bs, Qcow2BitmapTable *tb)return 0 ; } -/* Return the disk size covered by a single qcow2 cluster of bitmap data. */ -static uint64_t bytes_covered_by_bitmap_cluster (const BDRVQcow2State *s, -const BdrvDirtyBitmap *bitmap) -{ -uint64_t granularity =bdrv_dirty_bitmap_granularity (bitmap); -uint64_t limit = granularity * (s->cluster_size <<3 ); - -assert (QEMU_IS_ALIGNED (limit, -bdrv_dirty_bitmap_serialization_align (bitmap))); -return limit; -} -/* load_bitmap_data * @bitmap_table entries must satisfy specification constraints. * @bitmap must be cleared */ @@ -312,7 +300,7 @@ static int load_bitmap_data(BlockDriverState *bs, } buf =g_malloc (s->cluster_size); - limit =bytes_covered_by_bitmap_cluster (s, bitmap); + limit =bdrv_dirty_bitmap_serialization_coverage (s->cluster_size, bitmap);for (i =0 , offset =0 ; i < tab_size; ++i, offset += limit) {uint64_t count =MIN (bm_size - offset, limit);uint64_t entry = bitmap_table[i];
block/qcow2-bitmap.c static uint64_t *store_bitmap_data(BlockDriverState *bs, · line 1297
Quoted 8 lines
block/qcow2-bitmap.c +1
-1@@ -1303,7 +1291,7 @@ static uint64_t *store_bitmap_data(BlockDriverState *bs, } buf =g_malloc (s->cluster_size); - limit =bytes_covered_by_bitmap_cluster (s, bitmap); + limit =bdrv_dirty_bitmap_serialization_coverage (s->cluster_size, bitmap);assert (DIV_ROUND_UP (bm_size, limit) == tb_size); offset =0 ;
Re: [PATCH 2/5] parallels.txt: fix bitmap L1 table description
In reply to Vladimir Sementsov-OgievskiyQuoted 12 lines · Vladimir Sementsov-Ogievskiy
Actually L1 table entry offset is in 512 bytes sectors. Fix the spec.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- docs/interop/parallels.txt | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)docs/interop/parallels.txt +0
-0
docs/interop/parallels.txt of its data area are: · line 212
Quoted 4 lines
docs/interop/parallels.txt +0
-1@@ -209,15 +209,14 @@ of its data area are: The number of entries in the L1 table of the bitmap. variable: l1_table (8 * l1_size bytes) - L1 offset table (in bytes)
this change is unclear. First, we have specified here the size of this table. It is (8 * l1_size bytes). Thus it would be MUCH better to say
l1_table (size: 8 * l1_size bytes)
or
L1 offset table (l1_table), size: 8 * l1_size bytes or something like this.
docs/interop/parallels.txt of its data area are: · line 219
Quoted 12 lines
docs/interop/parallels.txt +4
-4A dirty bitmap is stored using a one-level structure for the mapping to host -clusters - an L1 table. +clusters - an L1 table. Each L1 table entry is a 64bit integer described +below: -Given an offset in bytes into the bitmap data, the offset in bytes into the -image file can be obtained as follows: +Given an offset in bytes into the bitmap data, corresponding L1 entry is - offset = l1_table[offset / cluster_size] + (offset % cluster_size) + l1_table[offset / cluster_size]
Dirty bitmap is stored in the array of clusters inside Parallels Image file. Offsets of these clusters are saved in L1 offset table here.
docs/interop/parallels.txt of its data area are: · line 222
Quoted 3 lines
docs/interop/parallels.txt +0
-0If an L1 table entry is 0, the corresponding cluster of the bitmap is assumed to be zero.
If L1 table entry is 0, all bits in the corresponding cluster of the bitmap are assumed to be 0.
docs/interop/parallels.txt to be zero. · line 225
Quoted 3 lines
docs/interop/parallels.txt +0
-0@@ -225,4 +224,8 @@ to be zero. If an L1 table entry is 1, the corresponding cluster of the bitmap is assumed to have all bits set.
If L1 table entry is 1, all bits in the corresponding cluster of the bitmap are assumed to be 1.
docs/interop/parallels.txt to be zero. · line 231
Quoted 7 lines
docs/interop/parallels.txt +5
-1-If an L1 table entry is not 0 or 1, it allocates a cluster from the data area. +If an L1 table entry is not 0 or 1, it contains corresponding cluster offset +(in 512b sectors). Given an offset in bytes into the bitmap data the offset in +bytes into the image file can be obtained as follows: + + offset = l1_table[offset / cluster_size] * 512 + (offset % cluster_size)
looks good
Re: [PATCH 3/5] parallels: support bitmap extension for read-only mode
In reply to Vladimir Sementsov-OgievskiyQuoted 37 lines · Vladimir Sementsov-Ogievskiy
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block/parallels.h | 6 +- block/parallels-ext.c | 286 ++++++++++++++++++++++++++++++++++++++++++ block/parallels.c | 18 +++ block/meson.build | 3 +- 4 files changed, 311 insertions(+), 2 deletions(-) create mode 100644 block/parallels-ext.cblock/parallels.h +5
-1@@ -48,7 +48,8 @@ typedef struct ParallelsHeader {uint64_t nb_sectors;uint32_t inuse;uint32_t data_off; -char padding[12 ]; +uint32_t flags; +uint64_t ext_off; }QEMU_PACKED ParallelsHeader; typedef enum ParallelsPreallocMode {@@ -84,4 +85,7 @@ typedef struct BDRVParallelsState { Error *migration_blocker; } BDRVParallelsState; +int parallels_read_format_extension (BlockDriverState *bs, +int64_t ext_off,Error **errp); +#endif block/parallels-ext.c new file +0
-0
block/parallels-ext.c line 3
Quoted 3 lines
block/parallels-ext.c new file +3
-0@@ -0,0 +1,286 @@ +/* +* Support for Parallels Format Extansion. It's a part of parallels format +* driver.
s/Extansion/Extension/
s/Support for Parallels/Support of Parallels/
s/It's a part of parallels format/It's a part of Parallels format/
Quoted 55 lines
block/parallels-ext.c new file +55
-0+* +* Copyright (c) 2021 Virtuozzo International GmbH +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "block/block_int.h" +#include "parallels.h" +#include "crypto/hash.h" +#include "qemu/uuid.h" + +#define PARALLELS_FORMAT_EXTENSION_MAGIC 0xAB234CEF23DCEA87ULL + +#define PARALLELS_END_OF_FEATURES_MAGIC 0x0ULL +#define PARALLELS_DIRTY_BITMAP_FEATURE_MAGIC 0x20385FAE252CB34AULL + +typedef struct ParallelsFormatExtensionHeader { +uint64_t magic;/* PARALLELS_FORMAT_EXTENSION_MAGIC */ +uint8_t check_sum[16 ]; +} QEMU_PACKED ParallelsFormatExtensionHeader; + +typedef struct ParallelsFeatureHeader { +uint64_t magic; +uint64_t flags; +uint32_t data_size; +uint32_t _unused; +} QEMU_PACKED ParallelsFeatureHeader; + +typedef struct ParallelsDirtyBitmapFeature { +uint64_t size; +uint8_t id[16 ]; +uint32_t granularity; +uint32_t l1_size; +/* L1 table follows */ +} QEMU_PACKED ParallelsDirtyBitmapFeature; + +/* Given L1 table read bitmap data from the image and populate @bitmap */
block/parallels-ext.c line 68
Quoted 10 lines
block/parallels-ext.c new file +10
-0+static int parallels_load_bitmap_data (BlockDriverState *bs, +const uint64_t *l1_table, +uint32_t l1_size, +BdrvDirtyBitmap *bitmap, +Error **errp) +{ +BDRVParallelsState *s = bs->opaque; +int ret =0 ; +uint64_t offset, limit; +int cluster_size = s->tracks <<BDRV_SECTOR_BITS ;
should we save cluster size to BDS or create helper for the purpose? Such operation through the code is looking terrible. Originally it was available in one place only and that was acceptable. Now it spreads over and over.
Quoted 60 lines
block/parallels-ext.c new file +60
-0+uint64_t bm_size =bdrv_dirty_bitmap_size (bitmap); +uint8_t *buf =NULL ; +uint64_t i, tab_size = +DIV_ROUND_UP (bdrv_dirty_bitmap_serialization_size (bitmap,0 , bm_size), + cluster_size); + +if (tab_size != l1_size) { +error_setg (errp,"Bitmap table size %" PRIu32" does not correspond " +"to bitmap size and cluster size. Expected %" PRIu64, + l1_size, tab_size); +return -EINVAL ; + } + + buf =qemu_blockalign (bs, cluster_size); + limit =bdrv_dirty_bitmap_serialization_coverage (cluster_size, bitmap); +for (i =0 , offset =0 ; i < tab_size; ++i, offset += limit) { +uint64_t count =MIN (bm_size - offset, limit); +uint64_t entry = l1_table[i]; + +if (entry ==0 ) { +/* No need to deserialize zeros because @bitmap is cleared. */ +continue ; + } + +if (entry ==1 ) { +bdrv_dirty_bitmap_deserialize_ones (bitmap, offset, count, false); + }else { + ret =bdrv_pread (bs->file, entry <<BDRV_SECTOR_BITS , buf, + cluster_size); +if (ret <0 ) { +error_setg_errno (errp, -ret, +"Failed to read bitmap data cluster" ); + goto finish; + } +bdrv_dirty_bitmap_deserialize_part (bitmap, buf, offset, count, + false); + } + } + ret =0 ; + +bdrv_dirty_bitmap_deserialize_finish (bitmap); + +finish: +qemu_vfree (buf); + +return ret; +} + +/* +* @data buffer (of @data_size size) is the Dirty bitmaps feature which +* consists of ParallelsDirtyBitmapFeature followed by L1 table. +*/ +static BdrvDirtyBitmap *parallels_load_bitmap (BlockDriverState *bs, +uint8_t *data, +size_t data_size, +Error **errp) +{ +int ret; +ParallelsDirtyBitmapFeature bf; + g_autofree uint64_t *l1_table = NULL;
block/parallels-ext.c line 138
Quoted 10 lines
block/parallels-ext.c new file +10
-0+BdrvDirtyBitmap *bitmap; +QemuUUID uuid; +char uuidstr[UUID_FMT_LEN +1 ]; + +memcpy (&bf, data,sizeof (bf)); + bf.size =le64_to_cpu (bf.size); + bf.granularity =le32_to_cpu (bf.granularity) <<BDRV_SECTOR_BITS ; + bf.l1_size =le32_to_cpu (bf.l1_size); + data +=sizeof (bf); + data_size -=sizeof (bf);
could this be underflowed? size_t is unsigned
Quoted 11 lines
block/parallels-ext.c new file +11
-0+ +if (bf.size != bs->total_sectors) { +error_setg (errp,"Bitmap size (in sectors) %" PRId64" differs from " +"disk size in sectors %" PRId64, bf.size, bs->total_sectors); +return NULL ; + } + +if (bf.l1_size *sizeof (uint64_t ) > data_size) { +error_setg (errp,"Bitmaps feature corrupted: l1 table exceeds " +"extension data_size" ); +return NULL ;
block/parallels-ext.c line 159
Quoted 10 lines
block/parallels-ext.c new file +10
-0+ } + +memcpy (&uuid, bf.id,sizeof (uuid)); +qemu_uuid_unparse (&uuid, uuidstr); + bitmap =bdrv_create_dirty_bitmap (bs, bf.granularity, uuidstr, errp); +if (!bitmap) { +return NULL ; + } + + l1_table =g_memdup (data, bf.l1_size *sizeof (uint64_t ));
le64 to CPU conversion seems missed
block/parallels-ext.c line 163
Quoted 4 lines
block/parallels-ext.c new file +4
-0+ + ret =parallels_load_bitmap_data (bs, l1_table, bf.l1_size, bitmap, errp); +if (ret <0 ) { +bdrv_release_dirty_bitmap (bitmap);
l1_table is leaked
block/parallels-ext.c line 168
Quoted 5 lines
block/parallels-ext.c new file +5
-0+return NULL ; + } + +/* We support format extension only for RO parallels images. */ +assert (!(bs->open_flags &BDRV_O_RDWR ));
This is what I am not fully agree with. We support bitmap RO, i.e. we will not
be able to continue tracking and write this again, but for the purpose of the reverse delta we could have the image RW.
and even for the case of consistency, do you feel that assert could be tooooooo strong. Our colleagues will come to us here with the blames that QEMU has been crashed even they are doing something completely wrong :)
block/parallels-ext.c line 169
Quoted 1 line
block/parallels-ext.c new file +1
-0+bdrv_dirty_bitmap_set_readonly (bitmap, true);
l1_table is leaked here too
Quoted 37 lines
block/parallels-ext.c new file +37
-0+ +return bitmap; +} + +static int parallels_parse_format_extension (BlockDriverState *bs, +uint8_t *ext_cluster,Error **errp) +{ +BDRVParallelsState *s = bs->opaque; +int ret; +int remaining = s->tracks <<BDRV_SECTOR_BITS ;/* one cluster */ +uint8_t *pos = ext_cluster; +ParallelsFormatExtensionHeader eh; + g_autofree uint8_t *hash = NULL; +size_t hash_len =0 ; +BdrvDirtyBitmap *bitmap =NULL ; + +memcpy (&eh, pos,sizeof (eh)); + eh.magic =le64_to_cpu (eh.magic); + pos +=sizeof (eh); + remaining -=sizeof (eh); + +if (eh.magic !=PARALLELS_FORMAT_EXTENSION_MAGIC ) { +error_setg (errp,"Wrong parallels Format Extension magic: 0x%" PRIx64 +", expected: 0x%llx" , eh.magic, +PARALLELS_FORMAT_EXTENSION_MAGIC ); + goto fail; + } + + ret =qcrypto_hash_bytes (QCRYPTO_HASH_ALG_MD5 , (char *)pos, remaining, + &hash, &hash_len, errp); +if (ret <0 ) { + goto fail; + } + +if (hash_len !=sizeof (eh.check_sum) || +memcmp (hash, eh.check_sum,sizeof (eh.check_sum)) !=0 ) { + error_setg(errp, "Wrong checksum in Format Extension header. Format "
block/parallels-ext.c line 216
Quoted 10 lines
block/parallels-ext.c new file +10
-0+ "extension is corrupted."); + goto fail; + } + +while (true) { +ParallelsFeatureHeader fh; + +memcpy (&fh, pos,sizeof (fh)); + pos +=sizeof (fh); + remaining -=sizeof (fh);
remaining is to be checked before memcpy to avoid reading beyond end of the cluster
Quoted 15 lines
block/parallels-ext.c new file +15
-0+ + fh.magic =le64_to_cpu (fh.magic); + fh.flags =le64_to_cpu (fh.flags); + fh.data_size =le32_to_cpu (fh.data_size); + +if (fh.flags) { +error_setg (errp,"Flags for extension feature are unsupported" ); + goto fail; + } + +if (fh.data_size > remaining) { +error_setg (errp,"Feature data_size exceedes Format Extension " +"cluster" ); + goto fail; + }
block/parallels-ext.c line 241
Quoted 10 lines
block/parallels-ext.c new file +10
-0+ +switch (fh.magic) { +case PARALLELS_END_OF_FEATURES_MAGIC : +return 0 ; + +case PARALLELS_DIRTY_BITMAP_FEATURE_MAGIC : +if (bitmap) { +error_setg (errp,"Multiple bitmaps in Format Extension" ); + goto fail; + }
unsure at the moment. May be this is too restrictive.
Quoted 95 lines
block/parallels-ext.c new file +45
-0+ bitmap =parallels_load_bitmap (bs, pos, fh.data_size, errp); +if (!bitmap) { + goto fail; + } +break ; + +default : +error_setg (errp,"Unknown feature: 0x%" PRIu64, fh.magic); + goto fail; + } + + pos = ext_cluster +QEMU_ALIGN_UP (pos + fh.data_size - ext_cluster,8 ); + } + +fail: +if (bitmap) { +bdrv_release_dirty_bitmap (bitmap); + } + +return -EINVAL ; +} + +int parallels_read_format_extension (BlockDriverState *bs, +int64_t ext_off,Error **errp) +{ +BDRVParallelsState *s = bs->opaque; +int ret; +int cluster_size = s->tracks <<BDRV_SECTOR_BITS ; +uint8_t *ext_cluster =qemu_blockalign (bs, cluster_size); + +assert (ext_off >0 ); + + ret =bdrv_pread (bs->file, ext_off, ext_cluster, cluster_size); +if (ret <0 ) { +error_setg_errno (errp, -ret,"Failed to read Format Extension cluster" ); + goto out; + } + + ret =parallels_parse_format_extension (bs, ext_cluster, errp); + +out: +qemu_vfree (ext_cluster); + +return ret; +}block/parallels.c +18
-0@@ -29,6 +29,7 @@*/ #include "qemu/osdep.h" +#include "qemu/error-report.h" #include "qapi/error.h" #include "block/block_int.h" #include "block/qdict.h" @@ -843,6 +844,23 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags, goto fail_options; } +if (ph.ext_off) { +if (flags &BDRV_O_RDWR ) { +/* +* It's unsafe to open image RW if there is an extension (as we +* don't support it). But parallels driver in QEMU historically +* ignores the extension, so print warning and don't care. +*/ +warn_report ("Format Extension ignored in RW mode" ); + }else { + ret =parallels_read_format_extension ( + bs,le64_to_cpu (ph.ext_off) <<BDRV_SECTOR_BITS , errp); +if (ret <0 ) { + goto fail; + } + } + } +if ((flags &BDRV_O_RDWR ) && !(flags &BDRV_O_INACTIVE )) { s->header->inuse =cpu_to_le32 (HEADER_INUSE_MAGIC ); ret =parallels_update_header (bs);block/meson.build +2
-1@@ -57,7 +57,8 @@ block_ss.add(when: 'CONFIG_QED', if_true: files( 'qed-table.c', 'qed.c', )) -block_ss.add(when: [libxml2, 'CONFIG_PARALLELS'], if_true: files('parallels.c')) +block_ss.add(when: [libxml2, 'CONFIG_PARALLELS'], + if_true: files('parallels.c', 'parallels-ext.c')) block_ss.add(when: 'CONFIG_WIN32', if_true: files('file-win32.c', 'win32-aio.c')) block_ss.add(when: 'CONFIG_POSIX', if_true: [files('file-posix.c'), coref, iokit]) block_ss.add(when: libiscsi, if_true: files('iscsi-opts.c'))
Re: [PATCH 5/5] iotests: add parallels-read-bitmap test
In reply to Vladimir Sementsov-Ogievskiytests/qemu-iotests/sample_images/parallels-with-bitmap.sh
Quoted 36 lines · Vladimir Sementsov-Ogievskiy
Test support for reading bitmap from parallels image format. parallels-with-bitmap.bz2 is generated on Virtuozzo by
parallels-with-bitmap.shSigned-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- .../sample_images/parallels-with-bitmap.bz2 | Bin 0 -> 203 bytes .../sample_images/parallels-with-bitmap.sh | 33 ++++++++++ .../qemu-iotests/tests/parallels-read-bitmap | 57 ++++++++++++++++++ .../tests/parallels-read-bitmap.out | 6 ++ 4 files changed, 96 insertions(+) create mode 100644 tests/qemu-iotests/sample_images/parallels-with-bitmap.bz2 create mode 100755 tests/qemu-iotests/sample_images/parallels-with-bitmap.sh create mode 100755 tests/qemu-iotests/tests/parallels-read-bitmap create mode 100644 tests/qemu-iotests/tests/parallels-read-bitmap.outtests/qemu-iotests/sample_images/parallels-with-bitmap.bz2 new file binary +0
-0tests/qemu-iotests/sample_images/parallels-with-bitmap.sh new file +0
-0@@ -0,0 +1,33 @@
do we need Copyright notice here? I am unsure that this script is to be acceptable in QEMU repo. Anyway, it looks fine :)
Quoted 65 lines
tests/qemu-iotests/sample_images/parallels-with-bitmap.sh new file +33
-0+#!/bin/bash + +CT=parallels-with-bitmap-ct +DIR=$PWD/parallels-with-bitmap-dir +IMG=$DIR/root.hds +XML=$DIR/DiskDescriptor.xml +TARGET=parallels-with-bitmap.bz2 + +rm -rf $DIR + +prlctl create $CT --vmtype ct +prlctl set $CT --device-add hdd --image $DIR --recreate --size 2G + +# cleanup the image +qemu-img create -f parallels $IMG 64G + +# create bitmap +prlctl backup $CT + +prlctl set $CT --device-del hdd1 +prlctl destroy $CT + +dev=$(ploop mount $XML | sed -n 's/^Adding delta dev=\(\/dev\/ploop[0-9]\+\).*/\1/p') +dd if=/dev/zero of=$dev bs=64K seek=5 count=2 oflag=direct +dd if=/dev/zero of=$dev bs=64K seek=30 count=1 oflag=direct +dd if=/dev/zero of=$dev bs=64K seek=10 count=3 oflag=direct +ploop umount $XML # bitmap name will be in the output + +bzip2 -z $IMG + +mv $IMG.bz2 $TARGET + +rm -rf $DIRtests/qemu-iotests/tests/parallels-read-bitmap new file +26
-0@@ -0,0 +1,57 @@ +#!/usr/bin/env python3 +# +# Test parallels load bitmap +# +# Copyright (c) 2021 Virtuozzo International GmbH. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# + +import json +import iotests +from iotests import qemu_nbd_popen, qemu_img_pipe, log, file_path + +iotests.script_initialize(supported_fmts=['parallels']) +
tests/qemu-iotests/tests/parallels-read-bitmap line 36
Quoted 10 lines
tests/qemu-iotests/tests/parallels-read-bitmap new file +10
-0+nbd_sock = file_path('nbd-sock', base_dir=iotests.sock_dir) +disk = iotests.file_path('disk') +bitmap = 'e4f2eed0-37fe-4539-b50b-85d2e7fd235f' +nbd_opts = f'driver=nbd,server.type=unix,server.path={nbd_sock}' \ + f',x-dirty-bitmap=qemu:dirty-bitmap:{bitmap}' + + +iotests.unarchive_sample_image('parallels-with-bitmap', disk) + +iotests.unarchive_sample_image('parallels-with-bitmap', '/work/mega')
no-no-no, '/work/mega' is absolutely no way
Quoted 33 lines
tests/qemu-iotests/tests/parallels-read-bitmap new file +21
-0+ + +with qemu_nbd_popen('--read-only', f'--socket={nbd_sock}', + f'--bitmap={bitmap}', '-f', iotests.imgfmt, disk): + out = qemu_img_pipe('map', '--output=json', '--image-opts', nbd_opts) + chunks = json.loads(out) + cluster = 64 * 1024 + + log('dirty clusters (cluster size is 64K):') + for c in chunks: + assert c['start'] % cluster == 0 + assert c['length'] % cluster == 0 + if c['data']: + continue + + a = c['start'] // cluster + b = (c['start'] + c['length']) // cluster + if b - a > 1: + log(f'{a}-{b-1}') + else: + log(a)tests/qemu-iotests/tests/parallels-read-bitmap.out new file +6
-0@@ -0,0 +1,6 @@ +Start NBD server +dirty clusters (cluster size is 64K): +5-6 +10-12 +30 +Kill NBD server
Re: [PATCH 3/5] parallels: support bitmap extension for read-only mode
In reply to Denis V. LunevQuoted 106 lines · Denis V. Lunev
Quoted 37 lines · Vladimir Sementsov-Ogievskiy
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block/parallels.h | 6 +- block/parallels-ext.c | 286 ++++++++++++++++++++++++++++++++++++++++++ block/parallels.c | 18 +++ block/meson.build | 3 +- 4 files changed, 311 insertions(+), 2 deletions(-)create mode 100644
block/parallels-ext.cblock/parallels.h +5
-1@@ -48,7 +48,8 @@ typedef struct ParallelsHeader {uint64_t nb_sectors;uint32_t inuse;uint32_t data_off; -char padding[12 ]; +uint32_t flags; +uint64_t ext_off; }QEMU_PACKED ParallelsHeader; typedef enum ParallelsPreallocMode {@@ -84,4 +85,7 @@ typedef struct BDRVParallelsState { Error *migration_blocker; } BDRVParallelsState; +int parallels_read_format_extension (BlockDriverState *bs, +int64_t ext_off,Error **errp); +#endif block/parallels-ext.c new file +0
-0block/parallels-ext.c line 3
Quoted 3 lines
block/parallels-ext.c new file +3
-0@@ -0,0 +1,286 @@ +/* +* Support for Parallels Format Extansion. It's a part of parallels format +* driver. s/Extansion/Extension/
s/Support for Parallels/Support of Parallels/
s/It's a part of parallels format/It's a part of Parallels format/Quoted 62 lines
block/parallels-ext.c new file +62
-0+* +* Copyright (c) 2021 Virtuozzo International GmbH +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "block/block_int.h" +#include "parallels.h" +#include "crypto/hash.h" +#include "qemu/uuid.h" + +#define PARALLELS_FORMAT_EXTENSION_MAGIC 0xAB234CEF23DCEA87ULL + +#define PARALLELS_END_OF_FEATURES_MAGIC 0x0ULL +#define PARALLELS_DIRTY_BITMAP_FEATURE_MAGIC 0x20385FAE252CB34AULL + +typedef struct ParallelsFormatExtensionHeader { +uint64_t magic;/* PARALLELS_FORMAT_EXTENSION_MAGIC */ +uint8_t check_sum[16 ]; +} QEMU_PACKED ParallelsFormatExtensionHeader; + +typedef struct ParallelsFeatureHeader { +uint64_t magic; +uint64_t flags; +uint32_t data_size; +uint32_t _unused; +} QEMU_PACKED ParallelsFeatureHeader; + +typedef struct ParallelsDirtyBitmapFeature { +uint64_t size; +uint8_t id[16 ]; +uint32_t granularity; +uint32_t l1_size; +/* L1 table follows */ +} QEMU_PACKED ParallelsDirtyBitmapFeature; + +/* Given L1 table read bitmap data from the image and populate @bitmap */ +static int parallels_load_bitmap_data (BlockDriverState *bs, +const uint64_t *l1_table, +uint32_t l1_size, +BdrvDirtyBitmap *bitmap, +Error **errp) +{ +BDRVParallelsState *s = bs->opaque;
Quoted 8 lines
block/parallels-ext.c line 68
Quoted 3 lines
block/parallels-ext.c new file +3
-0+int ret =0 ; +uint64_t offset, limit; +int cluster_size = s->tracks <<BDRV_SECTOR_BITS ;should we save cluster size to BDS or create helper for the purpose? Such operation through the code is looking terrible. Originally it was available in one place only and that was acceptable. Now it spreads over and over.
Agree, will do.
Quoted 64 lines
Quoted 64 lines
block/parallels-ext.c new file +63
-0+uint64_t bm_size =bdrv_dirty_bitmap_size (bitmap); +uint8_t *buf =NULL ; +uint64_t i, tab_size = +DIV_ROUND_UP (bdrv_dirty_bitmap_serialization_size (bitmap,0 , bm_size), + cluster_size); + +if (tab_size != l1_size) { +error_setg (errp,"Bitmap table size %" PRIu32" does not correspond " +"to bitmap size and cluster size. Expected %" PRIu64, + l1_size, tab_size); +return -EINVAL ; + } + + buf =qemu_blockalign (bs, cluster_size); + limit =bdrv_dirty_bitmap_serialization_coverage (cluster_size, bitmap); +for (i =0 , offset =0 ; i < tab_size; ++i, offset += limit) { +uint64_t count =MIN (bm_size - offset, limit); +uint64_t entry = l1_table[i]; + +if (entry ==0 ) { +/* No need to deserialize zeros because @bitmap is cleared. */ +continue ; + } + +if (entry ==1 ) { +bdrv_dirty_bitmap_deserialize_ones (bitmap, offset, count, false); + }else { + ret =bdrv_pread (bs->file, entry <<BDRV_SECTOR_BITS , buf, + cluster_size); +if (ret <0 ) { +error_setg_errno (errp, -ret, +"Failed to read bitmap data cluster" ); + goto finish; + } +bdrv_dirty_bitmap_deserialize_part (bitmap, buf, offset, count, + false); + } + } + ret =0 ; + +bdrv_dirty_bitmap_deserialize_finish (bitmap); + +finish: +qemu_vfree (buf); + +return ret; +} + +/* +* @data buffer (of @data_size size) is the Dirty bitmaps feature which +* consists of ParallelsDirtyBitmapFeature followed by L1 table. +*/ +static BdrvDirtyBitmap *parallels_load_bitmap (BlockDriverState *bs, +uint8_t *data, +size_t data_size, +Error **errp) +{ +int ret; +ParallelsDirtyBitmapFeature bf; + g_autofree uint64_t *l1_table = NULL; +BdrvDirtyBitmap *bitmap; +QemuUUID uuid; +char uuidstr[UUID_FMT_LEN +1 ];
Quoted 8 lines
block/parallels-ext.c line 138
Quoted 7 lines
block/parallels-ext.c new file +7
-0+ +memcpy (&bf, data,sizeof (bf)); + bf.size =le64_to_cpu (bf.size); + bf.granularity =le32_to_cpu (bf.granularity) <<BDRV_SECTOR_BITS ; + bf.l1_size =le32_to_cpu (bf.l1_size); + data +=sizeof (bf); + data_size -=sizeof (bf);could this be underflowed?
size_tis unsigned
I should check the size at function start
Quoted 15 lines
Quoted 15 lines
block/parallels-ext.c new file +14
-0+ +if (bf.size != bs->total_sectors) { +error_setg (errp,"Bitmap size (in sectors) %" PRId64" differs from " +"disk size in sectors %" PRId64, bf.size, bs->total_sectors); +return NULL ; + } + +if (bf.l1_size *sizeof (uint64_t ) > data_size) { +error_setg (errp,"Bitmaps feature corrupted: l1 table exceeds " +"extension data_size" ); +return NULL ; + } + +memcpy (&uuid, bf.id,sizeof (uuid));
Quoted 8 lines
block/parallels-ext.c line 159
Quoted 7 lines
block/parallels-ext.c new file +7
-0+qemu_uuid_unparse (&uuid, uuidstr); + bitmap =bdrv_create_dirty_bitmap (bs, bf.granularity, uuidstr, errp); +if (!bitmap) { +return NULL ; + } + + l1_table =g_memdup (data, bf.l1_size *sizeof (uint64_t ));le64 to CPU conversion seems missed
for l1_table entries.. yes. will fix
Quoted 6 lines
block/parallels-ext.c line 163
Quoted 5 lines
block/parallels-ext.c new file +4
-0+ + ret =parallels_load_bitmap_data (bs, l1_table, bf.l1_size, bitmap, errp); +if (ret <0 ) { +bdrv_release_dirty_bitmap (bitmap);
l1_tableis leaked
no, it's defined with g_autofree, so it's automatically freed.
Quoted 11 lines
block/parallels-ext.c line 168
Quoted 6 lines
block/parallels-ext.c new file +5
-0+return NULL ; + } + +/* We support format extension only for RO parallels images. */ +assert (!(bs->open_flags &BDRV_O_RDWR ));This is what I am not fully agree with. We support bitmap RO, i.e. we will not
be able to continue tracking and write this again, but for the purpose of the reverse delta we could have the image RW.
Quoted 4 lines
and even for the case of consistency, do you feel that assert could be tooooooo strong. Our colleagues will come to us here with the blames that QEMU has been crashed even they are doing something completely wrong :)
Assert here is OK, as we will not come here on RW image, as we don't load bitmaps for RW image, so it's OK...
About reverse delta, I think we'd better use qcow2 as delta. We can load bitmap from RO parallels and copy it to qcow2 delta.
What you want is loading bitmap in "disabled" mode for RW parallels image. In qcow2 bitmaps format we have corresponding flag, so we can store active and disabled bitmaps, and load them as active and disabled appropriately. In parallels format we don't have this flag. So, I think, by default the bitmap in parallels format is assumed to be active, and we must update it when disk is written to. If we want to change this behavior we'll need an open flag for parallels format like load-bitmaps-as-disabled=true..
Quoted 46 lines
block/parallels-ext.c line 169
Quoted 2 lines
block/parallels-ext.c new file +1
-0+bdrv_dirty_bitmap_set_readonly (bitmap, true);
l1_tableis leaked here tooQuoted 41 lines
block/parallels-ext.c new file +41
-0+ +return bitmap; +} + +static int parallels_parse_format_extension (BlockDriverState *bs, +uint8_t *ext_cluster,Error **errp) +{ +BDRVParallelsState *s = bs->opaque; +int ret; +int remaining = s->tracks <<BDRV_SECTOR_BITS ;/* one cluster */ +uint8_t *pos = ext_cluster; +ParallelsFormatExtensionHeader eh; + g_autofree uint8_t *hash = NULL; +size_t hash_len =0 ; +BdrvDirtyBitmap *bitmap =NULL ; + +memcpy (&eh, pos,sizeof (eh)); + eh.magic =le64_to_cpu (eh.magic); + pos +=sizeof (eh); + remaining -=sizeof (eh); + +if (eh.magic !=PARALLELS_FORMAT_EXTENSION_MAGIC ) { +error_setg (errp,"Wrong parallels Format Extension magic: 0x%" PRIx64 +", expected: 0x%llx" , eh.magic, +PARALLELS_FORMAT_EXTENSION_MAGIC ); + goto fail; + } + + ret =qcrypto_hash_bytes (QCRYPTO_HASH_ALG_MD5 , (char *)pos, remaining, + &hash, &hash_len, errp); +if (ret <0 ) { + goto fail; + } + +if (hash_len !=sizeof (eh.check_sum) || +memcmp (hash, eh.check_sum,sizeof (eh.check_sum)) !=0 ) { +error_setg (errp,"Wrong checksum in Format Extension header. Format " +"extension is corrupted." ); + goto fail; + } +
Quoted 8 lines
block/parallels-ext.c line 216
Quoted 6 lines
block/parallels-ext.c new file +6
-0+while (true) { +ParallelsFeatureHeader fh; + +memcpy (&fh, pos,sizeof (fh)); + pos +=sizeof (fh); + remaining -=sizeof (fh);remaining is to be checked before memcpy to avoid reading beyond end of the cluster
agree
Quoted 19 lines
Quoted 19 lines
block/parallels-ext.c new file +18
-0+ + fh.magic =le64_to_cpu (fh.magic); + fh.flags =le64_to_cpu (fh.flags); + fh.data_size =le32_to_cpu (fh.data_size); + +if (fh.flags) { +error_setg (errp,"Flags for extension feature are unsupported" ); + goto fail; + } + +if (fh.data_size > remaining) { +error_setg (errp,"Feature data_size exceedes Format Extension " +"cluster" ); + goto fail; + } + +switch (fh.magic) { +case PARALLELS_END_OF_FEATURES_MAGIC :
Quoted 8 lines
block/parallels-ext.c line 241
Quoted 7 lines
block/parallels-ext.c new file +7
-0+return 0 ; + +case PARALLELS_DIRTY_BITMAP_FEATURE_MAGIC : +if (bitmap) { +error_setg (errp,"Multiple bitmaps in Format Extension" ); + goto fail; + }unsure at the moment. May be this is too restrictive.
Hmm. Somehow I thought that it is mentioned in spec.. But now I see that it doesn't. Will improve to load several bitmaps it's not difficult.
Quoted 97 lines
Quoted 96 lines
block/parallels-ext.c new file +45
-0+ bitmap =parallels_load_bitmap (bs, pos, fh.data_size, errp); +if (!bitmap) { + goto fail; + } +break ; + +default : +error_setg (errp,"Unknown feature: 0x%" PRIu64, fh.magic); + goto fail; + } + + pos = ext_cluster +QEMU_ALIGN_UP (pos + fh.data_size - ext_cluster,8 ); + } + +fail: +if (bitmap) { +bdrv_release_dirty_bitmap (bitmap); + } + +return -EINVAL ; +} + +int parallels_read_format_extension (BlockDriverState *bs, +int64_t ext_off,Error **errp) +{ +BDRVParallelsState *s = bs->opaque; +int ret; +int cluster_size = s->tracks <<BDRV_SECTOR_BITS ; +uint8_t *ext_cluster =qemu_blockalign (bs, cluster_size); + +assert (ext_off >0 ); + + ret =bdrv_pread (bs->file, ext_off, ext_cluster, cluster_size); +if (ret <0 ) { +error_setg_errno (errp, -ret,"Failed to read Format Extension cluster" ); + goto out; + } + + ret =parallels_parse_format_extension (bs, ext_cluster, errp); + +out: +qemu_vfree (ext_cluster); + +return ret; +}block/parallels.c +18
-0@@ -29,6 +29,7 @@*/ #include "qemu/osdep.h" +#include "qemu/error-report.h" #include "qapi/error.h" #include "block/block_int.h" #include "block/qdict.h" @@ -843,6 +844,23 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags, goto fail_options; } +if (ph.ext_off) { +if (flags &BDRV_O_RDWR ) { +/* +* It's unsafe to open image RW if there is an extension (as we +* don't support it). But parallels driver in QEMU historically +* ignores the extension, so print warning and don't care. +*/ +warn_report ("Format Extension ignored in RW mode" ); + }else { + ret =parallels_read_format_extension ( + bs,le64_to_cpu (ph.ext_off) <<BDRV_SECTOR_BITS , errp); +if (ret <0 ) { + goto fail; + } + } + } +if ((flags &BDRV_O_RDWR ) && !(flags &BDRV_O_INACTIVE )) { s->header->inuse =cpu_to_le32 (HEADER_INUSE_MAGIC ); ret =parallels_update_header (bs);block/meson.build +2
-1@@ -57,7 +57,8 @@ block_ss.add(when: 'CONFIG_QED', if_true: files( 'qed-table.c', 'qed.c', )) -block_ss.add(when: [libxml2, 'CONFIG_PARALLELS'], if_true: files('parallels.c')) +block_ss.add(when: [libxml2, 'CONFIG_PARALLELS'], + if_true: files('parallels.c', 'parallels-ext.c')) block_ss.add(when: 'CONFIG_WIN32', if_true: files('file-win32.c', 'win32-aio.c')) block_ss.add(when: 'CONFIG_POSIX', if_true: [files('file-posix.c'), coref, iokit]) block_ss.add(when: libiscsi, if_true: files('iscsi-opts.c'))
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 5/5] iotests: add parallels-read-bitmap test
In reply to Denis V. LunevQuoted 31 lines · Denis V. Lunev
Quoted 30 lines · Vladimir Sementsov-Ogievskiy
Test support for reading bitmap from parallels image format. parallels-with-bitmap.bz2 is generated on Virtuozzo by
parallels-with-bitmap.shSigned-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- .../sample_images/parallels-with-bitmap.bz2 | Bin 0 -> 203 bytes .../sample_images/parallels-with-bitmap.sh | 33 ++++++++++ .../qemu-iotests/tests/parallels-read-bitmap | 57 ++++++++++++++++++ .../tests/parallels-read-bitmap.out | 6 ++ 4 files changed, 96 insertions(+)create mode 100644 tests/qemu-iotests/sample_images/parallels-with-bitmap.bz2 create mode 100755
tests/qemu-iotests/sample_images/parallels-with-bitmap.shcreate mode 100755 tests/qemu-iotests/tests/parallels-read-bitmap create mode 100644 tests/qemu-iotests/tests/parallels-read-bitmap.outtests/qemu-iotests/sample_images/parallels-with-bitmap.bz2 new file binary +0
-0
Quoted 7 lines
tests/qemu-iotests/sample_images/parallels-with-bitmap.sh
Quoted 6 lines
tests/qemu-iotests/sample_images/parallels-with-bitmap.sh new file +0
-0@@ -0,0 +1,33 @@do we need Copyright notice here?
Will add
Quoted 2 lines
I am unsure that this script is to be
acceptable in QEMU repo. Anyway, it looks fine :)
Yes, it requires some proprietary tools, not available for most of developers.. On the other hand, I should document somehow, how did I create the image (at least for future me). I can do it in a commit message, but I think better is just add a script, it will not hurt.
Quoted 70 lines
Quoted 70 lines
tests/qemu-iotests/sample_images/parallels-with-bitmap.sh new file +33
-0+#!/bin/bash + +CT=parallels-with-bitmap-ct +DIR=$PWD/parallels-with-bitmap-dir +IMG=$DIR/root.hds +XML=$DIR/DiskDescriptor.xml +TARGET=parallels-with-bitmap.bz2 + +rm -rf $DIR + +prlctl create $CT --vmtype ct +prlctl set $CT --device-add hdd --image $DIR --recreate --size 2G + +# cleanup the image +qemu-img create -f parallels $IMG 64G + +# create bitmap +prlctl backup $CT + +prlctl set $CT --device-del hdd1 +prlctl destroy $CT + +dev=$(ploop mount $XML | sed -n 's/^Adding delta dev=\(\/dev\/ploop[0-9]\+\).*/\1/p') +dd if=/dev/zero of=$dev bs=64K seek=5 count=2 oflag=direct +dd if=/dev/zero of=$dev bs=64K seek=30 count=1 oflag=direct +dd if=/dev/zero of=$dev bs=64K seek=10 count=3 oflag=direct +ploop umount $XML # bitmap name will be in the output + +bzip2 -z $IMG + +mv $IMG.bz2 $TARGET + +rm -rf $DIRtests/qemu-iotests/tests/parallels-read-bitmap new file +29
-0@@ -0,0 +1,57 @@ +#!/usr/bin/env python3 +# +# Test parallels load bitmap +# +# Copyright (c) 2021 Virtuozzo International GmbH. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# + +import json +import iotests +from iotests import qemu_nbd_popen, qemu_img_pipe, log, file_path + +iotests.script_initialize(supported_fmts=['parallels']) + +nbd_sock = file_path('nbd-sock', base_dir=iotests.sock_dir) +disk = iotests.file_path('disk') +bitmap = 'e4f2eed0-37fe-4539-b50b-85d2e7fd235f'
Quoted 9 lines
tests/qemu-iotests/tests/parallels-read-bitmap line 36
Quoted 7 lines
tests/qemu-iotests/tests/parallels-read-bitmap new file +7
-0+nbd_opts = f'driver=nbd,server.type=unix,server.path={nbd_sock}' \ + f',x-dirty-bitmap=qemu:dirty-bitmap:{bitmap}' + + +iotests.unarchive_sample_image('parallels-with-bitmap', disk) + +iotests.unarchive_sample_image('parallels-with-bitmap', '/work/mega')no-no-no, '/work/mega' is absolutely no way
Oops)
Quoted 35 lines
Quoted 34 lines
tests/qemu-iotests/tests/parallels-read-bitmap new file +21
-0+ + +with qemu_nbd_popen('--read-only', f'--socket={nbd_sock}', + f'--bitmap={bitmap}', '-f', iotests.imgfmt, disk): + out = qemu_img_pipe('map', '--output=json', '--image-opts', nbd_opts) + chunks = json.loads(out) + cluster = 64 * 1024 + + log('dirty clusters (cluster size is 64K):') + for c in chunks: + assert c['start'] % cluster == 0 + assert c['length'] % cluster == 0 + if c['data']: + continue + + a = c['start'] // cluster + b = (c['start'] + c['length']) // cluster + if b - a > 1: + log(f'{a}-{b-1}') + else: + log(a)tests/qemu-iotests/tests/parallels-read-bitmap.out new file +6
-0@@ -0,0 +1,6 @@ +Start NBD server +dirty clusters (cluster size is 64K): +5-6 +10-12 +30 +Kill NBD server
Signature · 2 lines
Best regards, Vladimir