From nobody Sat May 4 04:18:47 2024 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.zohomail.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 1501166069120525.5696916530937; Thu, 27 Jul 2017 07:34:29 -0700 (PDT) Received: from localhost ([::1]:43373 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dajrt-0001ND-Jz for importer@patchew.org; Thu, 27 Jul 2017 10:34:25 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57684) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dajpQ-0007oI-3u for qemu-devel@nongnu.org; Thu, 27 Jul 2017 10:31:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dajpN-0005hY-1X for qemu-devel@nongnu.org; Thu, 27 Jul 2017 10:31:52 -0400 Received: from mail-pg0-x241.google.com ([2607:f8b0:400e:c05::241]:38450) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dajpM-0005gq-RX; Thu, 27 Jul 2017 10:31:48 -0400 Received: by mail-pg0-x241.google.com with SMTP id 125so7258656pgi.5; Thu, 27 Jul 2017 07:31:48 -0700 (PDT) Received: from linux.local ([27.251.197.195]) by smtp.gmail.com with ESMTPSA id y14sm32579540pfa.52.2017.07.27.07.31.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 27 Jul 2017 07:31:47 -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=Frh6VDB0/xAj1TJL449/st2nOhRT2c4sZdDlRf5/ir8=; b=pejaz//BlyvrzEsiKj3KHu195FFCB/AwkVr+qw1W1xVfUZyOXaoMKYVPM59T5INfCv TGlPSYi6dWc+Sdapz5bICxn3uM2lfwPmvJCwprcBotGQFYYhKRpVSED5phYe8cMgB5LL bw4/zq3J+9yKhzIBgqSxC8F5P8XDiCd0JwdBp0UBZAP6LoNZTwkkYUqsKa2BUCHCM655 GZriWw9VeFFFCUGJibbNuqduxwwcwDYqh6pBH46vgqtXVhErk6NHyjRuzmVh5o4Qbb9H A5qFsw57FLLvaMwD9Jk5WB/SQM4nbOyyM0f1XdOHaGK0/kpAo0w90SkdRharxIO0zisH Zg9Q== 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=Frh6VDB0/xAj1TJL449/st2nOhRT2c4sZdDlRf5/ir8=; b=d/UJLY+4pzFRiSLRLkjRrr7EgSEApmBjFncfq99XEFYSyBExbk00kdZhAM/Vo3Mssj ohEzRf3K/vihgbbyQlNZJIMiuBOxGEaPZuwOlBRH45b1NEGb8GVM71SReyKu0zvwcO6S QFZXCEeY3jjjdlXo05VOsCxF29BFdF6ZUfwpmym5Wg3ZxbckNoKDePk02g4d2/aHa0eS UGEzJh9zrA7p3b4JtSbt7dDSI6Cfe7RQ10A7jM5R4KKPgemFJF7psiWlREXA1EeglHYq 9N1/qeQrNwVkiU3GB7Oj1L8OZwXBSD7Zur4f6X62UeQQs+jC2eJkMCkMO4a5JiAFV4Rd gPEg== X-Gm-Message-State: AIVw112E4jSQGhb4YLXDhWuvN9AJvEadcwFnETEUs34+7jclE7rNfXeu iJay5jkBZEU5Ig== X-Received: by 10.98.135.141 with SMTP id i135mr2721291pfe.254.1501165907941; Thu, 27 Jul 2017 07:31:47 -0700 (PDT) From: Ashijeet Acharya To: famz@redhat.com Date: Thu, 27 Jul 2017 20:03:03 +0530 Message-Id: <1501165990-28033-2-git-send-email-ashijeetacharya@gmail.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1501165990-28033-1-git-send-email-ashijeetacharya@gmail.com> References: <1501165990-28033-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:c05::241 Subject: [Qemu-devel] [PATCH v8 1/8] vmdk: Move vmdk_find_offset_in_cluster() to the top 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 existing vmdk_find_offset_in_cluster() function to the top of the driver. Signed-off-by: Ashijeet Acharya Reviewed-by: Fam Zheng --- block/vmdk.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/block/vmdk.c b/block/vmdk.c index a9bd22b..22be887 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -242,6 +242,18 @@ static void vmdk_free_last_extent(BlockDriverState *bs) s->extents =3D g_renew(VmdkExtent, s->extents, s->num_extents); } =20 +static inline uint64_t vmdk_find_offset_in_cluster(VmdkExtent *extent, + int64_t offset) +{ + uint64_t extent_begin_offset, extent_relative_offset; + uint64_t cluster_size =3D extent->cluster_sectors * BDRV_SECTOR_SIZE; + + extent_begin_offset =3D + (extent->end_sector - extent->sectors) * BDRV_SECTOR_SIZE; + extent_relative_offset =3D offset - extent_begin_offset; + return extent_relative_offset % cluster_size; +} + static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent) { char *desc; @@ -1266,18 +1278,6 @@ static VmdkExtent *find_extent(BDRVVmdkState *s, return NULL; } =20 -static inline uint64_t vmdk_find_offset_in_cluster(VmdkExtent *extent, - int64_t offset) -{ - uint64_t extent_begin_offset, extent_relative_offset; - uint64_t cluster_size =3D extent->cluster_sectors * BDRV_SECTOR_SIZE; - - extent_begin_offset =3D - (extent->end_sector - extent->sectors) * BDRV_SECTOR_SIZE; - extent_relative_offset =3D offset - extent_begin_offset; - return extent_relative_offset % cluster_size; -} - static inline uint64_t vmdk_find_index_in_cluster(VmdkExtent *extent, int64_t sector_num) { --=20 2.6.2 From nobody Sat May 4 04:18:47 2024 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.zohomail.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 1501166032025323.5371448905372; Thu, 27 Jul 2017 07:33:52 -0700 (PDT) Received: from localhost ([::1]:43370 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dajrK-0000cV-1U for importer@patchew.org; Thu, 27 Jul 2017 10:33:50 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57729) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dajpR-0007p2-Eb for qemu-devel@nongnu.org; Thu, 27 Jul 2017 10:31:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dajpQ-0005lQ-Kg for qemu-devel@nongnu.org; Thu, 27 Jul 2017 10:31:53 -0400 Received: from mail-pg0-x242.google.com ([2607:f8b0:400e:c05::242]:34193) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dajpQ-0005kC-G3; Thu, 27 Jul 2017 10:31:52 -0400 Received: by mail-pg0-x242.google.com with SMTP id v190so20733995pgv.1; Thu, 27 Jul 2017 07:31:52 -0700 (PDT) Received: from linux.local ([27.251.197.195]) by smtp.gmail.com with ESMTPSA id y14sm32579540pfa.52.2017.07.27.07.31.49 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 27 Jul 2017 07:31:51 -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=y0aLdU8eyizcBISOMqtI7Bfk7gF3uIFfg+flRUveKjg=; b=g7hMacC9QGyNJsen0Ls+9H+pbpdZwAB+uUWxdaTuDumd6ZbqBnAsclPZn0M4L/1Ozj 8qk1uQ5y//dgRw73rP1kYJZpRAXAkCGiQca42mQ0gDs022nq5rqFA3mVwsjaHOANTQeM QRs3bmr0BKkA+YdGv3OFZ4YV/r7wKZnbqkqzzNmM/bSs9IdFBjyNw7hGpd1ka0p9B4uY q7wu22GM9UfTI0ToapboweVsUWQduz8z4E7H7sYYbcVLaj4RdjjxZSpK3+J6oMtKhOvv KNenTIVvgEgbwkMZUOq8Tel5RVBhGtmzLtN4Ca7lAXrPH8TyNYjw74b9rFzEX7Wfs/sE /SVw== 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=y0aLdU8eyizcBISOMqtI7Bfk7gF3uIFfg+flRUveKjg=; b=KbmD2UUt3cY2lbKBW2a43KSVa2yojzjRNXCxC7fVLNOyAY29gAer4ZtbWVDIOFOyHU Z5DIwLllWlqhfLKisPfcH/r8q62B6HsU9vvyn8ZikYekFnYXIjiJ7TpyQ/SXrlkjTEPR yXj37kcGRBNrsoCiuJgdh5iDHCGsy5wWQBU6o0o5L2YYZslmgRZFEUzYHKTLoD6ndaq8 jfBG6aUL6pPPCuaHR0LgbL32/AlmrQOsw+9AFJJutPK3AW1OwRg2gI6u49L29Hy6I7Qg pVwFmVQ/6F9Ubv9oo8CGqqrMTvLB9oY86xwADjrUP6ObaY/OaGgGqKyfZ1gHBEfM1Ymz +/aQ== X-Gm-Message-State: AIVw111lcEDndtJaaezRXdUPp2n9BDBipuXwqZ+EwUUxs6t6WDVr0nl0 zLTuHQkbgEquhg== X-Received: by 10.99.142.73 with SMTP id k70mr4220940pge.86.1501165911545; Thu, 27 Jul 2017 07:31:51 -0700 (PDT) From: Ashijeet Acharya To: famz@redhat.com Date: Thu, 27 Jul 2017 20:03:04 +0530 Message-Id: <1501165990-28033-3-git-send-email-ashijeetacharya@gmail.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1501165990-28033-1-git-send-email-ashijeetacharya@gmail.com> References: <1501165990-28033-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:c05::242 Subject: [Qemu-devel] [PATCH v8 2/8] vmdk: Rename get_whole_cluster() to vmdk_perform_cow() 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" Rename the existing function get_whole_cluster() to vmdk_perform_cow() as its sole purpose is to perform COW for the first and the last allocated clusters if needed. Signed-off-by: Ashijeet Acharya Reviewed-by: Fam Zheng --- block/vmdk.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/block/vmdk.c b/block/vmdk.c index 22be887..73ae786 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -1028,8 +1028,8 @@ static void vmdk_refresh_limits(BlockDriverState *bs,= Error **errp) } } =20 -/** - * get_whole_cluster +/* + * vmdk_perform_cow * * Copy backing file's cluster that covers @sector_num, otherwise write ze= ro, * to the cluster at @cluster_sector_num. @@ -1037,13 +1037,18 @@ static void vmdk_refresh_limits(BlockDriverState *b= s, Error **errp) * If @skip_start_sector < @skip_end_sector, the relative range * [@skip_start_sector, @skip_end_sector) is not copied or written, and le= ave * it for call to write user data in the request. + * + * Returns: + * VMDK_OK: on success + * + * VMDK_ERROR: in error cases */ -static int get_whole_cluster(BlockDriverState *bs, - VmdkExtent *extent, - uint64_t cluster_offset, - uint64_t offset, - uint64_t skip_start_bytes, - uint64_t skip_end_bytes) +static int vmdk_perform_cow(BlockDriverState *bs, + VmdkExtent *extent, + uint64_t cluster_offset, + uint64_t offset, + uint64_t skip_start_bytes, + uint64_t skip_end_bytes) { int ret =3D VMDK_OK; int64_t cluster_bytes; @@ -1244,7 +1249,7 @@ static int get_cluster_offset(BlockDriverState *bs, * This problem may occur because of insufficient space on host di= sk * or inappropriate VM shutdown. */ - ret =3D get_whole_cluster(bs, extent, cluster_sector * BDRV_SECTOR= _SIZE, + ret =3D vmdk_perform_cow(bs, extent, cluster_sector * BDRV_SECTOR_= SIZE, offset, skip_start_bytes, skip_end_bytes); if (ret) { return ret; --=20 2.6.2 From nobody Sat May 4 04:18:47 2024 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.zohomail.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 1501166054029685.7951006279992; Thu, 27 Jul 2017 07:34:14 -0700 (PDT) Received: from localhost ([::1]:43372 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dajrf-00012n-Gp for importer@patchew.org; Thu, 27 Jul 2017 10:34:11 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57801) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dajpV-0007tb-8b for qemu-devel@nongnu.org; Thu, 27 Jul 2017 10:31:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dajpU-0005ph-BJ for qemu-devel@nongnu.org; Thu, 27 Jul 2017 10:31:57 -0400 Received: from mail-pf0-x244.google.com ([2607:f8b0:400e:c00::244]:37871) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dajpU-0005ot-3a; Thu, 27 Jul 2017 10:31:56 -0400 Received: by mail-pf0-x244.google.com with SMTP id y25so10923392pfk.4; Thu, 27 Jul 2017 07:31:55 -0700 (PDT) Received: from linux.local ([27.251.197.195]) by smtp.gmail.com with ESMTPSA id y14sm32579540pfa.52.2017.07.27.07.31.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 27 Jul 2017 07:31: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=wlzoG7oQRHBbI3gL8iwuPmGQOHTo9rMYaIJacXohg3U=; b=VwJ2tJh69oJBp16t0q+59sCV7S48nUKTh2+rD20WgAhAVa0JT1LiFY1iuZDEclsrpz UGTLk8/M1563AbENAKl7lpAQYIsXdFTgDh97R7KH9W5huANgkUb/AL6HvboGLGLDKW4I e3HTepMfMc6SToq8sMHBdZOJQC8xTSAQwF91XrhMD9ZyNcJ3qDZyztUqYAlRMgw5RIFO rbsPWYGhjPo6R+eaVYA5Ts7F9OCPLhVNibECi2K/uQ1ObaRs+ktVpXQ57XT/H+GtHPaa P0DeQo7Wm7HmFUJ/E05rTmzkzjiMCLhXeMIVQIvI6LBopFKphiyEy3afOJEgsNwN5P/S GFIg== 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=wlzoG7oQRHBbI3gL8iwuPmGQOHTo9rMYaIJacXohg3U=; b=A18IfV6ijhJ8EUc4ND/C5Wsx6ytsM+YVpNz796ZDVNUrnixSjIYYRHjv0/kG5Ss9SA QncGvPWx67B4kRIROehFZWW0kM6E6XWbTMK153PGfZNSDCTCqTltN+/qdqsEuwHXgjcW C/GQjLW7uIPJmuHk5sOJ/h8mPnBi7CyU43MQbnOTsjcKkVphJgG3WHo4pGm3/Uvsk7zQ DDG07aP+dZbYKbyOXWotGf5s/Nw7LDIny5QX2jTrSQzuqy0wPP6SyAn3+432nTimO97U SWI9tGVm0hd3eDw9jqIk0q/UobZ7WJXeWFIdJ3iLR0A1Gi+J6okyrTfjJW9TD9GSUDeP 7HUw== X-Gm-Message-State: AIVw113RCxwGJouZMV9nUFC1bbkZGmzAk1UvZ7et9qVmFO/Ym9vpL1Ds pHsmNbPGvZukBQ== X-Received: by 10.84.130.9 with SMTP id 9mr2121101plc.388.1501165915140; Thu, 27 Jul 2017 07:31:55 -0700 (PDT) From: Ashijeet Acharya To: famz@redhat.com Date: Thu, 27 Jul 2017 20:03:05 +0530 Message-Id: <1501165990-28033-4-git-send-email-ashijeetacharya@gmail.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1501165990-28033-1-git-send-email-ashijeetacharya@gmail.com> References: <1501165990-28033-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::244 Subject: [Qemu-devel] [PATCH v8 3/8] 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+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" Rename the existing get_cluster_offset() to vmdk_get_cluster_offset() and update name in all the callers accordingly. Signed-off-by: Ashijeet Acharya Reviewed-by: Fam Zheng --- block/vmdk.c | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/block/vmdk.c b/block/vmdk.c index 73ae786..f403981 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -1144,7 +1144,7 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMeta= Data *m_data, } =20 /** - * get_cluster_offset + * vmdk_get_cluster_offset * * Look up cluster offset in extent file by sector number, and store in * @cluster_offset. @@ -1163,14 +1163,14 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMe= taData *m_data, * VMDK_UNALLOC if cluster is not mapped and @allocate is false. * 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, + VmdkMetaData *m_data, + uint64_t offset, + bool allocate, + uint64_t *cluster_offset, + uint64_t skip_start_bytes, + uint64_t skip_end_bytes) { unsigned int l1_index, l2_offset, l2_index; int min_index, i, j; @@ -1304,9 +1304,9 @@ static int64_t coroutine_fn vmdk_co_get_block_status(= BlockDriverState *bs, return 0; } qemu_co_mutex_lock(&s->lock); - ret =3D get_cluster_offset(bs, extent, NULL, - sector_num * 512, false, &offset, - 0, 0); + ret =3D vmdk_get_cluster_offset(bs, extent, NULL, + sector_num * 512, false, &offset, + 0, 0); qemu_co_mutex_unlock(&s->lock); =20 index_in_cluster =3D vmdk_find_index_in_cluster(extent, sector_num); @@ -1497,8 +1497,8 @@ vmdk_co_preadv(BlockDriverState *bs, uint64_t offset,= uint64_t bytes, ret =3D -EIO; goto fail; } - ret =3D get_cluster_offset(bs, extent, NULL, - offset, false, &cluster_offset, 0, 0); + ret =3D vmdk_get_cluster_offset(bs, extent, NULL, + offset, false, &cluster_offset, 0, 0= ); offset_in_cluster =3D vmdk_find_offset_in_cluster(extent, offset); =20 n_bytes =3D MIN(bytes, extent->cluster_sectors * BDRV_SECTOR_SIZE @@ -1584,10 +1584,10 @@ static int vmdk_pwritev(BlockDriverState *bs, uint6= 4_t offset, n_bytes =3D MIN(bytes, extent->cluster_sectors * BDRV_SECTOR_SIZE - offset_in_cluster); =20 - ret =3D get_cluster_offset(bs, extent, &m_data, offset, - !(extent->compressed || zeroed), - &cluster_offset, offset_in_cluster, - offset_in_cluster + n_bytes); + ret =3D vmdk_get_cluster_offset(bs, extent, &m_data, offset, + !(extent->compressed || zeroed), + &cluster_offset, offset_in_cluster, + offset_in_cluster + n_bytes); if (extent->compressed) { if (ret =3D=3D VMDK_OK) { /* Refuse write to allocated cluster for streamOptimized */ @@ -1596,8 +1596,8 @@ static int vmdk_pwritev(BlockDriverState *bs, uint64_= t offset, return -EIO; } else { /* allocate */ - ret =3D get_cluster_offset(bs, extent, &m_data, offset, - true, &cluster_offset, 0, 0); + ret =3D vmdk_get_cluster_offset(bs, extent, &m_data, offse= t, + true, &cluster_offset, 0, 0); } } if (ret =3D=3D VMDK_ERROR) { @@ -2229,9 +2229,9 @@ static int vmdk_check(BlockDriverState *bs, BdrvCheck= Result *result, sector_num); break; } - ret =3D get_cluster_offset(bs, extent, NULL, - sector_num << BDRV_SECTOR_BITS, - false, &cluster_offset, 0, 0); + ret =3D vmdk_get_cluster_offset(bs, extent, NULL, + sector_num << BDRV_SECTOR_BITS, + false, &cluster_offset, 0, 0); if (ret =3D=3D VMDK_ERROR) { fprintf(stderr, "ERROR: could not get cluster_offset for sector %" --=20 2.6.2 From nobody Sat May 4 04:18:47 2024 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.zohomail.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 1501166051223124.94201871577548; Thu, 27 Jul 2017 07:34:11 -0700 (PDT) Received: from localhost ([::1]:43371 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dajrd-0000yM-Ju for importer@patchew.org; Thu, 27 Jul 2017 10:34:09 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57881) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dajpb-0007xs-EK for qemu-devel@nongnu.org; Thu, 27 Jul 2017 10:32:04 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dajpX-0005uS-PG for qemu-devel@nongnu.org; Thu, 27 Jul 2017 10:32:03 -0400 Received: from mail-pg0-x242.google.com ([2607:f8b0:400e:c05::242]:38470) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dajpX-0005tf-Hd; Thu, 27 Jul 2017 10:31:59 -0400 Received: by mail-pg0-x242.google.com with SMTP id 125so7259289pgi.5; Thu, 27 Jul 2017 07:31:59 -0700 (PDT) Received: from linux.local ([27.251.197.195]) by smtp.gmail.com with ESMTPSA id y14sm32579540pfa.52.2017.07.27.07.31.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 27 Jul 2017 07:31:58 -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=S23tD4Cwip2bygna450dQJYyIoSn4FyMWgR/xS9iU0hu8kOP9id4gTdSZjGlsfyCdK 6YzTQa1RMPmWE5mw1wvskg6COa8uZEFRaZNgfN9og2zda+MjTS5DP7S6o+2skDJmxzjC rmbtKhwdSnRj9hb/cTV5UgE2dvc4TAqr4qaDs/QcX24D+e56knTnEW6R0BX1jSH1P/gs abn9jNDDsmL1iBt9MUeNux9JBzcMjsS/qcf/Iu1Rt2IRuqLipsgQWw3XhI0iyq76014p NobaeKYfpvrpA/XI+FaOX5IsOZSSvJ+11SeVlCjLYw4TFXmgHmBZoBmBi++bD7aO39nl iElw== 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=AQazwave0U8akjrzTQ4CTGlicJiEZf49/fS4UmHtokEs0tfdClbTzmjxWW3AB9i68B q2rXQzDH84gjFxHE7ujaC/gmmn4xdJ76ADyPCABqySSzR2v9Qq8NfsZ6D9kjOnoRWStl C68ktqd0G8dQHg7/7ymuWwb2fd5QAMleXZ3Xz09oDqGU41MrAcFHTyMEB7ADOjR59km2 EZOAyK035YYIjkBgeUahYJPfw/bY576NwAPIcn6NCHN3y2TrI3KAww1ZExvQeJ7N1XqG wfp4aEmClaESvfrQ9tufIVo8dvi3fkL2QzV+nOYfraSVeAf8BFz2wsz8534QwtD9O4nR KqsA== X-Gm-Message-State: AIVw112wn4pQY7A4S+VY+lErqgBXnQPMAulBGJxitvVp5O4ZFT5e7Qyo oA8En85XHF4oXg== X-Received: by 10.84.233.132 with SMTP id l4mr4564994plk.298.1501165918670; Thu, 27 Jul 2017 07:31:58 -0700 (PDT) From: Ashijeet Acharya To: famz@redhat.com Date: Thu, 27 Jul 2017 20:03:06 +0530 Message-Id: <1501165990-28033-5-git-send-email-ashijeetacharya@gmail.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1501165990-28033-1-git-send-email-ashijeetacharya@gmail.com> References: <1501165990-28033-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:c05::242 Subject: [Qemu-devel] [PATCH v8 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 From nobody Sat May 4 04:18:47 2024 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.zohomail.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 1501166356070742.0836344319362; Thu, 27 Jul 2017 07:39:16 -0700 (PDT) Received: from localhost ([::1]:43405 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dajwT-0006GK-8w for importer@patchew.org; Thu, 27 Jul 2017 10:39:09 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57942) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dajpf-000802-CM for qemu-devel@nongnu.org; Thu, 27 Jul 2017 10:32:08 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dajpb-0005wo-H1 for qemu-devel@nongnu.org; Thu, 27 Jul 2017 10:32:07 -0400 Received: from mail-pg0-x243.google.com ([2607:f8b0:400e:c05::243]:35782) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dajpb-0005wC-Bl; Thu, 27 Jul 2017 10:32:03 -0400 Received: by mail-pg0-x243.google.com with SMTP id d193so20698758pgc.2; Thu, 27 Jul 2017 07:32:03 -0700 (PDT) Received: from linux.local ([27.251.197.195]) by smtp.gmail.com with ESMTPSA id y14sm32579540pfa.52.2017.07.27.07.31.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 27 Jul 2017 07:32:01 -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=ZHvK2Vys1FDOQRWCQbXjjaMPx12sSGaapnbVk2Pi7bM=; b=b5jVC3I6RBHJZkn3RlFeW0BaQcyntNaxoFBvILbjkmHOWp+XuRV3NsGeNjByyWpwSr r4CVSPzckNDEmckB237LjSDnLGa4l5nYIYaK86jdEr3uLaduA7KxqjkPuPJZ4TL3rYOj sd9pIomNCP7WHN0Du6C7SebdWMPIxj+WU3hQXdlDXnEmT6r0/QNK/NWi/UMc7tjcrZfZ k1bHpvwKUpb+sh06TWbg7Q9CpwcLp3FjuxorahhromZGLz5eoVRIQr8FoCGfYVU7tmF0 tYCqlj7fX8vk6rMMqnKAV7R5o0Wi86CS6wVitGs2cv5OKBnki7i6tsJJpUSu8QZBxXVC DqvA== 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=ZHvK2Vys1FDOQRWCQbXjjaMPx12sSGaapnbVk2Pi7bM=; b=t3QVjMJe5F0p18cZ1tSmAasXPmNlBAB6lAy4NEz0/YTu/6ZSIfnYm+LhEWxATyQrJy 8os3ULd1uI9Sf6bOPWx/hKDUz7yipkrxlZZWbu3LSShMEfiOFocjEX1R3lDWQZ1Gu6Iq /dzZZVacw3DMgp+nOS/47Y+cpRGZoPqptYZ/NhNYCw9+ekgLYz4NfWG9ctApM9TTzJKu zUJMyelimja5m0QeBzFXNo/bbdgCB3+S62UgepKlfS5nDUuXvH9jddWH8WP4egqUKlp+ K9RFFx1TqI5QNqpZGX1Z5AmUvVL60W8eqlvbaKcts/mEuHbv7+Pv2ANk9PCTtHf1og+3 dWIA== X-Gm-Message-State: AIVw111cO/YOMXp0WD5v8wAZIue+utdIwCUUptjBFxo+UN82BDWLixbP 3XltnQrNK1pKGiTXWRk= X-Received: by 10.99.105.70 with SMTP id e67mr4156185pgc.77.1501165922442; Thu, 27 Jul 2017 07:32:02 -0700 (PDT) From: Ashijeet Acharya To: famz@redhat.com Date: Thu, 27 Jul 2017 20:03:07 +0530 Message-Id: <1501165990-28033-6-git-send-email-ashijeetacharya@gmail.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1501165990-28033-1-git-send-email-ashijeetacharya@gmail.com> References: <1501165990-28033-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:c05::243 Subject: [Qemu-devel] [PATCH v8 5/8] vmdk: Set maximum bytes allocated in one cycle 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" Set the maximum bytes allowed to get allocated at once to be not more than the extent size boundary to handle writes at two separate extents appropriately. Signed-off-by: Ashijeet Acharya Reviewed-by: Fam Zheng --- block/vmdk.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/block/vmdk.c b/block/vmdk.c index 5647f53..fe2046b 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -1624,6 +1624,7 @@ static int vmdk_pwritev(BlockDriverState *bs, uint64_= t offset, uint64_t cluster_offset; uint64_t bytes_done =3D 0; VmdkMetaData m_data; + uint64_t extent_end; =20 if (DIV_ROUND_UP(offset, BDRV_SECTOR_SIZE) > bs->total_sectors) { error_report("Wrong offset: offset=3D0x%" PRIx64 @@ -1637,9 +1638,17 @@ static int vmdk_pwritev(BlockDriverState *bs, uint64= _t offset, if (!extent) { return -EIO; } + extent_end =3D extent->end_sector * BDRV_SECTOR_SIZE; + offset_in_cluster =3D vmdk_find_offset_in_cluster(extent, offset); - n_bytes =3D MIN(bytes, extent->cluster_sectors * BDRV_SECTOR_SIZE - - offset_in_cluster); + + /* truncate n_bytes to first cluster because we need to perform CO= W */ + if (offset_in_cluster > 0) { + n_bytes =3D MIN(bytes, extent->cluster_sectors * BDRV_SECTOR_S= IZE + - offset_in_cluster); + } else { + n_bytes =3D MIN(bytes, extent_end - offset); + } =20 ret =3D vmdk_get_cluster_offset(bs, extent, &m_data, offset, !(extent->compressed || zeroed), --=20 2.6.2 From nobody Sat May 4 04:18:47 2024 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.zohomail.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 1501166251984703.8191012504782; Thu, 27 Jul 2017 07:37:31 -0700 (PDT) Received: from localhost ([::1]:43394 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dajus-0004WG-Gu for importer@patchew.org; Thu, 27 Jul 2017 10:37:30 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57960) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dajpg-00081y-Se for qemu-devel@nongnu.org; Thu, 27 Jul 2017 10:32:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dajpf-0005zt-Ch for qemu-devel@nongnu.org; Thu, 27 Jul 2017 10:32:08 -0400 Received: from mail-pg0-x243.google.com ([2607:f8b0:400e:c05::243]:34218) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dajpf-0005zB-3h; Thu, 27 Jul 2017 10:32:07 -0400 Received: by mail-pg0-x243.google.com with SMTP id v190so20734669pgv.1; Thu, 27 Jul 2017 07:32:06 -0700 (PDT) Received: from linux.local ([27.251.197.195]) by smtp.gmail.com with ESMTPSA id y14sm32579540pfa.52.2017.07.27.07.32.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 27 Jul 2017 07:32:05 -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=Zo+g3gU5DRhDxe00qpK6uWjWM80mVNyq6V88dALyOD0=; b=mWX5E2aN9sferwUtNWzxnofeuNiz+tavJgkyP4H+6gP5C5Gl/LIUNGSyLAfE3wcoc9 1FbhCkSbXTS/7tBxnY4YMsL6dahrAHU8uXfdZi/CRjABKqqQdcZqj++VK8qsqgRNUPTt 3q6D+MQnWJKpDSFYAPN40vhaOndagmcX4WeG+owB/5FqtSLaxHEIx27v8hoPwIoOP64M Hq8eNux5WJwoAcwLcq7MptxA1EcAqHS7/2qpt32KMYoRRsMNyTTtFu6aBrN8y+RlVpjj ks+mcRsQgqlCY/+KlEE/6JrpkBMNc7SqhM0R2RgujUzYQp+L6rYkSU7a2sBjlqc1bkXL t3sA== 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=Zo+g3gU5DRhDxe00qpK6uWjWM80mVNyq6V88dALyOD0=; b=F01+enhCUFO1TUdwrGJp9oM4d2D9BN6Qgc75VNpVuBsLc6Ikx7Eh1OTiB0Bc2/f+0k BpFmkxT6NrobMGUKOOFAP3mXwRq9U/gfPuiNKK9aaElHm5DrktJW/mYFcd+TJ5MC8tWS JQj+GbbKjE8QSRStYd5ZwMNcePy8ya6uru/ykx4cn4W+iv526YKZx2Mo9iNiciBHZvHm W/QzC4gkc35TgkXMHNi33/3C7Nn2Wo5iMVy7ETOEoDF4tu6CBjrrckoBlPn8Hl3pvEi3 wv9DFGP3/Irc7bPIP6eAUPBjdMb1z87EDQ07iBmIrzj12XKX6qYMiOSaoAQaWlAbxTjg g9dg== X-Gm-Message-State: AIVw112af/gnwga6jyvGeQpNsmDFI5mJV7vNBOPa83YseQKHwRGVf8la rbl45X/n9Zzs3w== X-Received: by 10.98.69.91 with SMTP id s88mr4191822pfa.183.1501165926092; Thu, 27 Jul 2017 07:32:06 -0700 (PDT) From: Ashijeet Acharya To: famz@redhat.com Date: Thu, 27 Jul 2017 20:03:08 +0530 Message-Id: <1501165990-28033-7-git-send-email-ashijeetacharya@gmail.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1501165990-28033-1-git-send-email-ashijeetacharya@gmail.com> References: <1501165990-28033-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:c05::243 Subject: [Qemu-devel] [PATCH v8 6/8] vmdk: New functions to assist allocating 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+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" Introduce two new helper functions handle_alloc() and vmdk_alloc_cluster_offset(). handle_alloc() helps to allocate multiple clusters at once starting from a given offset on disk and performs COW if necessary for first and last allocated clusters. vmdk_alloc_cluster_offset() helps to return the offset of the first of the many newly allocated clusters. Also, provide proper documentation for both. Signed-off-by: Ashijeet Acharya Reviewed-by: Fam Zheng --- block/vmdk.c | 201 +++++++++++++++++++++++++++++++++++++++++++++++++++++++= +--- 1 file changed, 191 insertions(+), 10 deletions(-) diff --git a/block/vmdk.c b/block/vmdk.c index fe2046b..5f27dbb 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -136,6 +136,7 @@ typedef struct VmdkMetaData { unsigned int l2_offset; int valid; uint32_t *l2_cache_entry; + uint32_t nb_clusters; } VmdkMetaData; =20 typedef struct VmdkGrainMarker { @@ -1242,6 +1243,183 @@ static int get_cluster_table(VmdkExtent *extent, ui= nt64_t offset, return VMDK_OK; } =20 +/* + * vmdk_handle_alloc + * + * Allocate new clusters for an area that either is yet unallocated or nee= ds a + * copy on write. + * + * Returns: + * VMDK_OK: if new clusters were allocated, *bytes may be decrease= d if + * the new allocation doesn't cover all of the requested = area. + * *cluster_offset is updated to contain the offset of the + * first newly allocated cluster. + * + * VMDK_UNALLOC: if no clusters could be allocated. *cluster_offset is = left + * unchanged. + * + * VMDK_ERROR: in error cases + */ +static int vmdk_handle_alloc(BlockDriverState *bs, VmdkExtent *extent, + uint64_t offset, uint64_t *cluster_offset, + int64_t *bytes, VmdkMetaData *m_data, + bool allocate, uint32_t *alloc_clusters_count= er) +{ + int l1_index, l2_offset, l2_index; + uint32_t *l2_table; + uint32_t cluster_sector; + uint32_t nb_clusters; + bool zeroed =3D false; + uint64_t skip_start_bytes, skip_end_bytes; + int ret; + + ret =3D get_cluster_table(extent, offset, &l1_index, &l2_offset, + &l2_index, &l2_table); + if (ret < 0) { + return ret; + } + + cluster_sector =3D le32_to_cpu(l2_table[l2_index]); + + skip_start_bytes =3D vmdk_find_offset_in_cluster(extent, offset); + /* Calculate the number of clusters to look for. Here we truncate the = last + * cluster, i.e. 1 less than the actual value calculated as we may nee= d to + * perform COW for the last one. */ + nb_clusters =3D DIV_ROUND_UP(skip_start_bytes + *bytes, + extent->cluster_sectors << BDRV_SECTOR_BITS= ) - 1; + + nb_clusters =3D MIN(nb_clusters, extent->l2_size - l2_index); + assert(nb_clusters <=3D INT_MAX); + + /* update bytes according to final nb_clusters value */ + if (nb_clusters !=3D 0) { + *bytes =3D ((nb_clusters * extent->cluster_sectors) << BDRV_SECTOR= _BITS) + - skip_start_bytes; + } else { + nb_clusters =3D 1; + } + *alloc_clusters_counter +=3D nb_clusters; + + /* we need to use MIN() for basically 3 cases that arise : + * 1. alloc very first cluster : here skip_start_bytes >=3D 0 and + * *bytes <=3D cluster_size. + * 2. alloc middle clusters : here *bytes is a perfect multiple of + * cluster_size and skip_start_bytes is 0. + * 3. alloc very last cluster : here *bytes <=3D cluster_size and + * skip_start_bytes is 0 + */ + skip_end_bytes =3D skip_start_bytes + MIN(*bytes, + extent->cluster_sectors * BDRV_SECTOR_SIZE + - skip_start_bytes); + + if (extent->has_zero_grain && cluster_sector =3D=3D VMDK_GTE_ZEROED) { + zeroed =3D true; + } + + if (!cluster_sector || zeroed) { + if (!allocate) { + return zeroed ? VMDK_ZEROED : VMDK_UNALLOC; + } + + cluster_sector =3D extent->next_cluster_sector; + extent->next_cluster_sector +=3D extent->cluster_sectors + * nb_clusters; + + ret =3D vmdk_perform_cow(bs, extent, cluster_sector * BDRV_SECTOR_= SIZE, + offset, skip_start_bytes, + skip_end_bytes); + if (ret < 0) { + return ret; + } + if (m_data) { + m_data->valid =3D 1; + m_data->l1_index =3D l1_index; + m_data->l2_index =3D l2_index; + m_data->l2_offset =3D l2_offset; + m_data->l2_cache_entry =3D &l2_table[l2_index]; + m_data->nb_clusters =3D nb_clusters; + } + } + *cluster_offset =3D cluster_sector << BDRV_SECTOR_BITS; + return VMDK_OK; +} + +/* + * vmdk_alloc_clusters + * + * For a given offset on the virtual disk, find the cluster offset in vmdk + * file. If the offset is not found, allocate a new cluster. + * + * If the cluster is newly allocated, m_data->nb_clusters is set to the nu= mber + * of contiguous clusters that have been allocated. In this case, the other + * fields of m_data are valid and contain information about the first allo= cated + * cluster. + * + * Returns: + * + * VMDK_OK: on success and @cluster_offset was set + * + * VMDK_UNALLOC: if no clusters were allocated and @cluster_offset = is + * set to zero + * + * VMDK_ERROR: in error cases + */ +static int vmdk_alloc_clusters(BlockDriverState *bs, + VmdkExtent *extent, + VmdkMetaData *m_data, uint64_t offset, + bool allocate, uint64_t *cluster_offset, + int64_t bytes, + uint32_t *alloc_clusters_counter) +{ + uint64_t start, remaining; + uint64_t new_cluster_offset; + int64_t n_bytes; + int ret; + + if (extent->flat) { + *cluster_offset =3D extent->flat_start_offset; + return VMDK_OK; + } + + start =3D offset; + remaining =3D bytes; + new_cluster_offset =3D 0; + *cluster_offset =3D 0; + n_bytes =3D 0; + if (m_data) { + m_data->valid =3D 0; + } + + /* due to L2 table margins all bytes may not get allocated at once */ + while (true) { + + if (!*cluster_offset) { + *cluster_offset =3D new_cluster_offset; + } + + start +=3D n_bytes; + remaining -=3D n_bytes; + new_cluster_offset +=3D n_bytes; + + if (remaining =3D=3D 0) { + break; + } + + n_bytes =3D remaining; + + ret =3D vmdk_handle_alloc(bs, extent, start, &new_cluster_offset, + &n_bytes, m_data, allocate, + alloc_clusters_counter); + + if (ret < 0) { + return ret; + + } + } + + return VMDK_OK; +} + /** * vmdk_get_cluster_offset * @@ -1625,6 +1803,7 @@ static int vmdk_pwritev(BlockDriverState *bs, uint64_= t offset, uint64_t bytes_done =3D 0; VmdkMetaData m_data; uint64_t extent_end; + uint32_t alloc_clusters_counter =3D 0; =20 if (DIV_ROUND_UP(offset, BDRV_SECTOR_SIZE) > bs->total_sectors) { error_report("Wrong offset: offset=3D0x%" PRIx64 @@ -1650,10 +1829,10 @@ static int vmdk_pwritev(BlockDriverState *bs, uint6= 4_t offset, n_bytes =3D MIN(bytes, extent_end - offset); } =20 - ret =3D vmdk_get_cluster_offset(bs, extent, &m_data, offset, - !(extent->compressed || zeroed), - &cluster_offset, offset_in_cluster, - offset_in_cluster + n_bytes); + ret =3D vmdk_alloc_clusters(bs, extent, &m_data, offset, + !(extent->compressed || zeroed), + &cluster_offset, n_bytes, + &alloc_clusters_counter); if (extent->compressed) { if (ret =3D=3D VMDK_OK) { /* Refuse write to allocated cluster for streamOptimized */ @@ -1662,8 +1841,9 @@ static int vmdk_pwritev(BlockDriverState *bs, uint64_= t offset, return -EIO; } else { /* allocate */ - ret =3D vmdk_get_cluster_offset(bs, extent, &m_data, offse= t, - true, &cluster_offset, 0, 0); + ret =3D vmdk_alloc_clusters(bs, extent, &m_data, offset, + true, &cluster_offset, n_bytes, + &alloc_clusters_counter); } } if (ret =3D=3D VMDK_ERROR) { @@ -1671,10 +1851,11 @@ static int vmdk_pwritev(BlockDriverState *bs, uint6= 4_t offset, } if (zeroed) { /* Do zeroed write, buf is ignored */ - if (extent->has_zero_grain && - offset_in_cluster =3D=3D 0 && - n_bytes >=3D extent->cluster_sectors * BDRV_SECTOR_SIZ= E) { - n_bytes =3D extent->cluster_sectors * BDRV_SECTOR_SIZE; + if (extent->has_zero_grain && offset_in_cluster =3D=3D 0 && + n_bytes >=3D extent->cluster_sectors * BDRV_SECTOR_SIZ= E * + alloc_clusters_counter) { + n_bytes =3D extent->cluster_sectors * BDRV_SECTOR_SIZE * + alloc_clusters_counter; if (!zero_dry_run) { /* update L2 tables */ if (vmdk_L2update(extent, &m_data, VMDK_GTE_ZEROED) --=20 2.6.2 From nobody Sat May 4 04:18:47 2024 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.zohomail.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 150116623170592.44355253642925; Thu, 27 Jul 2017 07:37:11 -0700 (PDT) Received: from localhost ([::1]:43393 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dajuY-0004IB-A6 for importer@patchew.org; Thu, 27 Jul 2017 10:37:10 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58044) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dajpl-00086d-Ti for qemu-devel@nongnu.org; Thu, 27 Jul 2017 10:32:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dajpi-00062n-Np for qemu-devel@nongnu.org; Thu, 27 Jul 2017 10:32:13 -0400 Received: from mail-pf0-x244.google.com ([2607:f8b0:400e:c00::244]:38910) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dajpi-000623-GH; Thu, 27 Jul 2017 10:32:10 -0400 Received: by mail-pf0-x244.google.com with SMTP id e3so736041pfc.5; Thu, 27 Jul 2017 07:32:10 -0700 (PDT) Received: from linux.local ([27.251.197.195]) by smtp.gmail.com with ESMTPSA id y14sm32579540pfa.52.2017.07.27.07.32.07 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 27 Jul 2017 07:32:09 -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=4n0Q6dDopdZLQLVXUGOvTZm6RmuThPpkbuVTo5TKpx4=; b=pP5WRhFmQ1cbshqVXThhvjeb7buklf9EQY7D3a4hNq14yL64xqv5jF7Zw04nLIgaw3 0YRukP3tF6QX7bj31JC4cNAI9aS5xiYiIBtGQwK/reZiFwbHjbwl5Qn6/+eXeNYGQ3rD 3myNo5sPbviReLvLjdN1+VJlKJ/4ccNCsdUq0up4EFUJFRupe2gpDNBvtamsuTo6y5GM rpRQHyW0AN7SmEPHenUkFstBKoiSe1FBrbk0enNpqrHIr7pFnv1PN8/7syOd4Vy9/yWs c1Me6KwQRyLvnGNpRRoqv8ezL3Tf2dYC4H7Fs+TYXbMizbN9CbAuLKznksLQrAQQ7jHX ZzOQ== 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=4n0Q6dDopdZLQLVXUGOvTZm6RmuThPpkbuVTo5TKpx4=; b=AO6IQqzBAqCSpoLr1hc7RWamJetL7dKKi1DiTcp4AlILL9ZOi2wxRC/Yy+muB1fiB/ d3Z4+xU9/UOG0gRKqdFoN8kO8yOdCWXY+bY+1BlK6fGxccNMAATeRm6fo3tRiuh9OJub 3+piLY9v/rOXJc/yQhP+F7AshkNSNBuPyFoxdM2YBqYw6rU19k8mZKynwqYANjQ10gZi g9ldAOSRB5BZ8OJfy9MSxTY1MRTAJQZnQFcWZ5KadMTcR7sufJD5K8brqqvzzFYAva8c E4OFnXASKeZClTYxuyjxE058Kkpji2hyh/G5oophHzU3p5zK+nBGbk58j9qL8G43eB2K Yv7w== X-Gm-Message-State: AIVw110GeaTtSlzYeq48jNlDml3aptFOKzGTutuveNeKH8O6+BTs9nhI tQFF5QF3i8ylng== X-Received: by 10.98.198.208 with SMTP id x77mr4285985pfk.229.1501165929631; Thu, 27 Jul 2017 07:32:09 -0700 (PDT) From: Ashijeet Acharya To: famz@redhat.com Date: Thu, 27 Jul 2017 20:03:09 +0530 Message-Id: <1501165990-28033-8-git-send-email-ashijeetacharya@gmail.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1501165990-28033-1-git-send-email-ashijeetacharya@gmail.com> References: <1501165990-28033-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::244 Subject: [Qemu-devel] [PATCH v8 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+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" 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 Reviewed-by: Fam Zheng --- block/vmdk.c | 128 ++++++++++++++++++++++++++++++++++++++++++++++---------= ---- 1 file changed, 101 insertions(+), 27 deletions(-) diff --git a/block/vmdk.c b/block/vmdk.c index 5f27dbb..4a59ca4 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; =20 typedef struct VmdkGrainMarker { @@ -1116,34 +1118,87 @@ exit: return ret; } =20 -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 =3D cpu_to_le32(offset); + int i; + uint32_t offset, temp_offset; + int *l2_table_array; + int l2_array_size; + + if (zeroed) { + temp_offset =3D VMDK_GTE_ZEROED; + } else { + temp_offset =3D m_data->offset; + } + + l2_array_size =3D sizeof(uint32_t) * m_data->nb_clusters; + l2_table_array =3D qemu_try_blockalign(extent->file->bs, + QEMU_ALIGN_UP(l2_array_size, + BDRV_SECTOR_SIZE)); + if (l2_table_array =3D=3D NULL) { + return VMDK_ERROR; + } + memset(l2_table_array, 0, QEMU_ALIGN_UP(l2_array_size, BDRV_SECTOR_SIZ= E)); /* update L2 table */ + offset =3D temp_offset; + for (i =3D 0; i < m_data->nb_clusters; i++) { + l2_table_array[i] =3D cpu_to_le32(offset); + if (!zeroed) { + offset +=3D extent->cluster_sectors; + } + } 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 !=3D 0) { m_data->l2_offset =3D 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 =3D temp_offset; if (m_data->l2_cache_entry) { - *m_data->l2_cache_entry =3D offset; + for (i =3D 0; i < m_data->nb_clusters; i++) { + *m_data->l2_cache_entry =3D cpu_to_le32(offset); + m_data->l2_cache_entry++; + + if (!zeroed) { + offset +=3D extent->cluster_sectors; + } + } } =20 + qemu_vfree(l2_table_array); return VMDK_OK; } =20 +static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data, + bool zeroed) +{ + int ret; + + while (m_data->next !=3D NULL) { + + ret =3D vmdk_alloc_cluster_link_l2(extent, m_data, zeroed); + if (ret < 0) { + return ret; + } + + m_data =3D m_data->next; + } + + return VMDK_OK; +} + /* * vmdk_l2load * @@ -1260,9 +1315,10 @@ static int get_cluster_table(VmdkExtent *extent, uin= t64_t offset, * * VMDK_ERROR: in error cases */ + static int vmdk_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 *alloc_clusters_count= er) { int l1_index, l2_offset, l2_index; @@ -1271,6 +1327,7 @@ static int vmdk_handle_alloc(BlockDriverState *bs, Vm= dkExtent *extent, uint32_t nb_clusters; bool zeroed =3D false; uint64_t skip_start_bytes, skip_end_bytes; + VmdkMetaData *old_m_data; int ret; =20 ret =3D get_cluster_table(extent, offset, &l1_index, &l2_offset, @@ -1331,13 +1388,21 @@ static int vmdk_handle_alloc(BlockDriverState *bs, = VmdkExtent *extent, if (ret < 0) { return ret; } - if (m_data) { - m_data->valid =3D 1; - m_data->l1_index =3D l1_index; - m_data->l2_index =3D l2_index; - m_data->l2_offset =3D l2_offset; - m_data->l2_cache_entry =3D &l2_table[l2_index]; - m_data->nb_clusters =3D nb_clusters; + + if (m_data !=3D NULL) { + old_m_data =3D *m_data; + *m_data =3D g_malloc0(sizeof(**m_data)); + + **m_data =3D (VmdkMetaData) { + .valid =3D 1, + .l1_index =3D l1_index, + .l2_index =3D l2_index, + .l2_offset =3D l2_offset, + .l2_cache_entry =3D &l2_table[l2_index], + .nb_clusters =3D nb_clusters, + .offset =3D cluster_sector, + .next =3D old_m_data, + }; } } *cluster_offset =3D cluster_sector << BDRV_SECTOR_BITS; @@ -1366,7 +1431,7 @@ static int vmdk_handle_alloc(BlockDriverState *bs, Vm= dkExtent *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 *alloc_clusters_counter) @@ -1386,8 +1451,8 @@ static int vmdk_alloc_clusters(BlockDriverState *bs, new_cluster_offset =3D 0; *cluster_offset =3D 0; n_bytes =3D 0; - if (m_data) { - m_data->valid =3D 0; + if (*m_data) { + (*m_data)->valid =3D 0; } =20 /* due to L2 table margins all bytes may not get allocated at once */ @@ -1801,10 +1866,12 @@ static int vmdk_pwritev(BlockDriverState *bs, uint6= 4_t offset, int64_t offset_in_cluster, n_bytes; uint64_t cluster_offset; uint64_t bytes_done =3D 0; - VmdkMetaData m_data; uint64_t extent_end; + VmdkMetaData *m_data; uint32_t alloc_clusters_counter =3D 0; =20 + m_data =3D g_new0(VmdkMetaData, 1); + if (DIV_ROUND_UP(offset, BDRV_SECTOR_SIZE) > bs->total_sectors) { error_report("Wrong offset: offset=3D0x%" PRIx64 " total_sectors=3D0x%" PRIx64, @@ -1813,6 +1880,7 @@ static int vmdk_pwritev(BlockDriverState *bs, uint64_= t offset, } =20 while (bytes > 0) { + m_data->next =3D NULL; extent =3D find_extent(s, offset >> BDRV_SECTOR_BITS, extent); if (!extent) { return -EIO; @@ -1858,7 +1926,7 @@ static int vmdk_pwritev(BlockDriverState *bs, uint64_= t offset, alloc_clusters_counter; if (!zero_dry_run) { /* update L2 tables */ - if (vmdk_L2update(extent, &m_data, VMDK_GTE_ZEROED) + if (vmdk_L2update(extent, m_data, zeroed) !=3D VMDK_OK) { return -EIO; } @@ -1872,11 +1940,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) - !=3D VMDK_OK) { + if (vmdk_L2update(extent, m_data, zeroed) !=3D VMDK_OK) { return -EIO; } } @@ -1885,6 +1951,13 @@ static int vmdk_pwritev(BlockDriverState *bs, uint64= _t offset, offset +=3D n_bytes; bytes_done +=3D n_bytes; =20 + while (m_data->next !=3D NULL) { + VmdkMetaData *next; + next =3D m_data->next; + g_free(m_data); + m_data =3D next; + } + /* update CID on the first write every time the virtual disk is * opened */ if (!s->cid_updated) { @@ -1895,6 +1968,7 @@ static int vmdk_pwritev(BlockDriverState *bs, uint64_= t offset, s->cid_updated =3D true; } } + g_free(m_data); return 0; } =20 --=20 2.6.2 From nobody Sat May 4 04:18:47 2024 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.zohomail.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 150116621841735.03790939879627; Thu, 27 Jul 2017 07:36:58 -0700 (PDT) Received: from localhost ([::1]:43391 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dajuH-00044e-FN for importer@patchew.org; Thu, 27 Jul 2017 10:36:53 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58082) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dajpn-000886-Bg for qemu-devel@nongnu.org; Thu, 27 Jul 2017 10:32:16 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dajpm-00066o-Dr for qemu-devel@nongnu.org; Thu, 27 Jul 2017 10:32:15 -0400 Received: from mail-pg0-x244.google.com ([2607:f8b0:400e:c05::244]:35795) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dajpm-000662-6e; Thu, 27 Jul 2017 10:32:14 -0400 Received: by mail-pg0-x244.google.com with SMTP id d193so20699253pgc.2; Thu, 27 Jul 2017 07:32:14 -0700 (PDT) Received: from linux.local ([27.251.197.195]) by smtp.gmail.com with ESMTPSA id y14sm32579540pfa.52.2017.07.27.07.32.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 27 Jul 2017 07:32:12 -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=tM8UOu8eAzwq/xiojBsV091KEnmCM0u0ncyRbQWidxk=; b=Le8/9UFWEeM46lk+LlGZOW2jUQ5ftBerC8Si+IPTbStrfkuHGqbcKoHqqMb+DnRycl sJZ1+pmzjydEFGMBmx/DJ54GQ632trLz0Mw8WjMSmmrsgR/Xa2OJWeQ+lMPRjomhsMPy ipK2ngBQaug8+wF8k1GdZCIHmO1ulXYkc9G1CknQglqHT837WyDl3uD9ldE5ojJpenFS JKPYo+yPYo+ncfzPomrYqsFlPHOo+FSemRl1zJHzXgVcaAqrUrwuoEGe1t8nSaHwBZXy 6H2l5GIHOj01/vw8NFIDffuGaPmGzaIvJvhin7Iu2yztA21cSQ5O6Y1qAsnb4pL1kOwE Z/GA== 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=tM8UOu8eAzwq/xiojBsV091KEnmCM0u0ncyRbQWidxk=; b=FkM+shQFENlUQ579Xcd1zbEhhRN0iewqSOWo/SMewd70D2LiLYmROBa548+FffmJSA KcbLGc4z8fQuuDyVYOgogSlXZgjbFv5DOssvJkCJMI7E0tPPOX24oMTM9CYTR1SBIOmL weoWgVe21Ey2WgbXXGLh0Oqt2zN44V1W4rWkyWuo7FlQLT5wgto9/909pskdYIEzF5/H iaPp7VV5KwXz0xLvggKAy+vEazdq3bG7CngRk7WFVlSiHH79oWqKWJk6r4toFu1+24+D F51edxOjCQA51R4NSC0XAmVpRmP2Iq4Wsm3gIyFqyfMs5qZv7GZ+PuQr5VFAMkh8Xxq1 rVSQ== X-Gm-Message-State: AIVw113HB8EL8ARpyyCq1hZuHDaJGP9ptKTn/QkrtROwlPhlNPocOuNT EHjfZ4rAG6CN2g== X-Received: by 10.98.9.135 with SMTP id 7mr4347530pfj.15.1501165933211; Thu, 27 Jul 2017 07:32:13 -0700 (PDT) From: Ashijeet Acharya To: famz@redhat.com Date: Thu, 27 Jul 2017 20:03:10 +0530 Message-Id: <1501165990-28033-9-git-send-email-ashijeetacharya@gmail.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1501165990-28033-1-git-send-email-ashijeetacharya@gmail.com> References: <1501165990-28033-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:c05::244 Subject: [Qemu-devel] [PATCH v8 8/8] vmdk: Make vmdk_get_cluster_offset() return cluster offset only 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" vmdk_alloc_clusters() introduced earlier now handles the task of allocating clusters and performing COW when needed. Thus we can change vmdk_get_cluster_offset() to stick to the sole purpose of returning cluster offset using sector number. Update the changes at all call sites. Signed-off-by: Ashijeet Acharya Reviewed-by: Fam Zheng --- block/vmdk.c | 56 ++++++++++++-------------------------------------------- 1 file changed, 12 insertions(+), 44 deletions(-) diff --git a/block/vmdk.c b/block/vmdk.c index 4a59ca4..a84b26c 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -1494,25 +1494,16 @@ static int vmdk_alloc_clusters(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, retur= n an - * offset for a new cluster and update L2 cache. If there is a backing fil= e, - * COW is done before returning; otherwise, zeroes are written to the allo= cated - * cluster. Both COW and zero writing skips the sector range - * [@skip_start_sector, @skip_end_sector) passed in by caller, because cal= ler - * has new data to write there. + * For sparse extents, look up the 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 vmdk_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) + uint64_t *cluster_offset) { int l1_index, l2_offset, l2_index; uint32_t *l2_table; @@ -1537,31 +1528,9 @@ static int vmdk_get_cluster_offset(BlockDriverState = *bs, } =20 if (!cluster_sector || zeroed) { - if (!allocate) { - return zeroed ? VMDK_ZEROED : VMDK_UNALLOC; - } - - cluster_sector =3D extent->next_cluster_sector; - extent->next_cluster_sector +=3D 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 di= sk - * or inappropriate VM shutdown. - */ - ret =3D 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 =3D 1; - m_data->l1_index =3D l1_index; - m_data->l2_index =3D l2_index; - m_data->l2_offset =3D l2_offset; - m_data->l2_cache_entry =3D &l2_table[l2_index]; - } + return zeroed ? VMDK_ZEROED : VMDK_UNALLOC; } + *cluster_offset =3D cluster_sector << BDRV_SECTOR_BITS; return VMDK_OK; } @@ -1604,9 +1573,7 @@ static int64_t coroutine_fn vmdk_co_get_block_status(= BlockDriverState *bs, return 0; } qemu_co_mutex_lock(&s->lock); - ret =3D vmdk_get_cluster_offset(bs, extent, NULL, - sector_num * 512, false, &offset, - 0, 0); + ret =3D vmdk_get_cluster_offset(bs, extent, sector_num * 512, &offset); qemu_co_mutex_unlock(&s->lock); =20 index_in_cluster =3D vmdk_find_index_in_cluster(extent, sector_num); @@ -1797,13 +1764,14 @@ vmdk_co_preadv(BlockDriverState *bs, uint64_t offse= t, uint64_t bytes, ret =3D -EIO; goto fail; } - ret =3D vmdk_get_cluster_offset(bs, extent, NULL, - offset, false, &cluster_offset, 0, 0= ); + offset_in_cluster =3D vmdk_find_offset_in_cluster(extent, offset); =20 n_bytes =3D MIN(bytes, extent->cluster_sectors * BDRV_SECTOR_SIZE - offset_in_cluster); =20 + ret =3D vmdk_get_cluster_offset(bs, extent, offset, &cluster_offse= t); + if (ret !=3D VMDK_OK) { /* if not allocated, try to read from parent image, if exist */ if (bs->backing && ret !=3D VMDK_ZEROED) { @@ -2550,9 +2518,9 @@ static int vmdk_check(BlockDriverState *bs, BdrvCheck= Result *result, sector_num); break; } - ret =3D vmdk_get_cluster_offset(bs, extent, NULL, + ret =3D vmdk_get_cluster_offset(bs, extent, sector_num << BDRV_SECTOR_BITS, - false, &cluster_offset, 0, 0); + &cluster_offset); if (ret =3D=3D VMDK_ERROR) { fprintf(stderr, "ERROR: could not get cluster_offset for sector %" --=20 2.6.2