From patchwork Thu Dec 14 15:22:45 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hannes Reinecke X-Patchwork-Id: 10112487 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id DFF0660327 for ; Thu, 14 Dec 2017 15:25:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D10312915D for ; Thu, 14 Dec 2017 15:25:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C5B2229722; Thu, 14 Dec 2017 15:25:52 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 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.wl.linuxfoundation.org (Postfix) with ESMTPS id 3E82D2915D for ; Thu, 14 Dec 2017 15:25:52 +0000 (UTC) Received: from localhost ([::1]:41567 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ePVOR-0005IO-HE for patchwork-qemu-devel@patchwork.kernel.org; Thu, 14 Dec 2017 10:25:51 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45923) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ePVLc-00031n-Ji for qemu-devel@nongnu.org; Thu, 14 Dec 2017 10:22:57 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ePVLY-00032R-GZ for qemu-devel@nongnu.org; Thu, 14 Dec 2017 10:22:56 -0500 Received: from mx2.suse.de ([195.135.220.15]:32862) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ePVLY-000310-7o for qemu-devel@nongnu.org; Thu, 14 Dec 2017 10:22:52 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 749CEAE9F; Thu, 14 Dec 2017 15:22:50 +0000 (UTC) From: Hannes Reinecke To: Paolo Bonzini Date: Thu, 14 Dec 2017 16:22:45 +0100 Message-Id: <20171214152246.17503-2-hare@suse.de> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20171214152246.17503-1-hare@suse.de> References: <20171214152246.17503-1-hare@suse.de> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 195.135.220.15 Subject: [Qemu-devel] [PATCH 1/2] block: implement shared block device count 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: Hannes Reinecke , qemu-devel@nongnu.org, Hannes Reinecke Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP By setting 'locking=off' when creating a 'file' format drive one can simulate a multipath setup. This patch adds infrastructure for tracking shared block devices. Signed-off-by: Hannes Reinecke --- block.c | 36 ++++++++++++++++++++++++++++++++++++ hw/scsi/scsi-disk.c | 6 ++++++ include/block/block.h | 2 ++ include/block/block_int.h | 2 ++ 4 files changed, 46 insertions(+) diff --git a/block.c b/block.c index 9a1a0d1e73..cf8b94252c 100644 --- a/block.c +++ b/block.c @@ -3801,6 +3801,42 @@ BlockDriverState *bdrv_find_node(const char *node_name) return NULL; } +void bdrv_find_shared(BlockDriverState *bs) +{ + BlockDriverState *tmp_bs; + int max_shared_no = 0; + unsigned long shared_bits = 0; + + if (!bs->filename) + return; + QTAILQ_FOREACH(tmp_bs, &graph_bdrv_states, node_list) { + if (tmp_bs == bs) + continue; + if (!strcmp(bs->filename, tmp_bs->filename)) { + if (tmp_bs->shared_no > 0) { + set_bit(tmp_bs->shared_no - 1, &shared_bits); + } + max_shared_no++; + } + } + if (max_shared_no > 0) { + int bit; + + QTAILQ_FOREACH(tmp_bs, &graph_bdrv_states, node_list) { + if (!strcmp(bs->filename, tmp_bs->filename) && !tmp_bs->shared_no) { + bit = find_first_zero_bit(&shared_bits, sizeof(unsigned long)); + tmp_bs->shared_no = bit + 1; + set_bit(bit, &shared_bits); + } + } + } +} + +int bdrv_get_shared(BlockDriverState *bs) +{ + return bs->shared_no; +} + /* Put this QMP function here so it can access the static graph_bdrv_states. */ BlockDeviceInfoList *bdrv_named_nodes_list(Error **errp) { diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c index 12431177a7..32c1d656b1 100644 --- a/hw/scsi/scsi-disk.c +++ b/hw/scsi/scsi-disk.c @@ -2333,6 +2333,7 @@ static void scsi_realize(SCSIDevice *dev, Error **errp) { SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev); Error *err = NULL; + int port_no; if (!s->qdev.conf.blk) { error_setg(errp, "drive property not set"); @@ -2396,6 +2397,11 @@ static void scsi_realize(SCSIDevice *dev, Error **errp) blk_set_guest_block_size(s->qdev.conf.blk, s->qdev.blocksize); blk_iostatus_enable(s->qdev.conf.blk); + bdrv_find_shared(blk_bs(s->qdev.conf.blk)); + port_no = bdrv_get_shared(blk_bs(s->qdev.conf.blk)); + if (port_no && !s->port_index) { + s->port_index = port_no; + } } static void scsi_hd_realize(SCSIDevice *dev, Error **errp) diff --git a/include/block/block.h b/include/block/block.h index c05cac57e5..5c03c1acfa 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -443,6 +443,8 @@ void bdrv_lock_medium(BlockDriverState *bs, bool locked); void bdrv_eject(BlockDriverState *bs, bool eject_flag); const char *bdrv_get_format_name(BlockDriverState *bs); BlockDriverState *bdrv_find_node(const char *node_name); +void bdrv_find_shared(BlockDriverState *bs); +int bdrv_get_shared(BlockDriverState *bs); BlockDeviceInfoList *bdrv_named_nodes_list(Error **errp); BlockDriverState *bdrv_lookup_bs(const char *device, const char *node_name, diff --git a/include/block/block_int.h b/include/block/block_int.h index a5482775ec..79c9c3a3aa 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -634,6 +634,8 @@ struct BlockDriverState { /* Flags honored during pwrite_zeroes (so far: BDRV_REQ_FUA, * BDRV_REQ_MAY_UNMAP) */ unsigned int supported_zero_flags; + /* Shared instance count */ + unsigned int shared_no; /* the following member gives a name to every node on the bs graph. */ char node_name[32];