bitmaps: introduce 'bitmap' sync mode
v3 cover bitmaps: introduce 'bitmap' sync mode
This series adds a new "BITMAP" sync mode that is meant to replace the existing "INCREMENTAL" sync mode.
This mode can have its behavior modified by issuing any of three bitmap sync modes, passed as arguments to the job.
The three bitmap sync modes are:
- ON-SUCCESS: This is an alias for the old incremental mode. The bitmap is conditionally synchronized based on the return code of the job upon completion.
- NEVER: This is, effectively, the differential backup mode. It never clears the bitmap, as the name suggests.
- ALWAYS: Here is the new, exciting thing. The bitmap is always synchronized, even on failure. On success, this is identical to incremental, but on failure it clears only the bits that were copied successfully. This can be used to "resume" incremental backups from later points in times.
I wrote this series by accident on my way to implement incremental mode for mirror, but this happened first -- the problem is that Mirror mode uses its existing modes in a very particular way; and this was the best way to add bitmap support into the mirror job properly.
Summary:
- 01-03: refactor blockdev-backup and drive-backup to share more interface code
- 04-05: add the new 'bitmap' sync mode with sync policy 'conditional', which is functionally identical to 'incremental' sync mode.
- 06: add sync policy 'never' ("Differential" backups.)
- 07-11: rework some merging code to facilite patch 12;
- 12: add sync policy 'always' ("Resumable" backups)
- 13-16: test infrastructure changes to support patch 16:
- 17: new iotest!
- 18: minor policy loosening as a QOL improvement
Future work:
- Update bitmaps.rst to explain these. (WIP, it's hard, sorry!)
- Add these modes to Mirror. (Done*, but needs tests.)
- Allow the use of bitmaps and bitmap sync modes with non-BITMAP modes; This will allow for resumable/re-tryable full backups.
===
V3:
===
Key:
[----] : patches are identical
[####] : number of functional differences between upstream/downstream patch [down] : patch is downstream-only
The flags [FC] indicate (F)unctional and (C)ontextual differences, respectively
001/18:[0006] [FC] 'qapi/block-core: Introduce BackupCommon'
002/18:[0032] [FC] 'drive-backup: create do_backup_common'
003/18:[0020] [FC] 'blockdev-backup: utilize do_backup_common' 004/18:[0006] [FC] 'qapi: add BitmapSyncMode enum'
005/18:[0019] [FC] 'block/backup: Add mirror sync mode 'bitmap'' 006/18:[0010] [FC] 'block/backup: add 'never' policy to bitmap sync mode' 007/18:[----] [--] 'hbitmap: Fix merge when b is empty, and result is not an alias of a' 008/18:[----] [--] 'hbitmap: enable merging across granularities' 009/18:[0005] [FC] 'block/dirty-bitmap: add bdrv_dirty_bitmap_merge_internal' 010/18:[0003] [FC] 'block/dirty-bitmap: add bdrv_dirty_bitmap_get' 011/18:[0021] [FC] 'block/backup: upgrade copy_bitmap to BdrvDirtyBitmap' 012/18:[0020] [FC] 'block/backup: add 'always' bitmap sync policy' 013/18:[----] [--] 'iotests: add testing shim for script-style python tests' 014/18:[----] [--] 'iotests: teach run_job to cancel pending jobs' 015/18:[0005] [FC] 'iotests: teach FilePath to produce multiple paths' 016/18:[----] [--] 'iotests: Add virtio-scsi device helper'
017/18:[0038] [FC] 'iotests: add test 257 for bitmap-mode backups' 018/18:[----] [-C] 'block/backup: loosen restriction on readonly bitmaps'
001: Made suggested doc fixes.
Changed 'since' to 4.2.
002: Added bds and aio_context to backup_common
Removed accidental extraneous unref on target_bs
Removed local_err propagation
003: Fallout from #002; hoist aio_context acquisition up into do_blockdev_backup 004: 'conditional' --> 'on-success'
005: Rediscover the lost stanza that ensures a bitmap mode was given
Fallout from 2, 3, 4.
006: Block comment fix for patchew
Fallout from #4
009: Fix assert() style issue. Why'd they let a macro be lowercase like that?
Probably to make specifically my life difficult.
010: Fix style issue {
011: Fix long lines
rename "bs" --> "target_bs" where appropriate
Free copy_bitmap from the right node
012: Multiline comment changes for patchew
Fallout from #4
015: Fix long line for patchew
Reinstate that second newline that Max likes
017: Fallout from #4.
===
V2:
===
Changes:
004: Fixed typo
Change @conditional docstring
005: Moved desugaring code into blockdev.c, facilitated by patches 1-3. 006: Change @never docstring slightly.
007: Merge will clear the target bitmap when both components bitmaps are empty,
and the target bitmap is not an alias of either component bitmap.
008: Check orig_size (logical size) instead of size (actual size) to enable
cross-granularity merging.
Fix the sparse merge itself, based on the block/backup code. Clear the target bitmap before cross-granularity merge. Assert the size is itself equal when logical size and granularities are
equal.
---: Dropped bdrv_dirty_bitmap_claim.
012: Rewrote the cleanup logic to hopefully be clearer.
use merge intsead of dropped reclaim.
015: Changed docstring
factored out filename pattern generation.
017: Fix mkpattern indent.
Use integer division!!!
Add parenthesis to boolean assignment
Change test run ordering; update output to reflect this
Use virtio-scsi-ccw when appropriate
Update test output to reflect new test running order
018: Fallout from patches 1-3; restrictions only need loosened in one place
instead of two.
Changes not made:
- Allowing 'cancel' to skip synchronization on cancel:
Decided against it, opting for consistency. The user asked for a sync, and it's simpler logistically to execute on that desire. Use the new mode carefully, please!
John Snow (18):
qapi/block-core: Introduce BackupCommon
drive-backup: create do_backup_common
blockdev-backup: utilize do_backup_common
qapi: add BitmapSyncMode enum
block/backup: Add mirror sync mode 'bitmap' block/backup: add 'never' policy to bitmap sync mode
hbitmap: Fix merge when b is empty, and result is not an alias of a
hbitmap: enable merging across granularities block/dirty-bitmap: add bdrv_dirty_bitmap_merge_internal block/dirty-bitmap: add bdrv_dirty_bitmap_get block/backup: upgrade copy_bitmap to BdrvDirtyBitmap block/backup: add 'always' bitmap sync policy
iotests: add testing shim for script-style python tests
iotests: teach run_job to cancel pending jobs
iotests: teach FilePath to produce multiple paths
iotests: Add virtio-scsi device helper
iotests: add test 257 for bitmap-mode backups block/backup: loosen restriction on readonly bitmaps
block/backup.c | 135 +- block/dirty-bitmap.c | 69 +- block/mirror.c | 8 +- block/replication.c | 2 +- blockdev.c | 208 ++-- include/block/block_int.h | 7 +- include/block/dirty-bitmap.h | 4 +- migration/block.c | 5 +- nbd/server.c | 2 +- qapi/block-core.json | 138 +-- tests/qemu-iotests/040 | 6 +- tests/qemu-iotests/093 | 6 +- tests/qemu-iotests/139 | 7 +- tests/qemu-iotests/238 | 5 +- tests/qemu-iotests/257 | 409 ++++++ tests/qemu-iotests/257.out | 2199 +++++++++++++++++++++++++++++++++ tests/qemu-iotests/group | 1 + tests/qemu-iotests/iotests.py | 100 +- util/hbitmap.c | 49 +- 19 files changed, 3048 insertions(+), 312 deletions(-) create mode 100755 tests/qemu-iotests/257 create mode 100644 tests/qemu-iotests/257.out
Signature · 1 line
2.21.0
PATCH v3 3/18 blockdev-backup: utilize do_backup_common
--- blockdev.c | 65 +++++------------------------------------------------- 1 file changed, 6 insertions(+), 59 deletions(-)
blockdev.c +6 -59
@@ -3624,78 +3624,25 @@ BlockJob *do_blockdev_backup(BlockdevBackup *backup, JobTxn *txn, {BlockDriverState *bs;BlockDriverState *target_bs; -Error *local_err =NULL ; -BdrvDirtyBitmap *bmap =NULL ;AioContext *aio_context; -BlockJob *job =NULL ; -int job_flags =JOB_DEFAULT ; -int ret; - -if (!backup->has_speed) { - backup->speed =0 ; - } -if (!backup->has_on_source_error) { - backup->on_source_error =BLOCKDEV_ON_ERROR_REPORT ; - } -if (!backup->has_on_target_error) { - backup->on_target_error =BLOCKDEV_ON_ERROR_REPORT ; - } -if (!backup->has_job_id) { - backup->job_id =NULL ; - } -if (!backup->has_auto_finalize) { - backup->auto_finalize = true; - } -if (!backup->has_auto_dismiss) { - backup->auto_dismiss = true; - } -if (!backup->has_compress) { - backup->compress = false; - } +BlockJob *job; bs =bdrv_lookup_bs (backup->device, backup->device, errp);if (!bs) {return NULL ; } - aio_context =bdrv_get_aio_context (bs); -aio_context_acquire (aio_context); - target_bs =bdrv_lookup_bs (backup->target, backup->target, errp);if (!target_bs) { - goto out; +return NULL ; } - ret =bdrv_try_set_aio_context (target_bs, aio_context, errp); -if (ret <0 ) { - goto out; - } + aio_context =bdrv_get_aio_context (bs); +aio_context_acquire (aio_context); -if (backup->has_bitmap) { - bmap =bdrv_find_dirty_bitmap (bs, backup->bitmap); -if (!bmap) { -error_setg (errp,"Bitmap '%s' could not be found" , backup->bitmap); - goto out; - } -if (bdrv_dirty_bitmap_check (bmap,BDRV_BITMAP_DEFAULT , errp)) { - goto out; - } - } + job =do_backup_common (qapi_BlockdevBackup_base (backup), + bs, target_bs, aio_context, txn, errp); -if (!backup->auto_finalize) { - job_flags |=JOB_MANUAL_FINALIZE ; - } -if (!backup->auto_dismiss) { - job_flags |=JOB_MANUAL_DISMISS ; - } - job =backup_job_create (backup->job_id, bs, target_bs, backup->speed, - backup->sync, bmap, backup->compress, - backup->on_source_error, backup->on_target_error, - job_flags,NULL ,NULL , txn, &local_err); -if (local_err !=NULL ) { -error_propagate (errp, local_err); - } -out:aio_context_release (aio_context);return job; }
Signature · 1 line
2.21.0
PATCH v3 6/18 block/backup: add 'never' policy to bitmap sync mode
In reply to John SnowThis adds a "never" policy for bitmap synchronization. Regardless of if the job succeeds or fails, we never update the bitmap. This can be used to perform differential backups, or simply to avoid the job modifying a bitmap.
--- block/backup.c | 7 +++++-- qapi/block-core.json | 5 ++++- 2 files changed, 9 insertions(+), 3 deletions(-)
block/backup.c +5 -2
@@ -265,8 +265,11 @@ static void backup_cleanup_sync_bitmap(BackupBlockJob *job, int ret)BdrvDirtyBitmap *bm;BlockDriverState *bs =blk_bs (job->common.blk); -if (ret <0 ) { -/* Merge the successor back into the parent, delete nothing. */ +if (ret <0 || job->bitmap_mode ==BITMAP_SYNC_MODE_NEVER ) { +/* +* Failure, or we don't want to synchronize the bitmap. +* Merge the successor back into the parent, delete nothing. +*/ bm =bdrv_reclaim_dirty_bitmap (bs, job->sync_bitmap,NULL );assert (bm); }else {
qapi/block-core.json +4 -1
@@ -1146,10 +1146,13 @@ # @on-success: The bitmap is only synced when the operation is successful. # This is the behavior always used for 'INCREMENTAL' backups. # +# @never: The bitmap is never synchronized with the operation, and is +# treated solely as a read-only manifest of blocks to copy. +# # Since: 4.2 ## { 'enum': 'BitmapSyncMode', - 'data': ['on-success'] } + 'data': ['on-success', 'never'] } ## # @MirrorCopyMode:
Signature · 1 line
2.21.0
PATCH v3 8/18 hbitmap: enable merging across granularities
In reply to John Snow--- util/hbitmap.c | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-)
util/hbitmap.c +35 -1
@@ -777,7 +777,27 @@ void hbitmap_truncate(HBitmap *hb, uint64_t size)bool hbitmap_can_merge (const HBitmap *a,const HBitmap *b) { -return (a->size == b->size) && (a->granularity == b->granularity); +return (a->orig_size == b->orig_size); +} + +/** +* hbitmap_sparse_merge: performs dst = dst | src +* works with differing granularities. +* best used when src is sparsely populated. +*/ +static void hbitmap_sparse_merge (HBitmap *dst,const HBitmap *src) +{ +uint64_t offset =0 ; +uint64_t count = src->orig_size; + +while (hbitmap_next_dirty_area (src, &offset, &count)) { +hbitmap_set (dst, offset, count); + offset += count; +if (offset >= src->orig_size) { +break ; + } + count = src->orig_size - offset; + } }/**
@@ -808,10 +828,24 @@ bool hbitmap_merge(const HBitmap *a, const HBitmap *b, HBitmap *result)return true; } +if (a->granularity != b->granularity) { +if ((a != result) && (b != result)) { +hbitmap_reset_all (result); + } +if (a != result) { +hbitmap_sparse_merge (result, a); + } +if (b != result) { +hbitmap_sparse_merge (result, b); + } +return true; + } +/* This merge is O(size), as BITS_PER_LONG and HBITMAP_LEVELS are constant. * It may be possible to improve running times for sparsely populated maps * by using hbitmap_iter_next, but this is suboptimal for dense maps. */ +assert (a->size == b->size);for (i =HBITMAP_LEVELS -1 ; i >=0 ; i--) {for (j =0 ; j < a->sizes[i]; j++) { result->levels[i][j] = a->levels[i][j] | b->levels[i][j];
Signature · 1 line
2.21.0
PATCH v3 1/18 qapi/block-core: Introduce BackupCommon
In reply to John Snowdrive-backup and blockdev-backup have an awful lot of things in common that are the same. Let's fix that.
I don't deduplicate 'target', because the semantics actually did change between each structure. Leave that one alone so it can be documented separately.
--- qapi/block-core.json | 103 ++++++++++++++----------------------------- 1 file changed, 33 insertions(+), 70 deletions(-)
qapi/block-core.json +33 -70
@@ -1315,32 +1315,23 @@ 'data': { 'node': 'str', 'overlay': 'str' } } ## -# @DriveBackup: +# @BackupCommon: # # @job-id: identifier for the newly-created block job. If # omitted, the device name will be used. (Since 2.7) # # @device: the device name or node-name of a root node which should be copied. # -# @target: the target of the new image. If the file exists, or if it -# is a device, the existing file/device will be used as the new -# destination. If it does not exist, a new file will be created. -# -# @format: the format of the new destination, default is to -# probe if @mode is 'existing', else the format of the source -# # @sync: what parts of the disk image should be copied to the destination # (all the disk, only the sectors allocated in the topmost image, from a # dirty bitmap, or only new I/O). # -# @mode: whether and how QEMU should create a new image, default is -# 'absolute-paths'. -# -# @speed: the maximum speed, in bytes per second +# @speed: the maximum speed, in bytes per second. The default is 0, +# for unlimited. # # @bitmap: the name of dirty bitmap if sync is "incremental". # Must be present if sync is "incremental", must NOT be present -# otherwise. (Since 2.4) +# otherwise. (Since 2.4 (drive-backup), 3.1 (blockdev-backup)) # # @compress: true to compress data, if the target format supports it. # (default: false) (since 2.8)
@@ -1370,75 +1361,47 @@ # I/O. If an error occurs during a guest write request, the device's # rerror/werror actions will be used. # +# Since: 4.2 +## +{ 'struct': 'BackupCommon', + 'data': { '*job-id': 'str', 'device': 'str', + 'sync': 'MirrorSyncMode', '*speed': 'int', + '*bitmap': 'str', '*compress': 'bool', + '*on-source-error': 'BlockdevOnError', + '*on-target-error': 'BlockdevOnError', + '*auto-finalize': 'bool', '*auto-dismiss': 'bool' } } + +## +# @DriveBackup: +# +# @target: the target of the new image. If the file exists, or if it +# is a device, the existing file/device will be used as the new +# destination. If it does not exist, a new file will be created. +# +# @format: the format of the new destination, default is to +# probe if @mode is 'existing', else the format of the source +# +# @mode: whether and how QEMU should create a new image, default is +# 'absolute-paths'. +# # Since: 1.6 ## { 'struct': 'DriveBackup', - 'data': { '*job-id': 'str', 'device': 'str', 'target': 'str', - '*format': 'str', 'sync': 'MirrorSyncMode', - '*mode': 'NewImageMode', '*speed': 'int', - '*bitmap': 'str', '*compress': 'bool', - '*on-source-error': 'BlockdevOnError', - '*on-target-error': 'BlockdevOnError', - '*auto-finalize': 'bool', '*auto-dismiss': 'bool' } } + 'base': 'BackupCommon', + 'data': { 'target': 'str', + '*format': 'str', + '*mode': 'NewImageMode' } } ## # @BlockdevBackup: # -# @job-id: identifier for the newly-created block job. If -# omitted, the device name will be used. (Since 2.7) -# -# @device: the device name or node-name of a root node which should be copied. -# # @target: the device name or node-name of the backup target node. # -# @sync: what parts of the disk image should be copied to the destination -# (all the disk, only the sectors allocated in the topmost image, or -# only new I/O). -# -# @speed: the maximum speed, in bytes per second. The default is 0, -# for unlimited. -# -# @bitmap: the name of dirty bitmap if sync is "incremental". -# Must be present if sync is "incremental", must NOT be present -# otherwise. (Since 3.1) -# -# @compress: true to compress data, if the target format supports it. -# (default: false) (since 2.8) -# -# @on-source-error: the action to take on an error on the source, -# default 'report'. 'stop' and 'enospc' can only be used -# if the block device supports io-status (see BlockInfo). -# -# @on-target-error: the action to take on an error on the target, -# default 'report' (no limitations, since this applies to -# a different block device than @device). -# -# @auto-finalize: When false, this job will wait in a PENDING state after it has -# finished its work, waiting for @block-job-finalize before -# making any block graph changes. -# When true, this job will automatically -# perform its abort or commit actions. -# Defaults to true. (Since 2.12) -# -# @auto-dismiss: When false, this job will wait in a CONCLUDED state after it -# has completely ceased all work, and awaits @block-job-dismiss. -# When true, this job will automatically disappear from the query -# list without user intervention. -# Defaults to true. (Since 2.12) -# -# Note: @on-source-error and @on-target-error only affect background -# I/O. If an error occurs during a guest write request, the device's -# rerror/werror actions will be used. -# # Since: 2.3 ## { 'struct': 'BlockdevBackup', - 'data': { '*job-id': 'str', 'device': 'str', 'target': 'str', - 'sync': 'MirrorSyncMode', '*speed': 'int', - '*bitmap': 'str', '*compress': 'bool', - '*on-source-error': 'BlockdevOnError', - '*on-target-error': 'BlockdevOnError', - '*auto-finalize': 'bool', '*auto-dismiss': 'bool' } } + 'base': 'BackupCommon', + 'data': { 'target': 'str' } } ## # @blockdev-snapshot-sync:
Signature · 1 line
2.21.0
PATCH v3 4/18 qapi: add BitmapSyncMode enum
In reply to John SnowDepending on what a user is trying to accomplish, there might be a few bitmap cleanup actions that occur when an operation is finished that could be useful.
I am proposing three:
- NEVER: The bitmap is never synchronized against what was copied.
- ALWAYS: The bitmap is always synchronized, even on failures.
- ON-SUCCESS: The bitmap is synchronized only on success.
The existing incremental backup modes use 'on-success' semantics, so add just that one for right now.
--- qapi/block-core.json | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
qapi/block-core.json +14 -0
@@ -1134,6 +1134,20 @@ { 'enum': 'MirrorSyncMode', 'data': ['top', 'full', 'none', 'incremental'] } +## +# @BitmapSyncMode: +# +# An enumeration of possible behaviors for the synchronization of a bitmap +# when used for data copy operations. +# +# @on-success: The bitmap is only synced when the operation is successful. +# This is the behavior always used for 'INCREMENTAL' backups. +# +# Since: 4.2 +## +{ 'enum': 'BitmapSyncMode', + 'data': ['on-success'] } + ## # @MirrorCopyMode: #
Signature · 1 line
2.21.0
PATCH v3 10/18 block/dirty-bitmap: add bdrv_dirty_bitmap_get
In reply to John SnowAdd a public interface for get. While we're at it,
rename "bdrv_get_dirty_bitmap_locked" to "bdrv_dirty_bitmap_get_locked".
(There are more functions to rename to the bdrv_dirty_bitmap_VERB form, but they will wait until the conclusion of this series.)
--- block/dirty-bitmap.c | 19 ++++++++++++------- block/mirror.c | 2 +- include/block/dirty-bitmap.h | 4 ++-- migration/block.c | 5 ++--- nbd/server.c | 2 +- 5 files changed, 18 insertions(+), 14 deletions(-)
block/dirty-bitmap.c +12 -7
@@ -509,14 +509,19 @@ BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs) }/* Called within bdrv_dirty_bitmap_lock..unlock */ -bool bdrv_get_dirty_locked (BlockDriverState *bs,BdrvDirtyBitmap *bitmap, -int64_t offset) +bool bdrv_dirty_bitmap_get_locked (BdrvDirtyBitmap *bitmap,int64_t offset) { -if (bitmap) { -return hbitmap_get (bitmap->bitmap, offset); - }else { -return false; - } +return hbitmap_get (bitmap->bitmap, offset); +} + +bool bdrv_dirty_bitmap_get (BdrvDirtyBitmap *bitmap,int64_t offset) +{ +bool ret; +bdrv_dirty_bitmap_lock (bitmap); + ret =bdrv_dirty_bitmap_get_locked (bitmap, offset); +bdrv_dirty_bitmap_unlock (bitmap); + +return ret; }/**
block/mirror.c +1 -1
@@ -476,7 +476,7 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)int64_t next_offset = offset + nb_chunks * s->granularity;int64_t next_chunk = next_offset / s->granularity;if (next_offset >= s->bdev_length || - !bdrv_get_dirty_locked (source, s->dirty_bitmap, next_offset)) { + !bdrv_dirty_bitmap_get_locked (s->dirty_bitmap, next_offset)) {break ; }if (test_bit (next_chunk, s->in_flight_bitmap)) {
include/block/dirty-bitmap.h +2 -2
@@ -84,12 +84,12 @@ void bdrv_dirty_bitmap_set_busy(BdrvDirtyBitmap *bitmap, bool busy);void bdrv_merge_dirty_bitmap (BdrvDirtyBitmap *dest,const BdrvDirtyBitmap *src,HBitmap **backup,Error **errp);void bdrv_dirty_bitmap_set_migration (BdrvDirtyBitmap *bitmap,bool migration); +bool bdrv_dirty_bitmap_get (BdrvDirtyBitmap *bitmap,int64_t offset);/* Functions that require manual locking. */ void bdrv_dirty_bitmap_lock (BdrvDirtyBitmap *bitmap);void bdrv_dirty_bitmap_unlock (BdrvDirtyBitmap *bitmap); -bool bdrv_get_dirty_locked (BlockDriverState *bs,BdrvDirtyBitmap *bitmap, -int64_t offset); +bool bdrv_dirty_bitmap_get_locked (BdrvDirtyBitmap *bitmap,int64_t offset);void bdrv_set_dirty_bitmap_locked (BdrvDirtyBitmap *bitmap,int64_t offset,int64_t bytes); void bdrv_reset_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
migration/block.c +2 -3
@@ -520,7 +520,6 @@ static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds, int is_async) {BlkMigBlock *blk; -BlockDriverState *bs =blk_bs (bmds->blk);int64_t total_sectors = bmds->total_sectors;int64_t sector;int nr_sectors;
@@ -535,8 +534,8 @@ static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds,blk_mig_unlock (); }bdrv_dirty_bitmap_lock (bmds->dirty_bitmap); -if (bdrv_get_dirty_locked (bs, bmds->dirty_bitmap, - sector *BDRV_SECTOR_SIZE )) { +if (bdrv_dirty_bitmap_get_locked (bmds->dirty_bitmap, + sector *BDRV_SECTOR_SIZE )) {if (total_sectors - sector <BDRV_SECTORS_PER_DIRTY_CHUNK ) { nr_sectors = total_sectors - sector; }else {
nbd/server.c +1 -1
@@ -2003,7 +2003,7 @@ static unsigned int bitmap_to_extents(BdrvDirtyBitmap *bitmap, uint64_t offset,bdrv_dirty_bitmap_lock (bitmap); it =bdrv_dirty_iter_new (bitmap); - dirty =bdrv_get_dirty_locked (NULL , bitmap, offset); + dirty =bdrv_dirty_bitmap_get_locked (bitmap, offset);assert (begin < overall_end && nb_extents);while (begin < overall_end && i < nb_extents) {
Signature · 1 line
2.21.0
PATCH v3 7/18 hbitmap: Fix merge when b is empty, and result is not an alias of a
In reply to John SnowNobody calls the function like this currently, but we neither prohibit or cope with this behavior. I decided to make the function cope with it.
--- util/hbitmap.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
util/hbitmap.c +10 -3
@@ -781,8 +781,9 @@ bool hbitmap_can_merge(const HBitmap *a, const HBitmap *b) }/** -* Given HBitmaps A and B, let A := A (BITOR) B. -* Bitmap B will not be modified. +* Given HBitmaps A and B, let R := A (BITOR) B. +* Bitmaps A and B will not be modified, +* except when bitmap R is an alias of A or B. * * @return true if the merge was successful, * false if it was not attempted.
@@ -797,7 +798,13 @@ bool hbitmap_merge(const HBitmap *a, const HBitmap *b, HBitmap *result) }assert (hbitmap_can_merge (b, result)); -if (hbitmap_count (b) ==0 ) { +if ((!hbitmap_count (a) && result == b) || + (!hbitmap_count (b) && result == a)) { +return true; + } + +if (!hbitmap_count (a) && !hbitmap_count (b)) { +hbitmap_reset_all (result);return true; }
Signature · 1 line
2.21.0
PATCH v3 2/18 drive-backup: create do_backup_common
In reply to John SnowCreate a common core that comprises the actual meat of what the backup API boundary needs to do, and then switch drive-backup to use it.
--- blockdev.c | 122 +++++++++++++++++++++++++++++------------------------ 1 file changed, 67 insertions(+), 55 deletions(-)
blockdev.c +67 -55
@@ -3425,6 +3425,70 @@ out:aio_context_release (aio_context); } +/* Common QMP interface for drive-backup and blockdev-backup */ +static BlockJob *do_backup_common (BackupCommon *backup, +BlockDriverState *bs, +BlockDriverState *target_bs, +AioContext *aio_context, +JobTxn *txn,Error **errp) +{ +BlockJob *job =NULL ; +BdrvDirtyBitmap *bmap =NULL ; +int job_flags =JOB_DEFAULT ; +int ret; + +if (!backup->has_speed) { + backup->speed =0 ; + } +if (!backup->has_on_source_error) { + backup->on_source_error =BLOCKDEV_ON_ERROR_REPORT ; + } +if (!backup->has_on_target_error) { + backup->on_target_error =BLOCKDEV_ON_ERROR_REPORT ; + } +if (!backup->has_job_id) { + backup->job_id =NULL ; + } +if (!backup->has_auto_finalize) { + backup->auto_finalize = true; + } +if (!backup->has_auto_dismiss) { + backup->auto_dismiss = true; + } +if (!backup->has_compress) { + backup->compress = false; + } + + ret =bdrv_try_set_aio_context (target_bs, aio_context, errp); +if (ret <0 ) { +return NULL ; + } + +if (backup->has_bitmap) { + bmap =bdrv_find_dirty_bitmap (bs, backup->bitmap); +if (!bmap) { +error_setg (errp,"Bitmap '%s' could not be found" , backup->bitmap); +return NULL ; + } +if (bdrv_dirty_bitmap_check (bmap,BDRV_BITMAP_DEFAULT , errp)) { +return NULL ; + } + } + +if (!backup->auto_finalize) { + job_flags |=JOB_MANUAL_FINALIZE ; + } +if (!backup->auto_dismiss) { + job_flags |=JOB_MANUAL_DISMISS ; + } + + job =backup_job_create (backup->job_id, bs, target_bs, backup->speed, + backup->sync, bmap, backup->compress, + backup->on_source_error, backup->on_target_error, + job_flags,NULL ,NULL , txn, errp); +return job; +} +static BlockJob *do_drive_backup (DriveBackup *backup,JobTxn *txn,Error **errp) {
@@ -3432,39 +3496,16 @@ static BlockJob *do_drive_backup(DriveBackup *backup, JobTxn *txn,BlockDriverState *target_bs;BlockDriverState *source =NULL ;BlockJob *job =NULL ; -BdrvDirtyBitmap *bmap =NULL ;AioContext *aio_context;QDict *options =NULL ;Error *local_err =NULL ; -int flags, job_flags =JOB_DEFAULT ; +int flags;int64_t size;bool set_backing_hd = false; -int ret; -if (!backup->has_speed) { - backup->speed =0 ; - } -if (!backup->has_on_source_error) { - backup->on_source_error =BLOCKDEV_ON_ERROR_REPORT ; - } -if (!backup->has_on_target_error) { - backup->on_target_error =BLOCKDEV_ON_ERROR_REPORT ; - }if (!backup->has_mode) { backup->mode =NEW_IMAGE_MODE_ABSOLUTE_PATHS ; } -if (!backup->has_job_id) { - backup->job_id =NULL ; - } -if (!backup->has_auto_finalize) { - backup->auto_finalize = true; - } -if (!backup->has_auto_dismiss) { - backup->auto_dismiss = true; - } -if (!backup->has_compress) { - backup->compress = false; - } bs =bdrv_lookup_bs (backup->device, backup->device, errp);if (!bs) {
@@ -3541,12 +3582,6 @@ static BlockJob *do_drive_backup(DriveBackup *backup, JobTxn *txn, goto out; } - ret =bdrv_try_set_aio_context (target_bs, aio_context, errp); -if (ret <0 ) { -bdrv_unref (target_bs); - goto out; - } -if (set_backing_hd) {bdrv_set_backing_hd (target_bs, source, &local_err);if (local_err) {
@@ -3554,31 +3589,8 @@ static BlockJob *do_drive_backup(DriveBackup *backup, JobTxn *txn, } } -if (backup->has_bitmap) { - bmap =bdrv_find_dirty_bitmap (bs, backup->bitmap); -if (!bmap) { -error_setg (errp,"Bitmap '%s' could not be found" , backup->bitmap); - goto unref; - } -if (bdrv_dirty_bitmap_check (bmap,BDRV_BITMAP_DEFAULT , errp)) { - goto unref; - } - } -if (!backup->auto_finalize) { - job_flags |=JOB_MANUAL_FINALIZE ; - } -if (!backup->auto_dismiss) { - job_flags |=JOB_MANUAL_DISMISS ; - } - - job =backup_job_create (backup->job_id, bs, target_bs, backup->speed, - backup->sync, bmap, backup->compress, - backup->on_source_error, backup->on_target_error, - job_flags,NULL ,NULL , txn, &local_err); -if (local_err !=NULL ) { -error_propagate (errp, local_err); - goto unref; - } + job =do_backup_common (qapi_DriveBackup_base (backup), + bs, target_bs, aio_context, txn, errp); unref:bdrv_unref (target_bs);
Signature · 1 line
2.21.0
PATCH v3 13/18 iotests: add testing shim for script-style python tests
In reply to John SnowBecause the new-style python tests don't use the iotests.main() test launcher, we don't turn on the debugger logging for these scripts when invoked via ./check -d.
Refactor the launcher shim into new and old style shims so that they share environmental configuration.
Two cleanup notes: debug was not actually used as a global, and there was no reason to create a class in an inner scope just to achieve default variables; we can simply create an instance of the runner with the values we want instead.
--- tests/qemu-iotests/iotests.py | 40 +++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 14 deletions(-)
tests/qemu-iotests/iotests.py +26 -14
@@ -61,7 +61,6 @@ cachemode = os.environ.get('CACHEMODE') qemu_default_machine = os.environ.get('QEMU_DEFAULT_MACHINE') socket_scm_helper = os.environ.get('SOCKET_SCM_HELPER', 'socket_scm_helper') -debug = False luks_default_secret_object = 'secret,id=keysec0,data=' + \ os.environ.get('IMGKEYSECRET', '')
@@ -834,11 +833,22 @@ def skip_if_unsupported(required_formats=[], read_only=False): return func_wrapper return skip_test_decorator -def main(supported_fmts=[], supported_oses=['linux'], supported_cache_modes=[], - unsupported_fmts=[]): - '''Run tests''' +def execute_unittest(output, verbosity, debug): + runner = unittest.TextTestRunner(stream=output, descriptions=True, + verbosity=verbosity) + try: + # unittest.main() will use sys.exit(); so expect a SystemExit + # exception + unittest.main(testRunner=runner) + finally: + if not debug: + sys.stderr.write(re.sub(r'Ran (\d+) tests? in [\d.]+s', + r'Ran \1 tests', output.getvalue())) - global debug +def execute_test(test_function=None, + supported_fmts=[], supported_oses=['linux'], + supported_cache_modes=[], unsupported_fmts=[]): + """Run either unittest or script-style tests.""" # We are using TEST_DIR and QEMU_DEFAULT_MACHINE as proxies to # indicate that we're not being run via "check". There may be
@@ -870,13 +880,15 @@ def main(supported_fmts=[], supported_oses=['linux'], supported_cache_modes=[], logging.basicConfig(level=(logging.DEBUG if debug else logging.WARN)) - class MyTestRunner(unittest.TextTestRunner): - def __init__(self, stream=output, descriptions=True, verbosity=verbosity): - unittest.TextTestRunner.__init__(self, stream, descriptions, verbosity) + if not test_function: + execute_unittest(output, verbosity, debug) + else: + test_function() - # unittest.main() will use sys.exit() so expect a SystemExit exception - try: - unittest.main(testRunner=MyTestRunner) - finally: - if not debug: - sys.stderr.write(re.sub(r'Ran (\d+) tests? in [\d.]+s', r'Ran \1 tests', output.getvalue())) +def script_main(test_function, *args, **kwargs): + """Run script-style tests outside of the unittest framework""" + execute_test(test_function, *args, **kwargs) + +def main(*args, **kwargs): + """Run tests using the unittest framework""" + execute_test(None, *args, **kwargs)
Signature · 1 line
2.21.0
PATCH v3 12/18 block/backup: add 'always' bitmap sync policy
In reply to John SnowThis adds an "always" policy for bitmap synchronization. Regardless of if the job succeeds or fails, the bitmap is *always* synchronized. This means that for backups that fail part-way through, the bitmap retains a record of which sectors need to be copied out to accomplish a new backup using the old, partial result.
In effect, this allows us to "resume" a failed backup; however the new backup will be from the new point in time, so it isn't a "resume" as much as it is an "incremental retry." This can be useful in the case of extremely large backups that fail considerably through the operation and we'd like to not waste the work that was already performed.
--- block/backup.c | 27 +++++++++++++++++++-------- qapi/block-core.json | 5 ++++- 2 files changed, 23 insertions(+), 9 deletions(-)
block/backup.c +19 -8
@@ -268,18 +268,29 @@ static void backup_cleanup_sync_bitmap(BackupBlockJob *job, int ret) {BdrvDirtyBitmap *bm;BlockDriverState *bs =blk_bs (job->common.blk); +bool sync = (((ret ==0 ) || (job->bitmap_mode ==BITMAP_SYNC_MODE_ALWAYS )) \ + && (job->bitmap_mode !=BITMAP_SYNC_MODE_NEVER )); -if (ret <0 || job->bitmap_mode ==BITMAP_SYNC_MODE_NEVER ) { +if (sync) {/* -* Failure, or we don't want to synchronize the bitmap. -* Merge the successor back into the parent, delete nothing. +* We succeeded, or we always intended to sync the bitmap. +* Delete this bitmap and install the child. */ - bm =bdrv_reclaim_dirty_bitmap (bs, job->sync_bitmap,NULL ); -assert (bm); - }else { -/* Everything is fine, delete this bitmap and install the backup. */ bm =bdrv_dirty_bitmap_abdicate (bs, job->sync_bitmap,NULL ); -assert (bm); + }else { +/* +* We failed, or we never intended to sync the bitmap anyway. +* Merge the successor back into the parent, keeping all data. +*/ + bm =bdrv_reclaim_dirty_bitmap (bs, job->sync_bitmap,NULL ); + } + +assert (bm); + +if (ret <0 && job->bitmap_mode ==BITMAP_SYNC_MODE_ALWAYS ) { +/* If we failed and synced, merge in the bits we didn't copy: */ +bdrv_dirty_bitmap_merge_internal (bm, job->copy_bitmap, +NULL , true); } }
qapi/block-core.json +4 -1
@@ -1149,10 +1149,13 @@ # @never: The bitmap is never synchronized with the operation, and is # treated solely as a read-only manifest of blocks to copy. # +# @always: The bitmap is always synchronized with the operation, +# regardless of whether or not the operation was successful. +# # Since: 4.2 ## { 'enum': 'BitmapSyncMode', - 'data': ['on-success', 'never'] } + 'data': ['on-success', 'never', 'always'] } ## # @MirrorCopyMode:
Signature · 1 line
2.21.0
PATCH v3 11/18 block/backup: upgrade copy_bitmap to BdrvDirtyBitmap
In reply to John SnowThis simplifies some interface matters; namely the initialization and (later) merging the manifest back into the sync_bitmap if it was provided.
--- block/backup.c | 81 ++++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 39 deletions(-)
block/backup.c +42 -39
@@ -38,7 +38,10 @@ typedef struct CowRequest { typedef struct BackupBlockJob { BlockJob common; BlockBackend *target; + BdrvDirtyBitmap *sync_bitmap; + BdrvDirtyBitmap *copy_bitmap; + MirrorSyncMode sync_mode; BitmapSyncMode bitmap_mode; BlockdevOnError on_source_error;
@@ -51,7 +54,6 @@ typedef struct BackupBlockJob {NotifierWithReturn before_write; QLIST_HEAD(, CowRequest) inflight_reqs; -HBitmap *copy_bitmap;bool use_copy_range;int64_t copy_range_size;
@@ -113,7 +115,7 @@ static int coroutine_fn backup_cow_with_bounce_buffer(BackupBlockJob *job,int write_flags = job->serialize_target_writes ?BDRV_REQ_SERIALISING :0 ;assert (QEMU_IS_ALIGNED (start, job->cluster_size)); -hbitmap_reset (job->copy_bitmap, start, job->cluster_size); +bdrv_reset_dirty_bitmap (job->copy_bitmap, start, job->cluster_size); nbytes =MIN (job->cluster_size, job->len - start);if (!*bounce_buffer) { *bounce_buffer =blk_blockalign (blk, job->cluster_size);
@@ -146,7 +148,7 @@ static int coroutine_fn backup_cow_with_bounce_buffer(BackupBlockJob *job,return nbytes; fail: -hbitmap_set (job->copy_bitmap, start, job->cluster_size); +bdrv_set_dirty_bitmap (job->copy_bitmap, start, job->cluster_size);return ret; }
@@ -169,12 +171,14 @@ static int coroutine_fn backup_cow_with_offload(BackupBlockJob *job,assert (QEMU_IS_ALIGNED (start, job->cluster_size)); nbytes =MIN (job->copy_range_size, end - start); nr_clusters =DIV_ROUND_UP (nbytes, job->cluster_size); -hbitmap_reset (job->copy_bitmap, start, job->cluster_size * nr_clusters); +bdrv_reset_dirty_bitmap (job->copy_bitmap, start, + job->cluster_size * nr_clusters); ret =blk_co_copy_range (blk, start, job->target, start, nbytes, read_flags, write_flags);if (ret <0 ) {trace_backup_do_cow_copy_range_fail (job, start, ret); -hbitmap_set (job->copy_bitmap, start, job->cluster_size * nr_clusters); +bdrv_set_dirty_bitmap (job->copy_bitmap, start, + job->cluster_size * nr_clusters);return ret; }
@@ -202,7 +206,7 @@ static int coroutine_fn backup_do_cow(BackupBlockJob *job,cow_request_begin (&cow_request, job, start, end);while (start < end) { -if (!hbitmap_get (job->copy_bitmap, start)) { +if (!bdrv_dirty_bitmap_get (job->copy_bitmap, start)) {trace_backup_do_cow_skip (job, start); start += job->cluster_size;continue ;/* already copied */
@@ -298,14 +302,16 @@ static void backup_abort(Job *job)static void backup_clean (Job *job) {BackupBlockJob *s =container_of (job, BackupBlockJob, common.job); +BlockDriverState *target_bs =blk_bs (s->target); + +if (s->copy_bitmap) { +bdrv_release_dirty_bitmap (target_bs, s->copy_bitmap); + s->copy_bitmap =NULL ; + } +assert (s->target);blk_unref (s->target); s->target =NULL ; - -if (s->copy_bitmap) { -hbitmap_free (s->copy_bitmap); - s->copy_bitmap =NULL ; - } }void backup_do_checkpoint (BlockJob *job,Error **errp)
@@ -320,7 +326,7 @@ void backup_do_checkpoint(BlockJob *job, Error **errp)return ; } -hbitmap_set (backup_job->copy_bitmap,0 , backup_job->len); +bdrv_set_dirty_bitmap (backup_job->copy_bitmap,0 , backup_job->len); }static void backup_drain (BlockJob *job)
@@ -389,59 +395,52 @@ static bool bdrv_is_unallocated_range(BlockDriverState *bs, static int coroutine_fn backup_loop(BackupBlockJob *job) { -int ret;bool error_is_read;int64_t offset; -HBitmapIter hbi; +BdrvDirtyBitmapIter *bdbi;BlockDriverState *bs =blk_bs (job->common.blk); +int ret =0 ; -hbitmap_iter_init (&hbi, job->copy_bitmap,0 ); -while ((offset =hbitmap_iter_next (&hbi)) !=-1 ) { + bdbi =bdrv_dirty_iter_new (job->copy_bitmap); +while ((offset =bdrv_dirty_iter_next (bdbi)) !=-1 ) {if (job->sync_mode ==MIRROR_SYNC_MODE_TOP &&bdrv_is_unallocated_range (bs, offset, job->cluster_size)) { -hbitmap_reset (job->copy_bitmap, offset, job->cluster_size); +bdrv_reset_dirty_bitmap (job->copy_bitmap, offset, + job->cluster_size);continue ; }do {if (yield_and_check (job)) { -return 0 ; + goto out; } ret =backup_do_cow (job, offset, job->cluster_size, &error_is_read, false);if (ret <0 &&backup_error_action (job, error_is_read, -ret) ==BLOCK_ERROR_ACTION_REPORT ) { -return ret; + goto out; } }while (ret <0 ); } -return 0 ; + out: +bdrv_dirty_iter_free (bdbi); +return ret; }/* init copy_bitmap from sync_bitmap */ static void backup_incremental_init_copy_bitmap (BackupBlockJob *job) { -uint64_t offset =0 ; -uint64_t bytes = job->len; - -while (bdrv_dirty_bitmap_next_dirty_area (job->sync_bitmap, - &offset, &bytes)) - { -hbitmap_set (job->copy_bitmap, offset, bytes); - - offset += bytes; -if (offset >= job->len) { -break ; - } - bytes = job->len - offset; - } +bool ret =bdrv_dirty_bitmap_merge_internal (job->copy_bitmap, + job->sync_bitmap, +NULL , true); +assert (ret);/* TODO job_progress_set_remaining() would make more sense */ job_progress_update (&job->common.job, - job->len -hbitmap_count (job->copy_bitmap)); + job->len -bdrv_get_dirty_count (job->copy_bitmap)); } static int coroutine_fn backup_run(Job *job, Error **errp)
@@ -458,7 +457,7 @@ static int coroutine_fn backup_run(Job *job, Error **errp)if (s->sync_mode ==MIRROR_SYNC_MODE_BITMAP ) {backup_incremental_init_copy_bitmap (s); }else { -hbitmap_set (s->copy_bitmap,0 , s->len); +bdrv_set_dirty_bitmap (s->copy_bitmap,0 , s->len); } s->before_write.notify = backup_before_write_notify;
@@ -551,7 +550,7 @@ BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs,BackupBlockJob *job =NULL ;int ret;int64_t cluster_size; -HBitmap *copy_bitmap =NULL ; +BdrvDirtyBitmap *copy_bitmap =NULL ;assert (bs);assert (target);
@@ -621,7 +620,11 @@ BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs, goto error; } - copy_bitmap =hbitmap_alloc (len,ctz32 (cluster_size)); + copy_bitmap =bdrv_create_dirty_bitmap (target, cluster_size,NULL , errp); +if (!copy_bitmap) { + goto error; + } +bdrv_disable_dirty_bitmap (copy_bitmap);/* job->len is fixed, so we can't allow resize */ job = block_job_create(job_id, &backup_job_driver, txn, bs,
@@ -672,7 +675,7 @@ BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs, error:if (copy_bitmap) {assert (!job || !job->copy_bitmap); -hbitmap_free (copy_bitmap); +bdrv_release_dirty_bitmap (target, copy_bitmap); }if (sync_bitmap) {bdrv_reclaim_dirty_bitmap (bs, sync_bitmap,NULL );
Signature · 1 line
2.21.0
PATCH v3 5/18 block/backup: Add mirror sync mode 'bitmap'
In reply to John SnowWe don't need or want a new sync mode for simple differences in semantics. Create a new mode simply named "BITMAP" that is designed to make use of the new Bitmap Sync Mode field.
Because the only bitmap sync mode is 'on-success', this adds no new functionality to the backup job (yet). The old incremental backup mode is maintained as a syntactic sugar for sync=bitmap, mode=on-success.
Add all of the plumbing necessary to support this new instruction.
--- block/backup.c | 20 ++++++++++++-------- block/mirror.c | 6 ++++-- block/replication.c | 2 +- blockdev.c | 25 +++++++++++++++++++++++-- include/block/block_int.h | 4 +++- qapi/block-core.json | 21 +++++++++++++++------ 6 files changed, 58 insertions(+), 20 deletions(-)
block/backup.c +12 -8
@@ -38,9 +38,9 @@ typedef struct CowRequest { typedef struct BackupBlockJob { BlockJob common; BlockBackend *target; - /* bitmap for sync=incremental */ BdrvDirtyBitmap *sync_bitmap; MirrorSyncMode sync_mode; + BitmapSyncMode bitmap_mode; BlockdevOnError on_source_error; BlockdevOnError on_target_error; CoRwlock flush_rwlock;
@@ -452,7 +452,7 @@ static int coroutine_fn backup_run(Job *job, Error **errp)job_progress_set_remaining (job, s->len); -if (s->sync_mode ==MIRROR_SYNC_MODE_INCREMENTAL ) { +if (s->sync_mode ==MIRROR_SYNC_MODE_BITMAP ) {backup_incremental_init_copy_bitmap (s); }else {hbitmap_set (s->copy_bitmap,0 , s->len);
@@ -536,6 +536,7 @@ static int64_t backup_calculate_cluster_size(BlockDriverState *target, BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs, BlockDriverState *target, int64_t speed, MirrorSyncMode sync_mode, BdrvDirtyBitmap *sync_bitmap, + BitmapSyncMode bitmap_mode, bool compress, BlockdevOnError on_source_error, BlockdevOnError on_target_error,
@@ -583,10 +584,13 @@ BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs,return NULL ; } -if (sync_mode ==MIRROR_SYNC_MODE_INCREMENTAL ) { +/* QMP interface should have handled translating this to bitmap mode */ +assert (sync_mode !=MIRROR_SYNC_MODE_INCREMENTAL ); + +if (sync_mode ==MIRROR_SYNC_MODE_BITMAP ) {if (!sync_bitmap) {error_setg (errp,"must provide a valid bitmap name for " -"\"incremental\" sync mode" ); +"'%s' sync mode" ,MirrorSyncMode_str (sync_mode));return NULL ; }
@@ -596,8 +600,8 @@ BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs, } }else if (sync_bitmap) {error_setg (errp, -"a sync_bitmap was provided to backup_run, " -"but received an incompatible sync_mode (%s)" , +"a bitmap was given to backup_job_create, " +"but it received an incompatible sync_mode (%s)" ,MirrorSyncMode_str (sync_mode));return NULL ; }
@@ -639,8 +643,8 @@ BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs, job->on_source_error = on_source_error; job->on_target_error = on_target_error; job->sync_mode = sync_mode; - job->sync_bitmap = sync_mode ==MIRROR_SYNC_MODE_INCREMENTAL ? - sync_bitmap :NULL ; + job->sync_bitmap = sync_bitmap; + job->bitmap_mode = bitmap_mode; job->compress = compress;/* Detect image-fleecing (and similar) schemes */
block/mirror.c +4 -2
@@ -1717,8 +1717,10 @@ void mirror_start(const char *job_id, BlockDriverState *bs,bool is_none_mode;BlockDriverState *base; -if (mode ==MIRROR_SYNC_MODE_INCREMENTAL ) { -error_setg (errp,"Sync mode 'incremental' not supported" ); +if ((mode ==MIRROR_SYNC_MODE_INCREMENTAL ) || + (mode ==MIRROR_SYNC_MODE_BITMAP )) { +error_setg (errp,"Sync mode '%s' not supported" , +MirrorSyncMode_str (mode));return ; } is_none_mode = mode ==MIRROR_SYNC_MODE_NONE ;
block/replication.c +1 -1
@@ -543,7 +543,7 @@ static void replication_start(ReplicationState *rs, ReplicationMode mode, s->backup_job =backup_job_create (NULL , s->secondary_disk->bs, s->hidden_disk->bs, -0 ,MIRROR_SYNC_MODE_NONE ,NULL , false, +0 ,MIRROR_SYNC_MODE_NONE ,NULL ,0 , false,BLOCKDEV_ON_ERROR_REPORT ,BLOCKDEV_ON_ERROR_REPORT ,JOB_INTERNAL , backup_job_completed, bs,NULL , &local_err);
blockdev.c +23 -2
@@ -3464,12 +3464,31 @@ static BlockJob *do_backup_common(BackupCommon *backup,return NULL ; } +if (backup->sync ==MIRROR_SYNC_MODE_INCREMENTAL ) { +if (backup->has_bitmap_mode && + backup->bitmap_mode !=BITMAP_SYNC_MODE_ON_SUCCESS ) { +error_setg (errp,"Bitmap sync mode must be '%s' " +"when using sync mode '%s'" , +BitmapSyncMode_str (BITMAP_SYNC_MODE_ON_SUCCESS ), +MirrorSyncMode_str (backup->sync)); +return NULL ; + } + backup->has_bitmap_mode = true; + backup->sync =MIRROR_SYNC_MODE_BITMAP ; + backup->bitmap_mode =BITMAP_SYNC_MODE_ON_SUCCESS ; + } +if (backup->has_bitmap) { bmap =bdrv_find_dirty_bitmap (bs, backup->bitmap);if (!bmap) {error_setg (errp,"Bitmap '%s' could not be found" , backup->bitmap);return NULL ; } +if (!backup->has_bitmap_mode) { +error_setg (errp,"Bitmap sync mode must be given " +"when providing a bitmap" ); +return NULL ; + }if (bdrv_dirty_bitmap_check (bmap,BDRV_BITMAP_DEFAULT , errp)) {return NULL ; }
@@ -3483,8 +3502,10 @@ static BlockJob *do_backup_common(BackupCommon *backup, } job =backup_job_create (backup->job_id, bs, target_bs, backup->speed, - backup->sync, bmap, backup->compress, - backup->on_source_error, backup->on_target_error, + backup->sync, bmap, backup->bitmap_mode, + backup->compress, + backup->on_source_error, + backup->on_target_error, job_flags,NULL ,NULL , txn, errp);return job; }
include/block/block_int.h +3 -1
@@ -1132,7 +1132,8 @@ void mirror_start(const char *job_id, BlockDriverState *bs,* @target: Block device to write to. * @speed: The maximum speed, in bytes per second, or 0 for unlimited. * @sync_mode: What parts of the disk image should be copied to the destination. -* @sync_bitmap: The dirty bitmap if sync_mode is MIRROR_SYNC_MODE_INCREMENTAL. +* @sync_bitmap: The dirty bitmap if sync_mode is 'bitmap' or 'incremental' +* @bitmap_mode: The bitmap synchronization policy to use. * @on_source_error: The action to take upon error reading from the source. * @on_target_error: The action to take upon error writing to the target. * @creation_flags: Flags that control the behavior of the Job lifetime.
@@ -1148,6 +1149,7 @@ BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs, BlockDriverState *target, int64_t speed, MirrorSyncMode sync_mode, BdrvDirtyBitmap *sync_bitmap, + BitmapSyncMode bitmap_mode, bool compress, BlockdevOnError on_source_error, BlockdevOnError on_target_error,
qapi/block-core.json +15 -6
@@ -1127,12 +1127,15 @@ # # @none: only copy data written from now on # -# @incremental: only copy data described by the dirty bitmap. Since: 2.4 +# @incremental: only copy data described by the dirty bitmap. (since: 2.4) +# +# @bitmap: only copy data described by the dirty bitmap. (since: 4.2) +# Behavior on completion is determined by the BitmapSyncMode. # # Since: 1.3 ## { 'enum': 'MirrorSyncMode', - 'data': ['top', 'full', 'none', 'incremental'] } + 'data': ['top', 'full', 'none', 'incremental', 'bitmap'] } ## # @BitmapSyncMode:
@@ -1343,9 +1346,14 @@ # @speed: the maximum speed, in bytes per second. The default is 0, # for unlimited. # -# @bitmap: the name of dirty bitmap if sync is "incremental". -# Must be present if sync is "incremental", must NOT be present -# otherwise. (Since 2.4 (drive-backup), 3.1 (blockdev-backup)) +# @bitmap: the name of a dirty bitmap if sync is "bitmap" or "incremental". +# Must be present if sync is "bitmap" or "incremental". +# Must not be present otherwise. +# (Since 2.4 (drive-backup), 3.1 (blockdev-backup)) +# +# @bitmap-mode: Specifies the type of data the bitmap should contain after +# the operation concludes. Must be present if sync is "bitmap". +# Must NOT be present otherwise. (Since 4.2) # # @compress: true to compress data, if the target format supports it. # (default: false) (since 2.8)
@@ -1380,7 +1388,8 @@ { 'struct': 'BackupCommon', 'data': { '*job-id': 'str', 'device': 'str', 'sync': 'MirrorSyncMode', '*speed': 'int', - '*bitmap': 'str', '*compress': 'bool', + '*bitmap': 'str', '*bitmap-mode': 'BitmapSyncMode', + '*compress': 'bool', '*on-source-error': 'BlockdevOnError', '*on-target-error': 'BlockdevOnError', '*auto-finalize': 'bool', '*auto-dismiss': 'bool' } }
Signature · 1 line
2.21.0
PATCH v3 15/18 iotests: teach FilePath to produce multiple paths
In reply to John SnowUse "FilePaths" instead of "FilePath" to request multiple files be cleaned up after we leave that object's scope.
This is not crucial; but it saves a little typing.
--- tests/qemu-iotests/iotests.py | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-)
tests/qemu-iotests/iotests.py +24 -10
@@ -358,31 +358,45 @@ class Timeout: def timeout(self, signum, frame): raise Exception(self.errmsg) +def file_pattern(name): + return "{0}-{1}".format(os.getpid(), name) -class FilePath(object): - '''An auto-generated filename that cleans itself up. +class FilePaths(object): + """ + FilePaths is an auto-generated filename that cleans itself up. Use this context manager to generate filenames and ensure that the file gets deleted:: - with TestFilePath('test.img') as img_path: + with FilePaths(['test.img']) as img_path: qemu_img('create', img_path, '1G') # migration_sock_path is automatically deleted - ''' - def __init__(self, name): - filename = '{0}-{1}'.format(os.getpid(), name) - self.path = os.path.join(test_dir, filename) + """ + def __init__(self, names): + self.paths = [] + for name in names: + self.paths.append(os.path.join(test_dir, file_pattern(name))) def __enter__(self): - return self.path + return self.paths def __exit__(self, exc_type, exc_val, exc_tb): try: - os.remove(self.path) + for path in self.paths: + os.remove(path) except OSError: pass return False +class FilePath(FilePaths): + """ + FilePath is a specialization of FilePaths that takes a single filename. + """ + def __init__(self, name): + super(FilePath, self).__init__([name]) + + def __enter__(self): + return self.paths[0] def file_path_remover(): for path in reversed(file_path_remover.paths):
@@ -407,7 +421,7 @@ def file_path(*names): paths = [] for name in names: - filename = '{0}-{1}'.format(os.getpid(), name) + filename = file_pattern(name) path = os.path.join(test_dir, filename) file_path_remover.paths.append(path) paths.append(path)
Signature · 1 line
2.21.0
PATCH v3 14/18 iotests: teach run_job to cancel pending jobs
In reply to John Snowrun_job can cancel pending jobs to simulate failure. This lets us use the pending callback to issue test commands while the job is open, but then still have the job fail in the end.
--- tests/qemu-iotests/iotests.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-)
tests/qemu-iotests/iotests.py +20 -2
@@ -541,7 +541,22 @@ class VM(qtest.QEMUQtestMachine): # Returns None on success, and an error string on failure def run_job(self, job, auto_finalize=True, auto_dismiss=False, - pre_finalize=None, wait=60.0): + pre_finalize=None, cancel=False, wait=60.0): + """ + run_job moves a job from creation through to dismissal. + + :param job: String. ID of recently-launched job + :param auto_finalize: Bool. True if the job was launched with + auto_finalize. Defaults to True. + :param auto_dismiss: Bool. True if the job was launched with + auto_dismiss=True. Defaults to False. + :param pre_finalize: Callback. A callable that takes no arguments to be + invoked prior to issuing job-finalize, if any. + :param cancel: Bool. When true, cancels the job after the pre_finalize + callback. + :param wait: Float. Timeout value specifying how long to wait for any + event, in seconds. Defaults to 60.0. + """ match_device = {'data': {'device': job}} match_id = {'data': {'id': job}} events = [
@@ -568,7 +583,10 @@ class VM(qtest.QEMUQtestMachine): elif status == 'pending' and not auto_finalize: if pre_finalize: pre_finalize() - self.qmp_log('job-finalize', id=job) + if cancel: + self.qmp_log('job-cancel', id=job) + else: + self.qmp_log('job-finalize', id=job) elif status == 'concluded' and not auto_dismiss: self.qmp_log('job-dismiss', id=job) elif status == 'null':
Signature · 1 line
2.21.0
PATCH v3 9/18 block/dirty-bitmap: add bdrv_dirty_bitmap_merge_internal
In reply to John SnowI'm surprised it didn't come up sooner, but sometimes we have a +busy bitmap as a source. This is dangerous from the QMP API, but if we are the owner that marked the bitmap busy, it's safe to merge it using it as a read only source.
It is not safe in the general case to allow users to read from in-use bitmaps, so create an internal variant that foregoes the safety checking.
--- block/dirty-bitmap.c | 50 +++++++++++++++++++++++++++++++++++---- include/block/block_int.h | 3 +++ 2 files changed, 48 insertions(+), 5 deletions(-)
block/dirty-bitmap.c +45 -5
@@ -810,6 +810,12 @@ bool bdrv_dirty_bitmap_next_dirty_area(BdrvDirtyBitmap *bitmap,return hbitmap_next_dirty_area (bitmap->bitmap, offset, bytes); } +/** +* bdrv_merge_dirty_bitmap: merge src into dest. +* Ensures permissions on bitmaps are reasonable; use for public API. +* +* @backup: If provided, make a copy of dest here prior to merge. +*/ void bdrv_merge_dirty_bitmap (BdrvDirtyBitmap *dest,const BdrvDirtyBitmap *src,HBitmap **backup,Error **errp) {
@@ -833,6 +839,38 @@ void bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap *src, goto out; } + ret =bdrv_dirty_bitmap_merge_internal (dest, src, backup, false); +assert (ret); + +out: +qemu_mutex_unlock (dest->mutex); +if (src->mutex != dest->mutex) { +qemu_mutex_unlock (src->mutex); + } +} + +/** +* bdrv_dirty_bitmap_merge_internal: merge src into dest. +* Does NOT check bitmap permissions; not suitable for use as public API. +* +* @backup: If provided, make a copy of dest here prior to merge. +* @lock: If true, lock and unlock bitmaps on the way in/out. +* returns true if the merge succeeded; false if unattempted. +*/ +bool bdrv_dirty_bitmap_merge_internal (BdrvDirtyBitmap *dest, +const BdrvDirtyBitmap *src, +HBitmap **backup, +bool lock) +{ +bool ret; + +if (lock) { +qemu_mutex_lock (dest->mutex); +if (src->mutex != dest->mutex) { +qemu_mutex_lock (src->mutex); + } + } +if (backup) { *backup = dest->bitmap; dest->bitmap =hbitmap_alloc (dest->size,hbitmap_granularity (*backup));
@@ -840,11 +878,13 @@ void bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap *src, }else { ret =hbitmap_merge (dest->bitmap, src->bitmap, dest->bitmap); } -assert (ret); -out: -qemu_mutex_unlock (dest->mutex); -if (src->mutex != dest->mutex) { -qemu_mutex_unlock (src->mutex); +if (lock) { +qemu_mutex_unlock (dest->mutex); +if (src->mutex != dest->mutex) { +qemu_mutex_unlock (src->mutex); + } } + +return ret; }
include/block/block_int.h +3 -0
@@ -1238,6 +1238,9 @@ void bdrv_set_dirty(BlockDriverState *bs, int64_t offset, int64_t bytes);void bdrv_clear_dirty_bitmap (BdrvDirtyBitmap *bitmap,HBitmap **out);void bdrv_restore_dirty_bitmap (BdrvDirtyBitmap *bitmap,HBitmap *backup); +bool bdrv_dirty_bitmap_merge_internal (BdrvDirtyBitmap *dest, +const BdrvDirtyBitmap *src, +HBitmap **backup,bool lock);void bdrv_inc_in_flight (BlockDriverState *bs);void bdrv_dec_in_flight (BlockDriverState *bs);
Signature · 1 line
2.21.0
PATCH v3 16/18 iotests: Add virtio-scsi device helper
In reply to John SnowSeems that it comes up enough.
--- tests/qemu-iotests/040 | 6 +----- tests/qemu-iotests/093 | 6 ++---- tests/qemu-iotests/139 | 7 ++----- tests/qemu-iotests/238 | 5 +---- tests/qemu-iotests/iotests.py | 4 ++++ 5 files changed, 10 insertions(+), 18 deletions(-)
tests/qemu-iotests/040 +1 -5
@@ -85,11 +85,7 @@ class TestSingleDrive(ImageCommitTestCase): qemu_io('-f', 'raw', '-c', 'write -P 0xab 0 524288', backing_img) qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', mid_img) self.vm = iotests.VM().add_drive(test_img, "node-name=top,backing.node-name=mid,backing.backing.node-name=base", interface="none") - if iotests.qemu_default_machine == 's390-ccw-virtio': - self.vm.add_device("virtio-scsi-ccw") - else: - self.vm.add_device("virtio-scsi-pci") - + self.vm.add_device(iotests.get_virtio_scsi_device()) self.vm.add_device("scsi-hd,id=scsi0,drive=drive0") self.vm.launch()
tests/qemu-iotests/093 +2 -4
@@ -366,10 +366,8 @@ class ThrottleTestGroupNames(iotests.QMPTestCase): class ThrottleTestRemovableMedia(iotests.QMPTestCase): def setUp(self): self.vm = iotests.VM() - if iotests.qemu_default_machine == 's390-ccw-virtio': - self.vm.add_device("virtio-scsi-ccw,id=virtio-scsi") - else: - self.vm.add_device("virtio-scsi-pci,id=virtio-scsi") + self.vm.add_device("{},id=virtio-scsi".format( + iotests.get_virtio_scsi_device())) self.vm.launch() def tearDown(self):
tests/qemu-iotests/139 +2 -5
@@ -35,11 +35,8 @@ class TestBlockdevDel(iotests.QMPTestCase): def setUp(self): iotests.qemu_img('create', '-f', iotests.imgfmt, base_img, '1M') self.vm = iotests.VM() - if iotests.qemu_default_machine == 's390-ccw-virtio': - self.vm.add_device("virtio-scsi-ccw,id=virtio-scsi") - else: - self.vm.add_device("virtio-scsi-pci,id=virtio-scsi") - + self.vm.add_device("{},id=virtio-scsi".format( + iotests.get_virtio_scsi_device())) self.vm.launch() def tearDown(self):
tests/qemu-iotests/238 +1 -4
@@ -23,10 +23,7 @@ import os import iotests from iotests import log -if iotests.qemu_default_machine == 's390-ccw-virtio': - virtio_scsi_device = 'virtio-scsi-ccw' -else: - virtio_scsi_device = 'virtio-scsi-pci' +virtio_scsi_device = iotests.get_virtio_scsi_device() vm = iotests.VM() vm.launch()
tests/qemu-iotests/iotests.py +4 -0
@@ -164,6 +164,10 @@ def qemu_io_silent(*args): (-exitcode, ' '.join(args))) return exitcode +def get_virtio_scsi_device(): + if qemu_default_machine == 's390-ccw-virtio': + return 'virtio-scsi-ccw' + return 'virtio-scsi-pci' class QemuIoInteractive: def __init__(self, *args):
Signature · 1 line
2.21.0
PATCH v3 18/18 block/backup: loosen restriction on readonly bitmaps
In reply to John SnowWith the "never" sync policy, we actually can utilize readonly bitmaps now. Loosen the check at the QMP level, and tighten it based on provided arguments down at the job creation level instead.
--- block/backup.c | 6 ++++++ blockdev.c | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-)
block/backup.c +6 -0
@@ -607,6 +607,12 @@ BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs,return NULL ; } +/* If we need to write to this bitmap, check that we can: */ +if (bitmap_mode !=BITMAP_SYNC_MODE_NEVER && +bdrv_dirty_bitmap_check (sync_bitmap,BDRV_BITMAP_DEFAULT , errp)) { +return NULL ; + } +/* Create a new bitmap, and freeze/disable this one. */ if (bdrv_dirty_bitmap_create_successor (bs, sync_bitmap, errp) <0 ) {return NULL ;
blockdev.c +1 -1
@@ -3489,7 +3489,7 @@ static BlockJob *do_backup_common(BackupCommon *backup, "when providing a bitmap");return NULL ; } -if (bdrv_dirty_bitmap_check (bmap,BDRV_BITMAP_DEFAULT , errp)) { +if (bdrv_dirty_bitmap_check (bmap,BDRV_BITMAP_ALLOW_RO , errp)) {return NULL ; } }
Signature · 1 line
2.21.0
PATCH v3 17/18 iotests: add test 257 for bitmap-mode backups
In reply to John Snow--- tests/qemu-iotests/257 | 409 +++++++ tests/qemu-iotests/257.out | 2199 ++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/group | 1 + 3 files changed, 2609 insertions(+) create mode 100755 tests/qemu-iotests/257 create mode 100644 tests/qemu-iotests/257.out
tests/qemu-iotests/257 new file +409 -0
@@ -0,0 +1,409 @@ +#!/usr/bin/env python +# +# Test bitmap-sync backups (incremental, differential, and partials) +# +# Copyright (c) 2019 John Snow for Red Hat, Inc. +# +# 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/>. +# +# [email protected] + +from collections import namedtuple +import math +import os + +import iotests +from iotests import log, qemu_img + +SIZE = 64 * 1024 * 1024 +GRANULARITY = 64 * 1024 + +Pattern = namedtuple('Pattern', ['byte', 'offset', 'size']) +def mkpattern(byte, offset, size=GRANULARITY): + """Constructor for Pattern() with default size""" + return Pattern(byte, offset, size) + +class PatternGroup: + """Grouping of Pattern objects. Initialize with an iterable of Patterns.""" + def __init__(self, patterns): + self.patterns = patterns + + def bits(self, granularity): + """Calculate the unique bits dirtied by this pattern grouping""" + res = set() + for pattern in self.patterns: + lower = math.floor(pattern.offset / granularity) + upper = math.floor((pattern.offset + pattern.size - 1) / granularity) + res = res | set(range(lower, upper + 1)) + return res + +GROUPS = [ + PatternGroup([ + # Batch 0: 4 clusters + mkpattern('0x49', 0x0000000), + mkpattern('0x6c', 0x0100000), # 1M + mkpattern('0x6f', 0x2000000), # 32M + mkpattern('0x76', 0x3ff0000)]), # 64M - 64K + PatternGroup([ + # Batch 1: 6 clusters (3 new) + mkpattern('0x65', 0x0000000), # Full overwrite + mkpattern('0x77', 0x00f8000), # Partial-left (1M-32K) + mkpattern('0x72', 0x2008000), # Partial-right (32M+32K) + mkpattern('0x69', 0x3fe0000)]), # Adjacent-left (64M - 128K) + PatternGroup([ + # Batch 2: 7 clusters (3 new) + mkpattern('0x74', 0x0010000), # Adjacent-right + mkpattern('0x69', 0x00e8000), # Partial-left (1M-96K) + mkpattern('0x6e', 0x2018000), # Partial-right (32M+96K) + mkpattern('0x67', 0x3fe0000, + 2*GRANULARITY)]), # Overwrite [(64M-128K)-64M) + PatternGroup([ + # Batch 3: 8 clusters (5 new) + # Carefully chosen such that nothing re-dirties the one cluster + # that copies out successfully before failure in Group #1. + mkpattern('0xaa', 0x0010000, + 3*GRANULARITY), # Overwrite and 2x Adjacent-right + mkpattern('0xbb', 0x00d8000), # Partial-left (1M-160K) + mkpattern('0xcc', 0x2028000), # Partial-right (32M+160K) + mkpattern('0xdd', 0x3fc0000)]), # New; leaving a gap to the right +] + +class Drive: + """Represents, vaguely, a drive attached to a VM. + Includes format, graph, and device information.""" + + def __init__(self, path, vm=None): + self.path = path + self.vm = vm + self.fmt = None + self.size = None + self.node = None + self.device = None + + @property + def name(self): + return self.node or self.device + + def img_create(self, fmt, size): + self.fmt = fmt + self.size = size + iotests.qemu_img_create('-f', self.fmt, self.path, str(self.size)) + + def create_target(self, name, fmt, size): + basename = os.path.basename(self.path) + file_node_name = "file_{}".format(basename) + vm = self.vm + + log(vm.command('blockdev-create', job_id='bdc-file-job', + options={ + 'driver': 'file', + 'filename': self.path, + 'size': 0, + })) + vm.run_job('bdc-file-job') + log(vm.command('blockdev-add', driver='file', + node_name=file_node_name, filename=self.path)) + + log(vm.command('blockdev-create', job_id='bdc-fmt-job', + options={ + 'driver': fmt, + 'file': file_node_name, + 'size': size, + })) + vm.run_job('bdc-fmt-job') + log(vm.command('blockdev-add', driver=fmt, + node_name=name, + file=file_node_name)) + self.fmt = fmt + self.size = size + self.node = name + +def query_bitmaps(vm): + res = vm.qmp("query-block") + return {"bitmaps": {device['device'] or device['qdev']: + device.get('dirty-bitmaps', []) for + device in res['return']}} + +def get_bitmap(bitmaps, drivename, name): + for bitmap in bitmaps['bitmaps'][drivename]: + if bitmap.get('name', '') == name: + return bitmap + return None + +def reference_backup(drive, n, filepath): + log("--- Reference Backup #{:d} ---\n".format(n)) + target_id = "ref_target_{:d}".format(n) + job_id = "ref_backup_{:d}".format(n) + target_drive = Drive(filepath, vm=drive.vm) + + target_drive.create_target(target_id, drive.fmt, drive.size) + drive.vm.qmp_log("blockdev-backup", + job_id=job_id, device=drive.name, + target=target_id, sync="full") + drive.vm.run_job(job_id, auto_dismiss=True) + log('') + +def bitmap_backup(drive, n, filepath, bitmap, bitmap_mode): + log("--- Bitmap Backup #{:d} ---\n".format(n)) + target_id = "bitmap_target_{:d}".format(n) + job_id = "bitmap_backup_{:d}".format(n) + target_drive = Drive(filepath, vm=drive.vm) + + target_drive.create_target(target_id, drive.fmt, drive.size) + drive.vm.qmp_log("blockdev-backup", job_id=job_id, device=drive.name, + target=target_id, sync="bitmap", + bitmap_mode=bitmap_mode, + bitmap=bitmap, + auto_finalize=False) + return job_id + +def perform_writes(drive, n): + log("--- Write #{:d} ---\n".format(n)) + for pattern in GROUPS[n].patterns: + cmd = "write -P{:s} 0x{:07x} 0x{:x}".format( + pattern.byte, + pattern.offset, + pattern.size) + log(cmd) + log(drive.vm.hmp_qemu_io(drive.name, cmd)) + bitmaps = query_bitmaps(drive.vm) + log(bitmaps, indent=2) + log('') + return bitmaps + +def calculate_bits(groups=None): + """Calculate how many bits we expect to see dirtied.""" + if groups: + bits = set.union(*(GROUPS[group].bits(GRANULARITY) for group in groups)) + return len(bits) + return 0 + +def bitmap_comparison(bitmap, groups=None, want=0): + """ + Print a nice human-readable message checking that this bitmap has as + many bits set as we expect it to. + """ + log("= Checking Bitmap {:s} =".format(bitmap.get('name', '(anonymous)'))) + + if groups: + want = calculate_bits(groups) + have = bitmap['count'] // bitmap['granularity'] + + log("expecting {:d} dirty sectors; have {:d}. {:s}".format( + want, have, "OK!" if want == have else "ERROR!")) + log('') + +def compare_images(image, reference, baseimg=None, expected_match=True): + """ + Print a nice human-readable message comparing these images. + """ + expected_ret = 0 if expected_match else 1 + if baseimg: + assert qemu_img("rebase", "-u", "-b", baseimg, image) == 0 + ret = qemu_img("compare", image, reference) + log('qemu_img compare "{:s}" "{:s}" ==> {:s}, {:s}'.format( + image, reference, + "Identical" if ret == 0 else "Mismatch", + "OK!" if ret == expected_ret else "ERROR!"), + filters=[iotests.filter_testfiles]) + +def test_bitmap_sync(bsync_mode, failure=None): + """ + Test bitmap backup routines. + + :param bsync_mode: Is the Bitmap Sync mode, and can be any of: + - on-success: This is the "incremental" style mode. Bitmaps are + synchronized to what was copied out only on success. + (Partial images must be discarded.) + - never: This is the "differential" style mode. + Bitmaps are never synchronized. + - always: This is a "best effort" style mode. + Bitmaps are always synchronized, regardless of failure. + (Partial images must be kept.) + + :param failure: Is the (optional) failure mode, and can be any of: + - None: No failure. Test the normative path. Default. + - simulated: Cancel the job right before it completes. + This also tests writes "during" the job. + - intermediate: This tests a job that fails mid-process and produces + an incomplete backup. Testing limitations prevent + testing competing writes. + """ + with iotests.FilePaths(['img', 'bsync1', 'bsync2', + 'fbackup0', 'fbackup1', 'fbackup2']) as \ + (img_path, bsync1, bsync2, + fbackup0, fbackup1, fbackup2), \ + iotests.VM() as vm: + + mode = "Bitmap Sync Mode {:s}".format(bsync_mode) + preposition = "with" if failure else "without" + cond = "{:s} {:s}".format(preposition, + "{:s} failure".format(failure) if failure + else "failure") + log("\n=== {:s} {:s} ===\n".format(mode, cond)) + + log('--- Preparing image & VM ---\n') + drive0 = Drive(img_path, vm=vm) + drive0.img_create(iotests.imgfmt, SIZE) + vm.add_device("{},id=scsi0".format(iotests.get_virtio_scsi_device())) + vm.launch() + + file_config = { + 'driver': 'file', + 'filename': drive0.path + } + + if failure == 'intermediate': + file_config = { + 'driver': 'blkdebug', + 'image': file_config, + 'set-state': [{ + 'event': 'flush_to_disk', + 'state': 1, + 'new_state': 2 + }, { + 'event': 'read_aio', + 'state': 2, + 'new_state': 3 + }], + 'inject-error': [{ + 'event': 'read_aio', + 'errno': 5, + 'state': 3, + 'immediately': False, + 'once': True + }] + } + + vm.qmp_log('blockdev-add', + filters=[iotests.filter_qmp_testfiles], + node_name="drive0", + driver=drive0.fmt, + file=file_config) + drive0.node = 'drive0' + drive0.device = 'device0' + # Use share-rw to allow writes directly to the node; + # The anonymous block-backend for this configuration prevents us + # from using HMP's qemu-io commands to address the device. + vm.qmp_log("device_add", id=drive0.device, + drive=drive0.name, driver="scsi-hd", + share_rw=True) + log('') + + # 0 - Writes and Reference Backup + perform_writes(drive0, 0) + reference_backup(drive0, 0, fbackup0) + log('--- Add Bitmap ---\n') + vm.qmp_log("block-dirty-bitmap-add", node=drive0.name, + name="bitmap0", granularity=GRANULARITY) + log('') + + # 1 - Writes and Reference Backup + bitmaps = perform_writes(drive0, 1) + dirty_groups = {1} + bitmap = get_bitmap(bitmaps, drive0.device, 'bitmap0') + bitmap_comparison(bitmap, groups=dirty_groups) + reference_backup(drive0, 1, fbackup1) + + # 1 - Bitmap Backup (Optional induced failure) + if failure == 'intermediate': + # Activate blkdebug induced failure for second-to-next read + log(vm.hmp_qemu_io(drive0.name, 'flush')) + log('') + job = bitmap_backup(drive0, 1, bsync1, "bitmap0", bsync_mode) + + def _callback(): + """Issue writes while the job is open to test bitmap divergence.""" + # Note: when `failure` is 'intermediate', this isn't called. + log('') + bitmaps = perform_writes(drive0, 2) + # Named bitmap (static, should be unchanged) + bitmap_comparison(get_bitmap(bitmaps, drive0.device, 'bitmap0'), + groups=dirty_groups) + # Anonymous bitmap (dynamic, shows new writes) + bitmap_comparison(get_bitmap(bitmaps, drive0.device, ''), + groups={2}) + dirty_groups.add(2) + + vm.run_job(job, auto_dismiss=True, auto_finalize=False, + pre_finalize=_callback, + cancel=(failure == 'simulated')) + bitmaps = query_bitmaps(vm) + bitmap = get_bitmap(bitmaps, drive0.device, 'bitmap0') + log(bitmaps, indent=2) + log('') + + if ((bsync_mode == 'on-success' and not failure) or + (bsync_mode == 'always' and failure != 'intermediate')): + dirty_groups.remove(1) + + if bsync_mode == 'always' and failure == 'intermediate': + # We manage to copy one sector (one bit) before the error. + bitmap_comparison(bitmap, + want=calculate_bits(groups=dirty_groups) - 1) + else: + bitmap_comparison(bitmap, groups=dirty_groups) + + # 2 - Writes and Reference Backup + bitmaps = perform_writes(drive0, 3) + dirty_groups.add(3) + bitmap = get_bitmap(bitmaps, drive0.device, 'bitmap0') + if bsync_mode == 'always' and failure == 'intermediate': + # We're one bit short, still. + bitmap_comparison(bitmap, + want=calculate_bits(groups=dirty_groups) - 1) + else: + bitmap_comparison(bitmap, groups=dirty_groups) + reference_backup(drive0, 2, fbackup2) + + # 2 - Bitmap Backup (In failure modes, this is a recovery.) + job = bitmap_backup(drive0, 2, bsync2, "bitmap0", bsync_mode) + vm.run_job(job, auto_dismiss=True, auto_finalize=False) + bitmaps = query_bitmaps(vm) + bitmap = get_bitmap(bitmaps, drive0.device, 'bitmap0') + log(bitmaps, indent=2) + log('') + bitmap_comparison(bitmap, groups={} + if bsync_mode != 'never' + else dirty_groups) + + log('--- Cleanup ---\n') + vm.qmp_log("block-dirty-bitmap-remove", + node=drive0.name, name="bitmap0") + log(query_bitmaps(vm), indent=2) + vm.shutdown() + log('') + + log('--- Verification ---\n') + # 'simulated' failures will actually all pass here because we canceled + # while "pending". This is actually undefined behavior, + # don't rely on this to be true! + compare_images(bsync1, fbackup1, baseimg=fbackup0, + expected_match=failure != 'intermediate') + if not failure or bsync_mode == 'always': + # Always keep the last backup on success or when using 'always' + base = bsync1 + else: + base = fbackup0 + compare_images(bsync2, fbackup2, baseimg=base) + compare_images(img_path, fbackup2) + log('') + +def main(): + for bsync_mode in ("never", "on-success", "always"): + for failure in ("simulated", "intermediate", None): + test_bitmap_sync(bsync_mode, failure) + +if __name__ == '__main__': + iotests.script_main(main, supported_fmts=['qcow2'])
tests/qemu-iotests/257.out new file +2199 -0
2,199 lines not on this page · raw message
tests/qemu-iotests/group +1 -0
@@ -269,3 +269,4 @@ 254 rw auto backing quick 255 rw auto quick 256 rw auto quick +257 rw auto
Signature · 1 line
2.21.0
Re: [Qemu-devel] [PATCH v3 02/18] drive-backup: create do_backup_common
In reply to John SnowQuoted 7 lines · John Snow
Create a common core that comprises the actual meat of what the backup API boundary needs to do, and then switch drive-backup to use it.
Signed-off-by John Snow <[email protected]>--- blockdev.c | 122 +++++++++++++++++++++++++++++------------------------ 1 file changed, 67 insertions(+), 55 deletions(-)
Re: [Qemu-devel] [PATCH v3 03/18] blockdev-backup: utilize do_backup_common
In reply to John SnowQuoted 4 lines · John Snow
Signed-off-by John Snow <[email protected]>--- blockdev.c | 65 +++++------------------------------------------------- 1 file changed, 6 insertions(+), 59 deletions(-)
Re: [Qemu-devel] [PATCH v3 04/18] qapi: add BitmapSyncMode enum
In reply to John SnowQuoted 9 lines · John Snow
Depending on what a user is trying to accomplish, there might be a few bitmap cleanup actions that occur when an operation is finished that could be useful.
I am proposing three:
- NEVER: The bitmap is never synchronized against what was copied.
- ALWAYS: The bitmap is always synchronized, even on failures.
- ON-SUCCESS: The bitmap is synchronized only on success.
Quoted 7 lines
The existing incremental backup modes use 'on-success' semantics, so add just that one for right now.
Signed-off-by John Snow <[email protected]>--- qapi/block-core.json | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
Re: [Qemu-devel] [PATCH v3 06/18] block/backup: add 'never' policy to bitmap sync mode
In reply to John SnowQuoted 10 lines · John Snow
This adds a "never" policy for bitmap synchronization. Regardless of if the job succeeds or fails, we never update the bitmap. This can be used to perform differential backups, or simply to avoid the job modifying a bitmap.
Signed-off-by John Snow <[email protected]>--- block/backup.c | 7 +++++-- qapi/block-core.json | 5 ++++- 2 files changed, 9 insertions(+), 3 deletions(-)
Re: [Qemu-devel] [PATCH v3 05/18] block/backup: Add mirror sync mode 'bitmap'
In reply to John SnowQuoted 11 lines · John Snow
We don't need or want a new sync mode for simple differences in semantics. Create a new mode simply named "BITMAP" that is designed to make use of the new Bitmap Sync Mode field.
Because the only bitmap sync mode is 'on-success', this adds no new functionality to the backup job (yet). The old incremental backup mode is maintained as a syntactic sugar for sync=bitmap, mode=on-success.
Add all of the plumbing necessary to support this new instruction.
Signed-off-by John Snow <[email protected]>
Quoted 8 lines
--- block/backup.c | 20 ++++++++++++-------- block/mirror.c | 6 ++++-- block/replication.c | 2 +- blockdev.c | 25 +++++++++++++++++++++++-- include/block/block_int.h | 4 +++- qapi/block-core.json | 21 +++++++++++++++------ 6 files changed, 58 insertions(+), 20 deletions(-)
Re: [Qemu-devel] [PATCH v3 12/18] block/backup: add 'always' bitmap sync policy
In reply to John SnowQuoted 12 lines · John Snow
This adds an "always" policy for bitmap synchronization. Regardless of if the job succeeds or fails, the bitmap is *always* synchronized. This means that for backups that fail part-way through, the bitmap retains a record of which sectors need to be copied out to accomplish a new backup using the old, partial result.
In effect, this allows us to "resume" a failed backup; however the new backup will be from the new point in time, so it isn't a "resume" as much as it is an "incremental retry." This can be useful in the case of extremely large backups that fail considerably through the operation and we'd like to not waste the work that was already performed.
Quoted 5 lines
Signed-off-by John Snow <[email protected]>--- block/backup.c | 27 +++++++++++++++++++-------- qapi/block-core.json | 5 ++++- 2 files changed, 23 insertions(+), 9 deletions(-)
Re: [Qemu-devel] [PATCH v3 17/18] iotests: add test 257 for bitmap-mode backups
In reply to John SnowQuoted 8 lines · John Snow
Signed-off-by John Snow <[email protected]>--- tests/qemu-iotests/257 | 409 +++++++ tests/qemu-iotests/257.out | 2199 ++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/group | 1 + 3 files changed, 2609 insertions(+) create mode 100755 tests/qemu-iotests/257 create mode 100644 tests/qemu-iotests/257.out
Re: [Qemu-devel] [PATCH v3 17/18] iotests: add test 257 for bitmap-mode backups
Quoted 10 lines · Max Reitz
Quoted 8 lines · John Snow
Signed-off-by John Snow <[email protected]>--- tests/qemu-iotests/257 | 409 +++++++ tests/qemu-iotests/257.out | 2199 ++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/group | 1 + 3 files changed, 2609 insertions(+) create mode 100755 tests/qemu-iotests/257 create mode 100644 tests/qemu-iotests/257.out
Quoted 2 lines
Reviewed-by Max Reitz <[email protected]>
Thanks for all the reviews so far :)
Despite my grumpiness I do rather like the way this turned out.
--js