From nobody Tue Feb 10 23:53:08 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 151698279998251.94823073869145; Fri, 26 Jan 2018 08:06:39 -0800 (PST) Received: from localhost ([::1]:35686 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ef6WV-00054Z-5H for importer@patchew.org; Fri, 26 Jan 2018 11:06:39 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59968) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ef6UN-00030x-09 for qemu-devel@nongnu.org; Fri, 26 Jan 2018 11:04:28 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ef6UL-0002ax-Rw for qemu-devel@nongnu.org; Fri, 26 Jan 2018 11:04:27 -0500 Received: from mx1.redhat.com ([209.132.183.28]:59686) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ef6UJ-0002YV-7n; Fri, 26 Jan 2018 11:04:23 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8135E1752C2; Fri, 26 Jan 2018 16:04:22 +0000 (UTC) Received: from red.redhat.com (ovpn-124-3.rdu2.redhat.com [10.10.124.3]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8F4E75D720; Fri, 26 Jan 2018 16:04:19 +0000 (UTC) From: Eric Blake To: qemu-devel@nongnu.org Date: Fri, 26 Jan 2018 10:04:06 -0600 Message-Id: <20180126160411.4033-4-eblake@redhat.com> In-Reply-To: <20180126160411.4033-1-eblake@redhat.com> References: <20180126160411.4033-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Fri, 26 Jan 2018 16:04:22 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 3/8] qapi: add nbd-server-remove X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Block layer core" , Markus Armbruster , Max Reitz , Paolo Bonzini Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy Add command for removing an export. It is needed for cases when we don't want to keep the export after the operation on it was completed. The other example is a temporary node, created with blockdev-add. If we want to delete it we should firstly remove any corresponding NBD export. Signed-off-by: Vladimir Sementsov-Ogievskiy Message-Id: <20180119135719.24745-3-vsementsov@virtuozzo.com> [eblake: drop dead nb_clients code] Signed-off-by: Eric Blake --- qapi/block.json | 41 +++++++++++++++++++++++++++++++++++++++++ include/block/nbd.h | 1 + blockdev-nbd.c | 24 ++++++++++++++++++++++++ nbd/server.c | 13 +++++++++++++ 4 files changed, 79 insertions(+) diff --git a/qapi/block.json b/qapi/block.json index 353e3a45bdf..c6945240029 100644 --- a/qapi/block.json +++ b/qapi/block.json @@ -227,6 +227,47 @@ { 'command': 'nbd-server-add', 'data': {'device': 'str', '*name': 'str', '*writable': 'bool'} } +## +# @NbdServerRemoveMode: +# +# Mode for removing an NBD export. +# +# @safe: Remove export if there are no existing connections, fail otherwis= e. +# +# @hard: Drop all connections immediately and remove export. +# +# Potential additional modes to be added in the future: +# +# hide: Just hide export from new clients, leave existing connections as i= s. +# Remove export after all clients are disconnected. +# +# soft: Hide export from new clients, answer with ESHUTDOWN for all further +# requests from existing clients. +# +# 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: # diff --git a/include/block/nbd.h b/include/block/nbd.h index 978e4433664..ee74ec391a8 100644 --- a/include/block/nbd.h +++ b/include/block/nbd.h @@ -261,6 +261,7 @@ NBDExport *nbd_export_new(BlockDriverState *bs, off_t d= ev_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 **e= rrp); void nbd_export_get(NBDExport *exp); void nbd_export_put(NBDExport *exp); diff --git a/blockdev-nbd.c b/blockdev-nbd.c index 104789e521c..a9f79c6778e 100644 --- a/blockdev-nbd.c +++ b/blockdev-nbd.c @@ -189,6 +189,30 @@ void qmp_nbd_server_add(const char *device, bool has_n= ame, 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 =3D nbd_export_find(name); + if (exp =3D=3D NULL) { + error_setg(errp, "Export '%s' is not found", name); + return; + } + + if (!has_mode) { + mode =3D 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 6caa8d17be7..112e3f69dff 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -1177,6 +1177,19 @@ void nbd_export_close(NBDExport *exp) nbd_export_put(exp); } +void nbd_export_remove(NBDExport *exp, NbdServerRemoveMode mode, Error **e= rrp) +{ + if (mode =3D=3D NBD_SERVER_REMOVE_MODE_HARD || QTAILQ_EMPTY(&exp->clie= nts)) { + nbd_export_close(exp); + return; + } + + assert(mode =3D=3D NBD_SERVER_REMOVE_MODE_SAFE); + + error_setg(errp, "export '%s' still in use", exp->name); + error_append_hint(errp, "Use mode=3D'hard' to force client disconnect\= n"); +} + void nbd_export_get(NBDExport *exp) { assert(exp->refcount > 0); --=20 2.14.3