From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
To: qemu-devel@nongnu.org, qemu-block@nongnu.org
Cc: armbru@redhat.com, dgilbert@redhat.com, pbonzini@redhat.com,
eblake@redhat.com, mreitz@redhat.com, kwolf@redhat.com,
vsementsov@virtuozzo.com, den@openvz.org
Subject: [Qemu-devel] [PATCH v2 3/6] qapi: add nbd-server-remove
Date: Thu, 7 Dec 2017 18:50:59 +0300 [thread overview]
Message-ID: <20171207155102.66622-4-vsementsov@virtuozzo.com> (raw)
In-Reply-To: <20171207155102.66622-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 | 18 ++++++++++++++++++
include/block/nbd.h | 3 ++-
blockdev-nbd.c | 29 ++++++++++++++++++++++++++++-
nbd/server.c | 25 ++++++++++++++++++++++---
4 files changed, 70 insertions(+), 5 deletions(-)
diff --git a/qapi/block.json b/qapi/block.json
index 503d4b287b..f83485c325 100644
--- a/qapi/block.json
+++ b/qapi/block.json
@@ -228,6 +228,24 @@
'data': {'device': 'str', '*name': 'str', '*writable': 'bool'} }
##
+# @nbd-server-remove:
+#
+# Remove NBD export by name.
+#
+# @name: Export name.
+#
+# @force: Whether active connections to the export should be closed. If this
+# parameter is false the export only becomes hidden from clients (new
+# connections are impossible), and would be automatically freed after
+# all clients are disconnected (default false).
+#
+# Returns: error if the server is not running or export is not found.
+#
+# Since: 2.12
+##
+{ 'command': 'nbd-server-remove', 'data': {'name': 'str', '*force': 'bool'} }
+
+##
# @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 113c707a5e..b89d116597 100644
--- a/include/block/nbd.h
+++ b/include/block/nbd.h
@@ -261,12 +261,13 @@ 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_hide(NBDExport *exp);
void nbd_export_get(NBDExport *exp);
void nbd_export_put(NBDExport *exp);
BlockBackend *nbd_export_get_blockdev(NBDExport *exp);
-NBDExport *nbd_export_find(const char *name);
+NBDExport *nbd_export_find(const char *name, bool include_hidden);
void nbd_export_set_name(NBDExport *exp, const char *name);
void nbd_export_set_description(NBDExport *exp, const char *description);
void nbd_export_close_all(void);
diff --git a/blockdev-nbd.c b/blockdev-nbd.c
index 46c885aa35..2495d38f2c 100644
--- a/blockdev-nbd.c
+++ b/blockdev-nbd.c
@@ -174,7 +174,7 @@ void qmp_nbd_server_add(const char *device, bool has_name, const char *name,
name = device;
}
- if (nbd_export_find(name)) {
+ if (nbd_export_find(name, true)) {
error_setg(errp, "NBD server already has export named '%s'", name);
return;
}
@@ -207,6 +207,33 @@ 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_force, bool force,
+ Error **errp)
+{
+ NBDExport *exp;
+
+ if (!nbd_server) {
+ error_setg(errp, "NBD server not running");
+ return;
+ }
+
+ exp = nbd_export_find(name, true);
+ if (exp == NULL) {
+ error_setg(errp, "Export '%s' is not found", name);
+ return;
+ }
+
+ if (!has_force) {
+ force = false;
+ }
+
+ if (force) {
+ nbd_export_close(exp);
+ } else {
+ nbd_export_hide(exp);
+ }
+}
+
void qmp_nbd_server_stop(Error **errp)
{
nbd_export_close_all();
diff --git a/nbd/server.c b/nbd/server.c
index e817c48087..d85f2dc747 100644
--- a/nbd/server.c
+++ b/nbd/server.c
@@ -78,6 +78,10 @@ struct NBDExport {
BlockBackend *eject_notifier_blk;
Notifier eject_notifier;
+
+ /* hidden export is not available for new connections and should be removed
+ * after last client disconnect */
+ bool hidden;
};
static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
@@ -300,7 +304,7 @@ static int nbd_negotiate_handle_export_name(NBDClient *client, uint32_t length,
trace_nbd_negotiate_handle_export_name_request(name);
- client->exp = nbd_export_find(name);
+ client->exp = nbd_export_find(name, false);
if (!client->exp) {
error_setg(errp, "export not found");
return -EINVAL;
@@ -429,7 +433,7 @@ static int nbd_negotiate_handle_info(NBDClient *client, uint32_t length,
}
assert(length == 0);
- exp = nbd_export_find(name);
+ exp = nbd_export_find(name, false);
if (!exp) {
return nbd_negotiate_send_rep_err(client->ioc, NBD_REP_ERR_UNKNOWN,
opt, errp, "export '%s' not present",
@@ -966,6 +970,9 @@ void nbd_client_put(NBDClient *client)
if (client->exp) {
QTAILQ_REMOVE(&client->exp->clients, client, next);
nbd_export_put(client->exp);
+ if (client->exp->hidden && QTAILQ_EMPTY(&client->exp->clients)) {
+ nbd_export_close(client->exp);
+ }
}
g_free(client);
}
@@ -1125,10 +1132,14 @@ fail:
return NULL;
}
-NBDExport *nbd_export_find(const char *name)
+NBDExport *nbd_export_find(const char *name, bool include_hidden)
{
NBDExport *exp;
QTAILQ_FOREACH(exp, &exports, next) {
+ if (!include_hidden && exp->hidden) {
+ continue;
+ }
+
if (strcmp(name, exp->name) == 0) {
return exp;
}
@@ -1177,6 +1188,14 @@ void nbd_export_close(NBDExport *exp)
nbd_export_put(exp);
}
+void nbd_export_hide(NBDExport *exp)
+{
+ exp->hidden = true;
+ if (QTAILQ_EMPTY(&exp->clients)) {
+ nbd_export_close(exp);
+ }
+}
+
void nbd_export_get(NBDExport *exp)
{
assert(exp->refcount > 0);
--
2.11.1
next prev parent reply other threads:[~2017-12-07 15:51 UTC|newest]
Thread overview: 49+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-12-07 15:50 [Qemu-devel] [PATCH v2 0/6] nbd export qmp interface Vladimir Sementsov-Ogievskiy
2017-12-07 15:50 ` [Qemu-devel] [PATCH v2 1/6] nbd/server: add additional assert to nbd_export_put Vladimir Sementsov-Ogievskiy
2018-01-10 16:33 ` Eric Blake
2017-12-07 15:50 ` [Qemu-devel] [PATCH v2 2/6] qapi: add name parameter to nbd-server-add Vladimir Sementsov-Ogievskiy
2017-12-08 17:33 ` Dr. David Alan Gilbert
2017-12-09 9:28 ` Vladimir Sementsov-Ogievskiy
2018-01-09 19:06 ` Eric Blake
2018-01-10 16:01 ` Dr. David Alan Gilbert
2018-01-09 19:28 ` [Qemu-devel] [PATCH v2 2.5/6] hmp: Add name parameter to nbd_server_add Eric Blake
2018-01-11 17:59 ` Dr. David Alan Gilbert
2018-01-11 19:50 ` Eric Blake
2018-01-11 20:11 ` Dr. David Alan Gilbert
2018-01-09 19:05 ` [Qemu-devel] [PATCH v2 2/6] qapi: add name parameter to nbd-server-add Eric Blake
2017-12-07 15:50 ` Vladimir Sementsov-Ogievskiy [this message]
2018-01-09 19:52 ` [Qemu-devel] [PATCH v2 3/6] qapi: add nbd-server-remove 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
2017-12-07 15:51 ` [Qemu-devel] [PATCH v2 4/6] iotest 147: add cases to test new @name parameter of nbd-server-add Vladimir Sementsov-Ogievskiy
2018-01-09 20:21 ` Eric Blake
2017-12-07 15:51 ` [Qemu-devel] [PATCH v2 5/6] iotests: implement QemuIoInteractive class Vladimir Sementsov-Ogievskiy
2018-01-09 20:34 ` Eric Blake
2018-01-12 11:56 ` Vladimir Sementsov-Ogievskiy
2018-01-12 16:48 ` Eric Blake
2017-12-07 15:51 ` [Qemu-devel] [PATCH v2 6/6] iotest 201: new test for qmp nbd-server-remove Vladimir Sementsov-Ogievskiy
2018-01-09 20:49 ` Eric Blake
2018-01-12 11:43 ` Vladimir Sementsov-Ogievskiy
2018-01-12 16:54 ` Eric Blake
2018-01-15 14:40 ` Vladimir Sementsov-Ogievskiy
2018-01-15 15:05 ` Eric Blake
2018-01-15 18:28 ` Vladimir Sementsov-Ogievskiy
2017-12-21 11:52 ` [Qemu-devel] ping Re: [PATCH v2 0/6] nbd export qmp interface Vladimir Sementsov-Ogievskiy
2017-12-21 15:28 ` [Qemu-devel] " Markus Armbruster
2017-12-21 18:32 ` Eric Blake
2017-12-22 8:53 ` Vladimir Sementsov-Ogievskiy
2017-12-22 15:47 ` Eric Blake
-- strict thread matches above, loose matches on Subject: below --
2018-01-18 18:11 Vladimir Sementsov-Ogievskiy
2018-01-18 18:11 ` [Qemu-devel] [PATCH v2 3/6] qapi: add nbd-server-remove Vladimir Sementsov-Ogievskiy
2018-01-18 22:13 ` Eric Blake
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=20171207155102.66622-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).