Skip to contentSkip to conversation

Drop in_use from BlockDriverState and enable point-in-time snapshot exporting over NBD

PATCH Superseded Fam Zheng 5 participants 19 messages Jan 27, 2014 – Jan 29, 2014 Block layer

v12 of 20 Superseded Latest is v20 →

lore
Share & tools
lore

Fam Zheng

author
#0 lore raw

v12 cover Drop in_use from BlockDriverState and enable point-in-time snapshot exporting over NBD

This series adds for point-in-time snapshot NBD exporting based on blockdev-backup (variant of drive-backup with existing device as target).

We get a thin point-in-time snapshot by COW mechanism of drive-backup, and export it through built in NBD server. The steps are as below:

1. (SHELL) qemu-img create -f qcow2 BACKUP.qcow2 <source size here>

(Alternatively we can use -o backing_file=RUNNING-VM.img to omit explicitly providing the size by ourselves, but it's risky because RUNNING-VM.qcow2 is used r/w by guest. Whether or not setting backing file in the image file doesn't matter, as we are going to override the backing hd in the next step)

2. (QMP) blockdev-add backing=source-drive file.driver=file file.filename=BACKUP.qcow2 id=target0 if=none driver=qcow2

(where source-drive is the running BlockDriverState name for RUNNING-VM.img. This patch implements "backing=" option to override backing_hd for added drive)

3. (QMP) blockdev-backup device=source-drive sync=none target=target0

(this is the QMP command introduced by this series, which use a named device as target of drive-backup)

4. (QMP) nbd-server-add device=target0

When image fleecing done:

1. (QMP) block-job-cancel device=source-drive

2. (HMP) drive_del target0

3. (SHELL) rm BACKUP.qcow2

v11 -> v12: Rebase to qemu.git.
Call bdrv_refresh_limits() in bdrv_set_backing_hd().

v8 -> v11: Rebased to qemu.git. Address Stefan's comments:
(sorry for sending 3 revisions in a day)

[01/11] block: Add BlockOpType enum
Change enum definition as internal.

[05/11] block: Add bdrv_set_backing_hd()
Set bs->backing_file and bs->backing_format.

[06/11] block: Add backing_blocker in BlockDriverState
Reuse bdrv_set_backing_hd().

[07/11] block: Parse "backing" option to reference existing BDS
Update commit message about bdrv_swap assertion removal. Fix use-after-free.
Check for "backing=" and "backing.file=" conflict.

[08/11] block: Support dropping active in bdrv_drop_intermediate
Fix function comment.

[09/11] stream: Use bdrv_drop_intermediate and drop close_unused_images

Fam Zheng (11):
block: Add BlockOpType enum
block: Introduce op_blockers to BlockDriverState
block: Replace in_use with operation blocker
block: Move op_blocker check from block_job_create to its caller
block: Add bdrv_set_backing_hd()
block: Add backing_blocker in BlockDriverState
block: Parse "backing" option to reference existing BDS
block: Support dropping active in bdrv_drop_intermediate
stream: Use bdrv_drop_intermediate and drop close_unused_images
qmp: Add command 'blockdev-backup'
block: Allow backup on referenced named BlockDriverState

 block-migration.c               |   7 +-
 block.c                         | 310 ++++++++++++++++++++++++++--------------
 block/backup.c                  |  21 +++
 block/commit.c                  |   1 +
 block/stream.c                  |  30 +---
 blockdev.c                      |  70 +++++++--
 blockjob.c                      |  14 +-
 hw/block/dataplane/virtio-blk.c |  19 ++-
 include/block/block.h           |  29 +++-
 include/block/block_int.h       |   9 +-
 include/block/blockjob.h        |   3 +
 qapi-schema.json                |  49 +++++++
 qmp-commands.hx                 |  44 ++++++
 13 files changed, 446 insertions(+), 160 deletions(-)
Signature · 1 line
1.8.5.3
author
#1 lore raw

PATCH v12 1/11 block: Add BlockOpType enum

This adds the enum of all the operations that can be taken on a block device.

---
 include/block/block.h | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
include/block/block.h +19 -0
index 963a61f..9626650 100644
@@ -151,6 +151,25 @@ typedef struct BDRVReopenState {
     void *opaque;
 } BDRVReopenState;
 
+/*
+ * Block operation types
+ */
+typedef enum BlockOpType {
+    BLOCK_OP_TYPE_BACKUP_SOURCE,
+    BLOCK_OP_TYPE_BACKUP_TARGET,
+    BLOCK_OP_TYPE_CHANGE,
+    BLOCK_OP_TYPE_COMMIT,
+    BLOCK_OP_TYPE_DATAPLANE,
+    BLOCK_OP_TYPE_DRIVE_DEL,
+    BLOCK_OP_TYPE_EJECT,
+    BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT,
+    BLOCK_OP_TYPE_INTERNAL_SNAPSHOT,
+    BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE,
+    BLOCK_OP_TYPE_MIRROR,
+    BLOCK_OP_TYPE_RESIZE,
+    BLOCK_OP_TYPE_STREAM,
+    BLOCK_OP_TYPE_MAX,
+} BlockOpType;
 
 void bdrv_iostatus_enable(BlockDriverState *bs);
 void bdrv_iostatus_reset(BlockDriverState *bs);
Signature · 1 line
1.8.5.3
author
#2 lore raw

PATCH v12 2/11 block: Introduce op_blockers to BlockDriverState

In reply to Fam Zheng

BlockDriverState.op_blockers is an array of lists with BLOCK_OP_TYPE_MAX elements. Each list is a list of blockers of an operation type (BlockOpType), that marks this BDS as currently blocked for a certain type of operation with reason errors stored in the list. The rule of usage is:

* BDS user who wants to take an operation should check if there's any blocker of the type with bdrv_op_is_blocked().

* BDS user who wants to block certain types of operation, should call bdrv_op_block (or bdrv_op_block_all to block all types of operations, which is similar to the existing bdrv_set_in_use()).

* A blocker is only referenced by op_blockers, so the lifecycle is managed by caller, and shouldn't be lost until unblock, so typically a caller does these:

- Allocate a blocker with error_setg or similar, call bdrv_op_block() to block some operations.
- Hold the blocker, do his job.
- Unblock operations that it blocked, with the same reason pointer passed to bdrv_op_unblock().
- Release the blocker with error_free().

---
 block.c                   | 71 +++++++++++++++++++++++++++++++++++++++++++++++
 include/block/block.h     |  7 +++++
 include/block/block_int.h |  5 ++++
 3 files changed, 83 insertions(+)
