From patchwork Sat Apr 22 05:13:40 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ashijeet Acharya X-Patchwork-Id: 9694153 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 5BC276038D for ; Sat, 22 Apr 2017 05:20:00 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4E59A285E9 for ; Sat, 22 Apr 2017 05:20:00 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 41661285F0; Sat, 22 Apr 2017 05:20:00 +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 A493E285E9 for ; Sat, 22 Apr 2017 05:19:59 +0000 (UTC) Received: from localhost ([::1]:34234 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1nSg-00066P-Gk for patchwork-qemu-devel@patchwork.kernel.org; Sat, 22 Apr 2017 01:19:58 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60949) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1nMZ-0000D5-Ph for qemu-devel@nongnu.org; Sat, 22 Apr 2017 01:13:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d1nMY-0000WQ-6G for qemu-devel@nongnu.org; Sat, 22 Apr 2017 01:13:39 -0400 Received: from mail-io0-x242.google.com ([2607:f8b0:4001:c06::242]:33888) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1d1nMY-0000WH-0N; Sat, 22 Apr 2017 01:13:38 -0400 Received: by mail-io0-x242.google.com with SMTP id h41so35976131ioi.1; Fri, 21 Apr 2017 22:13:37 -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=lJ9ybdSgXdOFZMI2StLEFVSxi44gZu87O0/4qlVs3gQ=; b=LxdYst4ZP2UGpe2N61OG2qMDjLKFUNaaA+oGzhXshmyseN/hNkRq+C59im2a/HvOai GjFUhnC4UNB+pqnLHZ2zpRDzB+3g7VQ1AyRXmTD3NuZnpsMLJwLiPm5t9jAzGqb0Auu6 j6y+MUZJlVjeE225X2oeDFC76So0ZNTRE+n4xyX7V02hK9xIbv0adMaJZTVCTTQRG2mg ef4ap8CfbfERF0slTu9Xb0BDBPLx/nGkX5k/6Ct97qD7Rj0Fe43HxZjvG8YuFN4E0mBc /bIBiSuI47gIO6DYGZ0rbJAPAx2YflhBAQ9BStIrtVR2tZcnys3hVMhrXbIdsVLLIxqB fw7A== 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=lJ9ybdSgXdOFZMI2StLEFVSxi44gZu87O0/4qlVs3gQ=; b=k1PHpueHb2cMnly07qFf1r7aN1Pl9godA4oWHPw2zTT0GdD1kue8tzFtkYz6ybxrBO mBfxoUVVdRFhCVYV7rPsHdmx4eG1kUCbwCx3Rv09q3wjDoOKKiRF2G/fqzo40OdYIehs D4oB1717Wk3prxj4GxN8pLbaSn9iqKIlUUxtfGjxNQksB00r/2v5mhvGNZJYSRPMFAX9 1qq/NCMFLAqRC4dSpUCDOmvAmMa1FIHd+q26bRWdp7pzvGe9dWkZLnj9o2DNjVmW3cJg 6ghpu9VXA2VGNn9y58TTkjuuFDqvoQ9vKdRrmd0zABEq1XDS0lwA6zhB6bFyF/2HoB5R DgXg== X-Gm-Message-State: AN3rC/4rpW7IOxcDLOJGj2ycZwlcqMFVTTh40t/oMWTdSurBCGX8Nokt /8pFhAdzTSUmQw== X-Received: by 10.98.61.138 with SMTP id x10mr15313798pfj.170.1492838017324; Fri, 21 Apr 2017 22:13:37 -0700 (PDT) Received: from linux.local ([27.251.197.196]) by smtp.gmail.com with ESMTPSA id d82sm18780120pfl.124.2017.04.21.22.13.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 21 Apr 2017 22:13:36 -0700 (PDT) From: Ashijeet Acharya To: famz@redhat.com Date: Sat, 22 Apr 2017 10:43:40 +0530 Message-Id: <1492838021-10538-8-git-send-email-ashijeetacharya@gmail.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1492838021-10538-1-git-send-email-ashijeetacharya@gmail.com> References: <1492838021-10538-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:4001:c06::242 Subject: [Qemu-devel] [PATCH v4 7/8] vmdk: Update metadata for multiple clusters 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 Include a next pointer in VmdkMetaData struct to point to the previous allocated L2 table. Modify vmdk_L2update to start updating metadata for allocation of multiple clusters at once. Signed-off-by: Ashijeet Acharya --- block/vmdk.c | 129 ++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 102 insertions(+), 27 deletions(-) diff --git a/block/vmdk.c b/block/vmdk.c index 8d34cd9..e52c373 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -137,6 +137,8 @@ typedef struct VmdkMetaData { int valid; uint32_t *l2_cache_entry; uint32_t nb_clusters; + uint32_t offset; + struct VmdkMetaData *next; } VmdkMetaData; typedef struct VmdkGrainMarker { @@ -1116,34 +1118,89 @@ exit: return ret; } -static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data, - uint32_t offset) +static int vmdk_alloc_cluster_link_l2(VmdkExtent *extent, + VmdkMetaData *m_data, bool zeroed) { - offset = cpu_to_le32(offset); + int i; + uint32_t offset, temp_offset; + int *l2_table_array; + int l2_array_size; + + if (zeroed) { + temp_offset = VMDK_GTE_ZEROED; + } else { + temp_offset = m_data->offset; + } + + temp_offset = cpu_to_le32(temp_offset); + + l2_array_size = sizeof(uint32_t) * m_data->nb_clusters; + l2_table_array = qemu_try_blockalign(extent->file->bs, + QEMU_ALIGN_UP(l2_array_size, + BDRV_SECTOR_SIZE)); + if (l2_table_array == NULL) { + return VMDK_ERROR; + } + memset(l2_table_array, 0, QEMU_ALIGN_UP(l2_array_size, BDRV_SECTOR_SIZE)); /* update L2 table */ + offset = temp_offset; + for (i = 0; i < m_data->nb_clusters; i++) { + l2_table_array[i] = offset; + if (!zeroed) { + offset += 128; + } + } if (bdrv_pwrite_sync(extent->file, - ((int64_t)m_data->l2_offset * 512) - + (m_data->l2_index * sizeof(offset)), - &offset, sizeof(offset)) < 0) { + ((int64_t)m_data->l2_offset * 512) + + ((m_data->l2_index) * sizeof(offset)), + l2_table_array, l2_array_size) < 0) { return VMDK_ERROR; } /* update backup L2 table */ if (extent->l1_backup_table_offset != 0) { m_data->l2_offset = extent->l1_backup_table[m_data->l1_index]; if (bdrv_pwrite_sync(extent->file, - ((int64_t)m_data->l2_offset * 512) - + (m_data->l2_index * sizeof(offset)), - &offset, sizeof(offset)) < 0) { + ((int64_t)m_data->l2_offset * 512) + + ((m_data->l2_index) * sizeof(offset)), + l2_table_array, l2_array_size) < 0) { return VMDK_ERROR; } } + + offset = temp_offset; if (m_data->l2_cache_entry) { - *m_data->l2_cache_entry = offset; + for (i = 0; i < m_data->nb_clusters; i++) { + *m_data->l2_cache_entry = offset; + m_data->l2_cache_entry++; + + if (!zeroed) { + offset += 128; + } + } } + qemu_vfree(l2_table_array); return VMDK_OK; } +static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data, + bool zeroed) +{ + int ret; + + while (m_data->next != NULL) { + + ret = vmdk_alloc_cluster_link_l2(extent, m_data, zeroed); + if (ret < 0) { + return ret; + } + + m_data = m_data->next; + } + + return VMDK_OK; +} + /* * vmdk_l2load * @@ -1263,7 +1320,7 @@ static int get_cluster_table(VmdkExtent *extent, uint64_t offset, */ static int handle_alloc(BlockDriverState *bs, VmdkExtent *extent, uint64_t offset, uint64_t *cluster_offset, - int64_t *bytes, VmdkMetaData *m_data, + int64_t *bytes, VmdkMetaData **m_data, bool allocate, uint32_t *total_alloc_clusters) { int l1_index, l2_offset, l2_index; @@ -1272,6 +1329,7 @@ static int handle_alloc(BlockDriverState *bs, VmdkExtent *extent, uint32_t nb_clusters; bool zeroed = false; uint64_t skip_start_bytes, skip_end_bytes; + VmdkMetaData *old_m_data; int ret; ret = get_cluster_table(extent, offset, &l1_index, &l2_offset, @@ -1323,13 +1381,21 @@ static int handle_alloc(BlockDriverState *bs, VmdkExtent *extent, if (ret < 0) { 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]; - m_data->nb_clusters = nb_clusters; + + if (*m_data) { + old_m_data = *m_data; + *m_data = g_malloc0(sizeof(**m_data)); + + **m_data = (VmdkMetaData) { + .valid = 1, + .l1_index = l1_index, + .l2_index = l2_index, + .l2_offset = l2_offset, + .l2_cache_entry = &l2_table[l2_index], + .nb_clusters = nb_clusters, + .offset = cluster_sector, + .next = old_m_data, + }; } } *cluster_offset = cluster_sector << BDRV_SECTOR_BITS; @@ -1358,7 +1424,7 @@ static int handle_alloc(BlockDriverState *bs, VmdkExtent *extent, */ static int vmdk_alloc_clusters(BlockDriverState *bs, VmdkExtent *extent, - VmdkMetaData *m_data, uint64_t offset, + VmdkMetaData **m_data, uint64_t offset, bool allocate, uint64_t *cluster_offset, int64_t bytes, uint32_t *total_alloc_clusters) @@ -1378,8 +1444,8 @@ static int vmdk_alloc_clusters(BlockDriverState *bs, new_cluster_offset = 0; *cluster_offset = 0; n_bytes = 0; - if (m_data) { - m_data->valid = 0; + if (*m_data) { + (*m_data)->valid = 0; } /* due to L2 table margins all bytes may not get allocated at once */ @@ -1792,10 +1858,12 @@ static int vmdk_pwritev(BlockDriverState *bs, uint64_t offset, int64_t offset_in_cluster, n_bytes; uint64_t cluster_offset; uint64_t bytes_done = 0; - VmdkMetaData m_data; uint64_t extent_end; + VmdkMetaData *m_data; uint32_t total_alloc_clusters = 0; + m_data = g_malloc0(sizeof(*m_data)); + if (DIV_ROUND_UP(offset, BDRV_SECTOR_SIZE) > bs->total_sectors) { error_report("Wrong offset: offset=0x%" PRIx64 " total_sectors=0x%" PRIx64, @@ -1804,6 +1872,7 @@ static int vmdk_pwritev(BlockDriverState *bs, uint64_t offset, } while (bytes > 0) { + m_data->next = NULL; extent = find_extent(s, offset >> BDRV_SECTOR_BITS, extent); if (!extent) { return -EIO; @@ -1849,7 +1918,7 @@ static int vmdk_pwritev(BlockDriverState *bs, uint64_t offset, total_alloc_clusters; if (!zero_dry_run) { /* update L2 tables */ - if (vmdk_L2update(extent, &m_data, VMDK_GTE_ZEROED) + if (vmdk_L2update(extent, m_data, zeroed) != VMDK_OK) { return -EIO; } @@ -1863,11 +1932,9 @@ static int vmdk_pwritev(BlockDriverState *bs, uint64_t offset, if (ret) { return ret; } - if (m_data.valid) { + if (m_data->valid) { /* update L2 tables */ - if (vmdk_L2update(extent, &m_data, - cluster_offset >> BDRV_SECTOR_BITS) - != VMDK_OK) { + if (vmdk_L2update(extent, m_data, zeroed) != VMDK_OK) { return -EIO; } } @@ -1876,6 +1943,13 @@ static int vmdk_pwritev(BlockDriverState *bs, uint64_t offset, offset += n_bytes; bytes_done += n_bytes; + while (m_data->next != NULL) { + VmdkMetaData *next; + next = m_data->next; + g_free(m_data); + m_data = next; + } + /* update CID on the first write every time the virtual disk is * opened */ if (!s->cid_updated) { @@ -1886,6 +1960,7 @@ static int vmdk_pwritev(BlockDriverState *bs, uint64_t offset, s->cid_updated = true; } } + g_free(m_data); return 0; }