From patchwork Sat Apr 1 14:44:36 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ashijeet Acharya X-Patchwork-Id: 9658061 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 2805C602BC for ; Sat, 1 Apr 2017 14:45:56 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1DF76285CC for ; Sat, 1 Apr 2017 14:45:56 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 11ACE285E7; Sat, 1 Apr 2017 14:45:56 +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 70AF0285CC for ; Sat, 1 Apr 2017 14:45:55 +0000 (UTC) Received: from localhost ([::1]:51683 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cuKHq-00068m-Fu for patchwork-qemu-devel@patchwork.kernel.org; Sat, 01 Apr 2017 10:45:54 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40588) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cuKGZ-00063R-Un for qemu-devel@nongnu.org; Sat, 01 Apr 2017 10:44:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cuKGY-00042O-Jg for qemu-devel@nongnu.org; Sat, 01 Apr 2017 10:44:36 -0400 Received: from mail-pg0-x244.google.com ([2607:f8b0:400e:c05::244]:33322) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cuKGY-00042D-Bl; Sat, 01 Apr 2017 10:44:34 -0400 Received: by mail-pg0-x244.google.com with SMTP id 79so22400643pgf.0; Sat, 01 Apr 2017 07:44: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=SbXzQdJIreQxKnBDtUcHB+QUhk6uBCJ4DbIlhq7Hl+I=; b=TSL5GNUGBdfSQ2Vv/2n4zuEiiZhai0sLLNW3A7tG2sOEeIUum3xsWBJb6zxbfa6Drg 2AqGaCH9XSiKscOolV0o/gq1vAS8iTFbRbW0tQYjn1ltp7w34pwpj1fW6bSvEMsqU+xQ ypGBcPFwioJkwaMpFmSs+UhWKTAV/vScXKheSBFu6xvDM0LEq2ePtx26RRH42eYaet5h ycQVHmmOA5WaStyO7Py5texxOyHD7OUQ2Hc/IP1WDuE1x9xeLTjdt5Vz4ixWNWygpi25 OntIkCPVdMXydK2xl2MNrI3Fl9kteBxxHR3DPJCGRBcAeYzcO+PtH4fGepFBImVOEBau ozWg== 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=SbXzQdJIreQxKnBDtUcHB+QUhk6uBCJ4DbIlhq7Hl+I=; b=qQFdX6258fXKEjfuJ/bzvuoKDJd/S9bzeAOYXSu2NGR/Y45C+OTQtd0Vmb1o+xTeeP oAQPsK4n9BCBL4Hvb/6+TiPFXIXiwC1RfiiIUAL9PrK+JP91bs2Y/5TQv8Et5E1RYeye tXiXVD0JNgG1H70E2sLRnhQsCRzae3NtqVQeeVs/Vi+GA9mAi4GRVrWALApoLNhQuiF+ bQAuoNy4Fpi9yxqU8KnQ75sMPBAD/Rqh7hthlUPM9OeUpnWhQEMmU7/zrjwghcVn5o3j KM24ISS/1+quGvEtGwOKzgRtI5J8JQBg+QCpeAOfMTJSlWxQObBXzRJwqCut2NttslCl WIBg== X-Gm-Message-State: AFeK/H3UErjkK7U16w24zs1EMTsMcE4bjtUPFDi2Y4DqRSub7C+/4MXlYWnW3eodbloSWQ== X-Received: by 10.98.78.22 with SMTP id c22mr7525635pfb.191.1491057873453; Sat, 01 Apr 2017 07:44:33 -0700 (PDT) Received: from linux.local ([27.251.197.196]) by smtp.gmail.com with ESMTPSA id h14sm9618461pgn.64.2017.04.01.07.44.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 01 Apr 2017 07:44:32 -0700 (PDT) From: Ashijeet Acharya To: famz@redhat.com Date: Sat, 1 Apr 2017 20:14:36 +0530 Message-Id: <1491057878-27868-5-git-send-email-ashijeetacharya@gmail.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1491057878-27868-1-git-send-email-ashijeetacharya@gmail.com> References: <1491057878-27868-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::244 Subject: [Qemu-devel] [PATCH v3 4/6] 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 e5a289d..a8babd7 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 = vmdk_perform_cow(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) { @@ -2508,9 +2434,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 %"