block.c +71 -0
index cb21a5f..967de98 100644
@@ -1838,6 +1838,8 @@ static void bdrv_move_feature_fields(BlockDriverState *bs_dest,
      * We do want to swap name but don't want to swap linked list entries
      */
     bs_dest->node_list   = bs_src->node_list;
+    memcpy(bs_dest->op_blockers, bs_src->op_blockers,
+           sizeof(bs_dest->op_blockers));
 }
 
 /*
@@ -5119,6 +5121,75 @@ void bdrv_unref(BlockDriverState *bs)
     }
 }
 
+struct BdrvOpBlocker {
+    Error *reason;
+    QLIST_ENTRY(BdrvOpBlocker) list;
+};
+
+bool bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **errp)
+{
+    BdrvOpBlocker *blocker;
+    assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
+    if (!QLIST_EMPTY(&bs->op_blockers[op])) {
+        blocker = QLIST_FIRST(&bs->op_blockers[op]);
+        if (errp) {
+            *errp = error_copy(blocker->reason);
+        }
+        return true;
+    }
+    return false;
+}
+
+void bdrv_op_block(BlockDriverState *bs, BlockOpType op, Error *reason)
+{
+    BdrvOpBlocker *blocker;
+    assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
+
+    blocker = g_malloc0(sizeof(BdrvOpBlocker));
+    blocker->reason = reason;
+    QLIST_INSERT_HEAD(&bs->op_blockers[op], blocker, list);
+}
+
+void bdrv_op_unblock(BlockDriverState *bs, BlockOpType op, Error *reason)
+{
+    BdrvOpBlocker *blocker, *next;
+    assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
+    QLIST_FOREACH_SAFE(blocker, &bs->op_blockers[op], list, next) {
+        if (blocker->reason == reason) {
+            QLIST_REMOVE(blocker, list);
+            g_free(blocker);
+        }
+    }
+}
+
+void bdrv_op_block_all(BlockDriverState *bs, Error *reason)
+{
+    int i;
+    for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
+        bdrv_op_block(bs, i, reason);
+    }
+}
+
+void bdrv_op_unblock_all(BlockDriverState *bs, Error *reason)
+{
+    int i;
+    for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
+        bdrv_op_unblock(bs, i, reason);
+    }
+}
+
+bool bdrv_op_blocker_is_empty(BlockDriverState *bs)
+{
+    int i;
+
+    for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
+        if (!QLIST_EMPTY(&bs->op_blockers[i])) {
+            return false;
+        }
+    }
+    return true;
+}
+
 void bdrv_set_in_use(BlockDriverState *bs, int in_use)
 {
     assert(bs->in_use != in_use);
include/block/block.h +7 -0
index 9626650..f4a70c9 100644
@@ -473,6 +473,13 @@ void bdrv_unref(BlockDriverState *bs);
 void bdrv_set_in_use(BlockDriverState *bs, int in_use);
 int bdrv_in_use(BlockDriverState *bs);
 
+bool bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **errp);
+void bdrv_op_block(BlockDriverState *bs, BlockOpType op, Error *reason);
+void bdrv_op_unblock(BlockDriverState *bs, BlockOpType op, Error *reason);
+void bdrv_op_block_all(BlockDriverState *bs, Error *reason);
+void bdrv_op_unblock_all(BlockDriverState *bs, Error *reason);
+bool bdrv_op_blocker_is_empty(BlockDriverState *bs);
+
 #ifdef CONFIG_LINUX_AIO
 int raw_get_aio_fd(BlockDriverState *bs);
 #else
include/block/block_int.h +5 -0
index 0bcf1c9..4e558d0 100644
@@ -270,6 +270,8 @@ typedef struct BlockLimits {
     size_t opt_mem_alignment;
 } BlockLimits;
 
+typedef struct BdrvOpBlocker BdrvOpBlocker;
+
 /*
  * Note: the function bdrv_append() copies and swaps contents of
  * BlockDriverStates, so if you add new fields to this struct, please
@@ -361,6 +363,9 @@ struct BlockDriverState {
 
     QLIST_HEAD(, BdrvTrackedRequest) tracked_requests;
 
+    /* operation blockers */
+    QLIST_HEAD(, BdrvOpBlocker) op_blockers[BLOCK_OP_TYPE_MAX];
+
     /* long-running background operation */
     BlockJob *job;
 
Signature · 1 line
1.8.5.3
author
#3 lore raw

PATCH v12 3/11 block: Replace in_use with operation blocker

In reply to Fam Zheng

This drops BlockDriverState.in_use with op_blockers:

- Call bdrv_op_block_all in place of bdrv_set_in_use(bs, 1).
- Call bdrv_op_unblock_all in place of bdrv_set_in_use(bs, 0).
- Check bdrv_op_is_blocked() in place of bdrv_in_use(bs). The specific types are used, e.g. in place of starting block backup, bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP, ...).
- Check bdrv_op_blocker_is_empty() in place of assert(!bs->in_use).

Note: there is only bdrv_op_block_all and bdrv_op_unblock_all callers at this moment. So although the checks are specific to op types, this changes can still be seen as identical logic with previously with in_use. The difference is error message are improved because of blocker error info.

---
 block-migration.c               |  7 +++++--
 block.c                         | 24 +++++++-----------------
 blockdev.c                      | 15 ++++++---------
 blockjob.c                      | 14 +++++++++-----
 hw/block/dataplane/virtio-blk.c | 19 ++++++++++++-------
 include/block/block.h           |  2 --
 include/block/block_int.h       |  1 -
 include/block/blockjob.h        |  3 +++
 8 files changed, 42 insertions(+), 43 deletions(-)
block-migration.c +5 -2
index 897fdba..bf9a25f 100644
@@ -59,6 +59,7 @@ typedef struct BlkMigDevState {
     unsigned long *aio_bitmap;
     int64_t completed_sectors;
     BdrvDirtyBitmap *dirty_bitmap;
+    Error *blocker;
 } BlkMigDevState;
 
 typedef struct BlkMigBlock {
@@ -346,7 +347,8 @@ static void init_blk_migration_it(void *opaque, BlockDriverState *bs)
         bmds->completed_sectors = 0;
         bmds->shared_base = block_mig_state.shared_base;
         alloc_aio_bitmap(bmds);
-        bdrv_set_in_use(bs, 1);
+        error_setg(&bmds->blocker, "block device is in use by migration");
+        bdrv_op_block_all(bs, bmds->blocker);
         bdrv_ref(bs);
 
         block_mig_state.total_sector_sum += sectors;
@@ -584,7 +586,8 @@ static void blk_mig_cleanup(void)
     blk_mig_lock();
     while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
         QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
-        bdrv_set_in_use(bmds->bs, 0);
+        bdrv_op_unblock_all(bmds->bs, bmds->blocker);
+        error_free(bmds->blocker);
         bdrv_unref(bmds->bs);
         g_free(bmds->aio_bitmap);
         g_free(bmds);
block.c +7 -17
index 967de98..8000ac0 100644
@@ -1826,7 +1826,6 @@ static void bdrv_move_feature_fields(BlockDriverState *bs_dest,
     bs_dest->refcnt             = bs_src->refcnt;
 
     /* job */
-    bs_dest->in_use             = bs_src->in_use;
     bs_dest->job                = bs_src->job;
 
     /* keep the same entry in bdrv_states */
@@ -1863,7 +1862,7 @@ void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old)
     assert(QLIST_EMPTY(&bs_new->dirty_bitmaps));
     assert(bs_new->job == NULL);
     assert(bs_new->dev == NULL);
-    assert(bs_new->in_use == 0);
+    assert(bdrv_op_blocker_is_empty(bs_new));
     assert(bs_new->io_limits_enabled == false);
     assert(!throttle_have_timer(&bs_new->throttle_state));
 
@@ -1882,7 +1881,7 @@ void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old)
     /* Check a few fields that should remain attached to the device */
     assert(bs_new->dev == NULL);
     assert(bs_new->job == NULL);
