From patchwork Sat Mar 25 11:18:17 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ashijeet Acharya X-Patchwork-Id: 9644517 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 C23C960327 for ; Sat, 25 Mar 2017 11:19:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C3801265B9 for ; Sat, 25 Mar 2017 11:19:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B86B726907; Sat, 25 Mar 2017 11:19:45 +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=-5.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_WEB, T_DKIM_INVALID 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 59625265B9 for ; Sat, 25 Mar 2017 11:19:45 +0000 (UTC) Received: from localhost ([::1]:36729 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1crjjU-0001FU-8F for patchwork-qemu-devel@patchwork.kernel.org; Sat, 25 Mar 2017 07:19:44 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35176) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1crjiF-0001CU-01 for qemu-devel@nongnu.org; Sat, 25 Mar 2017 07:18:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1crjiD-0000q8-W1 for qemu-devel@nongnu.org; Sat, 25 Mar 2017 07:18:27 -0400 Received: from mail-pg0-x241.google.com ([2607:f8b0:400e:c05::241]:35496) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1crjiD-0000pn-Pp; Sat, 25 Mar 2017 07:18:25 -0400 Received: by mail-pg0-x241.google.com with SMTP id g2so2552124pge.2; Sat, 25 Mar 2017 04:18:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=e1yCXwtD3/vYsb3zxKicRIvBtcSq4yunTLXhi9NQPio=; b=OCaEPZhKRaoPoQj90HazS92T35EjC49x6bWxfrwJEk86iHxumcSdihe+n1tCPJS0kJ QUaWg24s2eDrIAFR85a8vaKI5LdIPgPeB0PNUj6T0hj4gGDsu7kylUNqUYHMeMygZESZ kHQ7/za8q5bXaSgmGTh30VlAED+bms+ALav5Lv+4MKVwrGtYGyx+D1R52LvgC79DhyPK QrWlxVByAq2HTAAx96Fp2pQew+GFvxF/9n56j1ukYOgYNvXlKBHH14IGzt7mFF3Rv7I3 tAvmCJh2L2/tWdC5ei2pIl5S34GudxbuyzVC2Lne22Rn60erQEa6cRIl4xEeXM1/pRMA jPRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=e1yCXwtD3/vYsb3zxKicRIvBtcSq4yunTLXhi9NQPio=; b=Lji9uAHEaFSZm62gBskbDEqhxb509xS8029lA0j1yOP78lsTfHOXVz+d0uSuCa6Xqq bwFKw6FicMs+fHOURDKZOgqOw0t0WRUjkESZnLlJlX4ckvJz/SeKaXF4mu2ftm5JSEa6 DECtwAZPePFTcy5LhQvTGbrSSJ3rXGBaYrlW3H7yDB3Fu5Ah9tmH6ZdMlGQdqHsIpZ/V DjEV2bYPwsSF0HWx3/YelAZLYXrplyj8y88/b2JFwG+WX/7Bvvfon02yDlP+GXcq225E j93TYG26hJQzBje0bHYUkmqDgeoavZSFgsOpHj24O0ylNGp91lOR7bfgb8Z8fepzp+gh WD3w== X-Gm-Message-State: AFeK/H21R1vI4fzux6yrBy3nHI+ndQjJSeZLKTLMDMFAezVH4HTibGd5G95GFnfhURv5Gg== X-Received: by 10.98.96.65 with SMTP id u62mr14809184pfb.219.1490440704904; Sat, 25 Mar 2017 04:18:24 -0700 (PDT) Received: from linux.local ([27.251.197.196]) by smtp.gmail.com with ESMTPSA id p189sm9952620pfb.128.2017.03.25.04.18.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 25 Mar 2017 04:18:24 -0700 (PDT) From: Ashijeet Acharya To: famz@redhat.com Date: Sat, 25 Mar 2017 16:48:17 +0530 Message-Id: <1490440701-12037-4-git-send-email-ashijeetacharya@gmail.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1490440701-12037-1-git-send-email-ashijeetacharya@gmail.com> References: <1490440701-12037-1-git-send-email-ashijeetacharya@gmail.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400e:c05::241 Subject: [Qemu-devel] [PATCH v2 3/7] vmdk: Factor out metadata loading code out of get_cluster_offset() 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: kwolf@redhat.com, qemu-block@nongnu.org, stefanha@gmail.com, qemu-devel@nongnu.org, mreitz@redhat.com, Ashijeet Acharya , jsnow@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Move the cluster tables loading code out of the existing get_cluster_offset() function and implement it in separate get_cluster_table() and vmdk_L2load() functions. This patch will help us avoid code duplication in future patches of this series. Signed-off-by: Ashijeet Acharya --- block/vmdk.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/block/vmdk.c b/block/vmdk.c index f5fda2c..a42322e 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -1037,6 +1037,105 @@ static void vmdk_refresh_limits(BlockDriverState *bs, Error **errp) } /* + * vmdk_L2load + * + * Loads a new L2 table into memory. If the table is in the cache, the cache + * is used; otherwise the L2 table is loaded from the image file. + * + * Returns: + * VMDK_OK: on success + * VMDK_ERROR: in error cases + */ +static int vmdk_L2load(VmdkExtent *extent, uint64_t offset, int l2_offset, + uint32_t **new_l2_table, int *new_l2_index) +{ + int min_index, i, j; + uint32_t *l2_table; + uint32_t min_count; + + for (i = 0; i < L2_CACHE_SIZE; i++) { + if (l2_offset == extent->l2_cache_offsets[i]) { + /* increment the hit count */ + if (++extent->l2_cache_counts[i] == 0xffffffff) { + for (j = 0; j < L2_CACHE_SIZE; j++) { + extent->l2_cache_counts[j] >>= 1; + } + } + l2_table = extent->l2_cache + (i * extent->l2_size); + goto found; + } + } + /* not found: load a new entry in the least used one */ + min_index = 0; + min_count = 0xffffffff; + for (i = 0; i < L2_CACHE_SIZE; i++) { + if (extent->l2_cache_counts[i] < min_count) { + min_count = extent->l2_cache_counts[i]; + min_index = i; + } + } + l2_table = extent->l2_cache + (min_index * extent->l2_size); + if (bdrv_pread(extent->file, + (int64_t)l2_offset * 512, + l2_table, + extent->l2_size * sizeof(uint32_t) + ) != extent->l2_size * sizeof(uint32_t)) { + return VMDK_ERROR; + } + + extent->l2_cache_offsets[min_index] = l2_offset; + extent->l2_cache_counts[min_index] = 1; +found: + *new_l2_index = ((offset >> 9) / extent->cluster_sectors) % extent->l2_size; + *new_l2_table = l2_table; + + return VMDK_OK; +} + +/* + * get_cluster_table + * + * for a given offset, load (and allocate if needed) the l2 table. + * + * Returns: + * VMDK_OK: on success + * + * VMDK_UNALLOC: if cluster is not mapped + * + * VMDK_ERROR: in error cases + */ +static int get_cluster_table(VmdkExtent *extent, uint64_t offset, + int *new_l1_index, int *new_l2_offset, + int *new_l2_index, uint32_t **new_l2_table) +{ + int l1_index, l2_offset, l2_index; + uint32_t *l2_table; + int ret; + + offset -= (extent->end_sector - extent->sectors) * SECTOR_SIZE; + l1_index = (offset >> 9) / extent->l1_entry_sectors; + if (l1_index >= extent->l1_size) { + return VMDK_ERROR; + } + l2_offset = extent->l1_table[l1_index]; + if (!l2_offset) { + return VMDK_UNALLOC; + } + + ret = vmdk_L2load(extent, offset, l2_offset, &l2_table, &l2_index); + if (ret < 0) { + return ret; + } + + *new_l1_index = l1_index; + *new_l2_offset = l2_offset; + *new_l2_index = l2_index; + *new_l2_table = l2_table; + + return VMDK_OK; +} + +/* * vmdk_perform_cow * * Copy backing file's cluster that covers @sector_num, otherwise write zero,