qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
To: qemu-block@nongnu.org
Cc: qemu-devel@nongnu.org, armbru@redhat.com, jsnow@redhat.com,
	vsementsov@virtuozzo.com, eblake@redhat.com, hreitz@redhat.com,
	kwolf@redhat.com, den@openvz.org, ktkhai@virtuozzo.com,
	igor@virtuozzo.com
Subject: [PATCH 12/14] qcow2: implement .bdrv_close_safe
Date: Mon,  7 Feb 2022 17:37:26 +0100	[thread overview]
Message-ID: <20220207163728.30362-13-vsementsov@virtuozzo.com> (raw)
In-Reply-To: <20220207163728.30362-1-vsementsov@virtuozzo.com>

Implement new API, so that qcow2 supports blockdev-del with
force=false.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
---
 include/block/block.h |  2 +
 block.c               |  4 +-
 block/qcow2.c         | 85 ++++++++++++++++++++++++++++++++++---------
 3 files changed, 72 insertions(+), 19 deletions(-)

diff --git a/include/block/block.h b/include/block/block.h
index 42d78a7a31..fbb5f3ff6d 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -672,6 +672,8 @@ void bdrv_disable_copy_on_read(BlockDriverState *bs);
 void bdrv_ref(BlockDriverState *bs);
 void bdrv_unref(BlockDriverState *bs);
 void bdrv_unref_child(BlockDriverState *parent, BdrvChild *child);
+int bdrv_unref_child_safe(BlockDriverState *parent, BdrvChild *child,
+                          Transaction *tran, Error **errp);
 int bdrv_try_unref(BlockDriverState *bs, Error **errp);
 BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs,
                              BlockDriverState *child_bs,
