qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
To: qemu-devel@nongnu.org, qemu-block@nongnu.org
Cc: armbru@redhat.com, dgilbert@redhat.com, mreitz@redhat.com,
	kwolf@redhat.com, pbonzini@redhat.com, eblake@redhat.com,
	vsementsov@virtuozzo.com, den@openvz.org
Subject: [Qemu-devel] [PATCH v2 3/6] qapi: add nbd-server-remove
Date: Thu, 18 Jan 2018 21:11:20 +0300	[thread overview]
Message-ID: <20180118181123.37056-4-vsementsov@virtuozzo.com> (raw)
In-Reply-To: <20180118181123.37056-1-vsementsov@virtuozzo.com>

Add command for export removing. It is needed for cases when we
don't want to keep export after the operation on it was completed.
The other example is temporary node, created with blockdev-add.
If we want to delete it we should firstly remove corresponding
NBD export.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
---
 qapi/block.json     | 45 +++++++++++++++++++++++++++++++++++++++++++++
 include/block/nbd.h |  1 +
 blockdev-nbd.c      | 24 ++++++++++++++++++++++++
 nbd/server.c        | 21 +++++++++++++++++++++
 4 files changed, 91 insertions(+)

diff --git a/qapi/block.json b/qapi/block.json
index 353e3a45bd..ddf73932ce 100644
--- a/qapi/block.json
+++ b/qapi/block.json
@@ -228,6 +228,51 @@
   'data': {'device': 'str', '*name': 'str', '*writable': 'bool'} }
 
 ##
+# @NbdServerRemoveMode:
+#
+# Mode of NBD export removing.
+#
+# @safe: Remove export if there are no existing connections, fail otherwise.
+#
+# @hard: Drop all connections immediately and remove export.
+#
+# Postponed, not realized yet modes:
+#
+# hide: Just hide export from new clients, leave existing connections as is.
+#       Remove export after all clients are disconnected. nbd-server-remove
+#       with mode=soft or mode=hard may be called after nbd-server-remove
+#       with mode=hide.
+#
+# soft: Hide export from new clients, answer with ESHUTDOWN for all further
+#       requests from existing clients. Remove export after all clients are
+#       disconnected. nbd-server-requests with mode=hard may be called after
+#       nbd-server-remove with mode=soft
+#
+# Since: 2.12
+##
+{'enum': 'NbdServerRemoveMode', 'data': ['safe', 'hard']}
+
+##
+# @nbd-server-remove:
+#
+# Remove NBD export by name.
+#
+# @name: Export name.
+#
+# @mode: Mode of command operation. See @NbdServerRemoveMode description.
+#        Default is 'safe'.
+#
+# Returns: error if
+#            - the server is not running
+#            - export is not found
+#            - mode is 'safe' and there are existing connections
+#
+# Since: 2.12
+##
+{ 'command': 'nbd-server-remove',
+  'data': {'name': 'str', '*mode': 'NbdServerRemoveMode'} }
+
+##
 # @nbd-server-stop:
 #
 # Stop QEMU's embedded NBD server, and unregister all devices previously
diff --git a/include/block/nbd.h b/include/block/nbd.h
index 978e443366..ee74ec391a 100644
--- a/include/block/nbd.h
+++ b/include/block/nbd.h
@@ -261,6 +261,7 @@ NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset, off_t size,
                           bool writethrough, BlockBackend *on_eject_blk,
                           Error **errp);
 void nbd_export_close(NBDExport *exp);
+void nbd_export_remove(NBDExport *exp, NbdServerRemoveMode mode, Error **errp);
 void nbd_export_get(NBDExport *exp);
 void nbd_export_put(NBDExport *exp);
 
diff --git a/blockdev-nbd.c b/blockdev-nbd.c
index 104789e521..a9f79c6778 100644
--- a/blockdev-nbd.c
+++ b/blockdev-nbd.c
@@ -189,6 +189,30 @@ void qmp_nbd_server_add(const char *device, bool has_name, const char *name,
     nbd_export_put(exp);
 }
 
+void qmp_nbd_server_remove(const char *name,
+                           bool has_mode, NbdServerRemoveMode mode,
+                           Error **errp)
+{
+    NBDExport *exp;
+
+    if (!nbd_server) {
+        error_setg(errp, "NBD server not running");
+        return;
+    }
+
+    exp = nbd_export_find(name);
+    if (exp == NULL) {
+        error_setg(errp, "Export '%s' is not found", name);
+        return;
+    }
+
+    if (!has_mode) {
+        mode = NBD_SERVER_REMOVE_MODE_SAFE;
+    }
+
+    nbd_export_remove(exp, mode, errp);
+}
+
 void qmp_nbd_server_stop(Error **errp)
 {
     nbd_export_close_all();
diff --git a/nbd/server.c b/nbd/server.c
index 6cf2eeb2c1..fdb6be7016 100644
--- a/nbd/server.c
+++ b/nbd/server.c
@@ -1177,6 +1177,27 @@ void nbd_export_close(NBDExport *exp)
     nbd_export_put(exp);
 }
 
