Skip to contentSkip to conversation

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

PATCH Superseded Fam Zheng 2 participants 13 messages Nov 26, 2013 – Nov 28, 2013 Block layer

v5 of 20 Superseded Latest is v20 →

lore
Share & tools
lore

Fam Zheng

author
#0 lore raw

v5 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 ide0-hd0 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-complete device=ide0-hd0

2. (HMP) drive_del target0

3. (SHELL) rm BACKUP.qcow2

v5: Address reviewer comments:
[01/07] qapi: Add BlockOperationType enum
Add comments for types. (Eric)
Remove "nbd-server-add", "passwd" and "set-io-throttle".

[02/07] block: Introduce op_blockers to BlockDriverState
Fix wording of commit message. (Kevin)
Fix spacing and compiler error for assertion. (Kevin) Allow NULL errp in bdrv_op_is_blocked. (Stefan)

[03/07] block: Replace in_use with operation blocker
Separate "source" and "target" for backup operation check. (Kevin)

[04/07] block: Add checks of blocker in block operations
Removed checks for nbd, passwd and throttle. (Paolo)

[05/07] block: Parse "backing" option to reference existing BDS
Moved backing reference code to bdrv_open_backing_file. (Kevin) Unblock and free blocker in bdrv_close unconditionally. (Kevin)

[06/07] qmp: add command 'blockdev-backup'
Check op blocker before creating target in qmp_drive_backup. Fix "." in EOL. (Eric)
Fix (Since 1.8). (Eric)
Fix comment indent, remove "mode" in hmp-commands.hx. (Kevin)

[07/07] block: Allow backup on referenced named BlockDriverState
Only allow "source" on backing referenced BDS. (Kevin)

Experimental for reviewers: the side by side diff against previous series:

http://goo.gl/x6s2cI

v4: Dropping RFC, this series tries to address the crashing cases with an added
safety mechanics.

In the first half of series, replace the in_use flag with an operation blocker list, and block all operations on named backing hd:

[01/07] qapi: Add BlockOperationType enum [02/07] block: Introduce op_blockers to BlockDriverState [03/07] block: Replace in_use with operation blocker [04/07] block: Add checks of blocker in block operations

The second half enables point in time snapshot over NBD, with fixes from last revision:

[05/07] block: Parse "backing" option to reference existing BDS
Fix NULL dereference if device not found.
[06/07] qmp: add command 'blockdev-backup'
Moved some checks into backup_run. (Paolo)
[07/07] block: Allow backup on referenced named BlockDriverState
New. Removes one specific blocker on backing referenced BDS, so we can start a backup job on it.

Fam Zheng (7):
qapi: Add BlockOperationType enum
block: Introduce op_blockers to BlockDriverState
block: Replace in_use with operation blocker
block: Add checks of blocker in block operations
block: Parse "backing" option to reference existing BDS
qmp: add command 'blockdev-backup'
block: Allow backup on referenced named BlockDriverState

 block-migration.c               |   8 ++-
 block.c                         | 120 +++++++++++++++++++++++++++++++++++-----
 block/backup.c                  |  21 +++++++
 block/mirror.c                  |   2 +-
 blockdev.c                      |  93 +++++++++++++++++++++++++++----
 blockjob.c                      |  12 ++--
 hw/block/dataplane/virtio-blk.c |  16 ++++--
 include/block/block.h           |  11 +++-
 include/block/block_int.h       |   9 ++-
 include/block/blockjob.h        |   3 +
 qapi-schema.json                |  98 ++++++++++++++++++++++++++++++++
 qmp-commands.hx                 |  44 +++++++++++++++
 12 files changed, 395 insertions(+), 42 deletions(-)
Signature · 1 line
1.8.4.2
author
#1 lore raw

PATCH v5 1/7 qapi: Add BlockOperationType enum

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

---
 qapi-schema.json | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
qapi-schema.json +49 -0
index 83fa485..c9e513c 100644
@@ -1440,6 +1440,55 @@
   'data': ['commit', 'stream', 'mirror', 'backup'] }
 
 ##
