From patchwork Tue Oct 13 13:48:51 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alberto Garcia X-Patchwork-Id: 529820 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 733C61402A8 for ; Wed, 14 Oct 2015 02:39:54 +1100 (AEDT) Received: from localhost ([::1]:35995 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zm00O-0001cF-3V for incoming@patchwork.ozlabs.org; Tue, 13 Oct 2015 09:52:40 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40879) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zlzxg-0007ch-P9 for qemu-devel@nongnu.org; Tue, 13 Oct 2015 09:49:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zlzxe-0004pC-EJ for qemu-devel@nongnu.org; Tue, 13 Oct 2015 09:49:52 -0400 Received: from smtp3.mundo-r.com ([212.51.32.191]:5664 helo=smtp4.mundo-r.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zlzxe-0004or-16; Tue, 13 Oct 2015 09:49:50 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A2B7DgA0Cx1W/5tjdVteHAEBAYMHgUKpKwEBAQEBAQUBgQ0BjyyGCoMTggp/AoE/PBABAQEBAQEBgQpBB4NfAQEEJ0kJED8SPBsZiDIBwSUBAQgihi6KUgeELgWHPYVRcogWjRqcCDgrgUpHHYFXboIvhEIBAQE X-IPAS-Result: A2B7DgA0Cx1W/5tjdVteHAEBAYMHgUKpKwEBAQEBAQUBgQ0BjyyGCoMTggp/AoE/PBABAQEBAQEBgQpBB4NfAQEEJ0kJED8SPBsZiDIBwSUBAQgihi6KUgeELgWHPYVRcogWjRqcCDgrgUpHHYFXboIvhEIBAQE X-IronPort-AV: E=Sophos;i="5.17,678,1437429600"; d="scan'208";a="32901420" Received: from fanzine.igalia.com ([91.117.99.155]) by smtp4.mundo-r.com with ESMTP; 13 Oct 2015 15:49:12 +0200 Received: from dsl-hkibrasgw4-50df50-201.dhcp.inet.fi ([80.223.80.201] helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim) id 1Zlzx1-0003xP-EV; Tue, 13 Oct 2015 15:49:11 +0200 Received: from berto by perseus.local with local (Exim 4.86) (envelope-from ) id 1Zlzwo-00068j-0k; Tue, 13 Oct 2015 16:48:58 +0300 From: Alberto Garcia To: qemu-devel@nongnu.org Date: Tue, 13 Oct 2015 16:48:51 +0300 Message-Id: <8dc2b9b2a8c5307eba7fafa2dee8f6e8ef17a26c.1444743418.git.berto@igalia.com> X-Mailer: git-send-email 2.6.1 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 212.51.32.191 Cc: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Markus Armbruster , Max Reitz , Stefan Hajnoczi Subject: [Qemu-devel] [PATCH 2/3] block: Add 'blockdev-del' QMP command X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This is the companion to 'blockdev-add'. It allows deleting a BlockBackend with its associated BlockDriverState tree, or a BlockDriverState that is not attached to any backend. In either case, the command fails if the reference count is greater than 1 or the BlockDriverState has any parents. Signed-off-by: Alberto Garcia --- blockdev.c | 42 ++++++++++++++++++++++++++++++++++++++++++ qapi/block-core.json | 21 +++++++++++++++++++++ qmp-commands.hx | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+) diff --git a/blockdev.c b/blockdev.c index 2360c1f..c448a0b 100644 --- a/blockdev.c +++ b/blockdev.c @@ -3402,6 +3402,48 @@ fail: qmp_output_visitor_cleanup(ov); } +void qmp_blockdev_del(const char *device, Error **errp) +{ + AioContext *aio_context; + BlockBackend *blk; + BlockDriverState *bs; + + blk = blk_by_name(device); + if (blk) { + bs = blk_bs(blk); + aio_context = blk_get_aio_context(blk); + } else { + bs = bdrv_find_node(device); + if (!bs) { + error_setg(errp, "Cannot find block device %s", device); + return; + } + blk = bs->blk; + aio_context = bdrv_get_aio_context(bs); + } + + aio_context_acquire(aio_context); + + if (bs && bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) { + goto out; + } + + if (blk_get_refcnt(blk) > 1 || + (bs && (bs->refcnt > 1 || !QLIST_EMPTY(&bs->parents)))) { + error_setg(errp, "Block device %s is being used", device); + goto out; + } + + if (blk) { + blk_unref(blk); + } else { + bdrv_unref(bs); + } + +out: + aio_context_release(aio_context); +} + BlockJobInfoList *qmp_query_block_jobs(Error **errp) { BlockJobInfoList *head = NULL, **p_next = &head; diff --git a/qapi/block-core.json b/qapi/block-core.json index 5f12af7..20897eb 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -1877,6 +1877,27 @@ { 'command': 'blockdev-add', 'data': { 'options': 'BlockdevOptions' } } ## +# @blockdev-del: +# +# Deletes a block device. The selected device can be either a block +# backend or a graph node. +# +# In the former case the backend will be destroyed, along with its +# inserted medium if there's any. +# +# In the latter case the node will be destroyed, along with the +# backend it is attached to if there's any. +# +# The command will fail if the selected block device is still being +# used. +# +# @device: Name of the block backend or the graph node to be deleted. +# +# Since: 2.5 +## +{ 'command': 'blockdev-del', 'data': { 'device': 'str' } } + +## # @blockdev-open-tray: # # Opens a block device's tray. If there is a block driver state tree inserted as diff --git a/qmp-commands.hx b/qmp-commands.hx index 4f03d11..b8b133e 100644 --- a/qmp-commands.hx +++ b/qmp-commands.hx @@ -3921,6 +3921,42 @@ Example (2): EQMP { + .name = "blockdev-del", + .args_type = "device:s", + .mhandler.cmd_new = qmp_marshal_blockdev_del, + }, + +SQMP +blockdev-del +------------ +Since 2.5 + +Deletes a block device. The selected device can be either a block +backend or a graph node. + +In the former case the backend will be destroyed, along with its +inserted medium if there's any. + +In the latter case the node will be destroyed, along with the +backend it is attached to if there's any. + +The command will fail if the selected block device is still being +used. + +Arguments: + +- "device": Identifier of the block device or graph node name (json-string) + +Example: + +-> { "execute": "blockdev-del", + "arguments": { "device": "virtio0" } + } +<- { "return": {} } + +EQMP + + { .name = "blockdev-open-tray", .args_type = "device:s,force:b?", .mhandler.cmd_new = qmp_marshal_blockdev_open_tray,