From: Kevin Wolf <kwolf@redhat.com>
To: qemu-block@nongnu.org
Cc: kwolf@redhat.com, qemu-devel@nongnu.org, mreitz@redhat.com
Subject: [PATCH 20/29] block/export: Add block-export-del
Date: Mon, 7 Sep 2020 20:20:02 +0200 [thread overview]
Message-ID: <20200907182011.521007-21-kwolf@redhat.com> (raw)
In-Reply-To: <20200907182011.521007-1-kwolf@redhat.com>
Implement a new QMP command block-export-del and make nbd-server-remove
a wrapper around it.
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Max Reitz <mreitz@redhat.com>
---
qapi/block-export.json | 32 ++++++++++++++++++++-----
include/block/export.h | 1 +
include/block/nbd.h | 1 -
block/export/export.c | 43 +++++++++++++++++++++++++++++++++-
block/monitor/block-hmp-cmds.c | 4 ++--
blockdev-nbd.c | 25 +++++---------------
nbd/server.c | 14 -----------
7 files changed, 77 insertions(+), 43 deletions(-)
diff --git a/qapi/block-export.json b/qapi/block-export.json
index 9e05265b06..77a6b595e8 100644
--- a/qapi/block-export.json
+++ b/qapi/block-export.json
@@ -116,9 +116,9 @@
'data': 'NbdServerAddOptions', 'boxed': true }
##
-# @NbdServerRemoveMode:
+# @BlockExportRemoveMode:
#
-# Mode for removing an NBD export.
+# Mode for removing a block export.
#
# @safe: Remove export if there are no existing connections, fail otherwise.
#
@@ -134,16 +134,16 @@
#
# Since: 2.12
##
-{'enum': 'NbdServerRemoveMode', 'data': ['safe', 'hard']}
+{'enum': 'BlockExportRemoveMode', 'data': ['safe', 'hard']}
##
# @nbd-server-remove:
#
# Remove NBD export by name.
#
-# @name: Export name.
+# @name: Block export id.
#
-# @mode: Mode of command operation. See @NbdServerRemoveMode description.
+# @mode: Mode of command operation. See @BlockExportRemoveMode description.
# Default is 'safe'.
#
# Returns: error if
@@ -154,7 +154,7 @@
# Since: 2.12
##
{ 'command': 'nbd-server-remove',
- 'data': {'name': 'str', '*mode': 'NbdServerRemoveMode'} }
+ 'data': {'name': 'str', '*mode': 'BlockExportRemoveMode'} }
##
# @nbd-server-stop:
@@ -213,3 +213,23 @@
##
{ 'command': 'block-export-add',
'data': 'BlockExportOptions', 'boxed': true }
+
+##
+# @block-export-del:
+#
+# Request to remove a block export. This drops the user's reference to the
+# export, but the export may still stay around after this command returns until
+# the shutdown of the export has completed.
+#
+# @id: Block export id.
+#
+# @mode: Mode of command operation. See @BlockExportRemoveMode description.
+# Default is 'safe'.
+#
+# Returns: Error if the export is not found or @mode is 'safe' and the export
+# is still in use (e.g. by existing client connections)
+#
+# Since: 5.2
+##
+{ 'command': 'block-export-del',
+ 'data': { 'id': 'str', '*mode': 'BlockExportRemoveMode' } }
diff --git a/include/block/export.h b/include/block/export.h
index 4833947e89..ff54d35872 100644
--- a/include/block/export.h
+++ b/include/block/export.h
@@ -76,6 +76,7 @@ struct BlockExport {
};
BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp);
+BlockExport *blk_exp_find(const char *id);
void blk_exp_ref(BlockExport *exp);
void blk_exp_unref(BlockExport *exp);
void blk_exp_request_shutdown(BlockExport *exp);
diff --git a/include/block/nbd.h b/include/block/nbd.h
index 0b9f3e5d4e..a4dc1f9e54 100644
--- a/include/block/nbd.h
+++ b/include/block/nbd.h
@@ -337,7 +337,6 @@ int nbd_export_new(BlockExport *blk_exp, BlockDriverState *bs,
const char *bitmap, bool readonly, bool shared,
bool writethrough, Error **errp);
void nbd_export_set_on_eject_blk(BlockExport *exp, BlockBackend *blk);
-void nbd_export_remove(NBDExport *exp, NbdServerRemoveMode mode, Error **errp);
AioContext *nbd_export_aio_context(NBDExport *exp);
NBDExport *nbd_export_find(const char *name);
diff --git a/block/export/export.c b/block/export/export.c
index 42aac867e9..4af3b69186 100644
--- a/block/export/export.c
+++ b/block/export/export.c
@@ -29,7 +29,7 @@ static const BlockExportDriver *blk_exp_drivers[] = {
static QLIST_HEAD(, BlockExport) block_exports =
QLIST_HEAD_INITIALIZER(block_exports);
-static BlockExport *blk_exp_find(const char *id)
+BlockExport *blk_exp_find(const char *id)
{
BlockExport *exp;
@@ -136,12 +136,23 @@ void blk_exp_request_shutdown(BlockExport *exp)
AioContext *aio_context = exp->ctx;
aio_context_acquire(aio_context);
+
+ /*
+ * If the user doesn't own the export any more, it is already shutting
+ * down. We must not call .request_shutdown and decrease the refcount a
+ * second time.
+ */
+ if (!exp->user_owned) {
+ goto out;
+ }
+
exp->drv->request_shutdown(exp);
assert(exp->user_owned);
exp->user_owned = false;
blk_exp_unref(exp);
+out:
aio_context_release(aio_context);
}
@@ -192,3 +203,33 @@ void qmp_block_export_add(BlockExportOptions *export, Error **errp)
{
blk_exp_add(export, errp);
}
+
+void qmp_block_export_del(const char *id,
+ bool has_mode, BlockExportRemoveMode mode,
+ Error **errp)
+{
+ ERRP_GUARD();
+ BlockExport *exp;
+
+ exp = blk_exp_find(id);
+ if (exp == NULL) {
+ error_setg(errp, "Export '%s' is not found", id);
+ return;
+ }
+ if (!exp->user_owned) {
+ error_setg(errp, "Export '%s' is already shutting down", id);
+ return;
+ }
+
+ if (!has_mode) {
+ mode = BLOCK_EXPORT_REMOVE_MODE_SAFE;
+ }
+ if (mode == BLOCK_EXPORT_REMOVE_MODE_SAFE && exp->refcount > 1) {
+ error_setg(errp, "export '%s' still in use", exp->id);
+ error_append_hint(errp, "Use mode='hard' to force client "
+ "disconnect\n");
+ return;
+ }
+
+ blk_exp_request_shutdown(exp);
+}
diff --git a/block/monitor/block-hmp-cmds.c b/block/monitor/block-hmp-cmds.c
index db357cafcb..d15a2be827 100644
--- a/block/monitor/block-hmp-cmds.c
+++ b/block/monitor/block-hmp-cmds.c
@@ -476,8 +476,8 @@ void hmp_nbd_server_remove(Monitor *mon, const QDict *qdict)
bool force = qdict_get_try_bool(qdict, "force", false);
Error *err = NULL;
- /* Rely on NBD_SERVER_REMOVE_MODE_SAFE being the default */
- qmp_nbd_server_remove(name, force, NBD_SERVER_REMOVE_MODE_HARD, &err);
+ /* Rely on BLOCK_EXPORT_REMOVE_MODE_SAFE being the default */
+ qmp_nbd_server_remove(name, force, BLOCK_EXPORT_REMOVE_MODE_HARD, &err);
hmp_handle_error(mon, err);
}
diff --git a/blockdev-nbd.c b/blockdev-nbd.c
index 9efbaef8f7..4a9a1be571 100644
--- a/blockdev-nbd.c
+++ b/blockdev-nbd.c
@@ -307,31 +307,18 @@ fail:
}
void qmp_nbd_server_remove(const char *name,
- bool has_mode, NbdServerRemoveMode mode,
+ bool has_mode, BlockExportRemoveMode mode,
Error **errp)
{
- NBDExport *exp;
- AioContext *aio_context;
-
- if (!nbd_server) {
- error_setg(errp, "NBD server not running");
- return;
- }
+ BlockExport *exp;
- exp = nbd_export_find(name);
- if (exp == NULL) {
- error_setg(errp, "Export '%s' is not found", name);
+ exp = blk_exp_find(name);
+ if (exp && exp->drv->type != BLOCK_EXPORT_TYPE_NBD) {
+ error_setg(errp, "Block export '%s' is not an NBD export", name);
return;
}
- if (!has_mode) {
- mode = NBD_SERVER_REMOVE_MODE_SAFE;
- }
-
- aio_context = nbd_export_aio_context(exp);
- aio_context_acquire(aio_context);
- nbd_export_remove(exp, mode, errp);
- aio_context_release(aio_context);
+ qmp_block_export_del(name, has_mode, mode, errp);
}
void qmp_nbd_server_stop(Error **errp)
diff --git a/nbd/server.c b/nbd/server.c
index 22a1d66168..1a0e1db401 100644
--- a/nbd/server.c
+++ b/nbd/server.c
@@ -1669,20 +1669,6 @@ static void nbd_export_request_shutdown(BlockExport *blk_exp)
blk_exp_unref(&exp->common);
}
-void nbd_export_remove(NBDExport *exp, NbdServerRemoveMode mode, Error **errp)
-{
- ERRP_GUARD();
- if (mode == NBD_SERVER_REMOVE_MODE_HARD || QTAILQ_EMPTY(&exp->clients)) {
- nbd_export_request_shutdown(&exp->common);
- return;
- }
-
- assert(mode == NBD_SERVER_REMOVE_MODE_SAFE);
-
- error_setg(errp, "export '%s' still in use", exp->name);
- error_append_hint(errp, "Use mode='hard' to force client disconnect\n");
-}
-
static void nbd_export_delete(BlockExport *blk_exp)
{
NBDExport *exp = container_of(blk_exp, NBDExport, common);
--
2.25.4
next prev parent reply other threads:[~2020-09-07 18:30 UTC|newest]
Thread overview: 53+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-07 18:19 [PATCH 00/29] block/export: Add infrastructure and QAPI for block exports Kevin Wolf
2020-09-07 18:19 ` [PATCH 01/29] nbd: Remove unused nbd_export_get_blockdev() Kevin Wolf
2020-09-07 18:19 ` [PATCH 02/29] qapi: Create block-export module Kevin Wolf
2020-09-07 18:19 ` [PATCH 03/29] qapi: Rename BlockExport to BlockExportOptions Kevin Wolf
2020-09-07 18:19 ` [PATCH 04/29] block/export: Add BlockExport infrastructure and block-export-add Kevin Wolf
2020-09-10 10:16 ` Max Reitz
2020-09-07 18:19 ` [PATCH 05/29] qemu-storage-daemon: Use qmp_block_export_add() Kevin Wolf
2020-09-07 18:19 ` [PATCH 06/29] qemu-nbd: Use raw block driver for --offset Kevin Wolf
2020-09-07 18:19 ` [PATCH 07/29] block/export: Remove magic from block-export-add Kevin Wolf
2020-09-10 10:53 ` Max Reitz
2020-09-07 18:19 ` [PATCH 08/29] nbd: Add max-connections to nbd-server-start Kevin Wolf
2020-09-07 18:19 ` [PATCH 09/29] nbd: Add writethrough to block-export-add Kevin Wolf
2020-09-10 11:15 ` Max Reitz
2020-09-07 18:19 ` [PATCH 10/29] nbd: Remove NBDExport.close callback Kevin Wolf
2020-09-07 18:19 ` [PATCH 11/29] qemu-nbd: Use blk_exp_add() to create the export Kevin Wolf
2020-09-07 18:19 ` [PATCH 12/29] nbd/server: Simplify export shutdown Kevin Wolf
2020-09-07 18:19 ` [PATCH 13/29] block/export: Move refcount from NBDExport to BlockExport Kevin Wolf
2020-09-07 18:19 ` [PATCH 14/29] block/export: Move AioContext " Kevin Wolf
2020-09-10 11:52 ` Max Reitz
2020-09-07 18:19 ` [PATCH 15/29] block/export: Add node-name to BlockExportOptions Kevin Wolf
2020-09-10 12:35 ` Max Reitz
2020-09-07 18:19 ` [PATCH 16/29] block/export: Allocate BlockExport in blk_exp_add() Kevin Wolf
2020-09-16 10:56 ` Max Reitz
2020-09-07 18:19 ` [PATCH 17/29] block/export: Add blk_exp_close_all(_type) Kevin Wolf
2020-09-10 13:22 ` Max Reitz
2020-09-07 18:20 ` [PATCH 18/29] block/export: Add 'id' option to block-export-add Kevin Wolf
2020-09-10 13:26 ` Max Reitz
2020-09-07 18:20 ` [PATCH 19/29] block/export: Move strong user reference to block_exports Kevin Wolf
2020-09-10 13:33 ` Max Reitz
2020-09-10 13:36 ` Max Reitz
2020-09-07 18:20 ` Kevin Wolf [this message]
2020-09-07 18:20 ` [PATCH 21/29] block/export: Add BLOCK_EXPORT_DELETED event Kevin Wolf
2020-09-10 14:04 ` Max Reitz
2020-09-10 15:12 ` Max Reitz
2020-09-16 14:57 ` Max Reitz
2020-09-07 18:20 ` [PATCH 22/29] block/export: Move blk to BlockExport Kevin Wolf
2020-09-07 18:20 ` [PATCH 23/29] block/export: Create BlockBackend in blk_exp_add() Kevin Wolf
2020-09-10 15:09 ` Max Reitz
2020-09-07 18:20 ` [PATCH 24/29] block/export: Add query-block-exports Kevin Wolf
2020-09-10 15:10 ` Max Reitz
2020-09-07 18:20 ` [PATCH 25/29] block/export: Move writable to BlockExportOptions Kevin Wolf
2020-09-10 15:15 ` Max Reitz
2020-09-07 18:20 ` [PATCH 26/29] nbd: Merge nbd_export_new() and nbd_export_create() Kevin Wolf
2020-09-10 15:30 ` Max Reitz
2020-09-07 18:20 ` [PATCH 27/29] nbd: Deprecate nbd-server-add/remove Kevin Wolf
2020-09-10 15:34 ` Max Reitz
2020-09-23 16:19 ` Kevin Wolf
2020-09-07 18:20 ` [PATCH 28/29] iotests: Factor out qemu_tool_pipe_and_status() Kevin Wolf
2020-09-10 15:45 ` Max Reitz
2020-09-07 18:20 ` [PATCH 29/29] iotests: Test block-export-* QMP interface Kevin Wolf
2020-09-10 16:11 ` Max Reitz
2020-09-08 8:38 ` [PATCH 00/29] block/export: Add infrastructure and QAPI for block exports Markus Armbruster
2020-09-08 12:29 ` Kevin Wolf
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=20200907182011.521007-21-kwolf@redhat.com \
--to=kwolf@redhat.com \
--cc=mreitz@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.