qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Max Reitz <mreitz@redhat.com>
To: qemu-block@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
	Markus Armbruster <armbru@redhat.com>,
	qemu-devel@nongnu.org, Stefan Hajnoczi <stefanha@redhat.com>,
	Max Reitz <mreitz@redhat.com>
Subject: [Qemu-devel] [PATCH v4 09/17] blockdev: Separate BB name management
Date: Wed, 16 Mar 2016 19:54:37 +0100	[thread overview]
Message-ID: <1458154485-32536-10-git-send-email-mreitz@redhat.com> (raw)
In-Reply-To: <1458154485-32536-1-git-send-email-mreitz@redhat.com>

Introduce separate functions (monitor_add_blk() and
monitor_remove_blk()) which set or unset a BB name. Since the name is
equivalent to the monitor's reference to a BB, adding a name the same as
declaring the BB to be monitor-owned and removing it revokes this
status, hence the function names.

Signed-off-by: Max Reitz <mreitz@redhat.com>
---
 block/block-backend.c          | 92 ++++++++++++++++++++++++++++--------------
 include/sysemu/block-backend.h |  2 +
 2 files changed, 63 insertions(+), 31 deletions(-)

diff --git a/block/block-backend.c b/block/block-backend.c
index 35206be..a5b950c 100644
--- a/block/block-backend.c
+++ b/block/block-backend.c
@@ -90,30 +90,17 @@ BlockBackend *blk_new(const char *name, Error **errp)
 {
     BlockBackend *blk;
 
-    assert(name && name[0]);
-    if (!id_wellformed(name)) {
-        error_setg(errp, "Invalid device name");
-        return NULL;
-    }
-    if (blk_by_name(name)) {
-        error_setg(errp, "Device with id '%s' already exists", name);
-        return NULL;
-    }
-    if (bdrv_find_node(name)) {
-        error_setg(errp,
-                   "Device name '%s' conflicts with an existing node name",
-                   name);
-        return NULL;
-    }
-
     blk = g_new0(BlockBackend, 1);
-    blk->name = g_strdup(name);
     blk->refcnt = 1;
     notifier_list_init(&blk->remove_bs_notifiers);
     notifier_list_init(&blk->insert_bs_notifiers);
 
     QTAILQ_INSERT_TAIL(&block_backends, blk, link);
-    QTAILQ_INSERT_TAIL(&monitor_block_backends, blk, monitor_link);
+
+    if (!monitor_add_blk(blk, name, errp)) {
+        blk_unref(blk);
+        return NULL;
+    }
 
     return blk;
 }
