From patchwork Sat Mar 25 11:18:19 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ashijeet Acharya X-Patchwork-Id: 9644527 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 DCB316020B for ; Sat, 25 Mar 2017 11:22:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DAA8626220 for ; Sat, 25 Mar 2017 11:22:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CBFD826907; Sat, 25 Mar 2017 11:22: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 1396C26220 for ; Sat, 25 Mar 2017 11:22:45 +0000 (UTC) Received: from localhost ([::1]:36749 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1crjmO-0003bB-1i for patchwork-qemu-devel@patchwork.kernel.org; Sat, 25 Mar 2017 07:22:44 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35234) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1crjiN-0001Mn-Uu for qemu-devel@nongnu.org; Sat, 25 Mar 2017 07:18:37 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1crjiM-0000s2-GR for qemu-devel@nongnu.org; Sat, 25 Mar 2017 07:18:35 -0400 Received: from mail-pg0-x243.google.com ([2607:f8b0:400e:c05::243]:35506) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1crjiM-0000rk-7o; Sat, 25 Mar 2017 07:18:34 -0400 Received: by mail-pg0-x243.google.com with SMTP id g2so2552457pge.2; Sat, 25 Mar 2017 04:18:34 -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=2wGCm9WbUnZQr8S2AJIS0W79WxLwz23WH3BbpgR+Ln4=; b=aBv2JK+jjzMG2ijUo/a7ywdvUo60CsqI/F7sIsO8/dttQLXAev3sO4t+mejmAwmSCV 0EcBO+kWMHERog9C52ChsDe/HkcbTlvZHcz0QIfiuuk8nQGlxpTVAYmk7vJji4Oek8aO qHR0azdVuJ9QIP92vbqJ4MmMeYf9zRNYMSKF19D578AU7zQ1Pl8ieANREwPnLQXsk8hA 7I2mR5o2Xvee27WMDTSyOA8gTyanI87d6bZsVN2/mTu4Ejqg4Q2JMzd4ZDRz1w9uqUWZ DqDil6vKEqUhCf7NS85EbhtrY7Qx/4J4yRkHXchivi2kMSXS/PwCkrqeHNS+srpWuQLp H9Cw== 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=2wGCm9WbUnZQr8S2AJIS0W79WxLwz23WH3BbpgR+Ln4=; b=Bme+yq1nKOdXf9o0nJCoaeH9WbZ1+obXX+PcIEmzPndgMMlA34motXv84jZbkF/Hxt x0UclxMiruiTeH2dgsAHs8zi6E0aM61zq3QT370ceVXYUgBTIlHwVJUiZ/DHorwzLJBl d0tmaZrKVlcJHXpO0A+urk2PKSozfuwYfSIGLz89H7jdaCOwexv0irXuDBdm93FPB5MW 6u7xpVULJB+EDNyUXCrzVGYTQL1z4KYqhuuSgULbnIZch3SdBgF4fvgZFzwEHfDgwFO6 m7DAdY0GDlQicE2sPDSDaXk81yl+pzrI0gq3Z9UAJXiGC8/BzrjNvG9WNIuHbSvoYaNI rVmQ== X-Gm-Message-State: AFeK/H1WMJOo/tyOsD5hoD9b1Qdh6ZJzU4gLIMGLnJlQGF0An+kL6iSDWtFChQuVn0vEOQ== X-Received: by 10.99.5.133 with SMTP id 127mr14106328pgf.8.1490440713290; Sat, 25 Mar 2017 04:18:33 -0700 (PDT) Received: from linux.local ([27.251.197.196]) by smtp.gmail.com with ESMTPSA id p189sm9952620pfb.128.2017.03.25.04.18.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 25 Mar 2017 04:18:32 -0700 (PDT) From: Ashijeet Acharya To: famz@redhat.com Date: Sat, 25 Mar 2017 16:48:19 +0530 Message-Id: <1490440701-12037-6-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::243 Subject: [Qemu-devel] [PATCH v2 5/7] vmdk: Rename get_cluster_offset() to 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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Rename the existing get_cluster_offset() function to vmdk_get_cluster_offset() and have it make use of the new get_cluster_table() to load the cluster tables. Also, it is no longer used to allocate new clusters and hence perform COW. Make the necessary renames at all the occurrences of get_cluster_offset(). Signed-off-by: Ashijeet Acharya --- block/vmdk.c | 117 +++++++++++------------------------------------------------ 1 file changed, 21 insertions(+), 96 deletions(-) diff --git a/block/vmdk.c b/block/vmdk.c index 5a95929..387e45b 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -1419,7 +1419,7 @@ static int vmdk_alloc_cluster_offset(BlockDriverState *bs, } /** - * get_cluster_offset + * vmdk_get_cluster_offset * * Look up cluster offset in extent file by sector number, and store in * @cluster_offset. @@ -1427,84 +1427,34 @@ static int vmdk_alloc_cluster_offset(BlockDriverState *bs, * For flat extents, the start offset as parsed from the description file is * returned. * - * For sparse extents, look up in L1, L2 table. If allocate is true, return an - * offset for a new cluster and update L2 cache. If there is a backing file, - * COW is done before returning; otherwise, zeroes are written to the allocated - * cluster. Both COW and zero writing skips the sector range - * [@skip_start_sector, @skip_end_sector) passed in by caller, because caller - * has new data to write there. + * For sparse extents, look up in L1, L2 table. * * Returns: VMDK_OK if cluster exists and mapped in the image. - * VMDK_UNALLOC if cluster is not mapped and @allocate is false. - * VMDK_ERROR if failed. + * VMDK_UNALLOC if cluster is not mapped. + * VMDK_ERROR if failed */ -static int get_cluster_offset(BlockDriverState *bs, - VmdkExtent *extent, - VmdkMetaData *m_data, - uint64_t offset, - bool allocate, - uint64_t *cluster_offset, - uint64_t skip_start_bytes, - uint64_t skip_end_bytes) +static int vmdk_get_cluster_offset(BlockDriverState *bs, + VmdkExtent *extent, + uint64_t offset, + uint64_t *cluster_offset) { - 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 = false; int64_t ret; int64_t cluster_sector; - if (m_data) { - m_data->valid = 0; - } if (extent->flat) { *cluster_offset = extent->flat_start_offset; return VMDK_OK; } - 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; - } - 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; + ret = get_cluster_table(extent, offset, &l1_index, &l2_offset, + &l2_index, &l2_table); + if (ret < 0) { + return ret; } - extent->l2_cache_offsets[min_index] = l2_offset; - extent->l2_cache_counts[min_index] = 1; - found: - l2_index = ((offset >> 9) / extent->cluster_sectors) % extent->l2_size; cluster_sector = le32_to_cpu(l2_table[l2_index]); if (extent->has_zero_grain && cluster_sector == VMDK_GTE_ZEROED) { @@ -1512,31 +1462,9 @@ static int get_cluster_offset(BlockDriverState *bs, } if (!cluster_sector || zeroed) { - if (!allocate) { - return zeroed ? VMDK_ZEROED : VMDK_UNALLOC; - } - - cluster_sector = extent->next_cluster_sector; - extent->next_cluster_sector += extent->cluster_sectors; - - /* First of all we write grain itself, to avoid race condition - * that may to corrupt the image. - * This problem may occur because of insufficient space on host disk - * or inappropriate VM shutdown. - */ - ret = get_whole_cluster(bs, extent, cluster_sector * BDRV_SECTOR_SIZE, - offset, skip_start_bytes, skip_end_bytes); - if (ret) { - return ret; - } - if (m_data) { - m_data->valid = 1; - m_data->l1_index = l1_index; - m_data->l2_index = l2_index; - m_data->l2_offset = l2_offset; - m_data->l2_cache_entry = &l2_table[l2_index]; - } + return zeroed ? VMDK_ZEROED : VMDK_UNALLOC; } + *cluster_offset = cluster_sector << BDRV_SECTOR_BITS; return VMDK_OK; } @@ -1579,9 +1507,7 @@ static int64_t coroutine_fn vmdk_co_get_block_status(BlockDriverState *bs, return 0; } qemu_co_mutex_lock(&s->lock); - ret = get_cluster_offset(bs, extent, NULL, - sector_num * 512, false, &offset, - 0, 0); + ret = vmdk_get_cluster_offset(bs, extent, sector_num * 512, &offset); qemu_co_mutex_unlock(&s->lock); index_in_cluster = vmdk_find_index_in_cluster(extent, sector_num); @@ -1772,13 +1698,13 @@ vmdk_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes, ret = -EIO; goto fail; } - ret = get_cluster_offset(bs, extent, NULL, - offset, false, &cluster_offset, 0, 0); offset_in_cluster = vmdk_find_offset_in_cluster(extent, offset); n_bytes = MIN(bytes, extent->cluster_sectors * BDRV_SECTOR_SIZE - offset_in_cluster); + ret = vmdk_get_cluster_offset(bs, extent, offset, &cluster_offset); + if (ret != VMDK_OK) { /* if not allocated, try to read from parent image, if exist */ if (bs->backing && ret != VMDK_ZEROED) { @@ -2504,9 +2430,8 @@ static int vmdk_check(BlockDriverState *bs, BdrvCheckResult *result, sector_num); break; } - ret = get_cluster_offset(bs, extent, NULL, - sector_num << BDRV_SECTOR_BITS, - false, &cluster_offset, 0, 0); + ret = vmdk_get_cluster_offset(bs, extent, + sector_num << BDRV_SECTOR_BITS, &cluster_offset); if (ret == VMDK_ERROR) { fprintf(stderr, "ERROR: could not get cluster_offset for sector %"