From patchwork Thu Mar 17 15:56:24 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wolf X-Patchwork-Id: 8612651 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 1CC5CC0554 for ; Thu, 17 Mar 2016 16:03:22 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 5ACC920379 for ; Thu, 17 Mar 2016 16:03:21 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 59AB920351 for ; Thu, 17 Mar 2016 16:03:20 +0000 (UTC) Received: from localhost ([::1]:36182 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1agaON-0007DJ-Ld for patchwork-qemu-devel@patchwork.kernel.org; Thu, 17 Mar 2016 12:03:19 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37780) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1agaIO-00027P-P5 for qemu-devel@nongnu.org; Thu, 17 Mar 2016 11:57:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1agaIN-0001kx-Da for qemu-devel@nongnu.org; Thu, 17 Mar 2016 11:57:08 -0400 Received: from mx1.redhat.com ([209.132.183.28]:37801) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1agaIH-0001h1-Ka; Thu, 17 Mar 2016 11:57:01 -0400 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (Postfix) with ESMTPS id 18E16D7D55; Thu, 17 Mar 2016 15:57:01 +0000 (UTC) Received: from noname.redhat.com (ovpn-116-71.ams2.redhat.com [10.36.116.71]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u2HFuj1H010106; Thu, 17 Mar 2016 11:57:00 -0400 From: Kevin Wolf To: qemu-block@nongnu.org Date: Thu, 17 Mar 2016 16:56:24 +0100 Message-Id: <1458230202-29136-12-git-send-email-kwolf@redhat.com> In-Reply-To: <1458230202-29136-1-git-send-email-kwolf@redhat.com> References: <1458230202-29136-1-git-send-email-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Thu, 17 Mar 2016 15:57:01 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Cc: kwolf@redhat.com, qemu-devel@nongnu.org Subject: [Qemu-devel] [PULL 11/29] blockdev: Separate BB name management 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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Max Reitz Introduce separate functions (monitor_add_blk() and monitor_remove_blk()) which set or unset a BB name. Since the name is equivalent to the monitor's reference to a BB, adding a name the same as declaring the BB to be monitor-owned and removing it revokes this status, hence the function names. Signed-off-by: Max Reitz Signed-off-by: Kevin Wolf --- block/block-backend.c | 92 ++++++++++++++++++++++++++++-------------- include/sysemu/block-backend.h | 2 + 2 files changed, 63 insertions(+), 31 deletions(-) diff --git a/block/block-backend.c b/block/block-backend.c index 35206be..a5b950c 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -90,30 +90,17 @@ BlockBackend *blk_new(const char *name, Error **errp) { BlockBackend *blk; - assert(name && name[0]); - if (!id_wellformed(name)) { - error_setg(errp, "Invalid device name"); - return NULL; - } - if (blk_by_name(name)) { - error_setg(errp, "Device with id '%s' already exists", name); - return NULL; - } - if (bdrv_find_node(name)) { - error_setg(errp, - "Device name '%s' conflicts with an existing node name", - name); - return NULL; - } - blk = g_new0(BlockBackend, 1); - blk->name = g_strdup(name); blk->refcnt = 1; notifier_list_init(&blk->remove_bs_notifiers); notifier_list_init(&blk->insert_bs_notifiers); QTAILQ_INSERT_TAIL(&block_backends, blk, link); - QTAILQ_INSERT_TAIL(&monitor_block_backends, blk, monitor_link); + + if (!monitor_add_blk(blk, name, errp)) { + blk_unref(blk); + return NULL; + } return blk; } @@ -174,7 +161,10 @@ BlockBackend *blk_new_open(const char *name, const char *filename, static void blk_delete(BlockBackend *blk) { + monitor_remove_blk(blk); + assert(!blk->refcnt); + assert(!blk->name); assert(!blk->dev); if (blk->bs) { blk_remove_bs(blk); @@ -185,15 +175,7 @@ static void blk_delete(BlockBackend *blk) g_free(blk->root_state.throttle_group); throttle_group_unref(blk->root_state.throttle_state); } - - /* Avoid double-remove after blk_hide_on_behalf_of_hmp_drive_del() */ - if (blk->name[0]) { - QTAILQ_REMOVE(&monitor_block_backends, blk, monitor_link); - } - g_free(blk->name); - QTAILQ_REMOVE(&block_backends, blk, link); - drive_info_del(blk->legacy_dinfo); block_acct_cleanup(&blk->stats); g_free(blk); @@ -280,13 +262,62 @@ BlockBackend *blk_next(BlockBackend *blk) } /* + * Add a BlockBackend into the list of backends referenced by the monitor, with + * the given @name acting as the handle for the monitor. + * Strictly for use by blockdev.c. + * + * @name must not be null or empty. + * + * Returns true on success and false on failure. In the latter case, an Error + * object is returned through @errp. + */ +bool monitor_add_blk(BlockBackend *blk, const char *name, Error **errp) +{ + assert(!blk->name); + assert(name && name[0]); + + if (!id_wellformed(name)) { + error_setg(errp, "Invalid device name"); + return false; + } + if (blk_by_name(name)) { + error_setg(errp, "Device with id '%s' already exists", name); + return false; + } + if (bdrv_find_node(name)) { + error_setg(errp, + "Device name '%s' conflicts with an existing node name", + name); + return false; + } + + blk->name = g_strdup(name); + QTAILQ_INSERT_TAIL(&monitor_block_backends, blk, monitor_link); + return true; +} + +/* + * Remove a BlockBackend from the list of backends referenced by the monitor. + * Strictly for use by blockdev.c. + */ +void monitor_remove_blk(BlockBackend *blk) +{ + if (!blk->name) { + return; + } + + QTAILQ_REMOVE(&monitor_block_backends, blk, monitor_link); + g_free(blk->name); + blk->name = NULL; +} + +/* * Return @blk's name, a non-null string. - * Wart: the name is empty iff @blk has been hidden with - * blk_hide_on_behalf_of_hmp_drive_del(). + * Returns an empty string iff @blk is not referenced by the monitor. */ const char *blk_name(BlockBackend *blk) { - return blk->name; + return blk->name ?: ""; } /* @@ -377,8 +408,7 @@ BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo) */ void blk_hide_on_behalf_of_hmp_drive_del(BlockBackend *blk) { - QTAILQ_REMOVE(&monitor_block_backends, blk, monitor_link); - blk->name[0] = 0; + monitor_remove_blk(blk); if (blk->bs) { bdrv_make_anon(blk->bs); } diff --git a/include/sysemu/block-backend.h b/include/sysemu/block-backend.h index 84612ce..c906c20 100644 --- a/include/sysemu/block-backend.h +++ b/include/sysemu/block-backend.h @@ -71,6 +71,8 @@ void blk_remove_all_bs(void); const char *blk_name(BlockBackend *blk); BlockBackend *blk_by_name(const char *name); BlockBackend *blk_next(BlockBackend *blk); +bool monitor_add_blk(BlockBackend *blk, const char *name, Error **errp); +void monitor_remove_blk(BlockBackend *blk); BlockDriverState *blk_bs(BlockBackend *blk); void blk_remove_bs(BlockBackend *blk);