-    assert(bs_new->in_use == 0);
+    assert(bdrv_op_blocker_is_empty(bs_new));
     assert(bs_new->io_limits_enabled == false);
     assert(!throttle_have_timer(&bs_new->throttle_state));
 
@@ -1919,7 +1918,7 @@ static void bdrv_delete(BlockDriverState *bs)
 {
     assert(!bs->dev);
     assert(!bs->job);
-    assert(!bs->in_use);
+    assert(bdrv_op_blocker_is_empty(bs));
     assert(!bs->refcnt);
     assert(QLIST_EMPTY(&bs->dirty_bitmaps));
 
@@ -2101,7 +2100,8 @@ int bdrv_commit(BlockDriverState *bs)
         return -ENOTSUP;
     }
 
-    if (bdrv_in_use(bs) || bdrv_in_use(bs->backing_hd)) {
+    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT, NULL) ||
+        bdrv_op_is_blocked(bs->backing_hd, BLOCK_OP_TYPE_COMMIT, NULL)) {
         return -EBUSY;
     }
 
@@ -3334,8 +3334,9 @@ int bdrv_truncate(BlockDriverState *bs, int64_t offset)
         return -ENOTSUP;
     if (bs->read_only)
         return -EACCES;
-    if (bdrv_in_use(bs))
+    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
         return -EBUSY;
+    }
     ret = drv->bdrv_truncate(bs, offset);
     if (ret == 0) {
         ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
@@ -5190,17 +5191,6 @@ bool bdrv_op_blocker_is_empty(BlockDriverState *bs)
     return true;
 }
 
-void bdrv_set_in_use(BlockDriverState *bs, int in_use)
-{
-    assert(bs->in_use != in_use);
-    bs->in_use = in_use;
-}
-
-int bdrv_in_use(BlockDriverState *bs)
-{
-    return bs->in_use;
-}
-
 void bdrv_iostatus_enable(BlockDriverState *bs)
 {
     bs->iostatus_enabled = true;
blockdev.c +6 -9
index 36ceece..ca66b94 100644
@@ -1264,8 +1264,8 @@ static void external_snapshot_prepare(BlkTransactionState *common,
         return;
     }
 
-    if (bdrv_in_use(state->old_bs)) {
-        error_set(errp, QERR_DEVICE_IN_USE, device);
+    if (bdrv_op_is_blocked(state->old_bs,
+                           BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
         return;
     }
 
@@ -1489,8 +1489,7 @@ exit:
 
 static void eject_device(BlockDriverState *bs, int force, Error **errp)
 {
-    if (bdrv_in_use(bs)) {
-        error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
+    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
         return;
     }
     if (!bdrv_dev_has_removable_media(bs)) {
@@ -1696,7 +1695,7 @@ int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
         qerror_report(QERR_DEVICE_NOT_FOUND, id);
         return -1;
     }
-    if (bdrv_in_use(bs)) {
+    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, NULL)) {
         qerror_report(QERR_DEVICE_IN_USE, id);
         return -1;
     }
@@ -1949,8 +1948,7 @@ void qmp_drive_backup(const char *device, const char *target,
         }
     }
 
-    if (bdrv_in_use(bs)) {
-        error_set(errp, QERR_DEVICE_IN_USE, device);
+    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
         return;
     }
 
@@ -2084,8 +2082,7 @@ void qmp_drive_mirror(const char *device, const char *target,
         }
     }
 
-    if (bdrv_in_use(bs)) {
-        error_set(errp, QERR_DEVICE_IN_USE, device);
+    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR, errp)) {
         return;
     }
 
