From nobody Wed Nov 5 18:01:38 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; dkim=fail spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1496642309183649.0674075070232; Sun, 4 Jun 2017 22:58:29 -0700 (PDT) Received: from localhost ([::1]:59739 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dHl23-0002Ke-EO for importer@patchew.org; Mon, 05 Jun 2017 01:58:27 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42446) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dHkwl-0006Gc-Ev for qemu-devel@nongnu.org; Mon, 05 Jun 2017 01:53:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dHkwi-0007z3-Cn for qemu-devel@nongnu.org; Mon, 05 Jun 2017 01:52:59 -0400 Received: from mail-pf0-x243.google.com ([2607:f8b0:400e:c00::243]:35314) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dHkwi-0007yV-5U; Mon, 05 Jun 2017 01:52:56 -0400 Received: by mail-pf0-x243.google.com with SMTP id u26so19488793pfd.2; Sun, 04 Jun 2017 22:52:55 -0700 (PDT) Received: from linux.local ([47.247.8.225]) by smtp.gmail.com with ESMTPSA id s68sm961639pfj.33.2017.06.04.22.52.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 04 Jun 2017 22:52:54 -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=qAEkPGIHiVSIezdBZIr8XdEFG3fHyjwyJ/aTSGOQiII=; b=Gp3fzPnnfXckWsunGZpVzwXZY2HKCjua09Z8rqajXYOkfd8L0SUxgOtdaG+8VB6vED bu9AJneMGacfUCCJK2GezQLIYn7jQFEmJSS2Lj5Bh9Uxl8N9Ms17JHeeE0ZreZOd9x1C 5Z5fwqLB+lpHaOj+7qdC7QqK377wAwVRtvQhe+MDy6NbbPojY8vZtcE2p3nxU68Xp7yr kbLX3LlX3e/zWqVRzv75lPaHBDffziIDFYMEoetJd+fn9mdcbiojkB9O2wwKnT8Jif6/ cT4he03n3j2t8uvSFkE7mr9ish0nelCQy0FXPumxArzmK9SKnNInnWWG+9dTuZA9a4+0 QhAg== 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=qAEkPGIHiVSIezdBZIr8XdEFG3fHyjwyJ/aTSGOQiII=; b=VP+CcEf9ZN58VXCfVqKnExy/mLls46yV9fOmzDbBjzpZ5CO9BQXEvecFg2A0dzDwIT W3dOMUQ/cYVL2Xtk3WmQ6m7Bz8y9rJiwML7IF1HnMSouhmP0j2WAZwse44F+otxs7N9u pjE051UZJi5mhUxWkKYKZVLYpWpRxuzlv0yGuG4v9TRy5314M3TJlfPHFnBuKBErSCri Epr0rhhjet50JYnQkzLM3NsabSdPYuXjjOqoQLei6hTnrp5w3N0pq2VrVAPYnFBnR6+h 478a0QcalHv/nm5OjAvHDSukoN/kk5vSZba5rYTXyPYNcAK7mRSUdqYP2N36cTkFj9mp sVsA== X-Gm-Message-State: AODbwcDRotBbKl1vJH7oGaPEe42dx2fJQWtQfmmasfYtZrM2LinqyMVj rsondeoHDJlr+WCMLUg= X-Received: by 10.99.109.196 with SMTP id i187mr9018500pgc.56.1496641975282; Sun, 04 Jun 2017 22:52:55 -0700 (PDT) From: Ashijeet Acharya To: famz@redhat.com Date: Mon, 5 Jun 2017 11:23:23 +0530 Message-Id: <1496642007-8309-5-git-send-email-ashijeetacharya@gmail.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1496642007-8309-1-git-send-email-ashijeetacharya@gmail.com> References: <1496642007-8309-1-git-send-email-ashijeetacharya@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::243 Subject: [Qemu-devel] [PATCH v5 4/8] vmdk: Factor out metadata loading code out of vmdk_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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Move the cluster tables loading code out of the existing vmdk_get_cluster_offset() function and implement it in separate get_cluster_table() and vmdk_l2load() functions. Signed-off-by: Ashijeet Acharya Reviewed-by: Fam Zheng --- block/vmdk.c | 153 ++++++++++++++++++++++++++++++++++++++++---------------= ---- 1 file changed, 105 insertions(+), 48 deletions(-) diff --git a/block/vmdk.c b/block/vmdk.c index f403981..5647f53 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -1143,6 +1143,105 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMe= taData *m_data, return VMDK_OK; } =20 +/* + * vmdk_l2load + * + * Load 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 =3D 0; i < L2_CACHE_SIZE; i++) { + if (l2_offset =3D=3D extent->l2_cache_offsets[i]) { + /* increment the hit count */ + if (++extent->l2_cache_counts[i] =3D=3D UINT32_MAX) { + for (j =3D 0; j < L2_CACHE_SIZE; j++) { + extent->l2_cache_counts[j] >>=3D 1; + } + } + l2_table =3D extent->l2_cache + (i * extent->l2_size); + goto found; + } + } + /* not found: load a new entry in the least used one */ + min_index =3D 0; + min_count =3D UINT32_MAX; + for (i =3D 0; i < L2_CACHE_SIZE; i++) { + if (extent->l2_cache_counts[i] < min_count) { + min_count =3D extent->l2_cache_counts[i]; + min_index =3D i; + } + } + l2_table =3D 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) + ) !=3D extent->l2_size * sizeof(uint32_t)) { + return VMDK_ERROR; + } + + extent->l2_cache_offsets[min_index] =3D l2_offset; + extent->l2_cache_counts[min_index] =3D 1; +found: + *new_l2_index =3D ((offset >> 9) / extent->cluster_sectors) % extent->= l2_size; + *new_l2_table =3D 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 -=3D (extent->end_sector - extent->sectors) * SECTOR_SIZE; + l1_index =3D (offset >> 9) / extent->l1_entry_sectors; + if (l1_index >=3D extent->l1_size) { + return VMDK_ERROR; + } + l2_offset =3D extent->l1_table[l1_index]; + if (!l2_offset) { + return VMDK_UNALLOC; + } + + ret =3D vmdk_l2load(extent, offset, l2_offset, &l2_table, &l2_index); + if (ret < 0) { + return ret; + } + + *new_l1_index =3D l1_index; + *new_l2_offset =3D l2_offset; + *new_l2_index =3D l2_index; + *new_l2_table =3D l2_table; + + return VMDK_OK; +} + /** * vmdk_get_cluster_offset * @@ -1172,66 +1271,24 @@ static int vmdk_get_cluster_offset(BlockDriverState= *bs, uint64_t skip_start_bytes, uint64_t skip_end_bytes) { - unsigned int l1_index, l2_offset, l2_index; - int min_index, i, j; - uint32_t min_count, *l2_table; + int l1_index, l2_offset, l2_index; + uint32_t *l2_table; bool zeroed =3D false; int64_t ret; int64_t cluster_sector; =20 - if (m_data) { - m_data->valid =3D 0; - } if (extent->flat) { *cluster_offset =3D extent->flat_start_offset; return VMDK_OK; } =20 - offset -=3D (extent->end_sector - extent->sectors) * SECTOR_SIZE; - l1_index =3D (offset >> 9) / extent->l1_entry_sectors; - if (l1_index >=3D extent->l1_size) { - return VMDK_ERROR; - } - l2_offset =3D extent->l1_table[l1_index]; - if (!l2_offset) { - return VMDK_UNALLOC; - } - for (i =3D 0; i < L2_CACHE_SIZE; i++) { - if (l2_offset =3D=3D extent->l2_cache_offsets[i]) { - /* increment the hit count */ - if (++extent->l2_cache_counts[i] =3D=3D 0xffffffff) { - for (j =3D 0; j < L2_CACHE_SIZE; j++) { - extent->l2_cache_counts[j] >>=3D 1; - } - } - l2_table =3D extent->l2_cache + (i * extent->l2_size); - goto found; - } - } - /* not found: load a new entry in the least used one */ - min_index =3D 0; - min_count =3D 0xffffffff; - for (i =3D 0; i < L2_CACHE_SIZE; i++) { - if (extent->l2_cache_counts[i] < min_count) { - min_count =3D extent->l2_cache_counts[i]; - min_index =3D i; - } - } - l2_table =3D 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) - ) !=3D extent->l2_size * sizeof(uint32_t)) { - return VMDK_ERROR; + ret =3D get_cluster_table(extent, offset, &l1_index, &l2_offset, + &l2_index, &l2_table); + if (ret < 0) { + return ret; } =20 - extent->l2_cache_offsets[min_index] =3D l2_offset; - extent->l2_cache_counts[min_index] =3D 1; - found: - l2_index =3D ((offset >> 9) / extent->cluster_sectors) % extent->l2_si= ze; cluster_sector =3D le32_to_cpu(l2_table[l2_index]); - if (extent->has_zero_grain && cluster_sector =3D=3D VMDK_GTE_ZEROED) { zeroed =3D true; } --=20 2.6.2