From nobody Wed Nov 5 16:43:36 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; dkim=fail spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1496649396323131.14159888092536; Mon, 5 Jun 2017 00:56:36 -0700 (PDT) Received: from localhost ([::1]:60145 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dHmsJ-0007Gv-IE for importer@patchew.org; Mon, 05 Jun 2017 03:56:31 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39835) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dHmqY-00063D-QU for qemu-devel@nongnu.org; Mon, 05 Jun 2017 03:54:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dHmqX-0002Sj-Rz for qemu-devel@nongnu.org; Mon, 05 Jun 2017 03:54:42 -0400 Received: from mail-pg0-x244.google.com ([2607:f8b0:400e:c05::244]:36819) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dHmqX-0002Rs-MC; Mon, 05 Jun 2017 03:54:41 -0400 Received: by mail-pg0-x244.google.com with SMTP id v18so5611592pgb.3; Mon, 05 Jun 2017 00:54:41 -0700 (PDT) Received: from linux.local ([47.247.14.139]) by smtp.gmail.com with ESMTPSA id j71sm11775537pge.63.2017.06.05.00.54.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 05 Jun 2017 00:54:40 -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=BkZ1D9UUO5Zn4yGFmqFglamEdhGMXkZj0BULc5jtpMPFirHd+phSQSnBEqGDDodg/z 8jRR9UEiHQACNzhWUW+llAm3oJmEki6orGcIYxaGdcVdWKAf1M0mjFTBYWDaytCFWxPo U/iWoSRRwqlFi0Jq87SrAYBJGRzvHjV2GkArBvAx1Uq1QcUvK/6bh28p5CKqT4hRFgB5 jviLmJol0TZboOUv94UAazbf90DPHpPCOc9lipgDmnEYYyg1AIYsTVys3Xu5DUF3shIV Fn3EcremxsHYDSC+auLQzToizpRcGxpLwUYGyy1/dLbBkOsJR7OHjnV7JGjF+U0zRIGO rxww== 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=S6fAwaeFUbbvGY36QkxyfsoTda2jeoObNzrEiXVl9Dkh2wjJ5qLdPi8oIDXyjYTVP1 tkW6gmpDSGnMEhj4D/tVf5Hsxj0Yt6CZKeqF/YCnWXJot2xadAZwC9vmrQc1YokCOv1R tXccUOMIOLvXPf9t4wMzd9cgkswMq+3pl0V/fW4CmSzMPfk2+A1Vf0678H/85/29NYru Ws/gsHHpr9RCNePpe/vd6KusLtoWss/Ui5/bRSXQR11fJYEq5UmWGVnirN+RJcZ6n+eF vPaTWl7dBsjWhfTfjjjluXUzjiU65abwLrMnpKr3IjgyEG7sKkZn7NSS+gmTYN2mrdbG 7aHg== X-Gm-Message-State: AODbwcBMOeKOZmvNj6P16iskdX86eNql5z68eD9FjiJTiphKAvWNz/aM L/ydR4bYynY1ag== X-Received: by 10.98.69.68 with SMTP id s65mr19288933pfa.178.1496649280724; Mon, 05 Jun 2017 00:54:40 -0700 (PDT) From: Ashijeet Acharya To: famz@redhat.com Date: Mon, 5 Jun 2017 13:22:45 +0530 Message-Id: <1496649172-26982-2-git-send-email-ashijeetacharya@gmail.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1496649172-26982-1-git-send-email-ashijeetacharya@gmail.com> References: <1496649172-26982-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 v6 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 Wed Nov 5 16:43:36 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; dkim=fail spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1496649403921958.3888696967457; Mon, 5 Jun 2017 00:56:43 -0700 (PDT) Received: from localhost ([::1]:60146 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dHmsU-0007P4-DA for importer@patchew.org; Mon, 05 Jun 2017 03:56:42 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39858) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dHmqh-00069G-Nr for qemu-devel@nongnu.org; Mon, 05 Jun 2017 03:54:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dHmqe-0002Yp-Lb for qemu-devel@nongnu.org; Mon, 05 Jun 2017 03:54:51 -0400 Received: from mail-pg0-x242.google.com ([2607:f8b0:400e:c05::242]:34651) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dHmqe-0002Xj-GM; Mon, 05 Jun 2017 03:54:48 -0400 Received: by mail-pg0-x242.google.com with SMTP id v14so5261418pgn.1; Mon, 05 Jun 2017 00:54:48 -0700 (PDT) Received: from linux.local ([47.247.14.139]) by smtp.gmail.com with ESMTPSA id j71sm11775537pge.63.2017.06.05.00.54.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 05 Jun 2017 00:54: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=y0aLdU8eyizcBISOMqtI7Bfk7gF3uIFfg+flRUveKjg=; b=We6uKc/kPvHAECeGjbYNrpcekoWaZGw3dNmFv1XfLdYv8oGX8s50yuh43ragktxIP0 ncsemT673kIDtu+7dwk1TJBtqj+bfbq7z2+D8yW/91vpRDsHyYqtbFXo/CDUKfyOnEX/ 25+XMM06NcS6E1qAmv4cPTDPJIGO/wzfprdDqwmpa7JAtwuk0kJY6XjcyQAIK8haMjkT vEhsoBi7jr5bGZmsf3rPNI3qzltLTPKzfQO72haAzd2Sp8bq8NxUePn66DfgNGqogAzN dvJakfmv27qWPkRV3OZabbqHolnMCkrg043S5GjLA7bZc7RNiigRKbCUgYkeh+dtryiu jDSA== 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=osRdyI2ZE58i3zz2adDjXq9saoHQSUMnyhTYZsBJIoLVCHnZdHhTvAXdH4iWFn2+As G3rye/edBp6RJhc+ZLyhTCYMbrfkWNlylUCEGm8C1SuP2eSNYd71ZmglE1l7iovpDeHf CSQLLuZkaudzWseMBwx2Djl1xdW787G+F+sW6fZcSKeiEycVWFefgOjhLJBVZGTLWor6 B863OAYpN5mkUAJeHaaNDRs7nSNVdFKY3LDST9hUPJldFVApZKYIjN8pXzAuHLmNGTfT xoDxN5xKtvjCzpETU5Lnl0TDeOEz21TR/VPezrAdeUhmZrGaWuLrIzgvuTm64KIHnsHz NPOw== X-Gm-Message-State: AODbwcDPyDUVPBvn1I9D1mPN+nrbfviUBNRKcPUPwsUcNUlhsyK2dV12 WR8HiytuwW/yqQ== X-Received: by 10.99.247.83 with SMTP id f19mr20223429pgk.190.1496649287496; Mon, 05 Jun 2017 00:54:47 -0700 (PDT) From: Ashijeet Acharya To: famz@redhat.com Date: Mon, 5 Jun 2017 13:22:46 +0530 Message-Id: <1496649172-26982-3-git-send-email-ashijeetacharya@gmail.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1496649172-26982-1-git-send-email-ashijeetacharya@gmail.com> References: <1496649172-26982-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 v6 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 Wed Nov 5 16:43:36 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; dkim=fail spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1496649529627573.6862332818789; Mon, 5 Jun 2017 00:58:49 -0700 (PDT) Received: from localhost ([::1]:60160 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dHmuW-0001Lm-6j for importer@patchew.org; Mon, 05 Jun 2017 03:58:48 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39885) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dHmql-0006Cz-Mw for qemu-devel@nongnu.org; Mon, 05 Jun 2017 03:54:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dHmqk-0002fc-OA for qemu-devel@nongnu.org; Mon, 05 Jun 2017 03:54:55 -0400 Received: from mail-pg0-x244.google.com ([2607:f8b0:400e:c05::244]:34660) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dHmqk-0002eu-G0; Mon, 05 Jun 2017 03:54:54 -0400 Received: by mail-pg0-x244.google.com with SMTP id v14so5261656pgn.1; Mon, 05 Jun 2017 00:54:54 -0700 (PDT) Received: from linux.local ([47.247.14.139]) by smtp.gmail.com with ESMTPSA id j71sm11775537pge.63.2017.06.05.00.54.48 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 05 Jun 2017 00:54:53 -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=cPy1bR+YZsuqQ5C8FVYPeJqOzO+CM15b7GwUTFalwiiWt/I0cFwM3fxudFZ6wOyjdq 4xtPya9oeFFmAlrrIVNUaWeUrLSBpBnMx5PWyBhJi3YouKUYIhDHZ2OcjYs+7KPdu4BK oO7J82kU56n+RDfUMzk9K9ySJJ6Gf/B4+NpqyNcxvm8AtX+ZZ0T5KoERb9/SWiglhfmm 7uLsm3lzowwPf7NxUTK+k+ICnDYf4l2YoyfT2o8uzpkdJCwzGI5ydrwB0Em76IMQLpJH m8BtCzRuiqkjq1qndcEsYZ47DBsTHPMAofScss5C3NxHlL5OtIVRa/7IbGk24IMbrcUX i6Dw== 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=TcBa2pP+07LXwtab9/jQIAYyLv+e/2sqTjB5jfuVF6/iPk338H1vP9QooYf1eileeP YDzoSwYTTw9PeucQvS7nuHK4rhM1+BjvnPD4ggmpZC/3B95BJjbcletDf73Skkztwv+v 8NG21CZtczB0Nxr4PeSzjbXxXGQhByZc5hGEH9K5mwbrg6p2Fab9j0Z8JYyZ0loUlfQN 1gULGYlOrLHTUi+ezWr8jVe7kYoxTRA5W/5l0s4+H4ZgxAb1yP0+S2gQjQk7a//EmZg+ LFWbEx9wmGEwW696qXGqno4OC0IJlbhO/Pa55qb9+UhJeJoVyne58RLyP6L2uHTr63t2 T3Nw== X-Gm-Message-State: AODbwcDD+l418du/z8LRrjxsjrTBkLKA3Ekkbi6bHYkajIR0GPH6mFpZ M5T/JE0E28lwcA== X-Received: by 10.99.55.68 with SMTP id g4mr20143467pgn.115.1496649293579; Mon, 05 Jun 2017 00:54:53 -0700 (PDT) From: Ashijeet Acharya To: famz@redhat.com Date: Mon, 5 Jun 2017 13:22:47 +0530 Message-Id: <1496649172-26982-4-git-send-email-ashijeetacharya@gmail.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1496649172-26982-1-git-send-email-ashijeetacharya@gmail.com> References: <1496649172-26982-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 v6 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 Wed Nov 5 16:43:36 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; dkim=fail spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1496649624565813.0840885829284; Mon, 5 Jun 2017 01:00:24 -0700 (PDT) Received: from localhost ([::1]:60175 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dHmw2-0002gd-Pm for importer@patchew.org; Mon, 05 Jun 2017 04:00:22 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39934) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dHmqs-0006JV-QO for qemu-devel@nongnu.org; Mon, 05 Jun 2017 03:55:05 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dHmqr-0002oB-Ec for qemu-devel@nongnu.org; Mon, 05 Jun 2017 03:55:02 -0400 Received: from mail-pg0-x244.google.com ([2607:f8b0:400e:c05::244]:33723) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dHmqr-0002nT-7q; Mon, 05 Jun 2017 03:55:01 -0400 Received: by mail-pg0-x244.google.com with SMTP id a70so608421pge.0; Mon, 05 Jun 2017 00:55:01 -0700 (PDT) Received: from linux.local ([47.247.14.139]) by smtp.gmail.com with ESMTPSA id j71sm11775537pge.63.2017.06.05.00.54.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 05 Jun 2017 00:54:59 -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=nvvyK4hdHlDWLWQYKzfuS0ec9d9yQflcvaMOEdHrHxQfb1t7ehWm9jhxQ3p3JqzI4T v/0Zdyye1GhIlJarTCamSkmjwI3mUb1+MaVEiA8I0rEW1AcUO4CWQ67rFy0oPhkQfs7f Pb5JUP2yU4W3aa68coKMvUdHmeMW7lf7TYOZHwCXqRe8yBrGXLdwHZ/6PxeRJSAPabtk lQ+ViLn2wCxDLY0mdi1Hc94JwOennxAmBlNlhGLaKqlsK/i9sCX0bSv2rsgpV2xhts33 lnRSWDH4RpH5AhmVCBT/bxWGX/fEzQo0IyHjJx9GKBzBhSe0q4StclkzwtksuDckmC3a /VOA== 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=Xdbe4tKwtpCEuEdWmqffGE3Js1ml1FjbD8wf9N6gU2ugcpbTmkKuDG7u17VS1CtSo6 XiON92qvpx3gk2DEEnBqPsv+XCuTif5jy/YF85wG9XKZ9i0MOY7aL2EGzQHB4DZQ/p1A rCcvqIjTXSnRBtCHZzW5vHFJzhnf/Dj7Qlc6I0JgCJ3YarjofiNdWwYWd6KiMbTEi8LA +SaAY51sM8WakaBrmEFTrBOiaaV95beoKwJZNFJ4AqbsIqFxhVVitKiY9DUkKHUTwZHC BCvyEy8JReAtpwb6x2UvRBkut+my0u/SNakzNDsTStXe+fvFU0nNj4rq+c909AuLUbyB jBog== X-Gm-Message-State: AODbwcBS5qUVh0W3P6FX9ILiSbRe1h/bXsTRr2WCTv030XDOVOxt9ZC4 20Ndel0QdDOh3g== X-Received: by 10.98.163.221 with SMTP id q90mr9142189pfl.64.1496649300322; Mon, 05 Jun 2017 00:55:00 -0700 (PDT) From: Ashijeet Acharya To: famz@redhat.com Date: Mon, 5 Jun 2017 13:22:48 +0530 Message-Id: <1496649172-26982-5-git-send-email-ashijeetacharya@gmail.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1496649172-26982-1-git-send-email-ashijeetacharya@gmail.com> References: <1496649172-26982-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 v6 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 Wed Nov 5 16:43:36 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; dkim=fail spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1496649423264251.4686483254826; Mon, 5 Jun 2017 00:57:03 -0700 (PDT) Received: from localhost ([::1]:60147 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dHmsn-0007gc-JV for importer@patchew.org; Mon, 05 Jun 2017 03:57:01 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39995) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dHmqz-0006Qh-UH for qemu-devel@nongnu.org; Mon, 05 Jun 2017 03:55:11 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dHmqx-0002y0-Jk for qemu-devel@nongnu.org; Mon, 05 Jun 2017 03:55:10 -0400 Received: from mail-pg0-x244.google.com ([2607:f8b0:400e:c05::244]:35834) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dHmqx-0002vN-F5; Mon, 05 Jun 2017 03:55:07 -0400 Received: by mail-pg0-x244.google.com with SMTP id f127so8879724pgc.2; Mon, 05 Jun 2017 00:55:07 -0700 (PDT) Received: from linux.local ([47.247.14.139]) by smtp.gmail.com with ESMTPSA id j71sm11775537pge.63.2017.06.05.00.55.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 05 Jun 2017 00:55:06 -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=SIRKxbdu/OUHeLMgWd9K0FRC9WdFF0AUeGwR8A1Jsa7DY5pZKYSbruRe+zjXhMfpXY esISVi+68iCJ/osDltkmqPCFuUxX6FlJMVias/wkA584W+PTN6HpQaLJCe7rOeshJ9N1 bQIxOk9C2pHYfOHwdWxpoYYezWanYT524kzBTBC+ZwKaZ64WzkSMKVIkZ8/E0HJ/or7N y2x2TqqJmr7KVD1iSJMv+hgKvf2R5XTwtIsOq/cayNnYqzGht5C8hldL20YDd/j6pnX0 YLUysIyBrVE9To/LK2Vmd/Q0KRjkwxou8hezblMNYLYxJfPZ4Fg1t8u/hoB69wtPDdSz oZyw== 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=eq7bVGumrvjY2oDvl7hC5wGWI0K832diftfIYY+nAPfwegpNOZQaDfMR0EiijuKicP Kl4UqbT81Etm9DQm1n1sWwJqu1YYpnA4stO5gOleXnyd2pWQp1PmroE6fBxe3nYUzSBU B6AmSYMU8u/VS/JPbWDums2WRM09hGIezACAQjzLo0rJixxuMLRrhokeHuDlyfrCaibO N56HYEZakQPm81V3cMMYkOYDEJ03lNjy/iRXfB/WB3ksswhDcv0fgoSWIpfCyHFsGd5s JREAeUYdWDijALRyiWzjlxhyT2NHDe7+dJJkcxBH18D9pKWU6fXO92U4DwJQx/FNWW+I iHFg== X-Gm-Message-State: AODbwcAGmvv7X7J2u+7oYYcfPgSygUTz+44f1495p/NoIH57LC8wDAKu 8Qwb2sTqWMz5p1QPg4Y= X-Received: by 10.98.65.213 with SMTP id g82mr19222329pfd.187.1496649306622; Mon, 05 Jun 2017 00:55:06 -0700 (PDT) From: Ashijeet Acharya To: famz@redhat.com Date: Mon, 5 Jun 2017 13:22:49 +0530 Message-Id: <1496649172-26982-6-git-send-email-ashijeetacharya@gmail.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1496649172-26982-1-git-send-email-ashijeetacharya@gmail.com> References: <1496649172-26982-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 v6 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 Wed Nov 5 16:43:36 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; dkim=fail spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1496649552376392.3150848576479; Mon, 5 Jun 2017 00:59:12 -0700 (PDT) Received: from localhost ([::1]:60162 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dHmut-0001eA-2S for importer@patchew.org; Mon, 05 Jun 2017 03:59:11 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40070) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dHmr8-0006aL-FS for qemu-devel@nongnu.org; Mon, 05 Jun 2017 03:55:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dHmr5-0003C2-CD for qemu-devel@nongnu.org; Mon, 05 Jun 2017 03:55:18 -0400 Received: from mail-pf0-x241.google.com ([2607:f8b0:400e:c00::241]:36740) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dHmr5-00039c-4T; Mon, 05 Jun 2017 03:55:15 -0400 Received: by mail-pf0-x241.google.com with SMTP id y7so1258137pfd.3; Mon, 05 Jun 2017 00:55:14 -0700 (PDT) Received: from linux.local ([47.247.14.139]) by smtp.gmail.com with ESMTPSA id j71sm11775537pge.63.2017.06.05.00.55.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 05 Jun 2017 00:55:13 -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=xVEzyv3K9IzvDVWAMKclGGocDp6mGLMd1GK37bL4/9U=; b=JD+/4QCps6AwZyCllVtfvwa4cJtMgZrD/z6EQ2rxXsuIZ/ImogZBbh4u2N3wPpUkRI YhY2CwPtH6BGLUHELE/XD3C9ZhmbrEeejWVoonqegE6BHO4g7m6O0KLs1zj7AHQFVamR tX3Dl0RDs9/+0yloqjFs7P3boRMzKN6rHtoNXJT5ZLXJTSoP1HWTUK1A1xzikiywV600 CCTe6uxpFdI2SLMRKet85eq6zk3jcVXQYbDIEeWGomHTXgeMwRJ69uRm+Cc25Ht+PswX rNibeIDjBb24KvoTI+9GV0sTabG2Bo5ILgFuYJ7ioWtMS6hC0s7z60GFqUFj8SkJth9y Zchw== 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=xVEzyv3K9IzvDVWAMKclGGocDp6mGLMd1GK37bL4/9U=; b=ZyTbi7T9tYbeWDiOnXj9Wsov0vSGlZYEkcayaOKeteUSYPCV0tssOuiB8tbuzBPuE1 LfzFCSzLG2/evB6iZP9kkMBgdsUBpC4Cet5VenTiCkAt2JoLFpmy3QR9ojBhS4cVrdv8 xH1iBULSW2xWum/79vcmPkdqKZWZJA5yT570EzeXOCENlHOG9urbq0P0c2BTd47gq7vw zT1nEqWYlpj8dzZ13tZeEUYA10CWGIHCv+T+3vuaC1QcdJQs5UVWa8uV+yKwz966xqWc 2U2LDYVGpHHmSf9meiUJ6QtOqPW5AIpojOJ4gL6dp4S9OmtT8r+9IGlCnwXyLmQVwcXY TsVA== X-Gm-Message-State: AODbwcAY2Y9luC8lLQTe4/c5JHsPPBh3w8U6uB5E4+V7DgFWmzfM/79/ iW5xRIptYV/s8DbrAkc= X-Received: by 10.99.161.25 with SMTP id b25mr19713404pgf.26.1496649314065; Mon, 05 Jun 2017 00:55:14 -0700 (PDT) From: Ashijeet Acharya To: famz@redhat.com Date: Mon, 5 Jun 2017 13:22:50 +0530 Message-Id: <1496649172-26982-7-git-send-email-ashijeetacharya@gmail.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1496649172-26982-1-git-send-email-ashijeetacharya@gmail.com> References: <1496649172-26982-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::241 Subject: [Qemu-devel] [PATCH v6 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 --- block/vmdk.c | 192 +++++++++++++++++++++++++++++++++++++++++++++++++++++++= ---- 1 file changed, 182 insertions(+), 10 deletions(-) diff --git a/block/vmdk.c b/block/vmdk.c index fe2046b..b671dc9 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,174 @@ 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. If *cluster_offset is non_zero, clusters are only alloca= ted if + * the new allocation can match the specified host offset. + * + * 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; + 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 *total_alloc_clusters) +{ + 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, total_alloc_clusters); + + if (ret < 0) { + return ret; + + } + } + + return VMDK_OK; +} + /** * vmdk_get_cluster_offset * @@ -1625,6 +1794,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 total_alloc_clusters =3D 0; =20 if (DIV_ROUND_UP(offset, BDRV_SECTOR_SIZE) > bs->total_sectors) { error_report("Wrong offset: offset=3D0x%" PRIx64 @@ -1650,10 +1820,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, + &total_alloc_clusters); if (extent->compressed) { if (ret =3D=3D VMDK_OK) { /* Refuse write to allocated cluster for streamOptimized */ @@ -1662,8 +1832,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, + &total_alloc_clusters); } } if (ret =3D=3D VMDK_ERROR) { @@ -1671,10 +1842,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 * + total_alloc_clusters) { + n_bytes =3D extent->cluster_sectors * BDRV_SECTOR_SIZE * + total_alloc_clusters; if (!zero_dry_run) { /* update L2 tables */ if (vmdk_L2update(extent, &m_data, VMDK_GTE_ZEROED) --=20 2.6.2 From nobody Wed Nov 5 16:43:36 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; dkim=fail spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1496649530237522.3708350757602; Mon, 5 Jun 2017 00:58:50 -0700 (PDT) Received: from localhost ([::1]:60161 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dHmuW-0001Mh-VL for importer@patchew.org; Mon, 05 Jun 2017 03:58:49 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40114) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dHmrC-0006ff-WE for qemu-devel@nongnu.org; Mon, 05 Jun 2017 03:55:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dHmrB-0003In-Iy for qemu-devel@nongnu.org; Mon, 05 Jun 2017 03:55:23 -0400 Received: from mail-pf0-x241.google.com ([2607:f8b0:400e:c00::241]:34526) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dHmrB-0003Hw-AT; Mon, 05 Jun 2017 03:55:21 -0400 Received: by mail-pf0-x241.google.com with SMTP id w69so19848037pfk.1; Mon, 05 Jun 2017 00:55:21 -0700 (PDT) Received: from linux.local ([47.247.14.139]) by smtp.gmail.com with ESMTPSA id j71sm11775537pge.63.2017.06.05.00.55.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 05 Jun 2017 00:55:19 -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=6+A5mhEga+0t+KFHKd8885RQPycQnSV1m8SrH7iMqRg=; b=UMY//rzwlOczhBK4/oa6xHubALI7ByvPSl4dAnd44uEsT1uaROdIolXQhqEY6UbBRZ XwzDjerlSi/zYoTyyloM8+/CYYHbAx/zeMIRcKWoAulpt6cxS/78no5G4HEVtmwfkWbJ DpYNDIcMTWTG2jpiA+BcUY+OpSPEqCOQm09V9XM8LEaozM/rFuwuTXgqk52SPAkZpkZz ApJCUB/f0ojg/7MCJVBVuE+WKRCryIFeNVVhnh4skx0jZNaXiPG3h/RB0VixHQkSTqEj Kk6SM1+3MS4AlycwtEB9vSzKH0nIe27F1YOJs4QOTq5qANBRkWag8kQDb/BIPwREUvFz 7dEA== 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=6+A5mhEga+0t+KFHKd8885RQPycQnSV1m8SrH7iMqRg=; b=e5gZd999mBoxF2j2MjvGBcl4WsH1vx46W0/6BjbOcoF5TWkVBz+ZfMjHTG0HKGzJ+S i3iQctV+Yr3f1x7o6pWFdAhyhL8gSYaS0P3OWieLX8OUao8RML1zOSSDhR8sBzBwOKGG HGrZm5FMnijUASw76I6qUsMv2CiVjXc5K/z3aUHHe4HmTaV0wK/65N5ozw47UJHtXirU b+2/9bvLF/ykw/ixkmzA5/I8Klps4zcuh36fRbfYGI24FhPkzGSHqupxt9nQNYObI/0c pARo2hKyQCCzifVt6z3u4k30LepoZ6JUJ2vERn4nIwkSainkvgZK5oLN6ig3qKNkVwrQ DlQw== X-Gm-Message-State: AODbwcAQULWedxsY1MlOcLTHi+w4ksRLolOeDV/wepOBKeuz5n+b5u57 vbMXEHhbwZMYkg== X-Received: by 10.99.55.18 with SMTP id e18mr8117227pga.119.1496649320376; Mon, 05 Jun 2017 00:55:20 -0700 (PDT) From: Ashijeet Acharya To: famz@redhat.com Date: Mon, 5 Jun 2017 13:22:51 +0530 Message-Id: <1496649172-26982-8-git-send-email-ashijeetacharya@gmail.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1496649172-26982-1-git-send-email-ashijeetacharya@gmail.com> References: <1496649172-26982-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::241 Subject: [Qemu-devel] [PATCH v6 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 --- block/vmdk.c | 128 ++++++++++++++++++++++++++++++++++++++++++++++---------= ---- 1 file changed, 101 insertions(+), 27 deletions(-) diff --git a/block/vmdk.c b/block/vmdk.c index b671dc9..9fa2414 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 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 !=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 128; + } + } } =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 * @@ -1261,9 +1316,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; @@ -1272,6 +1328,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, @@ -1323,13 +1380,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) { + 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; @@ -1358,7 +1423,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 *total_alloc_clusters) @@ -1378,8 +1443,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 */ @@ -1792,10 +1857,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 total_alloc_clusters =3D 0; =20 + m_data =3D g_malloc0(sizeof(*m_data)); + if (DIV_ROUND_UP(offset, BDRV_SECTOR_SIZE) > bs->total_sectors) { error_report("Wrong offset: offset=3D0x%" PRIx64 " total_sectors=3D0x%" PRIx64, @@ -1804,6 +1871,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; @@ -1849,7 +1917,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) !=3D VMDK_OK) { return -EIO; } @@ -1863,11 +1931,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; } } @@ -1876,6 +1942,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) { @@ -1886,6 +1959,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 Wed Nov 5 16:43:36 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; dkim=fail spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1496649690396138.60328093235466; Mon, 5 Jun 2017 01:01:30 -0700 (PDT) Received: from localhost ([::1]:60184 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dHmx7-0003Hw-3H for importer@patchew.org; Mon, 05 Jun 2017 04:01:29 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40163) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dHmrJ-0006m6-Uy for qemu-devel@nongnu.org; Mon, 05 Jun 2017 03:55:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dHmrI-0003Qs-Nj for qemu-devel@nongnu.org; Mon, 05 Jun 2017 03:55:30 -0400 Received: from mail-pf0-x244.google.com ([2607:f8b0:400e:c00::244]:35808) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dHmrI-0003QI-Fp; Mon, 05 Jun 2017 03:55:28 -0400 Received: by mail-pf0-x244.google.com with SMTP id u26so19825336pfd.2; Mon, 05 Jun 2017 00:55:28 -0700 (PDT) Received: from linux.local ([47.247.14.139]) by smtp.gmail.com with ESMTPSA id j71sm11775537pge.63.2017.06.05.00.55.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 05 Jun 2017 00:55:27 -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=buLCrbEJPo+nPS5yIJ6nPrNl1oSB9HU8k6S2wXVDlAI=; b=hnvMzFfiLnm4+ZrPBA9CZXVm6oVWaX9Tf3O4TKt8fyGZ+XCGz8XANQYQhdw5VF+HiZ key+uuywlKGcS9KDoHdDHI464Ofu993T+NeZIA9dnmKRBBnaNcKeRQhBQIEo+sSyl4Z9 9fmS2zsvXImza6lSEsE3OhISqHQ4N4LBTBpN/lYrQVKuzv4dErvKWfmMnOPheJZ2/g0Y UryCJxt5oMk4penBr6u78oGn8l4HNIvW+ZhZGebOT05eVrMbENzW9pLQZmw765e7uv6+ CRVf3WbgfbHureB8t6/RME04iIfK/SD/k2jj8NwAQPsftWcmmdkAVmrvg6lh7McNlNJ3 Km8g== 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=buLCrbEJPo+nPS5yIJ6nPrNl1oSB9HU8k6S2wXVDlAI=; b=gjZNYaD1cCqHm+e3WqegiKQbpQK6u3a6enjwIohVue+G/hduYyJkGUQtJ3NX/QvYwK AgBGsBEhK2xI9wx1wMPs/XUAdMU+Oqc3QYkXF95uJ8l77Chi6xJ4Q3UltPN5pnmyuYeK CFJk1e3lhUsG3VhfPU6ZQvAz5iTNwPtOfPfuzQ/IHGCoGhOfo/Iq9h67zRVmTCqWmMuK rNgZytpXf9aEuxacfVEYfN/Vb886aJ6cN2xz8AFf79jUG7dt3IyiI3+g+Y5mJzX/YG0w L6T5LImYby44VF9hWauxia1s32zS2wQ3LFM/IQGZ3Khfvq9rCClZL3q2YIxilFwKWJrw Qaxw== X-Gm-Message-State: AODbwcCxZyFF6erVbBOnPkW5WrRi80WLaiXFjcBPvIcMyizLal+JeQI4 3/6XGs0zOKn5AA== X-Received: by 10.99.121.4 with SMTP id u4mr19808919pgc.167.1496649327457; Mon, 05 Jun 2017 00:55:27 -0700 (PDT) From: Ashijeet Acharya To: famz@redhat.com Date: Mon, 5 Jun 2017 13:22:52 +0530 Message-Id: <1496649172-26982-9-git-send-email-ashijeetacharya@gmail.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1496649172-26982-1-git-send-email-ashijeetacharya@gmail.com> References: <1496649172-26982-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 v6 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 9fa2414..accf1c3 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -1485,25 +1485,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; @@ -1528,31 +1519,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; } @@ -1595,9 +1564,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); @@ -1788,13 +1755,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) { @@ -2541,9 +2509,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