blockjob.c +9 -5
index 9e5fd5c..f1ff036 100644
@@ -41,14 +41,16 @@ void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs,
 {
     BlockJob *job;
 
-    if (bs->job || bdrv_in_use(bs)) {
+    if (bs->job || !bdrv_op_blocker_is_empty(bs)) {
         error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
         return NULL;
     }
     bdrv_ref(bs);
-    bdrv_set_in_use(bs, 1);
-
     job = g_malloc0(driver->instance_size);
+    error_setg(&job->blocker, "block device is in use by block job: %s",
+               BlockJobType_lookup[driver->job_type]);
+    bdrv_op_block_all(bs, job->blocker);
+
     job->driver        = driver;
     job->bs            = bs;
     job->cb            = cb;
@@ -63,8 +65,9 @@ void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs,
         block_job_set_speed(job, speed, &local_err);
         if (error_is_set(&local_err)) {
             bs->job = NULL;
+            bdrv_op_unblock_all(bs, job->blocker);
+            error_free(job->blocker);
             g_free(job);
-            bdrv_set_in_use(bs, 0);
             error_propagate(errp, local_err);
             return NULL;
         }
@@ -79,8 +82,9 @@ void block_job_completed(BlockJob *job, int ret)
     assert(bs->job == job);
     job->cb(job->opaque, ret);
     bs->job = NULL;
+    bdrv_op_unblock_all(bs, job->blocker);
+    error_free(job->blocker);
     g_free(job);
-    bdrv_set_in_use(bs, 0);
 }
 
 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
hw/block/dataplane/virtio-blk.c +12 -7
index 456d437..4d3e157 100644
@@ -69,6 +69,9 @@ struct VirtIOBlockDataPlane {
                                              queue */
 
     unsigned int num_reqs;
+
+    /* Operation blocker on BDS */
+    Error *blocker;
 };
 
 /* Raise an interrupt to signal guest, if necessary */
@@ -368,6 +371,7 @@ void virtio_blk_data_plane_create(VirtIODevice *vdev, VirtIOBlkConf *blk,
 {
     VirtIOBlockDataPlane *s;
     int fd;
+    Error *local_err = NULL;
 
     *dataplane = NULL;
 
@@ -390,9 +394,10 @@ void virtio_blk_data_plane_create(VirtIODevice *vdev, VirtIOBlkConf *blk,
     /* If dataplane is (re-)enabled while the guest is running there could be
      * block jobs that can conflict.
      */
-    if (bdrv_in_use(blk->conf.bs)) {
-        error_setg(errp,
-                   "cannot start dataplane thread while device is in use");
+    if (bdrv_op_is_blocked(blk->conf.bs, BLOCK_OP_TYPE_DATAPLANE, &local_err)) {
+        error_report("cannot start dataplane thread: %s",
+                      error_get_pretty(local_err));
+        error_free(local_err);
         return;
     }
 
@@ -407,9 +412,8 @@ void virtio_blk_data_plane_create(VirtIODevice *vdev, VirtIOBlkConf *blk,
     s->vdev = vdev;
     s->fd = fd;
     s->blk = blk;
-
-    /* Prevent block operations that conflict with data plane thread */
-    bdrv_set_in_use(blk->conf.bs, 1);
+    error_setg(&s->blocker, "block device is in use by data plane");
+    bdrv_op_block_all(blk->conf.bs, s->blocker);
 
     *dataplane = s;
 }
@@ -421,7 +425,8 @@ void virtio_blk_data_plane_destroy(VirtIOBlockDataPlane *s)
     }
 
     virtio_blk_data_plane_stop(s);
-    bdrv_set_in_use(s->blk->conf.bs, 0);
+    bdrv_op_unblock_all(s->blk->conf.bs, s->blocker);
+    error_free(s->blocker);
     g_free(s);
 }
 
include/block/block.h +0 -2
index f4a70c9..9125bbe 100644
@@ -470,8 +470,6 @@ void bdrv_disable_copy_on_read(BlockDriverState *bs);
 
 void bdrv_ref(BlockDriverState *bs);
 void bdrv_unref(BlockDriverState *bs);
-void bdrv_set_in_use(BlockDriverState *bs, int in_use);
-int bdrv_in_use(BlockDriverState *bs);
 
 bool bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **errp);
 void bdrv_op_block(BlockDriverState *bs, BlockOpType op, Error *reason);
include/block/block_int.h +0 -1
index 4e558d0..1d3f76f 100644
@@ -359,7 +359,6 @@ struct BlockDriverState {
     QTAILQ_ENTRY(BlockDriverState) device_list;
     QLIST_HEAD(, BdrvDirtyBitmap) dirty_bitmaps;
     int refcnt;
-    int in_use; /* users other than guest access, eg. block migration */
 
     QLIST_HEAD(, BdrvTrackedRequest) tracked_requests;
 
include/block/blockjob.h +3 -0
index d76de62..c0a7875 100644
@@ -106,6 +106,9 @@ struct BlockJob {
     /** The completion function that will be called when the job completes.  */
     BlockDriverCompletionFunc *cb;
 
+    /** Block other operations when block job is running */
+    Error *blocker;
+
     /** The opaque value that is passed to the completion function.  */
     void *opaque;
 };
Signature · 1 line
1.8.5.3
author
#4 lore raw

PATCH v12 4/11 block: Move op_blocker check from block_job_create to its caller

In reply to Fam Zheng

It makes no sense to check for "any" blocker on bs, we are here only because of the mechanical conversion from in_use to op_blockers. Remove it now, and let the callers check specific operation types. Backup and mirror already have it, add checker to stream and commit.

---
 blockdev.c | 8 ++++++++
 blockjob.c | 2 +-
 2 files changed, 9 insertions(+), 1 deletion(-)
blockdev.c +8 -0
index ca66b94..ffaa6a9 100644
@@ -1817,6 +1817,10 @@ void qmp_block_stream(const char *device, bool has_base,
         return;
     }
 
+    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) {
+        return;
+    }
+
     if (base) {
         base_bs = bdrv_find_backing_image(bs, base);
         if (base_bs == NULL) {
@@ -1857,6 +1861,10 @@ void qmp_block_commit(const char *device,
         return;
     }
 
+    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT, errp)) {
+        return;
+    }
+
     /* default top_bs is the active layer */
     top_bs = bs;
 
blockjob.c +1 -1
index f1ff036..21e21c0 100644
@@ -41,7 +41,7 @@ void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs,
 {
     BlockJob *job;
 
-    if (bs->job || !bdrv_op_blocker_is_empty(bs)) {
+    if (bs->job) {
         error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
         return NULL;
     }
Signature · 1 line
1.8.5.3
author
#5 lore raw

PATCH v12 5/11 block: Add bdrv_set_backing_hd()

In reply to Fam Zheng

This is the common but non-trivial steps to assign or change the backing_hd of BDS.

---
 block.c               | 39 ++++++++++++++++++++++++++++++---------
 include/block/block.h |  1 +
 2 files changed, 31 insertions(+), 9 deletions(-)
block.c +30 -9
index 8000ac0..af8922b 100644
@@ -1072,6 +1072,31 @@ fail:
     return ret;
 }
 
+void bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd)
+{
+    if (bs->backing_hd) {
+        bdrv_unref(bs->backing_hd);
+    }
+
+    bs->backing_hd = backing_hd;
+    if (!backing_hd) {
+        bs->backing_file[0] = '\0';
+        bs->backing_format[0] = '\0';
+        goto out;
+    }
+    pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_hd->filename);
+    pstrcpy(bs->backing_format, sizeof(bs->backing_file),
+            backing_hd->drv ? backing_hd->drv->format_name : "");
+    bdrv_ref(bs->backing_hd);
+
+    pstrcpy(bs->backing_file, sizeof(bs->backing_file),
+            bs->backing_hd->file->filename);
+    pstrcpy(bs->backing_format, sizeof(bs->backing_format),
+            bs->backing_hd->drv->format_name);
+out:
+    bdrv_refresh_limits(bs);
+}
+
 /*
  * Opens the backing file for a BlockDriverState if not yet open
  *
@@ -1085,6 +1110,7 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp)
     char backing_filename[PATH_MAX];
     int back_flags, ret;
     BlockDriver *back_drv = NULL;
+    BlockDriverState *backing_hd;
     Error *local_err = NULL;
 
     if (bs->backing_hd != NULL) {
@@ -1108,7 +1134,7 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp)
                                        sizeof(backing_filename));
     }
 
-    bs->backing_hd = bdrv_new("");
+    backing_hd = bdrv_new("");
 
     if (bs->backing_format[0] != '\0') {
         back_drv = bdrv_find_format(bs->backing_format);
@@ -1118,23 +1144,18 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp)
     back_flags = bs->open_flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT |
                                     BDRV_O_COPY_ON_READ);
 
-    ret = bdrv_open(bs->backing_hd,
+    ret = bdrv_open(backing_hd,
                     *backing_filename ? backing_filename : NULL, options,
                     back_flags, back_drv, &local_err);
     if (ret < 0) {
-        bdrv_unref(bs->backing_hd);
-        bs->backing_hd = NULL;
+        bdrv_unref(backing_hd);
         bs->open_flags |= BDRV_O_NO_BACKING;
         error_setg(errp, "Could not open backing file: %s",
                    error_get_pretty(local_err));
         error_free(local_err);
         return ret;
     }
-
-    if (bs->backing_hd->file) {
-        pstrcpy(bs->backing_file, sizeof(bs->backing_file),
-                bs->backing_hd->file->filename);
-    }
+    bdrv_set_backing_hd(bs, backing_hd);
 
     /* Recalculate the BlockLimits with the backing file */
     bdrv_refresh_limits(bs);
include/block/block.h +1 -0
index 9125bbe..f449753 100644
@@ -208,6 +208,7 @@ int bdrv_file_open(BlockDriverState **pbs, const char *filename,
 int bdrv_open_image(BlockDriverState **pbs, const char *filename,
                     QDict *options, const char *bdref_key, int flags,
                     bool force_raw, bool allow_none, Error **errp);
+void bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd);
 int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp);
 int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
               int flags, BlockDriver *drv, Error **errp);
Signature · 1 line
1.8.5.3
#6 lore raw

Re: [Qemu-devel] [PATCH v12 05/11] block: Add bdrv_set_backing_hd()

block.c fail: · line 1095

Quoted 9 lines · Fam Zheng
block.c +9 -0
+    pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_hd->filename);
+    pstrcpy(bs->backing_format, sizeof(bs->backing_file),
+            backing_hd->drv ? backing_hd->drv->format_name : "");
+    bdrv_ref(bs->backing_hd);
+
+    pstrcpy(bs->backing_file, sizeof(bs->backing_file),
+            bs->backing_hd->file->filename);
+    pstrcpy(bs->backing_format, sizeof(bs->backing_format),
+            bs->backing_hd->drv->format_name);

This is redundant, you already have the same above?

author
#7 lore raw

PATCH v12 6/11 block: Add backing_blocker in BlockDriverState

In reply to Fam Zheng

This makes use of op_blocker and blocks all the operations except for commit target, on each BlockDriverState->backing_hd.