+void nbd_export_remove(NBDExport *exp, NbdServerRemoveMode mode, Error **errp)
+{
+    NBDClient *client;
+    int nb_clients = 0;
+
+    if (mode == NBD_SERVER_REMOVE_MODE_HARD || QTAILQ_EMPTY(&exp->clients)) {
+        nbd_export_close(exp);
+        return;
+    }
+
+    assert(mode == NBD_SERVER_REMOVE_MODE_SAFE);
+
+    QTAILQ_FOREACH(client, &exp->clients, next) {
+        nb_clients++;
+    }
+
+    error_setg(errp, "NBD export '%s' has %d active connection%s. To force "
+               "remove it (and hard disconnect clients) use mode='hard'",
+               exp->name, nb_clients, nb_clients == 1 ? "" : "s");
+}
+
 void nbd_export_get(NBDExport *exp)
 {
     assert(exp->refcount > 0);
-- 
2.11.1

  parent reply	other threads:[~2018-01-18 18:11 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-18 18:11 [Qemu-devel] [PATCH v2 0/6] nbd export qmp interface Vladimir Sementsov-Ogievskiy
2018-01-18 18:11 ` [Qemu-devel] [PATCH v2 1/6] qapi: add name parameter to nbd-server-add Vladimir Sementsov-Ogievskiy
2018-01-18 18:11 ` [Qemu-devel] [PATCH v2 2/6] hmp: add name parameter to nbd_server_add Vladimir Sementsov-Ogievskiy
2018-01-18 21:08   ` Eric Blake
2018-01-19  9:59     ` Vladimir Sementsov-Ogievskiy
2018-01-18 18:11 ` Vladimir Sementsov-Ogievskiy [this message]
2018-01-18 22:13   ` [Qemu-devel] [PATCH v2 3/6] qapi: add nbd-server-remove Eric Blake
2018-01-18 18:11 ` [Qemu-devel] [PATCH v2 4/6] iotest 147: add cases to test new @name parameter of nbd-server-add Vladimir Sementsov-Ogievskiy
2018-01-18 18:11 ` [Qemu-devel] [PATCH v2 5/6] iotests: implement QemuIoInteractive class Vladimir Sementsov-Ogievskiy
2018-01-18 18:11 ` [Qemu-devel] [PATCH v2 6/6] iotest 201: new test for qmp nbd-server-remove Vladimir Sementsov-Ogievskiy
2018-01-18 22:43   ` Eric Blake
2018-01-19 10:22     ` Vladimir Sementsov-Ogievskiy
2018-01-18 22:45 ` [Qemu-devel] [PATCH v2 0/6] nbd export qmp interface Eric Blake
2018-01-19 10:29   ` Kevin Wolf
  -- strict thread matches above, loose matches on Subject: below --
2017-12-07 15:50 Vladimir Sementsov-Ogievskiy
2017-12-07 15:50 ` [Qemu-devel] [PATCH v2 3/6] qapi: add nbd-server-remove Vladimir Sementsov-Ogievskiy
2018-01-09 19:52   ` Eric Blake
2018-01-12  9:47     ` Vladimir Sementsov-Ogievskiy
2018-01-15 15:09       ` Eric Blake
2018-01-15 17:47         ` Vladimir Sementsov-Ogievskiy
2018-01-17 13:36           ` Vladimir Sementsov-Ogievskiy
2018-01-17 15:23             ` Eric Blake
2018-01-17 15:51               ` Vladimir Sementsov-Ogievskiy
2018-01-17 16:03                 ` Eric Blake
2018-01-17 16:39                   ` Vladimir Sementsov-Ogievskiy
2018-01-26 15:05                     ` Dr. David Alan Gilbert
2018-02-06 15:29                       ` Vladimir Sementsov-Ogievskiy
2018-02-06 16:06                         ` Eric Blake
2018-02-06 17:54                           ` Vladimir Sementsov-Ogievskiy
2018-02-06 18:38                         ` Dr. David Alan Gilbert
2018-02-07  7:14                           ` Markus Armbruster

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=20180118181123.37056-4-vsementsov@virtuozzo.com \
    --to=vsementsov@virtuozzo.com \
    --cc=armbru@redhat.com \
    --cc=den@openvz.org \
    --cc=dgilbert@redhat.com \
    --cc=eblake@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=pbonzini@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).