Point-in-time snapshot exporting with drive-backup
cover Point-in-time snapshot exporting with drive-backup
This series adds partial support for point-in-time snapshot NBD exporting based on drive-backup. The ideas is described below and patches followed (the missing part is item 3, which work is in progress by Ian Main and will have another patch on it). As the work does not overlap, these series should be quite reviewable by itself.
Background
==========
The goal of image fleecing is to provide a interface to inspect a point-in-time snapshot of guest image data, not being interfered with guest overwrites after it's created. With drive-backup we already have the point-in-time snapshot image (the target image), we only need three modifications to realize this:
1. Give backup target an id, so we can add it to NBD server.
2. Assign source device as backing of target, so reading the unallocated will be passed to source.
As there's copy-on-write mechanism with drive-backup job, all the modified data after snapshot created is copied to target, the unallocated data is guaranteed to be unchanged, so reading from the source is correct. Note that this requires target format supports backing file.
3. Adding sync mode 'none' to drive-backup, so the block job only copy changed data from source, which has minimal IO overhead.
Usage
=====
With above three, we can simply export a point-in-time snapshot with two QMP commands:
drive-backup device=virtio0 format=qcow2 target=point-in-time.qcow2 target-id=pit0 sync=none
nbd-server-add device=pit0 writable=no
Lifecycle
=========
The block job originally had total control over target bs: when the job completes, the target is deleted. Now it's shared with NBD, so we should make sure the job completion or canceling wouldn't crash NBD server. This patch doesn't handle this case: if the block job is ended before NBD exporting, there'll be problem.
To avoid this, we either add more delicate condition checks as we do with bs->in_use, or introduce reference count to BlockDriverState, which seems much better in long term but needs more work. I have done some work on it, but it's not ready yet. As we don't depend on it that heavily, let's do the things one by one.
Any comments?
Fam Zheng (3):
block: add target-id option to drive-backup QMP command
block: assign backing relationship in drive-backup
nbd: don't get ref if bs has no drive
blockdev-nbd.c | 6 +++++- blockdev.c | 8 +++++++- qapi-schema.json | 7 +++++-- qmp-commands.hx | 3 ++- 4 files changed, 19 insertions(+), 5 deletions(-)
Signature · 1 line
1.8.3.1
RFC PATCH 1/3 block: add target-id option to drive-backup QMP command
Add target-id (optional) to drive-backup command, to make the target bs a named drive so that we can operate on it (e.g. export with NBD).
--- blockdev.c | 4 +++- qapi-schema.json | 7 +++++-- qmp-commands.hx | 3 ++- 3 files changed, 10 insertions(+), 4 deletions(-)
blockdev.c +3 -1
@@ -935,6 +935,7 @@ static void drive_backup_prepare(BlkTransactionState *common, Error **errp) backup = common->action->drive_backup; qmp_drive_backup(backup->device, backup->target, + backup->has_target_id, backup->target_id, backup->has_format, backup->format, backup->has_mode, backup->mode, backup->has_speed, backup->speed,
@@ -1420,6 +1421,7 @@ void qmp_block_commit(const char *device, } void qmp_drive_backup(const char *device, const char *target, + bool has_target_id, const char *target_id, bool has_format, const char *format, bool has_mode, enum NewImageMode mode, bool has_speed, int64_t speed,
@@ -1494,7 +1496,7 @@ void qmp_drive_backup(const char *device, const char *target,return ; } - target_bs =bdrv_new ("" ); + target_bs =bdrv_new (has_target_id ? target_id :"" ); ret =bdrv_open (target_bs, target,NULL , flags, drv);if (ret <0 ) {bdrv_delete (target_bs);
qapi-schema.json +5 -2
@@ -1646,7 +1646,8 @@ # Since: 1.6 ## { 'type': 'DriveBackup', - 'data': { 'device': 'str', 'target': 'str', '*format': 'str', + 'data': { 'device': 'str', 'target': 'str', + '*target-id': 'str', '*format': 'str', '*mode': 'NewImageMode', '*speed': 'int', '*on-source-error': 'BlockdevOnError', '*on-target-error': 'BlockdevOnError' } }
@@ -1799,6 +1800,7 @@ # 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. # +# @target-id: #optional the drive id of the target. # @format: #optional the format of the new destination, default is to # probe if @mode is 'existing', else the format of the source #
@@ -1825,7 +1827,8 @@ # Since 1.6 ## { 'command': 'drive-backup', - 'data': { 'device': 'str', 'target': 'str', '*format': 'str', + 'data': { 'device': 'str', 'target': 'str', + '*target-id': 'str', '*format': 'str', '*mode': 'NewImageMode', '*speed': 'int', '*on-source-error': 'BlockdevOnError', '*on-target-error': 'BlockdevOnError' } }
qmp-commands.hx +2 -1
@@ -913,7 +913,7 @@ EQMP { .name = "drive-backup", - .args_type = "device:B,target:s,speed:i?,mode:s?,format:s?," + .args_type = "device:B,target:s,target-id:s?,speed:i?,mode:s?,format:s?," "on-source-error:s?,on-target-error:s?", .mhandler.cmd_new = qmp_marshal_input_drive_backup, },
@@ -936,6 +936,7 @@ Arguments: device, the existing file/device will be used as the new destination. If it does not exist, a new file will be created. (json-string) +- "target_id": the drive id of the target image. - "format": the format of the new destination, default is to probe if 'mode' is 'existing', else the format of the source (json-string, optional)
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [RFC PATCH 1/3] block: add target-id option to drive-backup QMP command
Quoted 31 lines · Fam Zheng
Add target-id (optional) to drive-backup command, to make the target bs a named drive so that we can operate on it (e.g. export with NBD).
Signed-off-by Fam Zheng <[email protected]>--- blockdev.c | 4 +++- qapi-schema.json | 7 +++++-- qmp-commands.hx | 3 ++- 3 files changed, 10 insertions(+), 4 deletions(-)blockdev.c +2
-0@@ -935,6 +935,7 @@ static void drive_backup_prepare(BlkTransactionState *common, Error **errp) backup = common->action->drive_backup; qmp_drive_backup(backup->device, backup->target, + backup->has_target_id, backup->target_id, backup->has_format, backup->format, backup->has_mode, backup->mode, backup->has_speed, backup->speed,@@ -1420,6 +1421,7 @@ void qmp_block_commit(const char *device, } void qmp_drive_backup(const char *device, const char *target, + bool has_target_id, const char *target_id, bool has_format, const char *format, bool has_mode, enum NewImageMode mode, bool has_speed, int64_t speed,
blockdev.c void qmp_drive_backup(const char *device, const char *target, · line 1499
Quoted 5 lines
blockdev.c +1
-1@@ -1494,7 +1496,7 @@ void qmp_drive_backup(const char *device, const char *target,return ; } - target_bs =bdrv_new ("" ); + target_bs =bdrv_new (has_target_id ? target_id :"" );
This raises a new issue:
Now that the target can be named, what happens when the user issues a monitor command, e.g. drive-del, block-resize, or drive-backup :)?
We have a clumsy form of protection with bdrv_set_in_use(). It makes several monitor commands refuse with -EBUSY.
Perhaps we should have a command permission set so it's possible to allow/deny specific commands.
Stefan
Re: [Qemu-devel] [RFC PATCH 1/3] block: add target-id option to drive-backup QMP command
Quoted 40 lines · Stefan Hajnoczi
Quoted 31 lines · Fam Zheng
Add target-id (optional) to drive-backup command, to make the target bs a named drive so that we can operate on it (e.g. export with NBD).
Signed-off-by Fam Zheng <[email protected]>--- blockdev.c | 4 +++- qapi-schema.json | 7 +++++-- qmp-commands.hx | 3 ++- 3 files changed, 10 insertions(+), 4 deletions(-)blockdev.c +2
-0@@ -935,6 +935,7 @@ static void drive_backup_prepare(BlkTransactionState *common, Error **errp) backup = common->action->drive_backup; qmp_drive_backup(backup->device, backup->target, + backup->has_target_id, backup->target_id, backup->has_format, backup->format, backup->has_mode, backup->mode, backup->has_speed, backup->speed,@@ -1420,6 +1421,7 @@ void qmp_block_commit(const char *device, } void qmp_drive_backup(const char *device, const char *target, + bool has_target_id, const char *target_id, bool has_format, const char *format, bool has_mode, enum NewImageMode mode, bool has_speed, int64_t speed,blockdev.c void qmp_drive_backup(const char *device, const char *target, · line 1499
Quoted 5 lines
blockdev.c +1
-1@@ -1494,7 +1496,7 @@ void qmp_drive_backup(const char *device, const char *target,return ; } - target_bs =bdrv_new ("" ); + target_bs =bdrv_new (has_target_id ? target_id :"" );This raises a new issue:
Quoted 9 lines
Now that the target can be named, what happens when the user issues a monitor command, e.g. drive-del, block-resize, or drive-backup :)?
We have a clumsy form of protection with
bdrv_set_in_use(). It makes several monitor commands refuse with-EBUSY.Perhaps we should have a command permission set so it's possible to allow/deny specific commands.
Yes, this makes me realize that ref count it not a solution to retire bs->in_use, because we can't tell if drive-del or block-resize is safe with only reference number. But I can't think of two situations to deny different subsets of commands, shouldn't a general blocker, like in_use does, be good enough?
Signature · 1 line
Fam
Re: [Qemu-devel] [RFC PATCH 1/3] block: add target-id option to drive-backup QMP command
Il 27/06/2013 11:41, Fam Zheng ha scritto:
Quoted 51 lines · Stefan Hajnoczi
Quoted 40 lines · Fam Zheng
Quoted 31 lines
Add target-id (optional) to drive-backup command, to make the target bs a named drive so that we can operate on it (e.g. export with NBD).
Signed-off-by Fam Zheng <[email protected]>--- blockdev.c | 4 +++- qapi-schema.json | 7 +++++-- qmp-commands.hx | 3 ++- 3 files changed, 10 insertions(+), 4 deletions(-)blockdev.c +2
-0@@ -935,6 +935,7 @@ static void drive_backup_prepare(BlkTransactionState *common, Error **errp) backup = common->action->drive_backup; qmp_drive_backup(backup->device, backup->target, + backup->has_target_id, backup->target_id, backup->has_format, backup->format, backup->has_mode, backup->mode, backup->has_speed, backup->speed,@@ -1420,6 +1421,7 @@ void qmp_block_commit(const char *device, } void qmp_drive_backup(const char *device, const char *target, + bool has_target_id, const char *target_id, bool has_format, const char *format, bool has_mode, enum NewImageMode mode, bool has_speed, int64_t speed,blockdev.c void qmp_drive_backup(const char *device, const char *target, · line 1499
Quoted 5 lines
blockdev.c +1
-1@@ -1494,7 +1496,7 @@ void qmp_drive_backup(const char *device, const char *target,return ; } - target_bs =bdrv_new ("" ); + target_bs =bdrv_new (has_target_id ? target_id :"" );This raises a new issue:
Quoted 9 lines
Now that the target can be named, what happens when the user issues a monitor command, e.g. drive-del, block-resize, or drive-backup :)?
We have a clumsy form of protection with
bdrv_set_in_use(). It makes several monitor commands refuse with-EBUSY.Perhaps we should have a command permission set so it's possible to allow/deny specific commands.
Quoted 5 lines
Yes, this makes me realize that ref count it not a solution to retire
bs->in_use, because we can't tell if drive-del or block-resize is safe with only reference number. But I can't think of two situations to deny different subsets of commands, shouldn't a general blocker, likein_usedoes, be good enough?
For example, right now nbd-server-add does not check bdrv_in_use. But shrinking a device that is exposed via NBD could be surprising to the NBD clients.
Paolo
Re: [Qemu-devel] [RFC PATCH 1/3] block: add target-id option to drive-backup QMP command
Quoted 58 lines · Paolo Bonzini
Il 27/06/2013 11:41, Fam Zheng ha scritto:
Quoted 51 lines · Stefan Hajnoczi
Quoted 40 lines · Fam Zheng
Quoted 31 lines
Add target-id (optional) to drive-backup command, to make the target bs a named drive so that we can operate on it (e.g. export with NBD).
Signed-off-by Fam Zheng <[email protected]>--- blockdev.c | 4 +++- qapi-schema.json | 7 +++++-- qmp-commands.hx | 3 ++- 3 files changed, 10 insertions(+), 4 deletions(-)blockdev.c +2
-0@@ -935,6 +935,7 @@ static void drive_backup_prepare(BlkTransactionState *common, Error **errp) backup = common->action->drive_backup; qmp_drive_backup(backup->device, backup->target, + backup->has_target_id, backup->target_id, backup->has_format, backup->format, backup->has_mode, backup->mode, backup->has_speed, backup->speed,@@ -1420,6 +1421,7 @@ void qmp_block_commit(const char *device, } void qmp_drive_backup(const char *device, const char *target, + bool has_target_id, const char *target_id, bool has_format, const char *format, bool has_mode, enum NewImageMode mode, bool has_speed, int64_t speed,blockdev.c void qmp_drive_backup(const char *device, const char *target, · line 1499
Quoted 5 lines
blockdev.c +1
-1@@ -1494,7 +1496,7 @@ void qmp_drive_backup(const char *device, const char *target,return ; } - target_bs =bdrv_new ("" ); + target_bs =bdrv_new (has_target_id ? target_id :"" );This raises a new issue:
Quoted 9 lines
Now that the target can be named, what happens when the user issues a monitor command, e.g. drive-del, block-resize, or drive-backup :)?
We have a clumsy form of protection with
bdrv_set_in_use(). It makes several monitor commands refuse with-EBUSY.Perhaps we should have a command permission set so it's possible to allow/deny specific commands.
Quoted 5 lines
Yes, this makes me realize that ref count it not a solution to retire
bs->in_use, because we can't tell if drive-del or block-resize is safe with only reference number. But I can't think of two situations to deny different subsets of commands, shouldn't a general blocker, likein_usedoes, be good enough?
Quoted 4 lines
For example, right now nbd-server-add does not check
bdrv_in_use. But shrinking a device that is exposed via NBD could be surprising to the NBD clients.
So it seems to me that both block job and nbd server have the same
restriction on device: don't resize, and notify on close. So my question
is if we implement bdrv_add_command_blocker(), do the callers still need to distinguish what actions to block, or it's generally to block all the actions those change the device parameter?
Signature · 1 line
Fam
Re: [Qemu-devel] [RFC PATCH 1/3] block: add target-id option to drive-backup QMP command
Il 27/06/2013 13:37, Fam Zheng ha scritto:
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines
Yes, this makes me realize that ref count it not a solution to retire
bs->in_use, because we can't tell if drive-del or block-resize is safe with only reference number. But I can't think of two situations to deny different subsets of commands, shouldn't a general blocker, likein_usedoes, be good enough?
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
For example, right now nbd-server-add does not check
bdrv_in_use. But shrinking a device that is exposed via NBD could be surprising to the NBD clients.So it seems to me that both block job and nbd server have the same
restriction on device: don't resize, and notify on close. So my question
is if we implementbdrv_add_command_blocker(), do the callers still need to distinguish what actions to block, or it's generally to block all the actions those change the device parameter?
It would be a good start to have a list of things that are setting and checking bdrv_in_use. Then we can make a matrix.
Paolo
Re: [Qemu-devel] [RFC PATCH 1/3] block: add target-id option to drive-backup QMP command
Quoted 12 lines · Paolo Bonzini
Il 27/06/2013 13:37, Fam Zheng ha scritto:
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 6 lines
Quoted 6 lines
Yes, this makes me realize that ref count it not a solution to retire
bs->in_use, because we can't tell if drive-del or block-resize is safe with only reference number. But I can't think of two situations to deny different subsets of commands, shouldn't a general blocker, likein_usedoes, be good enough?For example, right now nbd-server-add does not check
bdrv_in_use. But shrinking a device that is exposed via NBD could be surprising to the NBD clients.
Quoted 7 lines
Quoted 3 lines
So it seems to me that both block job and nbd server have the same
restriction on device: don't resize, and notify on close. So my question
is if we implementbdrv_add_command_blocker(), do the callers still need to distinguish what actions to block, or it's generally to block all the actions those change the device parameter?It would be a good start to have a list of things that are setting and checking
bdrv_in_use. Then we can make a matrix.
Grapping the code and get:
Commands fail with -EBUSY if bdrv_in_use():
bdrv_commit()
bdrv_truncate()
external_snapshot_prepare()
eject_device()
drive_del()
drive_mirror()
block_job_create()Commands to set bdrv in_use to 1:
init_blk_migration()
block_job_create()
virtio_blk_data_plane_create()
Signature · 1 line
Fam
RFC PATCH 2/3 block: assign backing relationship in drive-backup
In reply to Fam ZhengAssign source image as the backing hd of target bs, so reading target bs gets the point-in-time copy of data from source image.
--- blockdev.c | 4 ++++ 1 file changed, 4 insertions(+)
blockdev.c +4 -0
@@ -1504,6 +1504,10 @@ void qmp_drive_backup(const char *device, const char *target,return ; } + target_bs->backing_hd = bs; +pstrcpy (target_bs->backing_file,sizeof (target_bs->backing_file), + bs->filename); +backup_start (bs, target_bs, speed, on_source_error, on_target_error, block_job_cb, bs, &local_err);if (local_err !=NULL ) {
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [RFC PATCH 2/3] block: assign backing relationship in drive-backup
Il 26/06/2013 05:59, Fam Zheng ha scritto:
Quoted 13 lines
Assign source image as the backing hd of target bs, so reading target bs gets the point-in-time copy of data from source image.
Signed-off-by Fam Zheng <[email protected]>--- blockdev.c | 4 ++++ 1 file changed, 4 insertions(+)blockdev.c +0
-0
blockdev.c void qmp_drive_backup(const char *device, const char *target, · line 1509
Quoted 6 lines
blockdev.c +3
-0@@ -1504,6 +1504,10 @@ void qmp_drive_backup(const char *device, const char *target,return ; } + target_bs->backing_hd = bs; +pstrcpy (target_bs->backing_file,sizeof (target_bs->backing_file), + bs->filename);
backing_format missing.
Paolo
Quoted 4 lines
blockdev.c +0
-0backup_start (bs, target_bs, speed, on_source_error, on_target_error, block_job_cb, bs, &local_err);if (local_err !=NULL ) {
RFC PATCH 3/3 nbd: don't get ref if bs has no drive
In reply to Fam Zheng--- blockdev-nbd.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
blockdev-nbd.c +5 -1
@@ -81,6 +81,7 @@ void qmp_nbd_server_add(const char *device, bool has_writable, bool writable,BlockDriverState *bs;NBDExport *exp;NBDCloseNotifier *n; +DriveInfo *dinfo;if (server_fd ==-1 ) {error_setg (errp,"NBD server not running" );
@@ -109,7 +110,10 @@ void qmp_nbd_server_add(const char *device, bool has_writable, bool writable, nbd_server_put_ref);nbd_export_set_name (exp, device); -drive_get_ref (drive_get_by_blockdev (bs)); + dinfo =drive_get_by_blockdev (bs); +if (dinfo) { +drive_get_ref (dinfo); + } n =g_malloc0 (sizeof (NBDCloseNotifier)); n->n.notify = nbd_close_notifier;
Signature · 1 line
1.8.3.1
Il 26/06/2013 05:59, Fam Zheng ha scritto:
Quoted 6 lines
The block job originally had total control over target bs: when the job completes, the target is deleted. Now it's shared with NBD, so we should make sure the job completion or canceling wouldn't crash NBD server. This patch doesn't handle this case: if the block job is ended before NBD exporting, there'll be problem.
This should not be a problem. When the job completes or cancels it will close s->target. The NBD server has installed a close notifier for s->target, and will close connections with the clients.
Similarly, if the source disk is hot-unplugged, the bdrv_close will cancel the job, which in turn closes connections with the clients.
So this just works as long as no hot-unplug happens and as long as the job doesn't complete: the client can use the point-in-time snapshot via NBD as long as it cares to.
This leads to another observation: a sync:'none' block-backup job probably should never complete, and should instead go on until explicit cancellation. This is because the job does no background writes, and thus completion would only happen after the guest has written the whole disk. Writing the whole disk is rare enough that it will likely cause bugs in the clients. It is easier just to never complete the job.
Paolo
Quoted 12 lines · Paolo Bonzini
Il 26/06/2013 05:59, Fam Zheng ha scritto:
Quoted 6 lines
The block job originally had total control over target bs: when the job completes, the target is deleted. Now it's shared with NBD, so we should make sure the job completion or canceling wouldn't crash NBD server. This patch doesn't handle this case: if the block job is ended before NBD exporting, there'll be problem.
This should not be a problem. When the job completes or cancels it will close
s->target. The NBD server has installed a close notifier fors->target, and will close connections with the clients.
Quoted 6 lines
Similarly, if the source disk is hot-unplugged, the
bdrv_closewill cancel the job, which in turn closes connections with the clients.So this just works as long as no hot-unplug happens and as long as the job doesn't complete: the client can use the point-in-time snapshot via NBD as long as it cares to.
OK, thanks for pointing this out.
Quoted 8 lines
This leads to another observation: a sync:'none' block-backup job probably should never complete, and should instead go on until explicit cancellation. This is because the job does no background writes, and thus completion would only happen after the guest has written the whole disk. Writing the whole disk is rare enough that it will likely cause bugs in the clients. It is easier just to never complete the job.
Yes, the sync mode none will simply run forever until cancelled.
Signature · 1 line
Fam
Quoted 11 lines · Fam Zheng
Quoted 8 lines · Paolo Bonzini
Il 26/06/2013 05:59, Fam Zheng ha scritto:
This leads to another observation: a sync:'none' block-backup job probably should never complete, and should instead go on until explicit cancellation. This is because the job does no background writes, and thus completion would only happen after the guest has written the whole disk. Writing the whole disk is rare enough that it will likely cause bugs in the clients. It is easier just to never complete the job.Yes, the sync mode none will simply run forever until cancelled.
There is a dedicated command to successfully complete a job:
block-job-complete
Stefan
Il 27/06/2013 10:17, Stefan Hajnoczi ha scritto:
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
Il 26/06/2013 05:59, Fam Zheng ha scritto:
This leads to another observation: a sync:'none' block-backup job probably should never complete, and should instead go on until explicit cancellation. This is because the job does no background writes, and thus completion would only happen after the guest has written the whole disk. Writing the whole disk is rare enough that it will likely cause bugs in the clients. It is easier just to never complete the job.
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Yes, the sync mode none will simply run forever until cancelled.
There is a dedicated command to successfully complete a job:
block-job-complete
block-job-complete should only be called after a BLOCK_JOB_READY event, but when would the backup job raise it? Immediately after starting?
There is no behavioral difference in this case between cancel and complete in fact as far as I understand, so I think cancelling the job would match existing practice better.
Paolo
Quoted 13 lines · Paolo Bonzini
Il 27/06/2013 10:17, Stefan Hajnoczi ha scritto:
Quoted 12 lines
Quoted 10 lines
Quoted 10 lines
Quoted 8 lines
Quoted 8 lines
Il 26/06/2013 05:59, Fam Zheng ha scritto:
This leads to another observation: a sync:'none' block-backup job probably should never complete, and should instead go on until explicit cancellation. This is because the job does no background writes, and thus completion would only happen after the guest has written the whole disk. Writing the whole disk is rare enough that it will likely cause bugs in the clients. It is easier just to never complete the job.Yes, the sync mode none will simply run forever until cancelled.
There is a dedicated command to successfully complete a job:
Quoted 8 lines
Quoted 1 line
block-job-complete
block-job-complete should only be called after a
BLOCK_JOB_READYevent, but when would the backup job raise it? Immediately after starting?There is no behavioral difference in this case between cancel and complete in fact as far as I understand, so I think cancelling the job would match existing practice better.
Okay. Ian Main also preferred using block-job-cancel without the BLOCK_JOB_READY/block-job-complete lifecycle.
Thanks for the input!
Stefan