The asserts for op_blocker in bdrv_swap are removed because with this change, the target of block commit has at least the backing blocker of its child, so the assertion is not true. Callers should do their check.

---
 block.c                   | 18 ++++++++++++++----
 include/block/block_int.h |  3 +++
 2 files changed, 17 insertions(+), 4 deletions(-)
block.c +14 -4
index af8922b..4498ce7 100644
@@ -1075,13 +1075,22 @@ fail:
 void bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd)
 {
     if (bs->backing_hd) {
+        assert(error_is_set(&bs->backing_blocker));
+        bdrv_op_unblock_all(bs->backing_hd, bs->backing_blocker);
         bdrv_unref(bs->backing_hd);
+    } else if (backing_hd) {
+        error_setg(&bs->backing_blocker,
+                   "device is used as backing hd of '%s'",
+                   bs->device_name);
     }
 
     bs->backing_hd = backing_hd;
     if (!backing_hd) {
         bs->backing_file[0] = '\0';
         bs->backing_format[0] = '\0';
+        if (error_is_set(&bs->backing_blocker)) {
+            error_free(bs->backing_blocker);
+        }
         goto out;
     }
     pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_hd->filename);
@@ -1089,6 +1098,10 @@ void bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd)
             backing_hd->drv ? backing_hd->drv->format_name : "");
     bdrv_ref(bs->backing_hd);
 
+    bdrv_op_block_all(bs->backing_hd, bs->backing_blocker);
+    /* Otherwise we won't be able to commit due to check in bdrv_commit */
+    bdrv_op_unblock(bs->backing_hd, BLOCK_OP_TYPE_COMMIT,
+                    bs->backing_blocker);
     pstrcpy(bs->backing_file, sizeof(bs->backing_file),
             bs->backing_hd->file->filename);
     pstrcpy(bs->backing_format, sizeof(bs->backing_format),
@@ -1680,8 +1693,7 @@ void bdrv_close(BlockDriverState *bs)
 
     if (bs->drv) {
         if (bs->backing_hd) {
-            bdrv_unref(bs->backing_hd);
-            bs->backing_hd = NULL;
+            bdrv_set_backing_hd(bs, NULL);
         }
         bs->drv->bdrv_close(bs);
         g_free(bs->opaque);
@@ -1883,7 +1895,6 @@ void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old)
     assert(QLIST_EMPTY(&bs_new->dirty_bitmaps));
     assert(bs_new->job == NULL);
     assert(bs_new->dev == NULL);
-    assert(bdrv_op_blocker_is_empty(bs_new));
     assert(bs_new->io_limits_enabled == false);
     assert(!throttle_have_timer(&bs_new->throttle_state));
 
@@ -1902,7 +1913,6 @@ void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old)
     /* Check a few fields that should remain attached to the device */
     assert(bs_new->dev == NULL);
     assert(bs_new->job == NULL);
-    assert(bdrv_op_blocker_is_empty(bs_new));
     assert(bs_new->io_limits_enabled == false);
     assert(!throttle_have_timer(&bs_new->throttle_state));
 
include/block/block_int.h +3 -0
index 1d3f76f..1f4f78b 100644
@@ -369,6 +369,9 @@ struct BlockDriverState {
     BlockJob *job;
 
     QDict *options;
+
+    /* The error object in use for blocking operations on backing_hd */
+    Error *backing_blocker;
 };
 
 int get_tmp_filename(char *filename, int size);
Signature · 1 line
1.8.5.3
author
#8 lore raw

PATCH v12 7/11 block: Parse "backing" option to reference existing BDS

In reply to Fam Zheng

Now it's safe to allow reference for backing_hd in the interface.

---
 block.c | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)
