Received: from eggs.gnu.org ([2001:4830:134:3::10]:34565) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VQcC2-0000p9-7N for qemu-devel@nongnu.org; Mon, 30 Sep 2013 08:03:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VQcBw-0005lL-5U for qemu-devel@nongnu.org; Mon, 30 Sep 2013 08:03:14 -0400 Received: from mx1.redhat.com ([209.132.183.28]:14760) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VQcBv-0005l4-T3 for qemu-devel@nongnu.org; Mon, 30 Sep 2013 08:03:08 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r8UC37Fc001307 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 30 Sep 2013 08:03:07 -0400 From: Fam Zheng Date: Mon, 30 Sep 2013 20:02:52 +0800 Message-Id: <1380542578-2387-2-git-send-email-famz@redhat.com> In-Reply-To: <1380542578-2387-1-git-send-email-famz@redhat.com> References: <1380542578-2387-1-git-send-email-famz@redhat.com> Subject: [Qemu-devel] [PATCH v4 1/7] block: add bdrv_common_ancestor() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, pbonzini@redhat.com, jcody@redhat.com, famz@redhat.com, stefanha@redhat.com This function finds the common ancestor in backing chain of two BDSes. If there's no common ancestor, NULL is returned. Signed-off-by: Fam Zheng --- block.c | 15 +++++++++++++++ include/block/block.h | 2 ++ 2 files changed, 17 insertions(+) diff --git a/block.c b/block.c index ea4956d..db1381d 100644 --- a/block.c +++ b/block.c @@ -3036,6 +3036,21 @@ BlockDriverState *bdrv_find(const char *name) return NULL; } +BlockDriverState *bdrv_common_ancestor(BlockDriverState *bs1, + BlockDriverState *bs2) +{ + /* TODO: higher efficiency with reverse linked backing chain */ + BlockDriverState *base1, *base2; + for (base1 = bs1; base1; base1 = base1->backing_hd) { + for (base2 = bs2; base2; base2 = base2->backing_hd) { + if (base1 == base2) { + return base1; + } + } + } + return NULL; +} + BlockDriverState *bdrv_next(BlockDriverState *bs) { if (!bs) { diff --git a/include/block/block.h b/include/block/block.h index f808550..e095f16 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -321,6 +321,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(const char *name); +BlockDriverState *bdrv_common_ancestor(BlockDriverState *bs1, + BlockDriverState *bs2); BlockDriverState *bdrv_next(BlockDriverState *bs); void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque); -- 1.8.3.1