diff --git a/block.c b/block.c
index 187732c6f8..dd2ddedc86 100644
--- a/block.c
+++ b/block.c
@@ -3216,8 +3216,8 @@ static void bdrv_unset_inherits_from(BlockDriverState *root, BdrvChild *child,
  * When @tran is not NULL, first failure is returned and the action may be
  * rolled back.
  */
-static int bdrv_unref_child_safe(BlockDriverState *parent, BdrvChild *child,
-                                 Transaction *tran, Error **errp)
+int bdrv_unref_child_safe(BlockDriverState *parent, BdrvChild *child,
+                          Transaction *tran, Error **errp)
 {
     if (child == NULL) {
         return 0;
diff --git a/block/qcow2.c b/block/qcow2.c
index 8ad555feb7..a8f891ee34 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -2740,23 +2740,24 @@ static int qcow2_inactivate(BlockDriverState *bs)
     return qcow2_do_inactivate(bs, true, &error_abort);
 }
 
-static void qcow2_close(BlockDriverState *bs)
-{
-    BDRVQcow2State *s = bs->opaque;
-    qemu_vfree(s->l1_table);
-    /* else pre-write overlap checks in cache_destroy may crash */
-    s->l1_table = NULL;
+typedef struct Qcow2CloseState {
+    BlockDriverState *bs;
+    void *old_l1_table;
+} Qcow2CloseState;
 
-    if (!(s->flags & BDRV_O_INACTIVE)) {
-        qcow2_inactivate(bs);
-    }
+static void qcow2_close_commit(void *opaque)
+{
+    Qcow2CloseState *cs = opaque;
+    BlockDriverState *bs = cs->bs;
+    BDRVQcow2State *s = bs->opaque;
+
+    qemu_vfree(cs->old_l1_table);
 
     cache_clean_timer_del(bs);
     qcow2_cache_destroy(s->l2_table_cache);
     qcow2_cache_destroy(s->refcount_block_cache);
 
     qcrypto_block_free(s->crypto);
-    s->crypto = NULL;
     qapi_free_QCryptoBlockOpenOptions(s->crypto_opts);
 
     g_free(s->unknown_header_fields);
@@ -2766,15 +2767,65 @@ static void qcow2_close(BlockDriverState *bs)
     g_free(s->image_backing_file);
     g_free(s->image_backing_format);
 
-    if (has_data_file(bs)) {
-        bdrv_unref_child(bs, s->data_file);
-        s->data_file = NULL;
-    }
-
     qcow2_refcount_close(bs);
     qcow2_free_snapshots(bs);
 }
 
+static void qcow2_close_abort(void *opaque)
+{
+    Qcow2CloseState *cs = opaque;
+    BlockDriverState *bs = cs->bs;
+    BDRVQcow2State *s = bs->opaque;
+
+    s->l1_table = cs->old_l1_table;
+}
+
+static TransactionActionDrv qcow2_close_drv = {
+    .commit = qcow2_close_commit,
+    .abort = qcow2_close_abort,
+    .clean = g_free,
+};
+
+static int qcow2_close_safe(BlockDriverState *bs, Transaction *tran,
+                            Error **errp)
+{
+    BDRVQcow2State *s = bs->opaque;
+    int ret;
+    Qcow2CloseState *cs = g_new(Qcow2CloseState, 1);
+
+    *cs = (Qcow2CloseState) {
+        .bs = bs,
+        .old_l1_table = s->l1_table,
+    };
+
+    /* else pre-write overlap checks in cache_destroy may crash */
+    s->l1_table = NULL;
+    if (tran) {
+        tran_add(tran, &qcow2_close_drv, cs);
+    }
+
+    if (!(s->flags & BDRV_O_INACTIVE)) {
+        ret = qcow2_do_inactivate(bs, !tran, errp);
+        if (ret < 0 && tran) {
+            return ret;
+        }
+    }
+
+    if (has_data_file(bs)) {
+        ret = bdrv_unref_child_safe(bs, s->data_file, tran, errp);
+        if (ret < 0 && tran) {
+            return ret;
+        }
+    }
+
+    if (!tran) {
+        qcow2_close_commit(cs);
+        g_free(cs);
+    }
+
+    return 0;
+}
+
 static void coroutine_fn qcow2_co_invalidate_cache(BlockDriverState *bs,
                                                    Error **errp)
 {
@@ -2793,7 +2844,7 @@ static void coroutine_fn qcow2_co_invalidate_cache(BlockDriverState *bs,
     crypto = s->crypto;
     s->crypto = NULL;
 
-    qcow2_close(bs);
+    qcow2_close_safe(bs, NULL, &error_abort);
 
     memset(s, 0, sizeof(BDRVQcow2State));
     options = qdict_clone_shallow(bs->options);
@@ -6043,7 +6094,7 @@ BlockDriver bdrv_qcow2 = {
     .instance_size      = sizeof(BDRVQcow2State),
     .bdrv_probe         = qcow2_probe,
     .bdrv_open          = qcow2_open,
-    .bdrv_close         = qcow2_close,
+    .bdrv_close_safe    = qcow2_close_safe,
     .bdrv_reopen_prepare  = qcow2_reopen_prepare,
     .bdrv_reopen_commit   = qcow2_reopen_commit,
     .bdrv_reopen_commit_post = qcow2_reopen_commit_post,
-- 
2.31.1



  parent reply	other threads:[~2022-02-07 16:57 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-07 16:37 [PATCH 00/14] block: blockdev-del force=false Vladimir Sementsov-Ogievskiy
2022-02-07 16:37 ` [PATCH 01/14] block: refactor bdrv_remove_file_or_backing_child to bdrv_remove_child Vladimir Sementsov-Ogievskiy
2022-02-07 16:37 ` [PATCH 02/14] block: drop bdrv_detach_child() Vladimir Sementsov-Ogievskiy
2022-02-07 16:37 ` [PATCH 03/14] block: bdrv_refresh_perms(): allow external tran Vladimir Sementsov-Ogievskiy
2022-02-07 16:37 ` [PATCH 04/14] block: add bdrv_try_set_aio_context_tran transaction action Vladimir Sementsov-Ogievskiy
2022-02-07 16:37 ` [PATCH 05/14] block: merge bdrv_delete and bdrv_close Vladimir Sementsov-Ogievskiy
2022-02-07 16:37 ` [PATCH 06/14] block: bdrv_delete(): drop unnecessary zeroing Vladimir Sementsov-Ogievskiy
2022-02-07 16:37 ` [PATCH 07/14] block: implemet bdrv_try_unref() Vladimir Sementsov-Ogievskiy
2022-02-07 16:37 ` [PATCH 08/14] qapi/block-core: add 'force' argument to blockdev-del Vladimir Sementsov-Ogievskiy
2022-02-07 16:37 ` [PATCH 09/14] qcow2: qcow2_inactivate(): use qcow2_flush_caches() Vladimir Sementsov-Ogievskiy
2022-02-07 16:37 ` [PATCH 10/14] qcow2: qcow2_inactivate(): don't call qcow2_mark_clean() when RO Vladimir Sementsov-Ogievskiy
2022-02-07 16:37 ` [PATCH 11/14] qcow2: refactor qcow2_inactivate Vladimir Sementsov-Ogievskiy
2022-02-07 16:37 ` Vladimir Sementsov-Ogievskiy [this message]
2022-02-07 16:37 ` [PATCH 13/14] block/file-posix: implement .bdrv_close_safe Vladimir Sementsov-Ogievskiy
2022-02-07 16:37 ` [PATCH 14/14] iotests: add test for blockdev-del(force=false) Vladimir Sementsov-Ogievskiy

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=20220207163728.30362-13-vsementsov@virtuozzo.com \
    --to=vsementsov@virtuozzo.com \
    --cc=armbru@redhat.com \
    --cc=den@openvz.org \
    --cc=eblake@redhat.com \
    --cc=hreitz@redhat.com \
    --cc=igor@virtuozzo.com \
    --cc=jsnow@redhat.com \
    --cc=ktkhai@virtuozzo.com \
    --cc=kwolf@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /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).