block.c +24 -2
index 4498ce7..be58d93 100644
@@ -1396,12 +1396,34 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
     /* If there is a backing file, use it */
     if ((flags & BDRV_O_NO_BACKING) == 0) {
         QDict *backing_options;
+        const char *backing_name;
+        BlockDriverState *backing_hd;
 
+        backing_name = qdict_get_try_str(options, "backing");
         qdict_extract_subqdict(options, &backing_options, "backing.");
-        ret = bdrv_open_backing_file(bs, backing_options, &local_err);
-        if (ret < 0) {
+
+        if (backing_name && qdict_size(backing_options)) {
+            error_setg(&local_err,
+                       "Option \"backing\" and \"backing.*\" cannot be "
+                       "used together");
+            ret = -EINVAL;
             goto close_and_fail;
         }
+        if (backing_name) {
+            backing_hd = bdrv_find(backing_name);
+            if (!backing_hd) {
+                error_set(&local_err, QERR_DEVICE_NOT_FOUND, backing_name);
+                ret = -ENOENT;
+                goto close_and_fail;
+            }
+            qdict_del(options, "backing");
+            bdrv_set_backing_hd(bs, backing_hd);
+        } else {
+            ret = bdrv_open_backing_file(bs, backing_options, &local_err);
+            if (ret < 0) {
+                goto close_and_fail;
+            }
+        }
     }
 
     /* Check if any unknown options were used */
Signature · 1 line
1.8.5.3
author
#9 lore raw

PATCH v12 8/11 block: Support dropping active in bdrv_drop_intermediate

In reply to Fam Zheng

Dropping intermediate could be useful both for commit and stream, and BDS refcnt plus bdrv_swap could do most of the job nicely. It also needs to work with op blockers.

---
 block.c        | 146 +++++++++++++++++++++++++--------------------------------
 block/commit.c |   1 +
 2 files changed, 66 insertions(+), 81 deletions(-)
block.c +65 -81
index be58d93..e51ead3 100644
@@ -2468,115 +2468,99 @@ BlockDriverState *bdrv_find_overlay(BlockDriverState *active,
     return overlay;
 }
 
-typedef struct BlkIntermediateStates {
-    BlockDriverState *bs;
-    QSIMPLEQ_ENTRY(BlkIntermediateStates) entry;
-} BlkIntermediateStates;
-
-
 /*
- * Drops images above 'base' up to and including 'top', and sets the image
- * above 'top' to have base as its backing file.
+ * Drops images above 'base' up to and including 'top', and sets new 'base'
+ * as backing_hd of top_overlay (the image orignally has 'top' as backing
+ * file). top_overlay may be NULL if 'top' is active, no such update needed.
+ * Requires that the top_overlay to 'top' is opened r/w.
  *
- * Requires that the overlay to 'top' is opened r/w, so that the backing file
- * information in 'bs' can be properly updated.
+ * 1) This will convert the following chain:
  *
- * E.g., this will convert the following chain:
- * bottom <- base <- intermediate <- top <- active
+ *     ... <- base <- ... <- top <- overlay <-... <- active
  *
  * to
  *
- * bottom <- base <- active
+ *     ... <- base <- overlay <- active
  *
- * It is allowed for bottom==base, in which case it converts:
+ * 2) It is allowed for bottom==base, in which case it converts:
  *
- * base <- intermediate <- top <- active
+ *     base <- ... <- top <- overlay <- ... <- active
  *
  * to
  *
- * base <- active
+ *     base <- overlay <- active
+ *
+ * 2) It also allows active==top, in which case it converts:
+ *
+ *     ... <- base <- ... <- top (active)
+ *
+ * to
+ *
+ *     ... <- base == active == top
+ *
+ * i.e. only base and lower remains: *top == *base when return.
+ *
+ * 3) If base==NULL, it will drop all the BDS below overlay and set its
+ * backing_hd to NULL. I.e.:
+ *
+ *     base(NULL) <- ... <- overlay <- ... <- active
+ *
+ * to
  *
- * Error conditions:
- *  if active == top, that is considered an error
+ *     overlay <- ... <- active
  *
  */
 int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top,
                            BlockDriverState *base)
 {
-    BlockDriverState *intermediate;
-    BlockDriverState *base_bs = NULL;
-    BlockDriverState *new_top_bs = NULL;
-    BlkIntermediateStates *intermediate_state, *next;
-    int ret = -EIO;
-
-    QSIMPLEQ_HEAD(states_to_delete, BlkIntermediateStates) states_to_delete;
-    QSIMPLEQ_INIT(&states_to_delete);
-
-    if (!top->drv || !base->drv) {
-        goto exit;
-    }
-
-    new_top_bs = bdrv_find_overlay(active, top);
+    BlockDriverState *drop_start, *overlay;
+    int ret = -EINVAL;
 
-    if (new_top_bs == NULL) {
-        /* we could not find the image above 'top', this is an error */
+    if (!top->drv || (base && !base->drv)) {
         goto exit;
     }
-
-    /* special case of new_top_bs->backing_hd already pointing to base - nothing
-     * to do, no intermediate images */
-    if (new_top_bs->backing_hd == base) {
+    if (top == base) {
         ret = 0;
-        goto exit;
-    }
-
-    intermediate = top;
-
-    /* now we will go down through the list, and add each BDS we find
-     * into our deletion queue, until we hit the 'base'
-     */
-    while (intermediate) {
-        intermediate_state = g_malloc0(sizeof(BlkIntermediateStates));
-        intermediate_state->bs = intermediate;
-        QSIMPLEQ_INSERT_TAIL(&states_to_delete, intermediate_state, entry);
-
-        if (intermediate->backing_hd == base) {
-            base_bs = intermediate->backing_hd;
-            break;
+    } else if (top == active) {
+        assert(base);
+        drop_start = active->backing_hd;
+        bdrv_swap(active, base);
+        base->backing_hd = NULL;
+        bdrv_unref(drop_start);
+        ret = 0;
+    } else {
+        /* If there's an overlay, its backing_hd points to top's BDS now,
+         * the top image is dropped but this BDS structure is kept and swapped
+         * with base, this way we keep the pointers valid after dropping top */
+        overlay = bdrv_find_overlay(active, top);
+        if (!overlay) {
+            goto exit;
+        }
+        if (base) {
+            ret = bdrv_change_backing_file(overlay, base->filename,
+                                           base->drv->format_name);
+        } else {
+            ret = bdrv_change_backing_file(overlay, NULL, NULL);
+        }
+        if (ret) {
+            goto exit;
+        }
+        if (base) {
+            drop_start = top->backing_hd;
+            bdrv_swap(top, base);
+            /* Break the loop formed by bdrv_swap */
+            bdrv_set_backing_hd(base, NULL);
+        } else {
+            bdrv_set_backing_hd(overlay, NULL);
+            drop_start = top;
         }
-        intermediate = intermediate->backing_hd;
-    }
-    if (base_bs == NULL) {
-        /* something went wrong, we did not end at the base. safely
-         * unravel everything, and exit with error */
-        goto exit;
-    }
-
-    /* success - we can delete the intermediate states, and link top->base */
-    ret = bdrv_change_backing_file(new_top_bs, base_bs->filename,
-                                   base_bs->drv ? base_bs->drv->format_name : "");
-    if (ret) {
-        goto exit;
-    }
-    new_top_bs->backing_hd = base_bs;
-
-    bdrv_refresh_limits(new_top_bs);
 
-    QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) {
-        /* so that bdrv_close() does not recursively close the chain */
-        intermediate_state->bs->backing_hd = NULL;
-        bdrv_unref(intermediate_state->bs);
+        bdrv_unref(drop_start);
     }
-    ret = 0;
-
 exit:
-    QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) {
-        g_free(intermediate_state);
-    }
     return ret;
 }
 
-
 static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
                                    size_t size)
 {
block/commit.c +1 -0
index acec4ac..b10eb79 100644
@@ -142,6 +142,7 @@ wait:
     if (!block_job_is_cancelled(&s->common) && sector_num == end) {
         /* success */
         ret = bdrv_drop_intermediate(active, top, base);
+        base = top;
     }
 
 exit_free_buf:
Signature · 1 line
1.8.5.3
author
#10 lore raw

PATCH v12 9/11 stream: Use bdrv_drop_intermediate and drop close_unused_images

In reply to Fam Zheng

This reuses the new bdrv_drop_intermediate.

---
 block/stream.c | 30 +-----------------------------
 1 file changed, 1 insertion(+), 29 deletions(-)
block/stream.c +1 -29
index dd0b4ac..9cdcf0e 100644
@@ -51,34 +51,6 @@ static int coroutine_fn stream_populate(BlockDriverState *bs,
     return bdrv_co_copy_on_readv(bs, sector_num, nb_sectors, &qiov);
 }
 
-static void close_unused_images(BlockDriverState *top, BlockDriverState *base,
-                                const char *base_id)
-{
-    BlockDriverState *intermediate;
-    intermediate = top->backing_hd;
-
-    /* Must assign before bdrv_delete() to prevent traversing dangling pointer
-     * while we delete backing image instances.
-     */
-    top->backing_hd = base;
-
-    while (intermediate) {
-        BlockDriverState *unused;
-
-        /* reached base */
-        if (intermediate == base) {
-            break;
-        }
-
-        unused = intermediate;
-        intermediate = intermediate->backing_hd;
-        unused->backing_hd = NULL;
-        bdrv_unref(unused);
-    }
-
-    bdrv_refresh_limits(top);
-}
-
 static void coroutine_fn stream_run(void *opaque)
 {
     StreamBlockJob *s = opaque;
@@ -192,7 +164,7 @@ wait:
             }
         }
         ret = bdrv_change_backing_file(bs, base_id, base_fmt);
-        close_unused_images(bs, base, base_id);
+        bdrv_drop_intermediate(bs, bs->backing_hd, base);
     }
 
     qemu_vfree(buf);
Signature · 1 line
1.8.5.3
author
#11 lore raw

PATCH v12 10/11 qmp: Add command 'blockdev-backup'

In reply to Fam Zheng

Similar to drive-backup, but this command uses a device id as target instead of creating/opening an image file.

Also add blocker on target bs, since the target is also a named device now.

---
 block/backup.c   | 21 +++++++++++++++++++++
 blockdev.c       | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 qapi-schema.json | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 qmp-commands.hx  | 44 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 161 insertions(+)
block/backup.c +21 -0
index 15a2e55..7a9e83f 100644
@@ -344,6 +344,7 @@ static void coroutine_fn backup_run(void *opaque)
     hbitmap_free(job->bitmap);
 
     bdrv_iostatus_disable(target);
+    bdrv_op_unblock_all(target, job->common.blocker);
     bdrv_unref(target);
 
     block_job_completed(&job->common, ret);
@@ -369,6 +370,24 @@ void backup_start(BlockDriverState *bs, BlockDriverState *target,
         return;
     }
 