+# @BlockOperationType
+# Type of a block operation. (since 1.8)
+#
+# @backup-source: As a backup source. See the 'drive-backup' command.
+#
+# @backup-target: As a backup target. See the 'drive-backup' command.
+#
+# @change: See the 'change' command.
+#
+# @commit: See the 'block-commi' command.
+#
+# @dataplane: The virtio-blk dataplane feature.
+#
+# @drive-del: See the 'drive_del' HMP command.
+#
+# @eject: See the 'eject' command.
+#
+# @external-snapshot: See the 'blockdev-snapshot-sync' command.
+#
+# @internal-snapshot: See the 'blockdev-snapshot-internal-sync' command.
+#
+# @internal-snapshot-delete: See the 'blockdev-snapshot-delete-internal-sync' command.
+#
+# @mirror: See the 'drive-mirror' command.
+#
+# @resize: See the 'block-resize' command.
+#
+# @stream: See the 'block-stream' command.
+#
+# Since: 1.8
+##
+{ 'enum': 'BlockOpType',
+  'data': [
+    'backup-source',
+    'backup-target',
+    'change',
+    'commit',
+    'dataplane',
+    'drive-del',
+    'eject',
+    'external-snapshot',
+    'internal-snapshot',
+    'internal-snapshot-delete',
+    'mirror',
+    'resize',
+    'stream'
+] }
+
+##
 # @BlockJobInfo:
 #
 # Information about a long-running block device operation.
Signature · 1 line
1.8.4.2
#2 lore raw

Re: [Qemu-devel] [PATCH v5 1/7] qapi: Add BlockOperationType enum

Il 26/11/2013 05:05, Fam Zheng ha scritto:

qapi-schema.json line 1452

Quoted 8 lines
qapi-schema.json +8 -0
+#
+# @backup-source: As a backup source. See the 'drive-backup' command.
+#
+# @backup-target: As a backup target. See the 'drive-backup' command.
+#
+# @change: See the 'change' command.
+#
+# @commit: See the 'block-commi' command.

block-commit.

Paolo

Quoted 1 line

+#

author
#3 lore raw

PATCH v5 2/7 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 bdrv_set_in_use of now).

* 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                   | 57 +++++++++++++++++++++++++++++++++++++++++++++++
 include/block/block.h     |  6 +++++
 include/block/block_int.h |  5 +++++
 3 files changed, 68 insertions(+)
block.c +57 -0
index 382ea71..45410ef 100644
@@ -4425,6 +4425,63 @@ 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);
+    }
+}
+
 void bdrv_set_in_use(BlockDriverState *bs, int in_use)
 {
     assert(bs->in_use != in_use);
include/block/block.h +6 -0
index 3560deb..693d305 100644
@@ -403,6 +403,12 @@ 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);
+
 #ifdef CONFIG_LINUX_AIO
 int raw_get_aio_fd(BlockDriverState *bs);
 #else
include/block/block_int.h +5 -0
index 1666066..d8cef85 100644
@@ -230,6 +230,8 @@ struct BlockDriver {
     QLIST_ENTRY(BlockDriver) list;
 };
 
+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
@@ -308,6 +310,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.4.2
author
#4 lore raw

PATCH v5 3/7 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 no block for only specific types for now, i.e. a caller blocks all or none. So although the checks are type specific, the above changes can still be seen as identical in logic.

---
 block-migration.c               |  8 ++++++--
 block.c                         | 34 +++++++++++++++++++++-------------
 blockdev.c                      | 29 +++++++++++++++++++----------
 blockjob.c                      | 12 +++++++-----
 hw/block/dataplane/virtio-blk.c | 16 ++++++++++------
 include/block/block.h           |  2 --
 include/block/block_int.h       |  1 -
 include/block/blockjob.h        |  3 +++
 8 files changed, 66 insertions(+), 39 deletions(-)
block-migration.c +6 -2
index daf9ec1..12afcfa 100644
@@ -58,6 +58,8 @@ typedef struct BlkMigDevState {
     /* Protected by block migration lock.  */
     unsigned long *aio_bitmap;
     int64_t completed_sectors;
+
+    Error *blocker;
 } BlkMigDevState;
 
 typedef struct BlkMigBlock {
@@ -336,7 +338,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;
@@ -574,7 +577,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 +21 -13
index 45410ef..c2ab6d8 100644
@@ -1628,15 +1628,17 @@ 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 */
     pstrcpy(bs_dest->device_name, sizeof(bs_dest->device_name),
             bs_src->device_name);
+    memcpy(bs_dest->op_blockers, bs_src->op_blockers,
+           sizeof(bs_dest->op_blockers));
     bs_dest->list = bs_src->list;
 }
 
