qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: qemu-block@nongnu.org
Cc: kwolf@redhat.com, berto@igalia.com, qemu-devel@nongnu.org,
	jcody@redhat.com, armbru@redhat.com, mreitz@redhat.com,
	stefanha@redhat.com
Subject: [Qemu-devel] [PATCH 13/16] block: Implement bdrv_append() without bdrv_swap()
Date: Thu, 17 Sep 2015 15:48:17 +0200	[thread overview]
Message-ID: <1442497700-2536-14-git-send-email-kwolf@redhat.com> (raw)
In-Reply-To: <1442497700-2536-1-git-send-email-kwolf@redhat.com>

Remember all parent nodes and just change the pointers there instead of
swapping the contents of the BlockDriverState.

Handling of snapshot=on must be moved further down in bdrv_open()
because *pbs (which is the bs pointer in the BlockBackend) must already
be set before bdrv_append() is called. Otherwise bdrv_append() changes
the BB's pointer to the temporary snapshot, but bdrv_open() overwrites
it with the read-only original image.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 block.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 81 insertions(+), 28 deletions(-)

diff --git a/block.c b/block.c
index c196f83..98fc17c 100644
--- a/block.c
+++ b/block.c
@@ -1516,15 +1516,6 @@ static int bdrv_open_inherit(BlockDriverState **pbs, const char *filename,
 
     bdrv_refresh_filename(bs);
 
-    /* For snapshot=on, create a temporary qcow2 overlay. bs points to the
-     * temporary snapshot afterwards. */
-    if (snapshot_flags) {
-        ret = bdrv_append_temp_snapshot(bs, snapshot_flags, &local_err);
-        if (local_err) {
-            goto close_and_fail;
-        }
-    }
-
     /* Check if any unknown options were used */
     if (options && (qdict_size(options) != 0)) {
         const QDictEntry *entry = qdict_first(options);
@@ -1556,6 +1547,16 @@ static int bdrv_open_inherit(BlockDriverState **pbs, const char *filename,
 
     QDECREF(options);
     *pbs = bs;
+
+    /* For snapshot=on, create a temporary qcow2 overlay. bs points to the
+     * temporary snapshot afterwards. */
+    if (snapshot_flags) {
+        ret = bdrv_append_temp_snapshot(bs, snapshot_flags, &local_err);
+        if (local_err) {
+            goto close_and_fail;
+        }
+    }
+
     return 0;
 
 fail:
@@ -1999,20 +2000,6 @@ static void bdrv_move_feature_fields(BlockDriverState *bs_dest,
 
     bs_dest->enable_write_cache = bs_src->enable_write_cache;
 
-    /* i/o throttled req */
-    bs_dest->throttle_state     = bs_src->throttle_state,
-    bs_dest->io_limits_enabled  = bs_src->io_limits_enabled;
-    bs_dest->pending_reqs[0]    = bs_src->pending_reqs[0];
-    bs_dest->pending_reqs[1]    = bs_src->pending_reqs[1];
-    bs_dest->throttled_reqs[0]  = bs_src->throttled_reqs[0];
-    bs_dest->throttled_reqs[1]  = bs_src->throttled_reqs[1];
-    memcpy(&bs_dest->round_robin,
-           &bs_src->round_robin,
-           sizeof(bs_dest->round_robin));
-    memcpy(&bs_dest->throttle_timers,
-           &bs_src->throttle_timers,
-           sizeof(ThrottleTimers));
-
     /* r/w error */
     bs_dest->on_read_error      = bs_src->on_read_error;
     bs_dest->on_write_error     = bs_src->on_write_error;
@@ -2026,10 +2013,25 @@ static void bdrv_move_feature_fields(BlockDriverState *bs_dest,
 }
 
 /* Fields that only need to be swapped if the contents of BDSes is swapped
- * rather than pointers being changed in the parents. */
+ * rather than pointers being changed in the parents, and throttling fields
+ * because only bdrv_swap() messes with internals of throttling. */
 static void bdrv_move_reference_fields(BlockDriverState *bs_dest,
                                        BlockDriverState *bs_src)
 {
+    /* i/o throttled req */
+    bs_dest->throttle_state     = bs_src->throttle_state,
+    bs_dest->io_limits_enabled  = bs_src->io_limits_enabled;
+    bs_dest->pending_reqs[0]    = bs_src->pending_reqs[0];
+    bs_dest->pending_reqs[1]    = bs_src->pending_reqs[1];
+    bs_dest->throttled_reqs[0]  = bs_src->throttled_reqs[0];
+    bs_dest->throttled_reqs[1]  = bs_src->throttled_reqs[1];
+    memcpy(&bs_dest->round_robin,
+           &bs_src->round_robin,
+           sizeof(bs_dest->round_robin));
+    memcpy(&bs_dest->throttle_timers,
+           &bs_src->throttle_timers,
+           sizeof(ThrottleTimers));
+
     /* reference count */
     bs_dest->refcnt             = bs_src->refcnt;
 
@@ -2155,6 +2157,42 @@ void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old)
     bdrv_rebind(bs_old);
 }
 
+static void change_parent_backing_link(BlockDriverState *from,
+                                       BlockDriverState *to)
+{
+    BdrvChild *c, *next;
+
+    QLIST_FOREACH_SAFE(c, &from->parents, next_parent, next) {
+        assert(c->role != &child_backing);
+        c->bs = to;
+        QLIST_REMOVE(c, next_parent);
+        QLIST_INSERT_HEAD(&to->parents, c, next_parent);
+    }
+    if (from->blk) {
+        blk_set_bs(from->blk, to);
+        if (!to->device_list.tqe_prev) {
+            QTAILQ_INSERT_BEFORE(from, to, device_list);
+        }
+        QTAILQ_REMOVE(&bdrv_states, from, device_list);
+    }
+}
+
+static void swap_feature_fields(BlockDriverState *bs_top,
+                                BlockDriverState *bs_new)
+{
+    BlockDriverState tmp;
+
+    bdrv_move_feature_fields(&tmp, bs_top);
+    bdrv_move_feature_fields(bs_top, bs_new);
+    bdrv_move_feature_fields(bs_new, &tmp);
+
+    assert(!bs_new->io_limits_enabled);
+    if (bs_top->io_limits_enabled) {
+        bdrv_io_limits_enable(bs_new, throttle_group_get_name(bs_top));
+        bdrv_io_limits_disable(bs_top);
+    }
+}
+
 /*
  * Add new bs contents at the top of an image chain while the chain is
  * live, while keeping required fields on the top layer.
@@ -2165,14 +2203,29 @@ void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old)
  * bs_new must not be attached to a BlockBackend.
  *
  * This function does not create any image files.
+ *
+ * bdrv_append() takes ownership of a bs_new reference and unrefs it because
+ * that's what the callers commonly need. bs_new will be referenced by the old
+ * parents of bs_top after bdrv_append() returns. If the caller needs to keep a
+ * reference of its own, it must call bdrv_ref().
  */
 void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top)
 {
-    bdrv_swap(bs_new, bs_top);
+    assert(!bdrv_requests_pending(bs_top));
+    assert(!bdrv_requests_pending(bs_new));
+
+    bdrv_ref(bs_top);
+    change_parent_backing_link(bs_top, bs_new);
+
+    /* Some fields always stay on top of the backing file chain */
+    swap_feature_fields(bs_top, bs_new);
+
+    bdrv_set_backing_hd(bs_new, bs_top);
+    bdrv_unref(bs_top);
 
-    /* The contents of 'tmp' will become bs_top, as we are
-     * swapping bs_new and bs_top contents. */
-    bdrv_set_backing_hd(bs_top, bs_new);
+    /* bs_new is now referenced by its new parents, we don't need the
+     * additional reference any more. */
+    bdrv_unref(bs_new);
 }
 
 static void bdrv_delete(BlockDriverState *bs)
-- 
1.8.3.1

  parent reply	other threads:[~2015-09-17 13:49 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-17 13:48 [Qemu-devel] [PATCH 00/16] block: Get rid of bdrv_swap() Kevin Wolf
2015-09-17 13:48 ` [Qemu-devel] [PATCH 01/16] block: Introduce BDS.file_child Kevin Wolf
2015-09-22 17:14   ` Max Reitz
2015-09-22 17:17     ` Max Reitz
2015-09-23 14:26   ` Alberto Garcia
2015-09-17 13:48 ` [Qemu-devel] [PATCH 02/16] vmdk: Use BdrvChild instead of BDS for references to extents Kevin Wolf
2015-09-22 17:45   ` Max Reitz
2015-09-23 13:23   ` Alberto Garcia
2015-09-17 13:48 ` [Qemu-devel] [PATCH 03/16] blkverify: Convert s->test_file to BdrvChild Kevin Wolf
2015-09-22 17:51   ` Max Reitz
2015-09-23 13:01   ` Alberto Garcia
2015-09-23 13:58     ` Kevin Wolf
2015-09-23 14:05       ` Alberto Garcia
2015-09-23 14:11   ` Alberto Garcia
2015-09-17 13:48 ` [Qemu-devel] [PATCH 04/16] quorum: Convert " Kevin Wolf
2015-09-22 17:57   ` Max Reitz
2015-09-23 12:48   ` Alberto Garcia
2015-09-17 13:48 ` [Qemu-devel] [PATCH 05/16] block: Convert bs->file " Kevin Wolf
2015-09-22 18:36   ` Max Reitz
2015-09-23 13:13     ` Kevin Wolf
2015-09-23 14:54   ` Alberto Garcia
2015-09-25 14:09   ` Alberto Garcia
2015-09-17 13:48 ` [Qemu-devel] [PATCH 06/16] block: Remove bdrv_open_image() Kevin Wolf
2015-09-22 18:38   ` Max Reitz
2015-09-23 13:10   ` Alberto Garcia
2015-09-17 13:48 ` [Qemu-devel] [PATCH 07/16] block: Convert bs->backing_hd to BdrvChild Kevin Wolf
2015-09-22 19:21   ` Max Reitz
2015-09-17 13:48 ` [Qemu-devel] [PATCH 08/16] block: Manage backing file references in bdrv_set_backing_hd() Kevin Wolf
2015-09-23 15:22   ` Max Reitz
2015-09-28 12:29   ` Alberto Garcia
2015-09-17 13:48 ` [Qemu-devel] [PATCH 09/16] block: Split bdrv_move_feature_fields() Kevin Wolf
2015-09-23 16:36   ` Max Reitz
2015-09-29 13:37     ` Kevin Wolf
2015-09-30 14:51   ` Max Reitz
2015-09-17 13:48 ` [Qemu-devel] [PATCH 10/16] block/io: Make bdrv_requests_pending() public Kevin Wolf
2015-09-23 15:40   ` Max Reitz
2015-09-29 12:29     ` Kevin Wolf
2015-09-28 12:32   ` Alberto Garcia
2015-09-17 13:48 ` [Qemu-devel] [PATCH 11/16] block-backend: Add blk_set_bs() Kevin Wolf
2015-09-23 15:46   ` Max Reitz
2015-09-23 15:51     ` Kevin Wolf
2015-09-17 13:48 ` [Qemu-devel] [PATCH 12/16] block: Introduce parents list Kevin Wolf
2015-09-23 16:39   ` Max Reitz
2015-09-28 13:09   ` Alberto Garcia
2015-09-29 12:21     ` Kevin Wolf
2015-09-29 14:00       ` Alberto Garcia
2015-09-17 13:48 ` Kevin Wolf [this message]
2015-09-18 11:45   ` [Qemu-devel] [PATCH 13/16] block: Implement bdrv_append() without bdrv_swap() Alberto Garcia
2015-09-18 12:24     ` Kevin Wolf
2015-09-18 13:31       ` Alberto Garcia
2015-09-18 14:23         ` Kevin Wolf
2015-09-23 16:36   ` Max Reitz
2015-09-29 13:51     ` Kevin Wolf
2015-09-30 14:45       ` Max Reitz
2015-09-30 15:33         ` Kevin Wolf
2015-09-17 13:48 ` [Qemu-devel] [PATCH 14/16] blockjob: Store device name at job creation Kevin Wolf
2015-09-23 16:46   ` Max Reitz
2015-09-17 13:48 ` [Qemu-devel] [PATCH 15/16] block: Add and use bdrv_replace_in_backing_chain() Kevin Wolf
2015-09-23 17:08   ` Max Reitz
2015-09-29 15:22     ` Kevin Wolf
2015-09-30 14:50       ` Max Reitz
2015-09-30 14:50   ` Max Reitz
2015-09-17 13:48 ` [Qemu-devel] [PATCH 16/16] block: Remove bdrv_swap() Kevin Wolf
2015-09-23 17:09   ` Max Reitz
2015-09-28 13:24   ` Alberto Garcia
2015-09-18 11:03 ` [Qemu-devel] [PATCH 00/16] block: Get rid of bdrv_swap() Alberto Garcia
2015-09-22 10:07   ` Kevin Wolf
2015-09-24 18:32 ` Alberto Garcia

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1442497700-2536-14-git-send-email-kwolf@redhat.com \
    --to=kwolf@redhat.com \
    --cc=armbru@redhat.com \
    --cc=berto@igalia.com \
    --cc=jcody@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).