+    if (!bdrv_is_inserted(bs)) {
+        error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, bs->device_name);
+        return;
+    }
+
+    if (!bdrv_is_inserted(target)) {
+        error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, target->device_name);
+        return;
+    }
+
+    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
+        return;
+    }
+
+    if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_BACKUP_TARGET, errp)) {
+        return;
+    }
+
     len = bdrv_getlength(bs);
     if (len < 0) {
         error_setg_errno(errp, -len, "unable to get length for '%s'",
@@ -382,6 +401,8 @@ void backup_start(BlockDriverState *bs, BlockDriverState *target,
         return;
     }
 
+    bdrv_op_block_all(target, job->common.blocker);
+
     job->on_source_error = on_source_error;
     job->on_target_error = on_target_error;
     job->target = target;
blockdev.c +47 -0

65 lines not on this page · raw message

qapi-schema.json +49 -0

61 lines not on this page · raw message

qmp-commands.hx +44 -0

50 lines not on this page · raw message

Signature · 1 line
1.8.5.3
author
#12 lore raw

PATCH v12 11/11 block: Allow backup on referenced named BlockDriverState

In reply to Fam Zheng

Drive backup is a read only operation on source bs. We want to allow this specific case to enable image-fleecing. Note that when image-fleecing job starts, the job still add its blocker to source bs, and any other operation on it will be blocked by that.

---
 block.c | 2 ++
 1 file changed, 2 insertions(+)
block.c +2 -0
index e51ead3..1e50f1e 100644
@@ -1102,6 +1102,8 @@ void bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd)
     /* Otherwise we won't be able to commit due to check in bdrv_commit */
     bdrv_op_unblock(bs->backing_hd, BLOCK_OP_TYPE_COMMIT,
                     bs->backing_blocker);
+    bdrv_op_unblock(bs->backing_hd, BLOCK_OP_TYPE_BACKUP_SOURCE,
+                    bs->backing_blocker);
     pstrcpy(bs->backing_file, sizeof(bs->backing_file),
             bs->backing_hd->file->filename);
     pstrcpy(bs->backing_format, sizeof(bs->backing_format),
Signature · 1 line
1.8.5.3
#13 lore raw
In reply to Fam Zheng
Quoted 27 lines · Fam Zheng

This series adds for point-in-time snapshot NBD exporting based on blockdev-backup (variant of drive-backup with existing device as target).

We get a thin point-in-time snapshot by COW mechanism of drive-backup, and export it through built in NBD server. The steps are as below:

1. (SHELL) qemu-img create -f qcow2 BACKUP.qcow2 <source size here>

(Alternatively we can use -o backing_file=RUNNING-VM.img to omit explicitly providing the size by ourselves, but it's risky because RUNNING-VM.qcow2 is used r/w by guest. Whether or not setting backing file in the image file doesn't matter, as we are going to override the backing hd in the next step)

2. (QMP) blockdev-add backing=source-drive file.driver=file file.filename=BACKUP.qcow2 id=target0 if=none driver=qcow2

(where source-drive is the running BlockDriverState name for RUNNING-VM.img. This patch implements "backing=" option to override backing_hd for added drive)

3. (QMP) blockdev-backup device=source-drive sync=none target=target0

(this is the QMP command introduced by this series, which use a named device as target of drive-backup)

4. (QMP) nbd-server-add device=target0

Quoted 7 lines

When image fleecing done:

1. (QMP) block-job-cancel device=source-drive

2. (HMP) drive_del target0

3. (SHELL) rm BACKUP.qcow2

Seems to work as advertized but do you have qemu-iotests for blockdev-backup and image fleecing in particular?

Stefan

#14 lore raw
Quoted 30 lines · Stefan Hajnoczi
Quoted 29 lines · Fam Zheng

This series adds for point-in-time snapshot NBD exporting based on blockdev-backup (variant of drive-backup with existing device as target).

We get a thin point-in-time snapshot by COW mechanism of drive-backup, and export it through built in NBD server. The steps are as below:

1. (SHELL) qemu-img create -f qcow2 BACKUP.qcow2 <source size here>

(Alternatively we can use -o backing_file=RUNNING-VM.img to omit explicitly providing the size by ourselves, but it's risky because RUNNING-VM.qcow2 is used r/w by guest. Whether or not setting backing file in the image file doesn't matter, as we are going to override the backing hd in the next step)

2. (QMP) blockdev-add backing=source-drive file.driver=file file.filename=BACKUP.qcow2 id=target0 if=none driver=qcow2

(where source-drive is the running BlockDriverState name for RUNNING-VM.img. This patch implements "backing=" option to override backing_hd for added drive)

3. (QMP) blockdev-backup device=source-drive sync=none target=target0

(this is the QMP command introduced by this series, which use a named device as target of drive-backup)

4. (QMP) nbd-server-add device=target0

When image fleecing done:

Quoted 8 lines
Quoted 5 lines

1. (QMP) block-job-cancel device=source-drive

2. (HMP) drive_del target0

3. (SHELL) rm BACKUP.qcow2

Seems to work as advertized but do you have qemu-iotests for blockdev-backup and image fleecing in particular?

We have them for drive-backup but not blockdev-backup afaik. Mostly the same code but certainly a different API.

It shouldn't be too hard to duplicate the 055 drive-backup code and make it a blockdev-backup test. Testing nbd export would be difficult however.

Ian

#15 lore raw
Quoted 40 lines · Ian Main
Quoted 30 lines · Stefan Hajnoczi
Quoted 29 lines · Fam Zheng

This series adds for point-in-time snapshot NBD exporting based on blockdev-backup (variant of drive-backup with existing device as target).

We get a thin point-in-time snapshot by COW mechanism of drive-backup, and export it through built in NBD server. The steps are as below:

1. (SHELL) qemu-img create -f qcow2 BACKUP.qcow2 <source size here>

(Alternatively we can use -o backing_file=RUNNING-VM.img to omit explicitly providing the size by ourselves, but it's risky because RUNNING-VM.qcow2 is used r/w by guest. Whether or not setting backing file in the image file doesn't matter, as we are going to override the backing hd in the next step)

2. (QMP) blockdev-add backing=source-drive file.driver=file file.filename=BACKUP.qcow2 id=target0 if=none driver=qcow2

(where source-drive is the running BlockDriverState name for RUNNING-VM.img. This patch implements "backing=" option to override backing_hd for added drive)

3. (QMP) blockdev-backup device=source-drive sync=none target=target0

(this is the QMP command introduced by this series, which use a named device as target of drive-backup)

4. (QMP) nbd-server-add device=target0

When image fleecing done:

Quoted 8 lines
Quoted 5 lines

1. (QMP) block-job-cancel device=source-drive

2. (HMP) drive_del target0

3. (SHELL) rm BACKUP.qcow2

Seems to work as advertized but do you have qemu-iotests for blockdev-backup and image fleecing in particular?

Quoted 6 lines

We have them for drive-backup but not blockdev-backup afaik. Mostly the same code but certainly a different API.

It shouldn't be too hard to duplicate the 055 drive-backup code and make it a blockdev-backup test. Testing nbd export would be difficult however.

A Python NBD client is pretty easy to implement. Here is a *server* I wrote for testing a while back:

https://lists.gnu.org/archive/html/qemu-devel/2013-03/msg01535.html

Stefan

#16 lore raw

Am 29.01.2014 um 09:44 hat Stefan Hajnoczi geschrieben:

Quoted 44 lines · Ian Main
Quoted 43 lines · Stefan Hajnoczi
Quoted 30 lines · Fam Zheng
Quoted 29 lines

This series adds for point-in-time snapshot NBD exporting based on blockdev-backup (variant of drive-backup with existing device as target).

We get a thin point-in-time snapshot by COW mechanism of drive-backup, and export it through built in NBD server. The steps are as below:

1. (SHELL) qemu-img create -f qcow2 BACKUP.qcow2 <source size here>

(Alternatively we can use -o backing_file=RUNNING-VM.img to omit explicitly providing the size by ourselves, but it's risky because RUNNING-VM.qcow2 is used r/w by guest. Whether or not setting backing file in the image file doesn't matter, as we are going to override the backing hd in the next step)

2. (QMP) blockdev-add backing=source-drive file.driver=file file.filename=BACKUP.qcow2 id=target0 if=none driver=qcow2

(where source-drive is the running BlockDriverState name for RUNNING-VM.img. This patch implements "backing=" option to override backing_hd for added drive)

3. (QMP) blockdev-backup device=source-drive sync=none target=target0

(this is the QMP command introduced by this series, which use a named device as target of drive-backup)

4. (QMP) nbd-server-add device=target0

When image fleecing done:

Quoted 8 lines
Quoted 5 lines

1. (QMP) block-job-cancel device=source-drive

2. (HMP) drive_del target0

3. (SHELL) rm BACKUP.qcow2

Seems to work as advertized but do you have qemu-iotests for blockdev-backup and image fleecing in particular?

We have them for drive-backup but not blockdev-backup afaik. Mostly the same code but certainly a different API.

Quoted 6 lines
Quoted 3 lines

It shouldn't be too hard to duplicate the 055 drive-backup code and make it a blockdev-backup test. Testing nbd export would be difficult however.

A Python NBD client is pretty easy to implement. Here is a *server* I wrote for testing a while back:

qemu-io should be a good enough NBD client.

Kevin

author
#17 lore raw
In reply to Stefan Hajnoczi
Quoted 30 lines · Stefan Hajnoczi
Quoted 29 lines · Fam Zheng

This series adds for point-in-time snapshot NBD exporting based on blockdev-backup (variant of drive-backup with existing device as target).

We get a thin point-in-time snapshot by COW mechanism of drive-backup, and export it through built in NBD server. The steps are as below:

1. (SHELL) qemu-img create -f qcow2 BACKUP.qcow2 <source size here>

(Alternatively we can use -o backing_file=RUNNING-VM.img to omit explicitly providing the size by ourselves, but it's risky because RUNNING-VM.qcow2 is used r/w by guest. Whether or not setting backing file in the image file doesn't matter, as we are going to override the backing hd in the next step)

2. (QMP) blockdev-add backing=source-drive file.driver=file file.filename=BACKUP.qcow2 id=target0 if=none driver=qcow2

(where source-drive is the running BlockDriverState name for RUNNING-VM.img. This patch implements "backing=" option to override backing_hd for added drive)

3. (QMP) blockdev-backup device=source-drive sync=none target=target0

(this is the QMP command introduced by this series, which use a named device as target of drive-backup)

4. (QMP) nbd-server-add device=target0

When image fleecing done:

Quoted 8 lines
Quoted 5 lines

1. (QMP) block-job-cancel device=source-drive

2. (HMP) drive_del target0

3. (SHELL) rm BACKUP.qcow2

Seems to work as advertized but do you have qemu-iotests for blockdev-backup and image fleecing in particular?

I'll respin to address the comment you left and add test cases for blockdev-backup and image fleecing. Thanks for the review. :)

Fam

Reviewed-by on the v12 series
#18 lore raw
In reply to Fam Zheng
Quoted 74 lines · Fam Zheng

This series adds for point-in-time snapshot NBD exporting based on blockdev-backup (variant of drive-backup with existing device as target).

We get a thin point-in-time snapshot by COW mechanism of drive-backup, and export it through built in NBD server. The steps are as below:

1. (SHELL) qemu-img create -f qcow2 BACKUP.qcow2 <source size here>

(Alternatively we can use -o backing_file=RUNNING-VM.img to omit explicitly providing the size by ourselves, but it's risky because RUNNING-VM.qcow2 is used r/w by guest. Whether or not setting backing file in the image file doesn't matter, as we are going to override the backing hd in the next step)

2. (QMP) blockdev-add backing=source-drive file.driver=file file.filename=BACKUP.qcow2 id=target0 if=none driver=qcow2

(where source-drive is the running BlockDriverState name for RUNNING-VM.img. This patch implements "backing=" option to override backing_hd for added drive)

3. (QMP) blockdev-backup device=source-drive sync=none target=target0

(this is the QMP command introduced by this series, which use a named device as target of drive-backup)

4. (QMP) nbd-server-add device=target0

When image fleecing done:

1. (QMP) block-job-cancel device=source-drive

2. (HMP) drive_del target0

3. (SHELL) rm BACKUP.qcow2

v11 -> v12: Rebase to qemu.git.
Call bdrv_refresh_limits() in bdrv_set_backing_hd().

v8 -> v11: Rebased to qemu.git. Address Stefan's comments:
(sorry for sending 3 revisions in a day)

[01/11] block: Add BlockOpType enum
Change enum definition as internal.

[05/11] block: Add bdrv_set_backing_hd()
Set bs->backing_file and bs->backing_format.

[06/11] block: Add backing_blocker in BlockDriverState
Reuse bdrv_set_backing_hd().

[07/11] block: Parse "backing" option to reference existing BDS
Update commit message about bdrv_swap assertion removal. Fix use-after-free.
Check for "backing=" and "backing.file=" conflict.

[08/11] block: Support dropping active in bdrv_drop_intermediate
Fix function comment.

[09/11] stream: Use bdrv_drop_intermediate and drop close_unused_images

Fam Zheng (11):
block: Add BlockOpType enum
block: Introduce op_blockers to BlockDriverState
block: Replace in_use with operation blocker
block: Move op_blocker check from block_job_create to its caller
block: Add bdrv_set_backing_hd()
block: Add backing_blocker in BlockDriverState
block: Parse "backing" option to reference existing BDS
block: Support dropping active in bdrv_drop_intermediate
stream: Use bdrv_drop_intermediate and drop close_unused_images
qmp: Add command 'blockdev-backup'
block: Allow backup on referenced named BlockDriverState

Quoted 14 lines
 block-migration.c               |   7 +-
 block.c                         | 310 ++++++++++++++++++++++++++--------------
 block/backup.c                  |  21 +++
 block/commit.c                  |   1 +
 block/stream.c                  |  30 +---
 blockdev.c                      |  70 +++++++--
 blockjob.c                      |  14 +-
 hw/block/dataplane/virtio-blk.c |  19 ++-
 include/block/block.h           |  29 +++-
 include/block/block_int.h       |   9 +-
 include/block/blockjob.h        |   3 +
 qapi-schema.json                |  49 +++++++
 qmp-commands.hx                 |  44 ++++++
 13 files changed, 446 insertions(+), 160 deletions(-)

I had only one comment on the code, see my reply. But we need tests in order to merge this.