+static bool bdrv_op_blocker_is_empty(BlockDriverState *bs);
 /*
  * Swap bs contents for two image chains while they are live,
  * while keeping required fields on the BlockDriverState that is
@@ -1658,7 +1660,7 @@ void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old)
     assert(bs_new->dirty_bitmap == NULL);
     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));
 
@@ -1677,7 +1679,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));
 
@@ -1714,7 +1716,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);
 
     bdrv_close(bs);
@@ -1887,6 +1889,7 @@ int bdrv_commit(BlockDriverState *bs)
     int ret = 0;
     uint8_t *buf;
     char filename[PATH_MAX];
+    Error *local_err;
 
     if (!drv)
         return -ENOMEDIUM;
@@ -1895,7 +1898,9 @@ 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, &local_err) ||
+        bdrv_op_is_blocked(bs->backing_hd, BLOCK_OP_TYPE_COMMIT, &local_err)) {
+        error_free(local_err);
         return -EBUSY;
     }
 
@@ -2831,6 +2836,7 @@ int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs,
 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
 {
     BlockDriver *drv = bs->drv;
+    Error *local_err;
     int ret;
     if (!drv)
         return -ENOMEDIUM;
@@ -2838,8 +2844,10 @@ 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, &local_err)) {
+        error_free(local_err);
         return -EBUSY;
+    }
     ret = drv->bdrv_truncate(bs, offset);
     if (ret == 0) {
         ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
@@ -4482,15 +4490,15 @@ void bdrv_op_unblock_all(BlockDriverState *bs, Error *reason)
     }
 }
 
-void bdrv_set_in_use(BlockDriverState *bs, int in_use)
+static bool bdrv_op_blocker_is_empty(BlockDriverState *bs)
 {
-    assert(bs->in_use != in_use);
-    bs->in_use = in_use;
-}
+    bool ret = true;
+    int i;
 
-int bdrv_in_use(BlockDriverState *bs)
-{
-    return bs->in_use;
+    for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
+        ret = ret && QLIST_EMPTY(&bs->op_blockers[i]);
+    }
+    return ret;
 }
 
 void bdrv_iostatus_enable(BlockDriverState *bs)
blockdev.c +19 -10
index 330aa4a..1efa806 100644
@@ -1224,8 +1224,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;
     }
 
@@ -1441,10 +1441,10 @@ 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)) {
         error_set(errp, QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
         return;
@@ -1637,14 +1637,16 @@ int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
 {
     const char *id = qdict_get_str(qdict, "id");
     BlockDriverState *bs;
+    Error *local_err;
 
     bs = bdrv_find(id);
     if (!bs) {
         qerror_report(QERR_DEVICE_NOT_FOUND, id);
         return -1;
     }
-    if (bdrv_in_use(bs)) {
-        qerror_report(QERR_DEVICE_IN_USE, id);
+    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
+        error_report("%s", error_get_pretty(local_err));
+        error_free(local_err);
         return -1;
     }
 
@@ -1755,6 +1757,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) {
@@ -1795,6 +1801,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;
 
@@ -1881,8 +1891,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;
     }
 
@@ -2011,11 +2020,11 @@ 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;
     }
 
+
     flags = bs->open_flags | BDRV_O_RDWR;
     source = bs->backing_hd;
     if (!source && sync == MIRROR_SYNC_MODE_TOP) {
blockjob.c +7 -5
index 9e5fd5c..e198cb2 100644
@@ -41,13 +41,11 @@ void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs,
 {
     BlockJob *job;
 
-    if (bs->job || bdrv_in_use(bs)) {
+    if (bs->job) {
         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);
     job->driver        = driver;
     job->bs            = bs;
@@ -64,11 +62,14 @@ void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs,
         if (error_is_set(&local_err)) {
             bs->job = NULL;
             g_free(job);
-            bdrv_set_in_use(bs, 0);
             error_propagate(errp, local_err);
             return NULL;
         }
     }
+    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);
+
     return job;
 }
 
@@ -79,8 +80,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 +10 -6
index f2d7350..0a7b759 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 */
@@ -385,6 +388,7 @@ bool virtio_blk_data_plane_create(VirtIODevice *vdev, VirtIOBlkConf *blk,
 {
     VirtIOBlockDataPlane *s;
     int fd;
+    Error *local_err = NULL;
 
     *dataplane = NULL;
 
@@ -406,8 +410,9 @@ bool 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_report("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("%s", error_get_pretty(local_err));
+        error_free(local_err);
         return false;
     }
 
@@ -422,9 +427,8 @@ bool 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;
     return true;
@@ -437,7 +441,7 @@ 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);
     g_free(s);
 }
 
