From nobody Sat May 4 08:06:51 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.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 1491058096233293.11298123531003; Sat, 1 Apr 2017 07:48:16 -0700 (PDT) Received: from localhost ([::1]:51699 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cuKK7-0000TE-1N for importer@patchew.org; Sat, 01 Apr 2017 10:48:15 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40494) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cuKGM-0005s0-Aa for qemu-devel@nongnu.org; Sat, 01 Apr 2017 10:44:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cuKGL-0003vZ-IQ for qemu-devel@nongnu.org; Sat, 01 Apr 2017 10:44:22 -0400 Received: from mail-pg0-x243.google.com ([2607:f8b0:400e:c05::243]:34398) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cuKGL-0003v5-Ca; Sat, 01 Apr 2017 10:44:21 -0400 Received: by mail-pg0-x243.google.com with SMTP id o123so22356113pga.1; Sat, 01 Apr 2017 07:44:21 -0700 (PDT) Received: from linux.local ([27.251.197.196]) by smtp.gmail.com with ESMTPSA id h14sm9618461pgn.64.2017.04.01.07.44.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 01 Apr 2017 07:44: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=1faKV22GHrDVBYD9PXGcSxdK5iwLqbnfKNfj8zAbbxE=; b=SaCH6AczEDQIqJ+BUkEyl4qMTXryMEYukIjUguuP4iXwDOinOW/+59rj3nlzt7qk2e z/52cGINVqc04HVjidO9fhrSmOe9ayZz7Kt+FOQKEBkO49a8vuPSYxswCMmfVNsWcFzC s2iVXB0QPNXIH0fN7YUiamPm5fHDjQPpSMSa6HtvhRSQLM6ju7k7FMNynFpBf+Uk8wid +jg298gMd3yvQdyZbn44Zcw7fwUnhVffSxKyUGrpqQb+AUV4097jJ7t4lw6IsYE+lsfj QUvDonLlALEgVLcEbl9pFi4Bu25mUWhw91Wj4tAyoxS0/dLLfowm32RMyuHdk6gDXB/h ZaAg== 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=1faKV22GHrDVBYD9PXGcSxdK5iwLqbnfKNfj8zAbbxE=; b=EZyVTBReK3wQ126TnqW05lyY9LBs+nxBAUrn+2xdatxqEDfKtSMCu4/eh1/VCnpIjU lHXpKtXsBBrlt2WossM2s1NjD51vbpq2vfyph+sZQxiNW08F8C5d3V58F2j7+4lYL/Hm gFD6hhOK2+Qs73y4++ng9xXabsPwnLvc4kSruZ16Zqv7q6G8x5ULtthXb+cPHJZqTlz7 kNRy/whhWZBkpDvGphirS5bmbHlqlzSEOGwG2tCZTrtHFPtQnM1BUAf5XU9Hr6/5Z29s L1baoIpIOSONGluirEZGWrypYCLF2Bq1hfl761NMRoN1piIIc0zdjbJ7GsaevSvbfJaC UQcA== X-Gm-Message-State: AFeK/H3B40rGFoSmUtEgIB+gqLL8rpoIXCUkdjKG7hO/dymnvByIa+AwsA4MPvLSpjNmHg== X-Received: by 10.99.151.10 with SMTP id n10mr8462945pge.199.1491057860429; Sat, 01 Apr 2017 07:44:20 -0700 (PDT) From: Ashijeet Acharya To: famz@redhat.com Date: Sat, 1 Apr 2017 20:14:33 +0530 Message-Id: <1491057878-27868-2-git-send-email-ashijeetacharya@gmail.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1491057878-27868-1-git-send-email-ashijeetacharya@gmail.com> References: <1491057878-27868-1-git-send-email-ashijeetacharya@gmail.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400e:c05::243 Subject: [Qemu-devel] [PATCH v3 1/6] 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. Also, introduce a new helper function size_to_clusters() which returns the number of clusters for a given size in bytes. Here, we leave the last cluster as we need to perform COW for that one. 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 08:06:51 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.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 1491057974218304.0722648084959; Sat, 1 Apr 2017 07:46:14 -0700 (PDT) Received: from localhost ([::1]:51693 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cuKI8-0007Dx-RD for importer@patchew.org; Sat, 01 Apr 2017 10:46:12 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40534) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cuKGQ-0005vo-Ko for qemu-devel@nongnu.org; Sat, 01 Apr 2017 10:44:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cuKGP-0003yD-Iz for qemu-devel@nongnu.org; Sat, 01 Apr 2017 10:44:26 -0400 Received: from mail-pg0-x242.google.com ([2607:f8b0:400e:c05::242]:33313) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cuKGP-0003xb-CZ; Sat, 01 Apr 2017 10:44:25 -0400 Received: by mail-pg0-x242.google.com with SMTP id 79so22400431pgf.0; Sat, 01 Apr 2017 07:44:25 -0700 (PDT) Received: from linux.local ([27.251.197.196]) by smtp.gmail.com with ESMTPSA id h14sm9618461pgn.64.2017.04.01.07.44.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 01 Apr 2017 07:44:24 -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=FPSZpNKpzIRPSXpKydMDE7vfOSTZnnMPzTbo92Nrq+8=; b=WQWzSlAI3GqjI9puKDCfQCjl/aqB1ZV8GC7rLBwY8UOJRrNcWrKYF8qz+L0501fM8f Ormh9oGKIRPg/FoDL8QyMiuyQj3ULTCnjf5vPASoe5/wTGrdKMInmc1sCj2ooqVaqATN zh6JKLH8P+iacG67xUgIIrxJuQ9Cwcbq6aYKDPkMNQAwNzdcsTWG+IFuswXWo+L9KPxU cCkB2dT/9GQdpFQQAtzHqh4JqGl51RZhZUOHo3NB8l3c9DJwW7j6pV/wMNPi3FaBtVYA 7/F/inJtzwe4nKITXqdLDXYlx+u4P9cJ8bLJJBvzTiJ3kCVpYISSTu2nwBH8l98EGP0c LJ3g== 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=FPSZpNKpzIRPSXpKydMDE7vfOSTZnnMPzTbo92Nrq+8=; b=JpkV6v+59iDuQd1KXYEWEbxRcZZgkDCf47m2MMWTuYv0vxswtpvJIiIMhu85AbYutT 4mswNGcNsxlCeYCXwM0wnrCYuEXV1Jb1oiHey6g/gZkfj0W1GdYOokeOD09OQCpEJUCh xQ84kT2WSnN2WfS5nkGOyxDeu9BLhOljHyrrzXSutIuYfiLj/e1OnLHjbOlvPFBDDvQl P7HR6vG2B1v085w9Hx7FTUXTgIM/r3wSY12qL5V5a6twd0D5wBfiqcrH1V13xux8luc6 E42fGYSt3VX9McJEhI74VQu3PN0P3GOxtfb2dHJjrnjyO617sDZpWyLxkm+EeS4jZp+u zpxw== X-Gm-Message-State: AFeK/H0VEFjd1Fdvwjo+Km2pDyeAM8CQbb6rHID6sUGbDngdn0QTbhKR03DJTgLVJoK9zw== X-Received: by 10.84.233.143 with SMTP id l15mr9741690plk.93.1491057864445; Sat, 01 Apr 2017 07:44:24 -0700 (PDT) From: Ashijeet Acharya To: famz@redhat.com Date: Sat, 1 Apr 2017 20:14:34 +0530 Message-Id: <1491057878-27868-3-git-send-email-ashijeetacharya@gmail.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1491057878-27868-1-git-send-email-ashijeetacharya@gmail.com> References: <1491057878-27868-1-git-send-email-ashijeetacharya@gmail.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400e:c05::242 Subject: [Qemu-devel] [PATCH v3 2/6] 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 08:06:51 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.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 149105804073247.93242467820687; Sat, 1 Apr 2017 07:47:20 -0700 (PDT) Received: from localhost ([::1]:51696 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cuKJD-00083I-8C for importer@patchew.org; Sat, 01 Apr 2017 10:47:19 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40561) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cuKGV-0005zv-VT for qemu-devel@nongnu.org; Sat, 01 Apr 2017 10:44:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cuKGU-00040h-40 for qemu-devel@nongnu.org; Sat, 01 Apr 2017 10:44:32 -0400 Received: from mail-pg0-x244.google.com ([2607:f8b0:400e:c05::244]:33318) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cuKGT-000405-R9; Sat, 01 Apr 2017 10:44:30 -0400 Received: by mail-pg0-x244.google.com with SMTP id 79so22400527pgf.0; Sat, 01 Apr 2017 07:44:29 -0700 (PDT) Received: from linux.local ([27.251.197.196]) by smtp.gmail.com with ESMTPSA id h14sm9618461pgn.64.2017.04.01.07.44.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 01 Apr 2017 07:44:28 -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=dt6PzM9XW/4Jk9nTg4bykvRoP4EthX6d/I4X4+60wr8=; b=n8fnl3okOBBHql6UmD5QI2lznTG1iAXWd0g1ZgKZYO3/pJVEjY8RhMbkac3qq4W0Vj QJ8Ht78kMJEerxfRXnydEsb1xrjXovEoaV8B6G9xuBbZHh511yYwhc1q/sTq39AWw51W Vz9/tsb6GVyjG6FkQvf8W+ggDfHT+gD7LvieIjYE/Julfxb5xv4GBv101UV9B1vjp8Zv M68Xvzq5ya7DmsKJoM6L8R3iSzDBnZP0VT6UGWcJ1bNlgoUXQnlYCAxgERSHNgGrcPXg oF4XeY4CKNx/fMezZHayUnS2YjZIc9t8/A2fqrnr4B0vX3QQqLhCWLaUoDf1O6hrMj/m GfPA== 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=dt6PzM9XW/4Jk9nTg4bykvRoP4EthX6d/I4X4+60wr8=; b=cVAqETCt7JRzjDflIUM4E7LpscqJYbCHQwTcDpYpLAIVY8XFnEo2bKXNHF8LmnTKvU q9Ntcpiw2Xd8Z71VYDykPf93QDaWuqRc6CdmYZ7ozdadPBzwJ/cOpCUM8SyBBmfvToIx NF/zQ+kOTqycYR/tyWvv6rZE/uvMUSa2NL751TGycCKVX78vx1WN1aJzP81XXTF6pdRc N0fQ9apCnBbX77ViA2EpMgE1YfzIpdnGMmfaV+ocd0q361QliMZy4ITC1SO10qujKnn2 NygSeDZzKbnPFCiec1FOeaIeRGiInW24FAjykqUngVScJU6Q6N40zOH0ToTpbLbjIylg zPqQ== X-Gm-Message-State: AFeK/H0nPe+Gpyc1+7zUd/hIZhdFrmKVg9hSsSj30jnzAX4RfUt0B8Nmond3bGIdhz25jQ== X-Received: by 10.99.53.195 with SMTP id c186mr8328395pga.182.1491057868711; Sat, 01 Apr 2017 07:44:28 -0700 (PDT) From: Ashijeet Acharya To: famz@redhat.com Date: Sat, 1 Apr 2017 20:14:35 +0530 Message-Id: <1491057878-27868-4-git-send-email-ashijeetacharya@gmail.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1491057878-27868-1-git-send-email-ashijeetacharya@gmail.com> References: <1491057878-27868-1-git-send-email-ashijeetacharya@gmail.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400e:c05::244 Subject: [Qemu-devel] [PATCH v3 3/6] 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" Move the cluster tables loading code out of the existing get_cluster_offset() function to avoid code duplication and implement it in separate get_cluster_table() and vmdk_L2load() functions. 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 | 337 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-= ---- 1 file changed, 308 insertions(+), 29 deletions(-) diff --git a/block/vmdk.c b/block/vmdk.c index 73ae786..e5a289d 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 { @@ -254,6 +255,14 @@ static inline uint64_t vmdk_find_offset_in_cluster(Vmd= kExtent *extent, return extent_relative_offset % cluster_size; } =20 +static inline uint64_t size_to_clusters(VmdkExtent *extent, uint64_t size) +{ + uint64_t cluster_size, round_off_size; + cluster_size =3D extent->cluster_sectors * BDRV_SECTOR_SIZE; + round_off_size =3D cluster_size - (size % cluster_size); + return DIV_ROUND_UP(size + round_off_size, BDRV_SECTOR_SIZE * 128) - 1; +} + static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent) { char *desc; @@ -1028,6 +1037,133 @@ static void vmdk_refresh_limits(BlockDriverState *b= s, Error **errp) } } =20 +static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data, + uint32_t offset) +{ + offset =3D cpu_to_le32(offset); + /* update L2 table */ + if (bdrv_pwrite_sync(extent->file, + ((int64_t)m_data->l2_offset * 512) + + (m_data->l2_index * sizeof(offset)), + &offset, sizeof(offset)) < 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) { + return VMDK_ERROR; + } + } + if (m_data->l2_cache_entry) { + *m_data->l2_cache_entry =3D offset; + } + + return VMDK_OK; +} + +/* + * vmdk_l2load + * + * Loads a new L2 table into memory. If the table is in the cache, the cac= he + * 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_perform_cow * @@ -1115,29 +1251,168 @@ exit: return ret; } =20 -static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data, - uint32_t offset) +/* + * handle_alloc + * + * Allocates new clusters for an area that either is yet unallocated or ne= eds 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 handle_alloc(BlockDriverState *bs, VmdkExtent *extent, + uint64_t offset, uint64_t *cluster_offset, + int64_t *bytes, VmdkMetaData *m_data, + bool allocate, uint32_t *total_alloc_clusters) { - offset =3D cpu_to_le32(offset); - /* update L2 table */ - if (bdrv_pwrite_sync(extent->file, - ((int64_t)m_data->l2_offset * 512) - + (m_data->l2_index * sizeof(offset)), - &offset, sizeof(offset)) < 0) { - return VMDK_ERROR; + 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; } - /* 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) { - return VMDK_ERROR; + + 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 it will return o= ne + * cluster less than the actual value calculated as we may need to per= from + * COW for the last one. */ + nb_clusters =3D size_to_clusters(extent, skip_start_bytes + *bytes); + + 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) << 9) + - skip_start_bytes; + } else { + nb_clusters =3D 1; + } + *total_alloc_clusters +=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; } } - if (m_data->l2_cache_entry) { - *m_data->l2_cache_entry =3D offset; + *cluster_offset =3D cluster_sector << BDRV_SECTOR_BITS; + return VMDK_OK; +} + +/* + * vmdk_alloc_cluster_offset + * + * 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_cluster_offset(BlockDriverState *bs, + VmdkExtent *extent, + VmdkMetaData *m_data, uint64_t offset, + bool allocate, uint64_t *cluster_offs= et, + 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 handle_alloc(bs, extent, start, &new_cluster_offset, &n_by= tes, + m_data, allocate, total_alloc_clusters); + + if (ret < 0) { + return ret; + + } } =20 return VMDK_OK; @@ -1567,6 +1842,7 @@ static int vmdk_pwritev(BlockDriverState *bs, uint64_= t offset, uint64_t cluster_offset; uint64_t bytes_done =3D 0; VmdkMetaData m_data; + 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 @@ -1584,10 +1860,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_alloc_cluster_offset(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 */ @@ -1596,19 +1872,22 @@ static int vmdk_pwritev(BlockDriverState *bs, uint6= 4_t offset, return -EIO; } else { /* allocate */ - ret =3D get_cluster_offset(bs, extent, &m_data, offset, - true, &cluster_offset, 0, 0); + ret =3D vmdk_alloc_cluster_offset(bs, extent, &m_data, off= set, + true, &cluster_offset, n_bytes, + &total_alloc_clusters); } } if (ret =3D=3D VMDK_ERROR) { return -EINVAL; } + 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 Sat May 4 08:06:51 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.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 1491057984998903.2241878387707; Sat, 1 Apr 2017 07:46:24 -0700 (PDT) Received: from localhost ([::1]:51694 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cuKIJ-0007M8-JR for importer@patchew.org; Sat, 01 Apr 2017 10:46:23 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40588) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cuKGZ-00063R-Un for qemu-devel@nongnu.org; Sat, 01 Apr 2017 10:44:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cuKGY-00042O-Jg for qemu-devel@nongnu.org; Sat, 01 Apr 2017 10:44:36 -0400 Received: from mail-pg0-x244.google.com ([2607:f8b0:400e:c05::244]:33322) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cuKGY-00042D-Bl; Sat, 01 Apr 2017 10:44:34 -0400 Received: by mail-pg0-x244.google.com with SMTP id 79so22400643pgf.0; Sat, 01 Apr 2017 07:44:34 -0700 (PDT) Received: from linux.local ([27.251.197.196]) by smtp.gmail.com with ESMTPSA id h14sm9618461pgn.64.2017.04.01.07.44.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 01 Apr 2017 07:44:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=SbXzQdJIreQxKnBDtUcHB+QUhk6uBCJ4DbIlhq7Hl+I=; b=TSL5GNUGBdfSQ2Vv/2n4zuEiiZhai0sLLNW3A7tG2sOEeIUum3xsWBJb6zxbfa6Drg 2AqGaCH9XSiKscOolV0o/gq1vAS8iTFbRbW0tQYjn1ltp7w34pwpj1fW6bSvEMsqU+xQ ypGBcPFwioJkwaMpFmSs+UhWKTAV/vScXKheSBFu6xvDM0LEq2ePtx26RRH42eYaet5h ycQVHmmOA5WaStyO7Py5texxOyHD7OUQ2Hc/IP1WDuE1x9xeLTjdt5Vz4ixWNWygpi25 OntIkCPVdMXydK2xl2MNrI3Fl9kteBxxHR3DPJCGRBcAeYzcO+PtH4fGepFBImVOEBau ozWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=SbXzQdJIreQxKnBDtUcHB+QUhk6uBCJ4DbIlhq7Hl+I=; b=qQFdX6258fXKEjfuJ/bzvuoKDJd/S9bzeAOYXSu2NGR/Y45C+OTQtd0Vmb1o+xTeeP oAQPsK4n9BCBL4Hvb/6+TiPFXIXiwC1RfiiIUAL9PrK+JP91bs2Y/5TQv8Et5E1RYeye tXiXVD0JNgG1H70E2sLRnhQsCRzae3NtqVQeeVs/Vi+GA9mAi4GRVrWALApoLNhQuiF+ bQAuoNy4Fpi9yxqU8KnQ75sMPBAD/Rqh7hthlUPM9OeUpnWhQEMmU7/zrjwghcVn5o3j KM24ISS/1+quGvEtGwOKzgRtI5J8JQBg+QCpeAOfMTJSlWxQObBXzRJwqCut2NttslCl WIBg== X-Gm-Message-State: AFeK/H3UErjkK7U16w24zs1EMTsMcE4bjtUPFDi2Y4DqRSub7C+/4MXlYWnW3eodbloSWQ== X-Received: by 10.98.78.22 with SMTP id c22mr7525635pfb.191.1491057873453; Sat, 01 Apr 2017 07:44:33 -0700 (PDT) From: Ashijeet Acharya To: famz@redhat.com Date: Sat, 1 Apr 2017 20:14:36 +0530 Message-Id: <1491057878-27868-5-git-send-email-ashijeetacharya@gmail.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1491057878-27868-1-git-send-email-ashijeetacharya@gmail.com> References: <1491057878-27868-1-git-send-email-ashijeetacharya@gmail.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400e:c05::244 Subject: [Qemu-devel] [PATCH v3 4/6] vmdk: Rename get_cluster_offset() to vmdk_get_cluster_offset() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, qemu-block@nongnu.org, stefanha@gmail.com, qemu-devel@nongnu.org, mreitz@redhat.com, Ashijeet Acharya , jsnow@redhat.com Errors-To: qemu-devel-bounces+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() function to vmdk_get_cluster_offset() and have it make use of the new get_cluster_table() to load the cluster tables. Also, it is no longer used to allocate new clusters and hence perform COW. Make the necessary renames at all the occurrences of get_cluster_offset(). Signed-off-by: Ashijeet Acharya --- block/vmdk.c | 117 +++++++++++--------------------------------------------= ---- 1 file changed, 21 insertions(+), 96 deletions(-) diff --git a/block/vmdk.c b/block/vmdk.c index e5a289d..a8babd7 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -1419,7 +1419,7 @@ static int vmdk_alloc_cluster_offset(BlockDriverState= *bs, } =20 /** - * get_cluster_offset + * vmdk_get_cluster_offset * * Look up cluster offset in extent file by sector number, and store in * @cluster_offset. @@ -1427,84 +1427,34 @@ static int vmdk_alloc_cluster_offset(BlockDriverSta= te *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 in L1, L2 table. * * Returns: VMDK_OK if cluster exists and mapped in the image. - * VMDK_UNALLOC if cluster is not mapped and @allocate is false. - * VMDK_ERROR if failed. + * VMDK_UNALLOC if cluster is not mapped. + * VMDK_ERROR if failed */ -static int get_cluster_offset(BlockDriverState *bs, - VmdkExtent *extent, - VmdkMetaData *m_data, - uint64_t offset, - bool allocate, - uint64_t *cluster_offset, - uint64_t skip_start_bytes, - uint64_t skip_end_bytes) +static int vmdk_get_cluster_offset(BlockDriverState *bs, + VmdkExtent *extent, + uint64_t offset, + uint64_t *cluster_offset) { - unsigned int l1_index, l2_offset, l2_index; - int min_index, i, j; - uint32_t min_count, *l2_table; + int l1_index, l2_offset, l2_index; + uint32_t *l2_table; bool zeroed =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]); =20 if (extent->has_zero_grain && cluster_sector =3D=3D VMDK_GTE_ZEROED) { @@ -1512,31 +1462,9 @@ static int 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; } @@ -1579,9 +1507,7 @@ static int64_t coroutine_fn vmdk_co_get_block_status(= BlockDriverState *bs, return 0; } qemu_co_mutex_lock(&s->lock); - ret =3D 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); @@ -1772,13 +1698,13 @@ vmdk_co_preadv(BlockDriverState *bs, uint64_t offse= t, uint64_t bytes, ret =3D -EIO; goto fail; } - ret =3D 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) { @@ -2508,9 +2434,8 @@ 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, + sector_num << BDRV_SECTOR_BITS, &cluster_offset); 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 08:06:51 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.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 1491058112006660.674790036188; Sat, 1 Apr 2017 07:48:32 -0700 (PDT) Received: from localhost ([::1]:51700 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cuKKM-0000di-Qe for importer@patchew.org; Sat, 01 Apr 2017 10:48:30 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40620) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cuKGd-00066y-KH for qemu-devel@nongnu.org; Sat, 01 Apr 2017 10:44:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cuKGc-000448-Ol for qemu-devel@nongnu.org; Sat, 01 Apr 2017 10:44:39 -0400 Received: from mail-pf0-x242.google.com ([2607:f8b0:400e:c00::242]:34726) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cuKGc-00043n-JW; Sat, 01 Apr 2017 10:44:38 -0400 Received: by mail-pf0-x242.google.com with SMTP id o126so4243862pfb.1; Sat, 01 Apr 2017 07:44:38 -0700 (PDT) Received: from linux.local ([27.251.197.196]) by smtp.gmail.com with ESMTPSA id h14sm9618461pgn.64.2017.04.01.07.44.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 01 Apr 2017 07:44:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=lY72x0MPqywAOJgpjEJBmkhKmQAY3I2IM26F2Jh4iT0=; b=HPYRuDwAchGf3y+uoX5V9sTl5fRirOXrIJcdlEP67oQrGnp40tgtTR1qn6s94MLdLD /ujwBITujhuQvdOE7eOot11igOTtxZsj2BwUrRuFwQKpbGnYbqUkt4OHA0nEfdOMFTHR o/7xI4iue/VV9XtPxuvamA2n/KAp0+RDMoaFB1S8ryO0+tMfOZgSmaucNQLCcA+i5J1U F3SY/d7Q9fCPnKu/I7fzNjPtVMuWNv9k3KXBBuH7eiYvjWEr03bHktgE3m3FsaDcppg1 hDJxj2MlXBQlf+PvHIZSUtb4jwuEA2TXKaNAvfxLTPRjGQAvUhiT27YzUy8sL7TJ0c/T L+zQ== 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=lY72x0MPqywAOJgpjEJBmkhKmQAY3I2IM26F2Jh4iT0=; b=m/lsonU/4IoeGaDfHkbdxnw/hUB8EM7XCdh0mCa50jbVp1Ih5gVx11LjnIvUochVzo GUevP9e03/OW+FCEijlebHPr9F881MVkJ3xxuZMlW6g/eUIfK4qm6ECot2+5qFde3PA0 exozHATfnLUhskEOm2hDDaZZRFQ3WVgjYNkrZpqhoYhOhN6SveVC0iqJR60FU2PUBvUY ats7ehXwolNWOqY0aSDmp62+l90wPmSVbYf0m9CMgd7s8PL3tEGaLSOk9luwc8FVQiRH Uik3eYTQKIOKQZSSKAwNLRWUiwrWaHYMieUKs8ZqWjQMIuqONptvtBhXUI/MYVWrVcCb zdOA== X-Gm-Message-State: AFeK/H04zDcM1RUzL8cESoSnZ6xc3AGQmWguuj2DuvoPYnZRWECh+9VLl77iStXnv7gG1Q== X-Received: by 10.98.138.193 with SMTP id o62mr7283997pfk.230.1491057877648; Sat, 01 Apr 2017 07:44:37 -0700 (PDT) From: Ashijeet Acharya To: famz@redhat.com Date: Sat, 1 Apr 2017 20:14:37 +0530 Message-Id: <1491057878-27868-6-git-send-email-ashijeetacharya@gmail.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1491057878-27868-1-git-send-email-ashijeetacharya@gmail.com> References: <1491057878-27868-1-git-send-email-ashijeetacharya@gmail.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400e:c00::242 Subject: [Qemu-devel] [PATCH v3 5/6] 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 --- block/vmdk.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/block/vmdk.c b/block/vmdk.c index a8babd7..9456ddd 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -1767,6 +1767,7 @@ static int vmdk_pwritev(BlockDriverState *bs, uint64_= t offset, int64_t offset_in_cluster, n_bytes; uint64_t cluster_offset; uint64_t bytes_done =3D 0; + uint64_t extent_size; VmdkMetaData m_data; uint32_t total_alloc_clusters =3D 0; =20 @@ -1782,9 +1783,17 @@ static int vmdk_pwritev(BlockDriverState *bs, uint64= _t offset, if (!extent) { return -EIO; } + extent_size =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_size - offset); + } =20 ret =3D vmdk_alloc_cluster_offset(bs, extent, &m_data, offset, !(extent->compressed || zeroed), --=20 2.6.2 From nobody Sat May 4 08:06:51 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.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 1491058188579540.652137450822; Sat, 1 Apr 2017 07:49:48 -0700 (PDT) Received: from localhost ([::1]:51708 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cuKLZ-0001PK-PA for importer@patchew.org; Sat, 01 Apr 2017 10:49:45 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40657) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cuKGi-0006CR-P3 for qemu-devel@nongnu.org; Sat, 01 Apr 2017 10:44:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cuKGh-00045o-HX for qemu-devel@nongnu.org; Sat, 01 Apr 2017 10:44:44 -0400 Received: from mail-pg0-x241.google.com ([2607:f8b0:400e:c05::241]:35340) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cuKGh-00045D-8q; Sat, 01 Apr 2017 10:44:43 -0400 Received: by mail-pg0-x241.google.com with SMTP id g2so22294149pge.2; Sat, 01 Apr 2017 07:44:43 -0700 (PDT) Received: from linux.local ([27.251.197.196]) by smtp.gmail.com with ESMTPSA id h14sm9618461pgn.64.2017.04.01.07.44.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 01 Apr 2017 07:44:41 -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=6OS+YDIgdp48242IGEknCrvdvxUaCEEifFV1jE5lc7I=; b=t6Qxo3nznk/Nt98oUjWpBenzCuIGbeCpxf6I0jgOuhO4n5ey17vHLJ+GiFNf62kzhM we3TRFm4Lu90JmyS713nQhun4O9jR158nGDRGtKn01NIEECYgw6KwcoTsgUTihpZ5Lbz rErr6XqXqb7bmm18ZRZrnAm1ET7V+6fwAsvRFdmp4qQXeEo8OkCciQD4JZ+0dEL2Mxi7 4nyg8YN7zRGja6rpXMXr/zQLe1U+tnRnrCR1nVh3GymmeMmgsVWbIkHRcsJ2kosks5kS O6NFk+v30VP9/M0fMzdV/NEHgIiBR8VRFR9APMI4Zsf8J0c3oA7Q8BnIYYd0u/dMgUc8 egAQ== 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=6OS+YDIgdp48242IGEknCrvdvxUaCEEifFV1jE5lc7I=; b=gNu3+HKDYTnJW2of8gE/6XynsuKVqZKNzy16wePui+Xt+EAG1rrUOkcHVoFjgCCW7a IhMtRNDNbcTN4WllcpeYh2s9kmJmncOjjQ/5OSP0sRcR/npVDbFNoGokDAQAP4lZYNBD xwHn3z9VzZeHTAVEqwFi/7PCeEbOLPiNvRuIF/bOg6KxAptFSkj7UM/Lh/s5c9hoFUhV zEkmvb5L92/m2QnyM+koNRa3t7EMZP6ZQYVU6WATYbHNbe+sRf4Gk+eRviy+oWJ3UvRi HtlH7YIEtaijl1snujVx1AB5JwfHVxN/xkY+LCcf0JR4fPoz1NMAM3nJJgh0GJYP+jJo GisQ== X-Gm-Message-State: AFeK/H0qmvacTJR9F39XV5SD9CTla2adyTpch4J6MqjRqAoRYN+XDshaWCXeR5NxXzN6dQ== X-Received: by 10.98.196.12 with SMTP id y12mr7682220pff.49.1491057882202; Sat, 01 Apr 2017 07:44:42 -0700 (PDT) From: Ashijeet Acharya To: famz@redhat.com Date: Sat, 1 Apr 2017 20:14:38 +0530 Message-Id: <1491057878-27868-7-git-send-email-ashijeetacharya@gmail.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1491057878-27868-1-git-send-email-ashijeetacharya@gmail.com> References: <1491057878-27868-1-git-send-email-ashijeetacharya@gmail.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400e:c05::241 Subject: [Qemu-devel] [PATCH v3 6/6] 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 | 136 ++++++++++++++++++++++++++++++++++++++++++++++++-------= ---- 1 file changed, 111 insertions(+), 25 deletions(-) diff --git a/block/vmdk.c b/block/vmdk.c index 9456ddd..c7675db 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 { @@ -263,6 +265,12 @@ static inline uint64_t size_to_clusters(VmdkExtent *ex= tent, uint64_t size) return (DIV_ROUND_UP(size + round_off_size, BDRV_SECTOR_SIZE * 128) - = 1); } =20 +static inline int64_t vmdk_align_offset(int64_t offset, int n) +{ + offset =3D (offset + n - 1) & ~(n - 1); + return offset; +} + static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent) { char *desc; @@ -1037,29 +1045,88 @@ static void vmdk_refresh_limits(BlockDriverState *b= s, Error **errp) } } =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; + } + + temp_offset =3D cpu_to_le32(temp_offset); + + l2_array_size =3D sizeof(uint32_t) * m_data->nb_clusters; + l2_table_array =3D qemu_try_blockalign(extent->file->bs, + vmdk_align_offset(l2_array_size, 512)); + if (l2_table_array =3D=3D NULL) { + return VMDK_ERROR; + } + memset(l2_table_array, 0, vmdk_align_offset(l2_array_size, 512)); + /* update L2 table */ + offset =3D temp_offset; + for (i =3D 0; i < m_data->nb_clusters; i++) { + l2_table_array[i] =3D 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) { + + ((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 offset; + m_data->l2_cache_entry++; + + if (!zeroed) { + offset +=3D 128; + } + } + } + + qemu_vfree(l2_table_array); + return VMDK_OK; +} + +static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data, + bool zeroed) +{ + int ret; + + while (m_data->next !=3D NULL) { + VmdkMetaData *next; + + ret =3D vmdk_alloc_cluster_link_l2(extent, m_data, zeroed); + if (ret < 0) { + return ret; + } + + next =3D m_data->next; + m_data =3D next; } =20 return VMDK_OK; @@ -1271,7 +1338,7 @@ exit: */ static int handle_alloc(BlockDriverState *bs, VmdkExtent *extent, uint64_t offset, uint64_t *cluster_offset, - int64_t *bytes, VmdkMetaData *m_data, + int64_t *bytes, VmdkMetaData **m_data, bool allocate, uint32_t *total_alloc_clusters) { int l1_index, l2_offset, l2_index; @@ -1280,6 +1347,7 @@ static int handle_alloc(BlockDriverState *bs, VmdkExt= ent *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, @@ -1330,13 +1398,21 @@ static int handle_alloc(BlockDriverState *bs, VmdkE= xtent *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; @@ -1365,7 +1441,7 @@ static int handle_alloc(BlockDriverState *bs, VmdkExt= ent *extent, */ static int vmdk_alloc_cluster_offset(BlockDriverState *bs, VmdkExtent *extent, - VmdkMetaData *m_data, uint64_t offset, + VmdkMetaData **m_data, uint64_t offse= t, bool allocate, uint64_t *cluster_offs= et, int64_t bytes, uint32_t *total_alloc_clusters) @@ -1385,8 +1461,8 @@ static int vmdk_alloc_cluster_offset(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 */ @@ -1768,9 +1844,11 @@ static int vmdk_pwritev(BlockDriverState *bs, uint64= _t offset, uint64_t cluster_offset; uint64_t bytes_done =3D 0; uint64_t extent_size; - VmdkMetaData m_data; + 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, @@ -1779,6 +1857,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; @@ -1825,7 +1904,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; } @@ -1839,10 +1918,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) + if (vmdk_L2update(extent, m_data, zeroed) !=3D VMDK_OK) { return -EIO; } @@ -1852,6 +1930,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) { @@ -1862,6 +1947,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