@@ -174,7 +161,10 @@ BlockBackend *blk_new_open(const char *name, const char *filename,
 
 static void blk_delete(BlockBackend *blk)
 {
+    monitor_remove_blk(blk);
+
     assert(!blk->refcnt);
+    assert(!blk->name);
     assert(!blk->dev);
     if (blk->bs) {
         blk_remove_bs(blk);
@@ -185,15 +175,7 @@ static void blk_delete(BlockBackend *blk)
         g_free(blk->root_state.throttle_group);
         throttle_group_unref(blk->root_state.throttle_state);
     }
-
-    /* Avoid double-remove after blk_hide_on_behalf_of_hmp_drive_del() */
-    if (blk->name[0]) {
-        QTAILQ_REMOVE(&monitor_block_backends, blk, monitor_link);
-    }
-    g_free(blk->name);
-
     QTAILQ_REMOVE(&block_backends, blk, link);
-
     drive_info_del(blk->legacy_dinfo);
     block_acct_cleanup(&blk->stats);
     g_free(blk);
@@ -280,13 +262,62 @@ BlockBackend *blk_next(BlockBackend *blk)
 }
 
 /*
+ * Add a BlockBackend into the list of backends referenced by the monitor, with
+ * the given @name acting as the handle for the monitor.
+ * Strictly for use by blockdev.c.
+ *
+ * @name must not be null or empty.
+ *
+ * Returns true on success and false on failure. In the latter case, an Error
+ * object is returned through @errp.
+ */
+bool monitor_add_blk(BlockBackend *blk, const char *name, Error **errp)
+{
+    assert(!blk->name);
+    assert(name && name[0]);
+
+    if (!id_wellformed(name)) {
+        error_setg(errp, "Invalid device name");
+        return false;
+    }
+    if (blk_by_name(name)) {
+        error_setg(errp, "Device with id '%s' already exists", name);
+        return false;
+    }
+    if (bdrv_find_node(name)) {
+        error_setg(errp,
+                   "Device name '%s' conflicts with an existing node name",
+                   name);
+        return false;
+    }
+
+    blk->name = g_strdup(name);
+    QTAILQ_INSERT_TAIL(&monitor_block_backends, blk, monitor_link);
+    return true;
+}
+
+/*
+ * Remove a BlockBackend from the list of backends referenced by the monitor.
+ * Strictly for use by blockdev.c.
+ */
+void monitor_remove_blk(BlockBackend *blk)
+{
+    if (!blk->name) {
+        return;
+    }
+
+    QTAILQ_REMOVE(&monitor_block_backends, blk, monitor_link);
+    g_free(blk->name);
+    blk->name = NULL;
+}
+
+/*
  * Return @blk's name, a non-null string.
- * Wart: the name is empty iff @blk has been hidden with
- * blk_hide_on_behalf_of_hmp_drive_del().
+ * Returns an empty string iff @blk is not referenced by the monitor.
  */
 const char *blk_name(BlockBackend *blk)
 {
-    return blk->name;
+    return blk->name ?: "";
 }
 
 /*
@@ -377,8 +408,7 @@ BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo)
  */
 void blk_hide_on_behalf_of_hmp_drive_del(BlockBackend *blk)
 {
-    QTAILQ_REMOVE(&monitor_block_backends, blk, monitor_link);
-    blk->name[0] = 0;
+    monitor_remove_blk(blk);
     if (blk->bs) {
         bdrv_make_anon(blk->bs);
     }
diff --git a/include/sysemu/block-backend.h b/include/sysemu/block-backend.h
index 84612ce..c906c20 100644
--- a/include/sysemu/block-backend.h
+++ b/include/sysemu/block-backend.h
@@ -71,6 +71,8 @@ void blk_remove_all_bs(void);
 const char *blk_name(BlockBackend *blk);
 BlockBackend *blk_by_name(const char *name);
 BlockBackend *blk_next(BlockBackend *blk);
+bool monitor_add_blk(BlockBackend *blk, const char *name, Error **errp);
+void monitor_remove_blk(BlockBackend *blk);
 
 BlockDriverState *blk_bs(BlockBackend *blk);
 void blk_remove_bs(BlockBackend *blk);
-- 
2.7.3

  parent reply	other threads:[~2016-03-16 18:55 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-16 18:54 [Qemu-devel] [PATCH v4 00/17] blockdev: Further BlockBackend work Max Reitz
2016-03-16 18:54 ` [Qemu-devel] [PATCH v4 01/17] monitor: Use BB list for BB name completion Max Reitz
2016-03-16 18:54 ` [Qemu-devel] [PATCH v4 02/17] block: Use blk_next() in block-backend.c Max Reitz
2016-03-16 18:54 ` [Qemu-devel] [PATCH v4 03/17] block: Add blk_commit_all() Max Reitz
2016-03-16 18:54 ` [Qemu-devel] [PATCH v4 04/17] block: Use blk_{commit, flush}_all() consistently Max Reitz
2016-03-16 18:54 ` [Qemu-devel] [PATCH v4 05/17] qapi: Drop QERR_UNKNOWN_BLOCK_FORMAT_FEATURE Max Reitz
2016-03-16 18:54 ` [Qemu-devel] [PATCH v4 06/17] block: Drop BB name from bad option error Max Reitz
2016-03-16 18:54 ` [Qemu-devel] [PATCH v4 07/17] blockdev: Rename blk_backends Max Reitz
2016-03-16 18:54 ` [Qemu-devel] [PATCH v4 08/17] blockdev: Add list of all BlockBackends Max Reitz
2016-03-16 18:54 ` Max Reitz [this message]
2016-03-16 18:54 ` [Qemu-devel] [PATCH v4 10/17] blockdev: Split monitor reference from BB creation Max Reitz
2016-03-16 18:54 ` [Qemu-devel] [PATCH v4 11/17] blockdev: Remove blk_hide_on_behalf_of_hmp_drive_del() Max Reitz
2016-03-16 18:54 ` [Qemu-devel] [PATCH v4 12/17] block: Move some bdrv_*_all() functions to BB Max Reitz
2016-03-16 18:54 ` [Qemu-devel] [PATCH v4 13/17] block: Add bdrv_next_monitor_owned() Max Reitz
2016-03-16 18:54 ` [Qemu-devel] [PATCH v4 14/17] block: Add blk_next_root_bs() Max Reitz
2016-03-16 18:54 ` [Qemu-devel] [PATCH v4 15/17] block: Rewrite bdrv_next() Max Reitz
2016-03-16 18:54 ` [Qemu-devel] [PATCH v4 16/17] block: Use bdrv_next() instead of bdrv_states Max Reitz
2016-03-16 18:54 ` [Qemu-devel] [PATCH v4 17/17] block: Remove bdrv_states list Max Reitz
2016-03-17 13:44 ` [Qemu-devel] [PATCH v4 00/17] blockdev: Further BlockBackend work Kevin Wolf
2016-03-23 19:06   ` Max Reitz

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=1458154485-32536-10-git-send-email-mreitz@redhat.com \
    --to=mreitz@redhat.com \
    --cc=armbru@redhat.com \
    --cc=kwolf@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).