include/block/block.h +0 -2
index 693d305..173c09a 100644
@@ -400,8 +400,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 d8cef85..60edc80 100644
@@ -305,7 +305,6 @@ struct BlockDriverState {
     char device_name[32];
     HBitmap *dirty_bitmap;
     int refcnt;
-    int in_use; /* users other than guest access, eg. block migration */
     QTAILQ_ENTRY(BlockDriverState) list;
 
     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.4.2
author
#5 lore raw

PATCH v5 4/7 block: Add checks of blocker in block operations

In reply to Fam Zheng

Before operate on a BlockDriverState, respective types are checked against bs->op_blockers and it will error out if there's a blocker.

---
 blockdev.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
blockdev.c +17 -0
index 1efa806..cfb815f 100644
@@ -1001,6 +1001,11 @@ SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
         return NULL;
     }
 
+    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE,
+                           errp)) {
+        return NULL;
+    }
+
     ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
     if (error_is_set(&local_err)) {
         error_propagate(errp, local_err);
@@ -1098,6 +1103,10 @@ static void internal_snapshot_prepare(BlkTransactionState *common,
         return;
     }
 
+    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
+        return;
+    }
+
     if (!bdrv_is_inserted(bs)) {
         error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
         return;
@@ -1536,6 +1545,10 @@ void qmp_change_blockdev(const char *device, const char *filename,
         return;
     }
 
+    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
+        return;
+    }
+
     if (format) {
         drv = bdrv_find_whitelisted_format(format, bs->read_only);
         if (!drv) {
@@ -1684,6 +1697,10 @@ void qmp_block_resize(const char *device, int64_t size, Error **errp)
         return;
     }
 
+    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, errp)) {
+        return;
+    }
+
     if (size < 0) {
         error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
         return;
Signature · 1 line
1.8.4.2
#6 lore raw

Re: [Qemu-devel] [PATCH v5 4/7] block: Add checks of blocker in block operations

Il 26/11/2013 05:05, Fam Zheng ha scritto:

blockdev.c SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, · line 1007

Quoted 10 lines
blockdev.c +4 -0
@@ -1001,6 +1001,11 @@ SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
         return NULL;
     }
 
+    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE,
+                           errp)) {
+        return NULL;
+    }

Why not check in bdrv_snapshot_delete...

blockdev.c static void internal_snapshot_prepare(BlkTransactionState *common, · line 1108

Quoted 10 lines
blockdev.c +3 -0
     ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
     if (error_is_set(&local_err)) {
         error_propagate(errp, local_err);
@@ -1098,6 +1103,10 @@ static void internal_snapshot_prepare(BlkTransactionState *common,
         return;
     }
 
+    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
+        return;
+    }

... and bdrv_snapshot_create?

Quoted 16 lines
blockdev.c +4 -0
     if (!bdrv_is_inserted(bs)) {
         error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
         return;
@@ -1536,6 +1545,10 @@ void qmp_change_blockdev(const char *device, const char *filename,
         return;
     }
 
+    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
+        return;
+    }
+
     if (format) {
         drv = bdrv_find_whitelisted_format(format, bs->read_only);
         if (!drv) {

blockdev.c void qmp_block_resize(const char *device, int64_t size, Error **errp) · line 1706

Quoted 10 lines
blockdev.c +4 -0
@@ -1684,6 +1697,10 @@ void qmp_block_resize(const char *device, int64_t size, Error **errp)
         return;
     }
 
+    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, errp)) {
+        return;
+    }
+
     if (size < 0) {
         error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
         return;

This should be redundant since bdrv_truncate already checks.

Paolo

author
#7 lore raw

Re: [Qemu-devel] [PATCH v5 4/7] block: Add checks of blocker in block operations

Quoted 21 lines · Paolo Bonzini

Il 26/11/2013 05:05, Fam Zheng ha scritto:

blockdev.c SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, · line 1007

Quoted 10 lines
blockdev.c +4 -0
@@ -1001,6 +1001,11 @@ SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
          return NULL;
      }
 
+    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE,
+                           errp)) {
+        return NULL;
+    }

Why not check in bdrv_snapshot_delete...

Quoted 7 lines
blockdev.c +0 -0
      ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
      if (error_is_set(&local_err)) {
          error_propagate(errp, local_err);
@@ -1098,6 +1103,10 @@ static void internal_snapshot_prepare(BlkTransactionState *common,
          return;
      }
 
Quoted 6 lines

blockdev.c static void internal_snapshot_prepare(BlkTransactionState *common, · line 1108

Quoted 3 lines
blockdev.c +3 -0
+    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
+        return;
+    }

... and bdrv_snapshot_create?

OK.

Quoted 18 lines
Quoted 18 lines
blockdev.c +4 -0
      if (!bdrv_is_inserted(bs)) {
          error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
          return;
@@ -1536,6 +1545,10 @@ void qmp_change_blockdev(const char *device, const char *filename,
          return;
      }
 
+    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
+        return;
+    }
+
      if (format) {
          drv = bdrv_find_whitelisted_format(format, bs->read_only);
          if (!drv) {
@@ -1684,6 +1697,10 @@ void qmp_block_resize(const char *device, int64_t size, Error **errp)
          return;
      }
 
Quoted 11 lines

blockdev.c void qmp_block_resize(const char *device, int64_t size, Error **errp)

Quoted 8 lines
blockdev.c +4 -1
+    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, errp)) {
+        return;
+    }
+
      if (size < 0) {
          error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
          return;
--

This should be redundant since bdrv_truncate already checks.

It doesn't hurt and skips bdrv_drain_all if op is blocked.

Fam

author
#8 lore raw

PATCH v5 5/7 block: Parse "backing" option to reference existing BDS

In reply to Fam Zheng
---
 block.c                   | 31 ++++++++++++++++++++++++++++---
 block/mirror.c            |  2 +-
 include/block/block.h     |  3 ++-
 include/block/block_int.h |  3 +++
 4 files changed, 34 insertions(+), 5 deletions(-)
block.c +28 -3
index c2ab6d8..d633f2b 100644
@@ -959,18 +959,39 @@ fail:
 /*
  * Opens the backing file for a BlockDriverState if not yet open
  *
+ * If backing_bs is not NULL or empty, find the BDS by name and reference it as
+ * the backing_hd, in this case options is ignored.
+ *
  * options is a QDict of options to pass to the block drivers, or NULL for an
  * empty set of options. The reference to the QDict is transferred to this
  * function (even on failure), so if the caller intends to reuse the dictionary,
  * it needs to use QINCREF() before calling bdrv_file_open.
  */
-int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp)
+int bdrv_open_backing_file(BlockDriverState *bs, const char *backing_bs,
+                           QDict *options, Error **errp)
 {
     char backing_filename[PATH_MAX];
     int back_flags, ret;
     BlockDriver *back_drv = NULL;
     Error *local_err = NULL;
 
+    if (backing_bs && *backing_bs != '\0') {
+        bs->backing_hd = bdrv_find(backing_bs);
+        if (!bs->backing_hd) {
+            error_setg(errp, "Backing device not found: %s", backing_bs);
+            return -ENOENT;
+        }
+        bdrv_ref(bs->backing_hd);
+        assert(!bs->backing_blocker);
+        error_setg(&bs->backing_blocker,
+                   "device is used as backing hd of '%s'",
+                   bs->device_name);
+        bdrv_op_block_all(bs->backing_hd, bs->backing_blocker);
+        pstrcpy(bs->backing_file, sizeof(bs->backing_file),
+                bs->backing_hd->filename);
+        pstrcpy(bs->backing_format, sizeof(bs->backing_format),
+                bs->backing_hd->drv->format_name);
+    }
     if (bs->backing_hd != NULL) {
         QDECREF(options);
         return 0;
@@ -1166,9 +1187,11 @@ 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;
-
         qdict_extract_subqdict(options, &backing_options, "backing.");
-        ret = bdrv_open_backing_file(bs, backing_options, &local_err);
+        ret = bdrv_open_backing_file(bs, qdict_get_try_str(options, "backing"),
+                                     backing_options, &local_err);
+
+        qdict_del(options, "backing");
         if (ret < 0) {
             goto close_and_fail;
         }
@@ -1461,6 +1484,8 @@ void bdrv_close(BlockDriverState *bs)
 
     if (bs->drv) {
         if (bs->backing_hd) {
+            bdrv_op_unblock_all(bs->backing_hd, bs->backing_blocker);
+            error_free(bs->backing_blocker);
             bdrv_unref(bs->backing_hd);
             bs->backing_hd = NULL;
         }
block/mirror.c +1 -1
index 7b95acf..ce0103a 100644
@@ -508,7 +508,7 @@ static void mirror_complete(BlockJob *job, Error **errp)
     Error *local_err = NULL;
     int ret;
 
-    ret = bdrv_open_backing_file(s->target, NULL, &local_err);
+    ret = bdrv_open_backing_file(s->target, NULL, NULL, &local_err);
     if (ret < 0) {
         char backing_filename[PATH_MAX];
         bdrv_get_full_backing_filename(s->target, backing_filename,
include/block/block.h +2 -1
index 173c09a..e6ec03e 100644
@@ -157,7 +157,8 @@ int bdrv_parse_cache_flags(const char *mode, int *flags);
 int bdrv_parse_discard_flags(const char *mode, int *flags);
 int bdrv_file_open(BlockDriverState **pbs, const char *filename,
                    QDict *options, int flags, Error **errp);
-int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp);
+int bdrv_open_backing_file(BlockDriverState *bs, const char *backing_bs,
+                           QDict *options, Error **errp);
 int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
               int flags, BlockDriver *drv, Error **errp);
 BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue,
include/block/block_int.h +3 -0
index 60edc80..6db30c9 100644
@@ -316,6 +316,9 @@ struct BlockDriverState {
     BlockJob *job;
 
     QDict *options;
+
+    /* For backing reference, block the operations of named backing device */
+    Error *backing_blocker;
 };
 
 int get_tmp_filename(char *filename, int size);
Signature · 1 line
1.8.4.2
#9 lore raw

Re: [Qemu-devel] [PATCH v5 5/7] block: Parse "backing" option to reference existing BDS

Il 26/11/2013 05:05, Fam Zheng ha scritto:

block.c fail: · line 989

Quoted 12 lines
block.c +12 -0
+    if (backing_bs && *backing_bs != '\0') {
+        bs->backing_hd = bdrv_find(backing_bs);
+        if (!bs->backing_hd) {
+            error_setg(errp, "Backing device not found: %s", backing_bs);
+            return -ENOENT;
+        }
+        bdrv_ref(bs->backing_hd);
+        assert(!bs->backing_blocker);
+        error_setg(&bs->backing_blocker,
+                   "device is used as backing hd of '%s'",
+                   bs->device_name);
+        bdrv_op_block_all(bs->backing_hd, bs->backing_blocker);

Why should this blocker only apply to the "named backing file" case, and not to all backing files?

Paolo

Quoted 5 lines
block.c +5 -0
+        pstrcpy(bs->backing_file, sizeof(bs->backing_file),
+                bs->backing_hd->filename);
+        pstrcpy(bs->backing_format, sizeof(bs->backing_format),
+                bs->backing_hd->drv->format_name);
+    }
author
#10 lore raw

Re: [Qemu-devel] [PATCH v5 5/7] block: Parse "backing" option to reference existing BDS

Quoted 14 lines · Paolo Bonzini

Il 26/11/2013 05:05, Fam Zheng ha scritto:

block.c fail: · line 989

Quoted 12 lines
block.c +12 -0
+    if (backing_bs && *backing_bs != '\0') {
+        bs->backing_hd = bdrv_find(backing_bs);
+        if (!bs->backing_hd) {
+            error_setg(errp, "Backing device not found: %s", backing_bs);
+            return -ENOENT;
+        }
+        bdrv_ref(bs->backing_hd);
+        assert(!bs->backing_blocker);
+        error_setg(&bs->backing_blocker,
+                   "device is used as backing hd of '%s'",
+                   bs->device_name);
+        bdrv_op_block_all(bs->backing_hd, bs->backing_blocker);
Quoted 3 lines

Why should this blocker only apply to the "named backing file" case, and not to all backing files?

Good point, thanks.

Fam

author
#11 lore raw

PATCH v5 6/7 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 cad14c9..b620531 100644
@@ -338,6 +338,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);
@@ -363,6 +364,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'",
@@ -376,6 +395,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.4.2
author
#12 lore raw

PATCH v5 7/7 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 d633f2b..bb279d2 100644
@@ -987,6 +987,8 @@ int bdrv_open_backing_file(BlockDriverState *bs, const char *backing_bs,
                    "device is used as backing hd of '%s'",
                    bs->device_name);
         bdrv_op_block_all(bs->backing_hd, 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->filename);
         pstrcpy(bs->backing_format, sizeof(bs->backing_format),
Signature · 1 line
1.8.4.2