[{"id":1772273,"web_url":"http://patchwork.ozlabs.org/comment/1772273/","msgid":"<ac533c95-a0f5-c6ce-cc6a-7fab63eb0517@redhat.com>","list_archive_url":null,"date":"2017-09-20T21:38:34","subject":"Re: [Qemu-devel] [Qemu-block] [PATCH 2/2] qcow2: truncate the tail\n\tof the image file after shrinking the image","submitter":{"id":64343,"url":"http://patchwork.ozlabs.org/api/people/64343/","name":"John Snow","email":"jsnow@redhat.com"},"content":"On 09/20/2017 09:58 AM, Pavel Butsykin wrote:\n> Now after shrinking the image, at the end of the image file, there might be a\n> tail that probably will never be used. So we can find the last used cluster and\n> cut the tail.\n> \n> Signed-off-by: Pavel Butsykin <pbutsykin@virtuozzo.com>\n> ---\n>  block/qcow2-refcount.c | 21 +++++++++++++++++++++\n>  block/qcow2.c          | 19 +++++++++++++++++++\n>  block/qcow2.h          |  1 +\n>  3 files changed, 41 insertions(+)\n> \n> diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c\n> index 88d5a3f1ad..5e221a166c 100644\n> --- a/block/qcow2-refcount.c\n> +++ b/block/qcow2-refcount.c\n> @@ -3181,3 +3181,24 @@ out:\n>      g_free(reftable_tmp);\n>      return ret;\n>  }\n> +\n> +int64_t qcow2_get_last_cluster(BlockDriverState *bs, int64_t size)\n> +{\n> +    BDRVQcow2State *s = bs->opaque;\n> +    int64_t i, last_cluster, nb_clusters = size_to_clusters(s, size);\n> +    uint64_t refcount;\n> +\n> +    for (i = 0, last_cluster = 0; i < nb_clusters; i++) {\n> +        int ret = qcow2_get_refcount(bs, i, &refcount);\n> +        if (ret < 0) {\n> +            fprintf(stderr, \"Can't get refcount for cluster %\" PRId64 \": %s\\n\",\n> +                    i, strerror(-ret));\n> +            continue;\n> +        }\n> +\n> +        if (refcount > 0) {\n> +            last_cluster = i;\n> +        }\n> +    }\n> +    return last_cluster;\n> +}> diff --git a/block/qcow2.c b/block/qcow2.c\n> index 8a4311d338..c3b6dd44c4 100644\n> --- a/block/qcow2.c\n> +++ b/block/qcow2.c\n> @@ -3106,6 +3106,7 @@ static int qcow2_truncate(BlockDriverState *bs, int64_t offset,\n>      new_l1_size = size_to_l1(s, offset);\n>  \n>      if (offset < old_length) {\n> +        int64_t image_end_offset, old_file_size;\n>          if (prealloc != PREALLOC_MODE_OFF) {\n>              error_setg(errp,\n>                         \"Preallocation can't be used for shrinking an image\");\n> @@ -3134,6 +3135,24 @@ static int qcow2_truncate(BlockDriverState *bs, int64_t offset,\n>                               \"Failed to discard unused refblocks\");\n>              return ret;\n>          }\n> +\n> +        old_file_size = bdrv_getlength(bs->file->bs);\n> +        if (old_file_size < 0) {\n> +            error_setg_errno(errp, -old_file_size,\n> +                             \"Failed to inquire current file length\");\n> +            return old_file_size;\n> +        }\n> +        image_end_offset = (qcow2_get_last_cluster(bs, old_file_size) + 1) *\n> +                           s->cluster_size;\n> +        if (image_end_offset < old_file_size) {\n> +            ret = bdrv_truncate(bs->file, image_end_offset,\n> +                                PREALLOC_MODE_OFF, NULL);\n> +            if (ret < 0) {\n> +                error_setg_errno(errp, -ret,\n> +                                 \"Failed to truncate the tail of the image\");\n\nI've recently become skeptical of what partial resize successes look\nlike, but that's an issue for another day entirely.\n\n> +                return ret;\n> +            }\n> +        }\n>      } else {\n>          ret = qcow2_grow_l1_table(bs, new_l1_size, true);\n>          if (ret < 0) {\n> diff --git a/block/qcow2.h b/block/qcow2.h\n> index 5a289a81e2..782a206ecb 100644\n> --- a/block/qcow2.h\n> +++ b/block/qcow2.h\n> @@ -597,6 +597,7 @@ int qcow2_change_refcount_order(BlockDriverState *bs, int refcount_order,\n>                                  BlockDriverAmendStatusCB *status_cb,\n>                                  void *cb_opaque, Error **errp);\n>  int qcow2_shrink_reftable(BlockDriverState *bs);\n> +int64_t qcow2_get_last_cluster(BlockDriverState *bs, int64_t size);\n>  \n>  /* qcow2-cluster.c functions */\n>  int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,\n> \n\nReviewed-by: John Snow <jsnow@redhat.com>\n\nLooks sane to me, but under which circumstances might we grow such a\ntail? I assume the actual truncate call aligns to cluster boundaries as\nappropriate, so is this a bit of a \"quick fix\" to cull unused clusters\nthat happened to be near the truncate boundary?\n\nIt might be worth documenting the circumstances that produces this\nunused space that will never get used. My hunch is that such unused\nspace should likely be getting reclaimed elsewhere and not here, but\nperhaps I'm misunderstanding the causal factors.\n\n--js","headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":["ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=nongnu.org\n\t(client-ip=2001:4830:134:3::11; helo=lists.gnu.org;\n\tenvelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n\treceiver=<UNKNOWN>)","ext-mx01.extmail.prod.ext.phx2.redhat.com;\n\tdmarc=none (p=none dis=none) header.from=redhat.com","ext-mx01.extmail.prod.ext.phx2.redhat.com;\n\tspf=fail smtp.mailfrom=jsnow@redhat.com"],"Received":["from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11])\n\t(using TLSv1 with cipher AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3xyCmv0Jhvz9sBd\n\tfor <incoming@patchwork.ozlabs.org>;\n\tThu, 21 Sep 2017 07:39:09 +1000 (AEST)","from localhost ([::1]:50776 helo=lists.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.71) (envelope-from\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>)\n\tid 1dumi1-0003pD-GM\n\tfor incoming@patchwork.ozlabs.org; Wed, 20 Sep 2017 17:39:05 -0400","from eggs.gnu.org ([2001:4830:134:3::10]:36326)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <jsnow@redhat.com>) id 1dumhf-0003me-6I\n\tfor qemu-devel@nongnu.org; Wed, 20 Sep 2017 17:38:44 -0400","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <jsnow@redhat.com>) id 1dumhe-00007G-38\n\tfor qemu-devel@nongnu.org; Wed, 20 Sep 2017 17:38:43 -0400","from mx1.redhat.com ([209.132.183.28]:49514)\n\tby eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32)\n\t(Exim 4.71) (envelope-from <jsnow@redhat.com>)\n\tid 1dumhZ-0008Ud-6g; Wed, 20 Sep 2017 17:38:37 -0400","from smtp.corp.redhat.com\n\t(int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12])\n\t(using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby mx1.redhat.com (Postfix) with ESMTPS id 215E081DF7;\n\tWed, 20 Sep 2017 21:38:36 +0000 (UTC)","from [10.18.17.130] (dhcp-17-130.bos.redhat.com [10.18.17.130])\n\tby smtp.corp.redhat.com (Postfix) with ESMTP id 2C7CB610B0;\n\tWed, 20 Sep 2017 21:38:35 +0000 (UTC)"],"DMARC-Filter":"OpenDMARC Filter v1.3.2 mx1.redhat.com 215E081DF7","To":"Pavel Butsykin <pbutsykin@virtuozzo.com>, qemu-block@nongnu.org,\n\tqemu-devel@nongnu.org","References":"<20170920135833.20472-1-pbutsykin@virtuozzo.com>\n\t<20170920135833.20472-3-pbutsykin@virtuozzo.com>","From":"John Snow <jsnow@redhat.com>","Message-ID":"<ac533c95-a0f5-c6ce-cc6a-7fab63eb0517@redhat.com>","Date":"Wed, 20 Sep 2017 17:38:34 -0400","User-Agent":"Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101\n\tThunderbird/52.3.0","MIME-Version":"1.0","In-Reply-To":"<20170920135833.20472-3-pbutsykin@virtuozzo.com>","Content-Type":"text/plain; charset=utf-8","Content-Language":"en-US","Content-Transfer-Encoding":"7bit","X-Scanned-By":"MIMEDefang 2.79 on 10.5.11.12","X-Greylist":"Sender IP whitelisted, not delayed by milter-greylist-4.5.16\n\t(mx1.redhat.com [10.5.110.25]);\n\tWed, 20 Sep 2017 21:38:36 +0000 (UTC)","X-detected-operating-system":"by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic]\n\t[fuzzy]","X-Received-From":"209.132.183.28","Subject":"Re: [Qemu-devel] [Qemu-block] [PATCH 2/2] qcow2: truncate the tail\n\tof the image file after shrinking the image","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.21","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<http://lists.nongnu.org/archive/html/qemu-devel/>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Cc":"kwolf@redhat.com, den@openvz.org, mreitz@redhat.com","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"\"Qemu-devel\"\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>"}},{"id":1772628,"web_url":"http://patchwork.ozlabs.org/comment/1772628/","msgid":"<79eb6628-130e-af9d-cd39-56bf71194ae3@virtuozzo.com>","list_archive_url":null,"date":"2017-09-21T09:49:55","subject":"Re: [Qemu-devel] [Qemu-block] [PATCH 2/2] qcow2: truncate the tail\n\tof the image file after shrinking the image","submitter":{"id":67271,"url":"http://patchwork.ozlabs.org/api/people/67271/","name":"Pavel Butsykin","email":"pbutsykin@virtuozzo.com"},"content":"On 21.09.2017 00:38, John Snow wrote:\n> \n> \n> On 09/20/2017 09:58 AM, Pavel Butsykin wrote:\n>> Now after shrinking the image, at the end of the image file, there might be a\n>> tail that probably will never be used. So we can find the last used cluster and\n>> cut the tail.\n>>\n>> Signed-off-by: Pavel Butsykin <pbutsykin@virtuozzo.com>\n>> ---\n>>   block/qcow2-refcount.c | 21 +++++++++++++++++++++\n>>   block/qcow2.c          | 19 +++++++++++++++++++\n>>   block/qcow2.h          |  1 +\n>>   3 files changed, 41 insertions(+)\n>>\n>> diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c\n>> index 88d5a3f1ad..5e221a166c 100644\n>> --- a/block/qcow2-refcount.c\n>> +++ b/block/qcow2-refcount.c\n>> @@ -3181,3 +3181,24 @@ out:\n>>       g_free(reftable_tmp);\n>>       return ret;\n>>   }\n>> +\n>> +int64_t qcow2_get_last_cluster(BlockDriverState *bs, int64_t size)\n>> +{\n>> +    BDRVQcow2State *s = bs->opaque;\n>> +    int64_t i, last_cluster, nb_clusters = size_to_clusters(s, size);\n>> +    uint64_t refcount;\n>> +\n>> +    for (i = 0, last_cluster = 0; i < nb_clusters; i++) {\n>> +        int ret = qcow2_get_refcount(bs, i, &refcount);\n>> +        if (ret < 0) {\n>> +            fprintf(stderr, \"Can't get refcount for cluster %\" PRId64 \": %s\\n\",\n>> +                    i, strerror(-ret));\n>> +            continue;\n>> +        }\n>> +\n>> +        if (refcount > 0) {\n>> +            last_cluster = i;\n>> +        }\n>> +    }\n>> +    return last_cluster;\n>> +}> diff --git a/block/qcow2.c b/block/qcow2.c\n>> index 8a4311d338..c3b6dd44c4 100644\n>> --- a/block/qcow2.c\n>> +++ b/block/qcow2.c\n>> @@ -3106,6 +3106,7 @@ static int qcow2_truncate(BlockDriverState *bs, int64_t offset,\n>>       new_l1_size = size_to_l1(s, offset);\n>>   \n>>       if (offset < old_length) {\n>> +        int64_t image_end_offset, old_file_size;\n>>           if (prealloc != PREALLOC_MODE_OFF) {\n>>               error_setg(errp,\n>>                          \"Preallocation can't be used for shrinking an image\");\n>> @@ -3134,6 +3135,24 @@ static int qcow2_truncate(BlockDriverState *bs, int64_t offset,\n>>                                \"Failed to discard unused refblocks\");\n>>               return ret;\n>>           }\n>> +\n>> +        old_file_size = bdrv_getlength(bs->file->bs);\n>> +        if (old_file_size < 0) {\n>> +            error_setg_errno(errp, -old_file_size,\n>> +                             \"Failed to inquire current file length\");\n>> +            return old_file_size;\n>> +        }\n>> +        image_end_offset = (qcow2_get_last_cluster(bs, old_file_size) + 1) *\n>> +                           s->cluster_size;\n>> +        if (image_end_offset < old_file_size) {\n>> +            ret = bdrv_truncate(bs->file, image_end_offset,\n>> +                                PREALLOC_MODE_OFF, NULL);\n>> +            if (ret < 0) {\n>> +                error_setg_errno(errp, -ret,\n>> +                                 \"Failed to truncate the tail of the image\");\n> \n> I've recently become skeptical of what partial resize successes look\n> like, but that's an issue for another day entirely.\n> \n>> +                return ret;\n>> +            }\n>> +        }\n>>       } else {\n>>           ret = qcow2_grow_l1_table(bs, new_l1_size, true);\n>>           if (ret < 0) {\n>> diff --git a/block/qcow2.h b/block/qcow2.h\n>> index 5a289a81e2..782a206ecb 100644\n>> --- a/block/qcow2.h\n>> +++ b/block/qcow2.h\n>> @@ -597,6 +597,7 @@ int qcow2_change_refcount_order(BlockDriverState *bs, int refcount_order,\n>>                                   BlockDriverAmendStatusCB *status_cb,\n>>                                   void *cb_opaque, Error **errp);\n>>   int qcow2_shrink_reftable(BlockDriverState *bs);\n>> +int64_t qcow2_get_last_cluster(BlockDriverState *bs, int64_t size);\n>>   \n>>   /* qcow2-cluster.c functions */\n>>   int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,\n>>\n> \n> Reviewed-by: John Snow <jsnow@redhat.com>\n> \n> Looks sane to me, but under which circumstances might we grow such a\n> tail? I assume the actual truncate call aligns to cluster boundaries as\n> appropriate, so is this a bit of a \"quick fix\" to cull unused clusters\n> that happened to be near the truncate boundary?\n> \n> It might be worth documenting the circumstances that produces this\n> unused space that will never get used. My hunch is that such unused\n> space should likely be getting reclaimed elsewhere and not here, but\n> perhaps I'm misunderstanding the causal factors.\n>\n\nThis is a consequence of how we implemented shrinking the qcow2 image.\n(https://lists.gnu.org/archive/html/qemu-devel/2017-09/msg04580.html)\nBut on the other hand, if we need to shrink the qcow2 image without\ncopying the data, this is the only way. The same guest offset can be\nconverted to almost any host offset in the file i.e. the first guest\ncluster may be located somewhere at the end or the middle of the image\nfile. So we can't just take and truncate the image file on the border of\nthe truncation, therefore to shrink the image we just discard the\nclusters that corresponds to the truncated area. The result is a\nsparse image file where the apparent file size differs from actual size.\nAnd the tail in this case is the difference between the actual size and\nlast used cluster in the image, so in fact the cutting of the tail does\nnot change the apparent file size.\n\n> --js\n>","headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":["ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=nongnu.org\n\t(client-ip=2001:4830:134:3::11; helo=lists.gnu.org;\n\tenvelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n\treceiver=<UNKNOWN>)","ozlabs.org;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=virtuozzo.com header.i=@virtuozzo.com\n\theader.b=\"ZbzQG9Rg\"; dkim-atps=neutral","spf=none (sender IP is )\n\tsmtp.mailfrom=pbutsykin@virtuozzo.com; "],"Received":["from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11])\n\t(using TLSv1 with cipher AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3xyX0t1Dzcz9t4B\n\tfor <incoming@patchwork.ozlabs.org>;\n\tThu, 21 Sep 2017 19:50:37 +1000 (AEST)","from localhost ([::1]:52657 helo=lists.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.71) (envelope-from\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>)\n\tid 1duy7w-000530-4C\n\tfor incoming@patchwork.ozlabs.org; Thu, 21 Sep 2017 05:50:36 -0400","from eggs.gnu.org ([2001:4830:134:3::10]:35877)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <pbutsykin@virtuozzo.com>) id 1duy7U-00050T-PH\n\tfor qemu-devel@nongnu.org; Thu, 21 Sep 2017 05:50:10 -0400","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <pbutsykin@virtuozzo.com>) id 1duy7Q-00046W-Qm\n\tfor qemu-devel@nongnu.org; Thu, 21 Sep 2017 05:50:08 -0400","from mail-he1eur01on0135.outbound.protection.outlook.com\n\t([104.47.0.135]:4923\n\thelo=EUR01-HE1-obe.outbound.protection.outlook.com)\n\tby eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32)\n\t(Exim 4.71) (envelope-from <pbutsykin@virtuozzo.com>)\n\tid 1duy7P-00042v-RK; Thu, 21 Sep 2017 05:50:04 -0400","from [172.16.25.50] (195.214.232.6) by\n\tVI1PR0802MB2559.eurprd08.prod.outlook.com (2603:10a6:800:ae::9) with\n\tMicrosoft SMTP Server (version=TLS1_2,\n\tcipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.77.7;\n\tThu, 21 Sep 2017 09:49:59 +0000"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtuozzo.com;\n\ts=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version;\n\tbh=fpUG/wZqs2InWlhRTJ+aKFX5JVlWpu+qvTJJzf+hbho=;\n\tb=ZbzQG9RgEeCsN7BVUT8bm3/kDqJL2fdOAsYhxQpd2pX1GRQlTtzg5+qAofFrKaJ7AXbMmk7IzUlosE9uagu/ZTRKiZycQgF09mENFVMheutuxpQfpUBzV8Nizf0aRWZhiRkAYjPlSXdUmpErmbbnxglDZKOYQHvbwhGMvKDmwUA=","To":"John Snow <jsnow@redhat.com>, qemu-block@nongnu.org,\n\tqemu-devel@nongnu.org","References":"<20170920135833.20472-1-pbutsykin@virtuozzo.com>\n\t<20170920135833.20472-3-pbutsykin@virtuozzo.com>\n\t<ac533c95-a0f5-c6ce-cc6a-7fab63eb0517@redhat.com>","From":"Pavel Butsykin <pbutsykin@virtuozzo.com>","Message-ID":"<79eb6628-130e-af9d-cd39-56bf71194ae3@virtuozzo.com>","Date":"Thu, 21 Sep 2017 12:49:55 +0300","User-Agent":"Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101\n\tThunderbird/52.2.1","MIME-Version":"1.0","In-Reply-To":"<ac533c95-a0f5-c6ce-cc6a-7fab63eb0517@redhat.com>","Content-Type":"text/plain; charset=utf-8; format=flowed","Content-Language":"en-US","Content-Transfer-Encoding":"7bit","X-Originating-IP":"[195.214.232.6]","X-ClientProxiedBy":"HE1PR0402CA0053.eurprd04.prod.outlook.com\n\t(2603:10a6:7:7c::42) To VI1PR0802MB2559.eurprd08.prod.outlook.com\n\t(2603:10a6:800:ae::9)","X-MS-PublicTrafficType":"Email","X-MS-Office365-Filtering-Correlation-Id":"ee94f623-5570-4344-c3f2-08d500d62032","X-Microsoft-Antispam":"UriScan:; BCL:0; PCL:0;\n\tRULEID:(300000500095)(300135000095)(300000501095)(300135300095)(22001)(300000502095)(300135100095)(2017030254152)(300000503095)(300135400095)(2017052603199)(201703131423075)(201703031133081)(201702281549075)(300000504095)(300135200095)(300000505095)(300135600095)(300000506095)(300135500095);\n\tSRVR:VI1PR0802MB2559; ","X-Microsoft-Exchange-Diagnostics":["1; VI1PR0802MB2559;\n\t3:5bPHeJjcZkLVv5rykLOclJnn32Zk6ETAoou3M4SG4xZGGNKFfzUBfqG9JPE87txpG5hY/LaTkicHk7TXnsUcq5fMCqMUpwE8J6e17C9Y3jWoQOudqzbmmuNdGewmPp3LY2GEYXHJ5+m2/or+YWrT+nBeIdX9ua2bmxOTR2stMtW3T1e0T+sg696gIkPi4wouO/4ApAZkrzq1sgaWJZzAr0a/vMBHwwSSz19ITe887pdFTZF/28r0dL54oszrM0/s;\n\t25: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antispam-Report-Test":"UriScan:(226959686518644)(22074186197030);","X-Microsoft-Antispam-PRVS":"<VI1PR0802MB2559842214BD6FBD6037474FAC660@VI1PR0802MB2559.eurprd08.prod.outlook.com>","X-Exchange-Antispam-Report-CFA-Test":"BCL:0; PCL:0;\n\tRULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(2401047)(8121501046)(5005006)(93006095)(93001095)(100000703101)(100105400095)(3002001)(10201501046)(6041248)(20161123564025)(20161123562025)(20161123555025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123560025)(20161123558100)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095);\n\tSRVR:VI1PR0802MB2559; BCL:0; PCL:0;\n\tRULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095);\n\tSRVR:VI1PR0802MB2559; ","X-Forefront-PRVS":"04371797A5","X-Forefront-Antispam-Report":"SFV:NSPM;\n\tSFS:(10019020)(6009001)(6049001)(346002)(376002)(199003)(189002)(24454002)(377454003)(58126008)(2906002)(6306002)(36756003)(31686004)(65826007)(50986999)(54356999)(76176999)(305945005)(97736004)(229853002)(478600001)(53546010)(6666003)(53936002)(101416001)(23676002)(6486002)(33646002)(77096006)(64126003)(68736007)(47776003)(316002)(230700001)(16576012)(16526017)(2950100002)(5660300001)(66066001)(65956001)(8936002)(65806001)(6116002)(105586002)(3846002)(106356001)(86362001)(25786009)(81166006)(81156014)(31696002)(189998001)(50466002)(8676002)(4326008)(7736002)(83506001)(6246003);\n\tDIR:OUT; SFP:1102; SCL:1; SRVR:VI1PR0802MB2559; H:[172.16.25.50];\n\tFPR:; SPF:None; PTR:InfoNoRecords; A:1; MX:1; LANG:en; ","Received-SPF":"None (protection.outlook.com: virtuozzo.com does not designate\n\tpermitted sender hosts)","SpamDiagnosticOutput":"1:99","SpamDiagnosticMetadata":"NSPM","X-OriginatorOrg":"virtuozzo.com","X-MS-Exchange-CrossTenant-OriginalArrivalTime":"21 Sep 2017 09:49:59.8648\n\t(UTC)","X-MS-Exchange-CrossTenant-FromEntityHeader":"Hosted","X-MS-Exchange-CrossTenant-Id":"0bc7f26d-0264-416e-a6fc-8352af79c58f","X-MS-Exchange-Transport-CrossTenantHeadersStamped":"VI1PR0802MB2559","X-detected-operating-system":"by eggs.gnu.org: Windows 7 or 8 [fuzzy]","X-Received-From":"104.47.0.135","Subject":"Re: [Qemu-devel] [Qemu-block] [PATCH 2/2] qcow2: truncate the tail\n\tof the image file after shrinking the image","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.21","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<http://lists.nongnu.org/archive/html/qemu-devel/>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Cc":"kwolf@redhat.com, den@openvz.org, mreitz@redhat.com","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"\"Qemu-devel\"\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>"}},{"id":1772927,"web_url":"http://patchwork.ozlabs.org/comment/1772927/","msgid":"<d64c9cdd-098b-bb41-cf80-de51c569288b@redhat.com>","list_archive_url":null,"date":"2017-09-21T15:28:19","subject":"Re: [Qemu-devel] [PATCH 2/2] qcow2: truncate the tail of the image\n\tfile after shrinking the image","submitter":{"id":36836,"url":"http://patchwork.ozlabs.org/api/people/36836/","name":"Max Reitz","email":"mreitz@redhat.com"},"content":"On 2017-09-20 15:58, Pavel Butsykin wrote:\n> Now after shrinking the image, at the end of the image file, there might be a\n> tail that probably will never be used. So we can find the last used cluster and\n> cut the tail.\n> \n> Signed-off-by: Pavel Butsykin <pbutsykin@virtuozzo.com>\n> ---\n>  block/qcow2-refcount.c | 21 +++++++++++++++++++++\n>  block/qcow2.c          | 19 +++++++++++++++++++\n>  block/qcow2.h          |  1 +\n>  3 files changed, 41 insertions(+)\n> \n> diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c\n> index 88d5a3f1ad..5e221a166c 100644\n> --- a/block/qcow2-refcount.c\n> +++ b/block/qcow2-refcount.c\n> @@ -3181,3 +3181,24 @@ out:\n>      g_free(reftable_tmp);\n>      return ret;\n>  }\n> +\n> +int64_t qcow2_get_last_cluster(BlockDriverState *bs, int64_t size)\n> +{\n> +    BDRVQcow2State *s = bs->opaque;\n> +    int64_t i, last_cluster, nb_clusters = size_to_clusters(s, size);\n> +    uint64_t refcount;\n> +\n> +    for (i = 0, last_cluster = 0; i < nb_clusters; i++) {\n> +        int ret = qcow2_get_refcount(bs, i, &refcount);\n> +        if (ret < 0) {\n> +            fprintf(stderr, \"Can't get refcount for cluster %\" PRId64 \": %s\\n\",\n> +                    i, strerror(-ret));\n> +            continue;\n> +        }\n> +\n> +        if (refcount > 0) {\n> +            last_cluster = i;\n> +        }\n> +    }\n> +    return last_cluster;\n> +}\n\nWouldn't it make more sense to start from the end of the image?\n\nMax","headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":["ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=nongnu.org\n\t(client-ip=2001:4830:134:3::11; helo=lists.gnu.org;\n\tenvelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n\treceiver=<UNKNOWN>)","ext-mx05.extmail.prod.ext.phx2.redhat.com;\n\tdmarc=none (p=none dis=none) header.from=redhat.com","ext-mx05.extmail.prod.ext.phx2.redhat.com;\n\tspf=fail smtp.mailfrom=mreitz@redhat.com"],"Received":["from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11])\n\t(using TLSv1 with cipher AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3xygWB2snvz9t49\n\tfor <incoming@patchwork.ozlabs.org>;\n\tFri, 22 Sep 2017 01:28:54 +1000 (AEST)","from localhost ([::1]:54202 helo=lists.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.71) (envelope-from\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>)\n\tid 1dv3PI-0005Pb-IK\n\tfor incoming@patchwork.ozlabs.org; Thu, 21 Sep 2017 11:28:52 -0400","from eggs.gnu.org ([2001:4830:134:3::10]:35934)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <mreitz@redhat.com>) id 1dv3Ow-0005OQ-8t\n\tfor qemu-devel@nongnu.org; Thu, 21 Sep 2017 11:28:31 -0400","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <mreitz@redhat.com>) id 1dv3Ov-0004ah-9P\n\tfor qemu-devel@nongnu.org; Thu, 21 Sep 2017 11:28:30 -0400","from mx1.redhat.com ([209.132.183.28]:49668)\n\tby eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32)\n\t(Exim 4.71) (envelope-from <mreitz@redhat.com>)\n\tid 1dv3Op-0004YI-TE; Thu, 21 Sep 2017 11:28:24 -0400","from smtp.corp.redhat.com\n\t(int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15])\n\t(using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby mx1.redhat.com (Postfix) with ESMTPS id CC79590902;\n\tThu, 21 Sep 2017 15:28:22 +0000 (UTC)","from dresden.str.redhat.com (ovpn-204-199.brq.redhat.com\n\t[10.40.204.199])\n\tby smtp.corp.redhat.com (Postfix) with ESMTPS id 37B765EDE1;\n\tThu, 21 Sep 2017 15:28:21 +0000 (UTC)"],"DMARC-Filter":"OpenDMARC Filter v1.3.2 mx1.redhat.com CC79590902","To":"Pavel Butsykin <pbutsykin@virtuozzo.com>, qemu-block@nongnu.org,\n\tqemu-devel@nongnu.org","References":"<20170920135833.20472-1-pbutsykin@virtuozzo.com>\n\t<20170920135833.20472-3-pbutsykin@virtuozzo.com>","From":"Max Reitz <mreitz@redhat.com>","Message-ID":"<d64c9cdd-098b-bb41-cf80-de51c569288b@redhat.com>","Date":"Thu, 21 Sep 2017 17:28:19 +0200","User-Agent":"Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101\n\tThunderbird/52.3.0","MIME-Version":"1.0","In-Reply-To":"<20170920135833.20472-3-pbutsykin@virtuozzo.com>","Content-Type":"multipart/signed; micalg=pgp-sha256;\n\tprotocol=\"application/pgp-signature\";\n\tboundary=\"gfrM87qRwCsCj946QDBplaDW8TbQfn0NC\"","X-Scanned-By":"MIMEDefang 2.79 on 10.5.11.15","X-Greylist":"Sender IP whitelisted, not delayed by milter-greylist-4.5.16\n\t(mx1.redhat.com [10.5.110.29]);\n\tThu, 21 Sep 2017 15:28:23 +0000 (UTC)","X-detected-operating-system":"by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic]\n\t[fuzzy]","X-Received-From":"209.132.183.28","X-Content-Filtered-By":"Mailman/MimeDel 2.1.21","Subject":"Re: [Qemu-devel] [PATCH 2/2] qcow2: truncate the tail of the image\n\tfile after shrinking the image","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.21","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<http://lists.nongnu.org/archive/html/qemu-devel/>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Cc":"kwolf@redhat.com, den@openvz.org","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"\"Qemu-devel\"\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>"}},{"id":1772931,"web_url":"http://patchwork.ozlabs.org/comment/1772931/","msgid":"<129077ca-af54-fe7c-40ee-850547f63cff@redhat.com>","list_archive_url":null,"date":"2017-09-21T15:30:38","subject":"Re: [Qemu-devel] [PATCH 2/2] qcow2: truncate the tail of the image\n\tfile after shrinking the image","submitter":{"id":36836,"url":"http://patchwork.ozlabs.org/api/people/36836/","name":"Max Reitz","email":"mreitz@redhat.com"},"content":"On 2017-09-20 15:58, Pavel Butsykin wrote:\n> Now after shrinking the image, at the end of the image file, there might be a\n> tail that probably will never be used. So we can find the last used cluster and\n> cut the tail.\n> \n> Signed-off-by: Pavel Butsykin <pbutsykin@virtuozzo.com>\n> ---\n>  block/qcow2-refcount.c | 21 +++++++++++++++++++++\n>  block/qcow2.c          | 19 +++++++++++++++++++\n>  block/qcow2.h          |  1 +\n>  3 files changed, 41 insertions(+)\n> \n> diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c\n> index 88d5a3f1ad..5e221a166c 100644\n> --- a/block/qcow2-refcount.c\n> +++ b/block/qcow2-refcount.c\n> @@ -3181,3 +3181,24 @@ out:\n>      g_free(reftable_tmp);\n>      return ret;\n>  }\n> +\n> +int64_t qcow2_get_last_cluster(BlockDriverState *bs, int64_t size)\n> +{\n> +    BDRVQcow2State *s = bs->opaque;\n> +    int64_t i, last_cluster, nb_clusters = size_to_clusters(s, size);\n> +    uint64_t refcount;\n> +\n> +    for (i = 0, last_cluster = 0; i < nb_clusters; i++) {\n> +        int ret = qcow2_get_refcount(bs, i, &refcount);\n> +        if (ret < 0) {\n> +            fprintf(stderr, \"Can't get refcount for cluster %\" PRId64 \": %s\\n\",\n> +                    i, strerror(-ret));\n> +            continue;\n\nOh, and another thing: If you decide to ignore errors here, I'd at least\nconsider the cluster allocated.\n\nOf course it would also be possible not to ignore errors, and instead\nreturn them to the caller which would then just not truncate the file.\n\nMax\n\n> +        }\n> +\n> +        if (refcount > 0) {\n> +            last_cluster = i;\n> +        }\n> +    }\n> +    return last_cluster;\n> +}","headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":["ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=nongnu.org\n\t(client-ip=2001:4830:134:3::11; helo=lists.gnu.org;\n\tenvelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n\treceiver=<UNKNOWN>)","ext-mx07.extmail.prod.ext.phx2.redhat.com;\n\tdmarc=none (p=none dis=none) header.from=redhat.com","ext-mx07.extmail.prod.ext.phx2.redhat.com;\n\tspf=fail smtp.mailfrom=mreitz@redhat.com"],"Received":["from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11])\n\t(using TLSv1 with cipher AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3xygZZ3pJZz9t4B\n\tfor <incoming@patchwork.ozlabs.org>;\n\tFri, 22 Sep 2017 01:31:50 +1000 (AEST)","from localhost ([::1]:54217 helo=lists.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.71) (envelope-from\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>)\n\tid 1dv3S8-00077Y-Li\n\tfor incoming@patchwork.ozlabs.org; Thu, 21 Sep 2017 11:31:48 -0400","from eggs.gnu.org ([2001:4830:134:3::10]:38315)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <mreitz@redhat.com>) id 1dv3Rk-00075q-RP\n\tfor qemu-devel@nongnu.org; Thu, 21 Sep 2017 11:31:29 -0400","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <mreitz@redhat.com>) id 1dv3Re-0007t8-NY\n\tfor qemu-devel@nongnu.org; Thu, 21 Sep 2017 11:31:24 -0400","from mx1.redhat.com ([209.132.183.28]:56974)\n\tby eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32)\n\t(Exim 4.71) (envelope-from <mreitz@redhat.com>)\n\tid 1dv3R4-0007DU-AO; Thu, 21 Sep 2017 11:30:42 -0400","from smtp.corp.redhat.com\n\t(int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12])\n\t(using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby mx1.redhat.com (Postfix) with ESMTPS id 4AC8FC071886;\n\tThu, 21 Sep 2017 15:30:41 +0000 (UTC)","from dresden.str.redhat.com (ovpn-204-199.brq.redhat.com\n\t[10.40.204.199])\n\tby smtp.corp.redhat.com (Postfix) with ESMTPS id ACF7D610B0;\n\tThu, 21 Sep 2017 15:30:39 +0000 (UTC)"],"DMARC-Filter":"OpenDMARC Filter v1.3.2 mx1.redhat.com 4AC8FC071886","To":"Pavel Butsykin <pbutsykin@virtuozzo.com>, qemu-block@nongnu.org,\n\tqemu-devel@nongnu.org","References":"<20170920135833.20472-1-pbutsykin@virtuozzo.com>\n\t<20170920135833.20472-3-pbutsykin@virtuozzo.com>","From":"Max Reitz <mreitz@redhat.com>","Message-ID":"<129077ca-af54-fe7c-40ee-850547f63cff@redhat.com>","Date":"Thu, 21 Sep 2017 17:30:38 +0200","User-Agent":"Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101\n\tThunderbird/52.3.0","MIME-Version":"1.0","In-Reply-To":"<20170920135833.20472-3-pbutsykin@virtuozzo.com>","Content-Type":"multipart/signed; micalg=pgp-sha256;\n\tprotocol=\"application/pgp-signature\";\n\tboundary=\"CiwGHoVTMa0hdqTqjokl2ooBqdH8x5L5t\"","X-Scanned-By":"MIMEDefang 2.79 on 10.5.11.12","X-Greylist":"Sender IP whitelisted, not delayed by milter-greylist-4.5.16\n\t(mx1.redhat.com [10.5.110.31]);\n\tThu, 21 Sep 2017 15:30:41 +0000 (UTC)","X-detected-operating-system":"by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic]\n\t[fuzzy]","X-Received-From":"209.132.183.28","X-Content-Filtered-By":"Mailman/MimeDel 2.1.21","Subject":"Re: [Qemu-devel] [PATCH 2/2] qcow2: truncate the tail of the image\n\tfile after shrinking the image","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.21","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<http://lists.nongnu.org/archive/html/qemu-devel/>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Cc":"kwolf@redhat.com, den@openvz.org","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"\"Qemu-devel\"\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>"}},{"id":1772951,"web_url":"http://patchwork.ozlabs.org/comment/1772951/","msgid":"<45a6e398-ebbf-fb7c-8d42-535376ae4234@virtuozzo.com>","list_archive_url":null,"date":"2017-09-21T15:52:29","subject":"Re: [Qemu-devel] [PATCH 2/2] qcow2: truncate the tail of the image\n\tfile after shrinking the image","submitter":{"id":67271,"url":"http://patchwork.ozlabs.org/api/people/67271/","name":"Pavel Butsykin","email":"pbutsykin@virtuozzo.com"},"content":"On 21.09.2017 18:30, Max Reitz wrote:\n> On 2017-09-20 15:58, Pavel Butsykin wrote:\n>> Now after shrinking the image, at the end of the image file, there might be a\n>> tail that probably will never be used. So we can find the last used cluster and\n>> cut the tail.\n>>\n>> Signed-off-by: Pavel Butsykin <pbutsykin@virtuozzo.com>\n>> ---\n>>   block/qcow2-refcount.c | 21 +++++++++++++++++++++\n>>   block/qcow2.c          | 19 +++++++++++++++++++\n>>   block/qcow2.h          |  1 +\n>>   3 files changed, 41 insertions(+)\n>>\n>> diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c\n>> index 88d5a3f1ad..5e221a166c 100644\n>> --- a/block/qcow2-refcount.c\n>> +++ b/block/qcow2-refcount.c\n>> @@ -3181,3 +3181,24 @@ out:\n>>       g_free(reftable_tmp);\n>>       return ret;\n>>   }\n>> +\n>> +int64_t qcow2_get_last_cluster(BlockDriverState *bs, int64_t size)\n>> +{\n>> +    BDRVQcow2State *s = bs->opaque;\n>> +    int64_t i, last_cluster, nb_clusters = size_to_clusters(s, size);\n>> +    uint64_t refcount;\n>> +\n>> +    for (i = 0, last_cluster = 0; i < nb_clusters; i++) {\n>> +        int ret = qcow2_get_refcount(bs, i, &refcount);\n>> +        if (ret < 0) {\n>> +            fprintf(stderr, \"Can't get refcount for cluster %\" PRId64 \": %s\\n\",\n>> +                    i, strerror(-ret));\n>> +            continue;\n> \n> Oh, and another thing: If you decide to ignore errors here, I'd at least\n> consider the cluster allocated.\n> \n> Of course it would also be possible not to ignore errors, and instead\n> return them to the caller which would then just not truncate the file.\n\nYes, it seems so safer.\n\n> Max\n> \n>> +        }\n>> +\n>> +        if (refcount > 0) {\n>> +            last_cluster = i;\n>> +        }\n>> +    }\n>> +    return last_cluster;\n>> +}\n>","headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":["ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=nongnu.org\n\t(client-ip=2001:4830:134:3::11; helo=lists.gnu.org;\n\tenvelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n\treceiver=<UNKNOWN>)","ozlabs.org;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=virtuozzo.com header.i=@virtuozzo.com\n\theader.b=\"IteC2jsl\"; dkim-atps=neutral","spf=none (sender IP is )\n\tsmtp.mailfrom=pbutsykin@virtuozzo.com; "],"Received":["from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11])\n\t(using TLSv1 with cipher AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3xyh3W07Xjz9t4B\n\tfor <incoming@patchwork.ozlabs.org>;\n\tFri, 22 Sep 2017 01:53:24 +1000 (AEST)","from localhost ([::1]:54290 helo=lists.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.71) (envelope-from\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>)\n\tid 1dv3n0-0004Zt-2k\n\tfor incoming@patchwork.ozlabs.org; Thu, 21 Sep 2017 11:53:22 -0400","from eggs.gnu.org ([2001:4830:134:3::10]:46779)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <pbutsykin@virtuozzo.com>) id 1dv3mL-0004YX-Io\n\tfor qemu-devel@nongnu.org; Thu, 21 Sep 2017 11:52:42 -0400","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <pbutsykin@virtuozzo.com>) id 1dv3mI-0006D8-9S\n\tfor qemu-devel@nongnu.org; Thu, 21 Sep 2017 11:52:41 -0400","from mail-db5eur01on0129.outbound.protection.outlook.com\n\t([104.47.2.129]:18752\n\thelo=EUR01-DB5-obe.outbound.protection.outlook.com)\n\tby eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32)\n\t(Exim 4.71) (envelope-from <pbutsykin@virtuozzo.com>)\n\tid 1dv3mH-0006CK-O6; Thu, 21 Sep 2017 11:52:38 -0400","from [172.16.25.50] (195.214.232.6) by\n\tHE1PR0802MB2554.eurprd08.prod.outlook.com (2603:10a6:3:e1::19) with\n\tMicrosoft SMTP Server (version=TLS1_2,\n\tcipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.56.11;\n\tThu, 21 Sep 2017 15:52:33 +0000"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtuozzo.com;\n\ts=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version;\n\tbh=up+3NSFEtGctTQ01kOO8ET9UR9oEfRB08Wzdm1R7pok=;\n\tb=IteC2jslW8/T+KztAhp2LvC6qBqKUXd3WYVYMCNoSk66dmyOdS4Gs4myKNX2m/q74RkRm/hd7+/euf7bux8d5//aYjkrt8RER9jvKqNoSED4BMfoOWZp+DhWq11GetBhUNIoxs71IRjM+bP19w+DgEi+lmgf3cCWMZC3FYHPU2c=","To":"Max Reitz <mreitz@redhat.com>, qemu-block@nongnu.org,\n\tqemu-devel@nongnu.org","References":"<20170920135833.20472-1-pbutsykin@virtuozzo.com>\n\t<20170920135833.20472-3-pbutsykin@virtuozzo.com>\n\t<129077ca-af54-fe7c-40ee-850547f63cff@redhat.com>","From":"Pavel Butsykin <pbutsykin@virtuozzo.com>","Message-ID":"<45a6e398-ebbf-fb7c-8d42-535376ae4234@virtuozzo.com>","Date":"Thu, 21 Sep 2017 18:52:29 +0300","User-Agent":"Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101\n\tThunderbird/52.2.1","MIME-Version":"1.0","In-Reply-To":"<129077ca-af54-fe7c-40ee-850547f63cff@redhat.com>","Content-Type":"text/plain; charset=utf-8; format=flowed","Content-Language":"en-US","Content-Transfer-Encoding":"7bit","X-Originating-IP":"[195.214.232.6]","X-ClientProxiedBy":"AM5PR0202CA0015.eurprd02.prod.outlook.com\n\t(2603:10a6:203:69::25) To HE1PR0802MB2554.eurprd08.prod.outlook.com\n\t(2603:10a6:3:e1::19)","X-MS-PublicTrafficType":"Email","X-MS-Office365-Filtering-Correlation-Id":"a6b45ca5-0a68-4973-a715-08d50108c667","X-Microsoft-Antispam":"UriScan:; BCL:0; PCL:0;\n\tRULEID:(300000500095)(300135000095)(300000501095)(300135300095)(22001)(300000502095)(300135100095)(2017030254152)(300000503095)(300135400095)(201703131423075)(201703031133081)(201702281549075)(300000504095)(300135200095)(300000505095)(300135600095)(300000506095)(300135500095);\n\tSRVR:HE1PR0802MB2554; ","X-Microsoft-Exchange-Diagnostics":["1; HE1PR0802MB2554;\n\t3:N5VpmbRXtLSWe8vgYiYVAgQS+uh670IuVISKgu03U8x4NqjP21dyusPby7QCY+oaPVuCRJDxNMXRKS1/xBlhuqIMZPMjlYSiBf8OLjAHlDrcL1DtZOxZaDFnpPVcacHMFs3u0ICjvVYa68dn1UwjqNp0nVtIZtP7n9lmD3OSi6yPaxjueh1HkK94BmHB8uMUAN2Qt3hnBGGT8H2zJTlB8yjF8B9OhR7j/hwDoZ4p4wechBVv7LtcnLz5Pbb74qN6;\n\t25:MQVlB3Qs76BElWuLu+JQmmvMC5rPrpOcYpZWQXrSP86FHhZHAQRk7cthNbP+YV/d1f+0BWCh65T4fJgjfcnSOo0F9hCahd9zvUbeAXekcMMNBE0ST5dU0ZF/gN4fhFWoU0X+LJXOgOyNYetkvpeE/CBGN+8tmxbh/jzG+ForuZqrxF0/v40sUwQd+5hvN6F6Sf53DCTbFGnNhD4/0VSyje7ghVyY414JLPxwcJ8F0eat8yfN8G6tj4iOSKBOjoydq1iZPrja62luAER1NILso5oLFKzbauvCpahQHz+6Dlp8lJt6Ki+Qn/M7ZX3oBu2j6Ut5ERa3hEPhQJNtLg51Pw==;\n\t31:QDPxwR+hRgoMEldGgsZkOBt0vLtgZK6a/c0R4fT0Yehl7Njo8Z5Mo9mVoUG+oSg5ysOb4ktP73P4GuOkr0PkNgtn02RwHuwFlQBmgceR9K34bhdDDg50E+uO3p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0ObKwg3YcdYrW65q4dgOU2Qw2QepLXGu8n6OuRHhTWV7ZbQIVET+QETBEcvmgNYVBE2WCWxi0u4=;\n\t7:laEmNq8mU/YG14BPiYPr0Kmnaqxf85FZG8XWP68UGUqD7Sc/x8/qZf3tE1ppOKe5ZQpyTlcm5hBmkKdwl79MUInlTn7GjQPnsq5UFA4DNBL/stglgAgJ7r07/XCoCbiHWCuM0QbyRZPAy4Y7JPlsSMIIW8T/WliU0pK9xG8UD/BlCcJJHzz7tdFppcINwMQ2ABT1pBh7rMv1nt9r5AvECj7zall+DL4tLPtgO5pEtyc=","1; HE1PR0802MB2554;\n\t20:YfCFHWElv1Qu9GhD/PjlOCYxM82KxbGYlAIaqm0es470+XySjZqWhVbk2f9Zhy+FTRiY7HnYQB4aeJUSEY0igzujy8XeEC780HKbMG/W+wwvf9tyXv7YCgLraheQcPF06dMifyNYJWRoY9mDYWFoWBIHbQNDXMKP7pViCGH0rKo="],"X-MS-TrafficTypeDiagnostic":"HE1PR0802MB2554:","X-Exchange-Antispam-Report-Test":"UriScan:;","X-Microsoft-Antispam-PRVS":"<HE1PR0802MB2554870ADB6EDE600A8D604EAC660@HE1PR0802MB2554.eurprd08.prod.outlook.com>","X-Exchange-Antispam-Report-CFA-Test":"BCL:0; PCL:0;\n\tRULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(2401047)(5005006)(8121501046)(93006095)(93001095)(10201501046)(100000703101)(100105400095)(3002001)(6041248)(20161123562025)(20161123564025)(20161123555025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123558100)(20161123560025)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095);\n\tSRVR:HE1PR0802MB2554; BCL:0; PCL:0;\n\tRULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095);\n\tSRVR:HE1PR0802MB2554; ","X-Forefront-PRVS":"04371797A5","X-Forefront-Antispam-Report":"SFV:NSPM;\n\tSFS:(10019020)(7370300001)(4630300001)(6009001)(6049001)(376002)(346002)(199003)(24454002)(377424004)(189002)(3846002)(33646002)(4326008)(31686004)(86362001)(101416001)(81156014)(230700001)(54356999)(16576012)(31696002)(50986999)(8676002)(76176999)(81166006)(16526017)(68736007)(189998001)(83506001)(305945005)(2906002)(7736002)(478600001)(107886003)(97736004)(58126008)(6246003)(53936002)(23676002)(47776003)(25786009)(5660300001)(36756003)(53546010)(50466002)(65806001)(316002)(65956001)(65826007)(66066001)(77096006)(6116002)(7350300001)(64126003)(6666003)(105586002)(2950100002)(106356001)(229853002)(6486002);\n\tDIR:OUT; SFP:1102; SCL:1; SRVR:HE1PR0802MB2554; H:[172.16.25.50];\n\tFPR:; SPF:None; PTR:InfoNoRecords; A:1; MX:1; LANG:en; ","Received-SPF":"None (protection.outlook.com: virtuozzo.com does not designate\n\tpermitted sender hosts)","SpamDiagnosticOutput":"1:99","SpamDiagnosticMetadata":"NSPM","X-OriginatorOrg":"virtuozzo.com","X-MS-Exchange-CrossTenant-OriginalArrivalTime":"21 Sep 2017 15:52:33.6591\n\t(UTC)","X-MS-Exchange-CrossTenant-FromEntityHeader":"Hosted","X-MS-Exchange-Transport-CrossTenantHeadersStamped":"HE1PR0802MB2554","X-detected-operating-system":"by eggs.gnu.org: Windows 7 or 8 [fuzzy]","X-Received-From":"104.47.2.129","Subject":"Re: [Qemu-devel] [PATCH 2/2] qcow2: truncate the tail of the image\n\tfile after shrinking the image","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.21","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<http://lists.nongnu.org/archive/html/qemu-devel/>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Cc":"kwolf@redhat.com, den@openvz.org","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"\"Qemu-devel\"\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>"}},{"id":1772962,"web_url":"http://patchwork.ozlabs.org/comment/1772962/","msgid":"<175657ad-e6b4-0323-4f32-db9217be6bac@virtuozzo.com>","list_archive_url":null,"date":"2017-09-21T16:16:56","subject":"Re: [Qemu-devel] [PATCH 2/2] qcow2: truncate the tail of the image\n\tfile after shrinking the image","submitter":{"id":67271,"url":"http://patchwork.ozlabs.org/api/people/67271/","name":"Pavel Butsykin","email":"pbutsykin@virtuozzo.com"},"content":"On 21.09.2017 18:28, Max Reitz wrote:\n> On 2017-09-20 15:58, Pavel Butsykin wrote:\n>> Now after shrinking the image, at the end of the image file, there might be a\n>> tail that probably will never be used. So we can find the last used cluster and\n>> cut the tail.\n>>\n>> Signed-off-by: Pavel Butsykin <pbutsykin@virtuozzo.com>\n>> ---\n>>   block/qcow2-refcount.c | 21 +++++++++++++++++++++\n>>   block/qcow2.c          | 19 +++++++++++++++++++\n>>   block/qcow2.h          |  1 +\n>>   3 files changed, 41 insertions(+)\n>>\n>> diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c\n>> index 88d5a3f1ad..5e221a166c 100644\n>> --- a/block/qcow2-refcount.c\n>> +++ b/block/qcow2-refcount.c\n>> @@ -3181,3 +3181,24 @@ out:\n>>       g_free(reftable_tmp);\n>>       return ret;\n>>   }\n>> +\n>> +int64_t qcow2_get_last_cluster(BlockDriverState *bs, int64_t size)\n>> +{\n>> +    BDRVQcow2State *s = bs->opaque;\n>> +    int64_t i, last_cluster, nb_clusters = size_to_clusters(s, size);\n>> +    uint64_t refcount;\n>> +\n>> +    for (i = 0, last_cluster = 0; i < nb_clusters; i++) {\n>> +        int ret = qcow2_get_refcount(bs, i, &refcount);\n>> +        if (ret < 0) {\n>> +            fprintf(stderr, \"Can't get refcount for cluster %\" PRId64 \": %s\\n\",\n>> +                    i, strerror(-ret));\n>> +            continue;\n>> +        }\n>> +\n>> +        if (refcount > 0) {\n>> +            last_cluster = i;\n>> +        }\n>> +    }\n>> +    return last_cluster;\n>> +}\n> \n> Wouldn't it make more sense to start from the end of the image?\n\nIf this will reduce the iterations, then yes. But it will depend on the\nsituation. If you truncate the image more than half, it can increase the\nnumber of iterations. But intuitively it seems that to start from the\nend would be better :)\n> Max\n>","headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":["ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=nongnu.org\n\t(client-ip=2001:4830:134:3::11; helo=lists.gnu.org;\n\tenvelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n\treceiver=<UNKNOWN>)","ozlabs.org;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=virtuozzo.com header.i=@virtuozzo.com\n\theader.b=\"Ur3ivWv0\"; dkim-atps=neutral","spf=none (sender IP is )\n\tsmtp.mailfrom=pbutsykin@virtuozzo.com; "],"Received":["from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11])\n\t(using TLSv1 with cipher AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3xyhbQ6C3xz9t4B\n\tfor <incoming@patchwork.ozlabs.org>;\n\tFri, 22 Sep 2017 02:17:38 +1000 (AEST)","from localhost ([::1]:54543 helo=lists.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.71) (envelope-from\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>)\n\tid 1dv4AS-0001Qn-Mr\n\tfor incoming@patchwork.ozlabs.org; Thu, 21 Sep 2017 12:17:36 -0400","from eggs.gnu.org ([2001:4830:134:3::10]:53717)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <pbutsykin@virtuozzo.com>) id 1dv4A0-0001Op-61\n\tfor qemu-devel@nongnu.org; Thu, 21 Sep 2017 12:17:09 -0400","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <pbutsykin@virtuozzo.com>) id 1dv49v-00009h-UQ\n\tfor qemu-devel@nongnu.org; Thu, 21 Sep 2017 12:17:08 -0400","from mail-eopbgr50105.outbound.protection.outlook.com\n\t([40.107.5.105]:1377\n\thelo=EUR03-VE1-obe.outbound.protection.outlook.com)\n\tby eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32)\n\t(Exim 4.71) (envelope-from <pbutsykin@virtuozzo.com>)\n\tid 1dv49v-00008x-81; Thu, 21 Sep 2017 12:17:03 -0400","from [172.16.25.50] (195.214.232.6) by\n\tHE1PR0802MB2553.eurprd08.prod.outlook.com (2603:10a6:3:e1::18) with\n\tMicrosoft SMTP Server (version=TLS1_2,\n\tcipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.56.11;\n\tThu, 21 Sep 2017 16:17:00 +0000"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtuozzo.com;\n\ts=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version;\n\tbh=EqpKvOTdKvY8/7meQgZQiDE6EE1WWwxS+t/g7MLql1w=;\n\tb=Ur3ivWv0PMQj1wHUYv9RA32ecXlvIny3pMuUV0jUM95B7808LQjf+h1vBALcb6MTnLNTFXKJk9szQogEoRKhrG9cCluE1NTw+ykb2kKJNBoj26dlO7s/E440c6GpWNDo3gdxvrzJu+R3Hj3b48p4F5CLzgZiYLC+zLxH44DRcss=","To":"Max Reitz <mreitz@redhat.com>, qemu-block@nongnu.org,\n\tqemu-devel@nongnu.org","References":"<20170920135833.20472-1-pbutsykin@virtuozzo.com>\n\t<20170920135833.20472-3-pbutsykin@virtuozzo.com>\n\t<d64c9cdd-098b-bb41-cf80-de51c569288b@redhat.com>","From":"Pavel Butsykin <pbutsykin@virtuozzo.com>","Message-ID":"<175657ad-e6b4-0323-4f32-db9217be6bac@virtuozzo.com>","Date":"Thu, 21 Sep 2017 19:16:56 +0300","User-Agent":"Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101\n\tThunderbird/52.2.1","MIME-Version":"1.0","In-Reply-To":"<d64c9cdd-098b-bb41-cf80-de51c569288b@redhat.com>","Content-Type":"text/plain; charset=utf-8; format=flowed","Content-Language":"en-US","Content-Transfer-Encoding":"7bit","X-Originating-IP":"[195.214.232.6]","X-ClientProxiedBy":"HE1PR0102CA0016.eurprd01.prod.exchangelabs.com\n\t(2603:10a6:7:14::29) To HE1PR0802MB2553.eurprd08.prod.outlook.com\n\t(2603:10a6:3:e1::18)","X-MS-PublicTrafficType":"Email","X-MS-Office365-Filtering-Correlation-Id":"d5c6d758-7afb-4093-2613-08d5010c30b8","X-Microsoft-Antispam":"UriScan:; BCL:0; PCL:0;\n\tRULEID:(300000500095)(300135000095)(300000501095)(300135300095)(22001)(300000502095)(300135100095)(2017030254152)(300000503095)(300135400095)(201703131423075)(201703031133081)(201702281549075)(300000504095)(300135200095)(300000505095)(300135600095)(300000506095)(300135500095);\n\tSRVR:HE1PR0802MB2553; ","X-Microsoft-Exchange-Diagnostics":["1; HE1PR0802MB2553;\n\t3:XrXKfAp60Cp1jw0GTFRtTRzkXoe5B0xF/MTF9EWgksPhebXASaAGgdiutYMcdmSxpW1Wx4Nc7oBPjIpm43oUfbmiZtJKuBKar/XOMlzgfn263uuodnrm8R7ZRYcV/1ETo2Fc45bp/GacdaVOYQRxtM2/wgXk1UODM0qa7qvEa3BEF3uiOK7GZtwKLwSelQPky52K9W8jgOIG/EuTYG46RwfSBFb85ln1+lmybRR+/D7FCJqo5k2gAACCPv3NO9sr;\n\t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1PR0802MB25537E712F5DFAD0C49823C3AC660@HE1PR0802MB2553.eurprd08.prod.outlook.com>","X-Exchange-Antispam-Report-CFA-Test":"BCL:0; PCL:0;\n\tRULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(2401047)(5005006)(8121501046)(3002001)(93006095)(93001095)(10201501046)(100000703101)(100105400095)(6041248)(20161123558100)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123562025)(20161123555025)(20161123564025)(20161123560025)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095);\n\tSRVR:HE1PR0802MB2553; BCL:0; PCL:0;\n\tRULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095);\n\tSRVR:HE1PR0802MB2553; ","X-Forefront-PRVS":"04371797A5","X-Forefront-Antispam-Report":"SFV:NSPM;\n\tSFS:(10019020)(7370300001)(4630300001)(6009001)(6049001)(376002)(346002)(189002)(377424004)(24454002)(199003)(33646002)(305945005)(4326008)(7736002)(189998001)(107886003)(8676002)(31696002)(81156014)(81166006)(6246003)(86362001)(53936002)(97736004)(16526017)(16576012)(77096006)(2950100002)(6486002)(58126008)(23676002)(65826007)(316002)(229853002)(6666003)(66066001)(65806001)(65956001)(68736007)(3846002)(2906002)(5660300001)(31686004)(478600001)(25786009)(47776003)(6116002)(50986999)(50466002)(7350300001)(230700001)(64126003)(76176999)(54356999)(83506001)(36756003)(106356001)(105586002)(101416001)(53546010);\n\tDIR:OUT; SFP:1102; SCL:1; SRVR:HE1PR0802MB2553; H:[172.16.25.50];\n\tFPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; ","Received-SPF":"None (protection.outlook.com: virtuozzo.com does not designate\n\tpermitted sender hosts)","SpamDiagnosticOutput":"1:99","SpamDiagnosticMetadata":"NSPM","X-OriginatorOrg":"virtuozzo.com","X-MS-Exchange-CrossTenant-OriginalArrivalTime":"21 Sep 2017 16:17:00.7184\n\t(UTC)","X-MS-Exchange-CrossTenant-FromEntityHeader":"Hosted","X-MS-Exchange-Transport-CrossTenantHeadersStamped":"HE1PR0802MB2553","X-detected-operating-system":"by eggs.gnu.org: Windows 7 or 8 [fuzzy]","X-Received-From":"40.107.5.105","Subject":"Re: [Qemu-devel] [PATCH 2/2] qcow2: truncate the tail of the image\n\tfile after shrinking the image","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.21","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<http://lists.nongnu.org/archive/html/qemu-devel/>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Cc":"kwolf@redhat.com, den@openvz.org","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"\"Qemu-devel\"\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>"}},{"id":1772966,"web_url":"http://patchwork.ozlabs.org/comment/1772966/","msgid":"<e8208bb2-941b-135c-419f-72a40f1973bf@redhat.com>","list_archive_url":null,"date":"2017-09-21T16:20:23","subject":"Re: [Qemu-devel] [PATCH 2/2] qcow2: truncate the tail of the image\n\tfile after shrinking the image","submitter":{"id":36836,"url":"http://patchwork.ozlabs.org/api/people/36836/","name":"Max Reitz","email":"mreitz@redhat.com"},"content":"On 2017-09-21 18:16, Pavel Butsykin wrote:\n> On 21.09.2017 18:28, Max Reitz wrote:\n>> On 2017-09-20 15:58, Pavel Butsykin wrote:\n>>> Now after shrinking the image, at the end of the image file, there\n>>> might be a\n>>> tail that probably will never be used. So we can find the last used\n>>> cluster and\n>>> cut the tail.\n>>>\n>>> Signed-off-by: Pavel Butsykin <pbutsykin@virtuozzo.com>\n>>> ---\n>>>   block/qcow2-refcount.c | 21 +++++++++++++++++++++\n>>>   block/qcow2.c          | 19 +++++++++++++++++++\n>>>   block/qcow2.h          |  1 +\n>>>   3 files changed, 41 insertions(+)\n>>>\n>>> diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c\n>>> index 88d5a3f1ad..5e221a166c 100644\n>>> --- a/block/qcow2-refcount.c\n>>> +++ b/block/qcow2-refcount.c\n>>> @@ -3181,3 +3181,24 @@ out:\n>>>       g_free(reftable_tmp);\n>>>       return ret;\n>>>   }\n>>> +\n>>> +int64_t qcow2_get_last_cluster(BlockDriverState *bs, int64_t size)\n>>> +{\n>>> +    BDRVQcow2State *s = bs->opaque;\n>>> +    int64_t i, last_cluster, nb_clusters = size_to_clusters(s, size);\n>>> +    uint64_t refcount;\n>>> +\n>>> +    for (i = 0, last_cluster = 0; i < nb_clusters; i++) {\n>>> +        int ret = qcow2_get_refcount(bs, i, &refcount);\n>>> +        if (ret < 0) {\n>>> +            fprintf(stderr, \"Can't get refcount for cluster %\"\n>>> PRId64 \": %s\\n\",\n>>> +                    i, strerror(-ret));\n>>> +            continue;\n>>> +        }\n>>> +\n>>> +        if (refcount > 0) {\n>>> +            last_cluster = i;\n>>> +        }\n>>> +    }\n>>> +    return last_cluster;\n>>> +}\n>>\n>> Wouldn't it make more sense to start from the end of the image?\n> \n> If this will reduce the iterations, then yes. But it will depend on the\n> situation. If you truncate the image more than half, it can increase the\n> number of iterations. But intuitively it seems that to start from the\n> end would be better :)\n\nThat's one thing (also, I think usually the end should coincide with\nsome allocated cluster, but yes, that's just intuition :-)); but also,\nthis would simplify things a bit because we would no longer need the\nlast_cluster variable (the loop would just stop at the first allocated\ncluster).\n\nMax","headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":["ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=nongnu.org\n\t(client-ip=2001:4830:134:3::11; helo=lists.gnu.org;\n\tenvelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n\treceiver=<UNKNOWN>)","ext-mx06.extmail.prod.ext.phx2.redhat.com;\n\tdmarc=none (p=none dis=none) header.from=redhat.com","ext-mx06.extmail.prod.ext.phx2.redhat.com;\n\tspf=fail smtp.mailfrom=mreitz@redhat.com"],"Received":["from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11])\n\t(using TLSv1 with cipher AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3xyhgl6GTWz9t42\n\tfor <incoming@patchwork.ozlabs.org>;\n\tFri, 22 Sep 2017 02:21:23 +1000 (AEST)","from localhost ([::1]:54559 helo=lists.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.71) (envelope-from\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>)\n\tid 1dv4E5-0002uj-VJ\n\tfor incoming@patchwork.ozlabs.org; Thu, 21 Sep 2017 12:21:21 -0400","from eggs.gnu.org ([2001:4830:134:3::10]:54491)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <mreitz@redhat.com>) id 1dv4DR-0002pp-Os\n\tfor qemu-devel@nongnu.org; Thu, 21 Sep 2017 12:20:43 -0400","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <mreitz@redhat.com>) id 1dv4DQ-0002DA-FT\n\tfor qemu-devel@nongnu.org; Thu, 21 Sep 2017 12:20:41 -0400","from mx1.redhat.com ([209.132.183.28]:50126)\n\tby eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32)\n\t(Exim 4.71) (envelope-from <mreitz@redhat.com>)\n\tid 1dv4DK-0002BL-Jw; Thu, 21 Sep 2017 12:20:34 -0400","from smtp.corp.redhat.com\n\t(int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16])\n\t(using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby mx1.redhat.com (Postfix) with ESMTPS id 71E3C1E31D;\n\tThu, 21 Sep 2017 16:20:33 +0000 (UTC)","from dresden.str.redhat.com (ovpn-204-199.brq.redhat.com\n\t[10.40.204.199])\n\tby smtp.corp.redhat.com (Postfix) with ESMTPS id 09CBE65E85;\n\tThu, 21 Sep 2017 16:20:24 +0000 (UTC)"],"DMARC-Filter":"OpenDMARC Filter v1.3.2 mx1.redhat.com 71E3C1E31D","To":"Pavel Butsykin <pbutsykin@virtuozzo.com>, qemu-block@nongnu.org,\n\tqemu-devel@nongnu.org","References":"<20170920135833.20472-1-pbutsykin@virtuozzo.com>\n\t<20170920135833.20472-3-pbutsykin@virtuozzo.com>\n\t<d64c9cdd-098b-bb41-cf80-de51c569288b@redhat.com>\n\t<175657ad-e6b4-0323-4f32-db9217be6bac@virtuozzo.com>","From":"Max Reitz <mreitz@redhat.com>","Message-ID":"<e8208bb2-941b-135c-419f-72a40f1973bf@redhat.com>","Date":"Thu, 21 Sep 2017 18:20:23 +0200","User-Agent":"Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101\n\tThunderbird/52.3.0","MIME-Version":"1.0","In-Reply-To":"<175657ad-e6b4-0323-4f32-db9217be6bac@virtuozzo.com>","Content-Type":"multipart/signed; micalg=pgp-sha256;\n\tprotocol=\"application/pgp-signature\";\n\tboundary=\"GjHv9St3LhmJLht028Sl9gk0pL9L85DmB\"","X-Scanned-By":"MIMEDefang 2.79 on 10.5.11.16","X-Greylist":"Sender IP whitelisted, not delayed by milter-greylist-4.5.16\n\t(mx1.redhat.com [10.5.110.30]);\n\tThu, 21 Sep 2017 16:20:33 +0000 (UTC)","X-detected-operating-system":"by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic]\n\t[fuzzy]","X-Received-From":"209.132.183.28","X-Content-Filtered-By":"Mailman/MimeDel 2.1.21","Subject":"Re: [Qemu-devel] [PATCH 2/2] qcow2: truncate the tail of the image\n\tfile after shrinking the image","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.21","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<http://lists.nongnu.org/archive/html/qemu-devel/>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Cc":"kwolf@redhat.com, den@openvz.org","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"\"Qemu-devel\"\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>"}},{"id":1772998,"web_url":"http://patchwork.ozlabs.org/comment/1772998/","msgid":"<ccbd7d7e-512a-76ea-5b03-8853fd88fda2@redhat.com>","list_archive_url":null,"date":"2017-09-21T16:48:30","subject":"Re: [Qemu-devel] [Qemu-block] [PATCH 2/2] qcow2: truncate the tail\n\tof the image file after shrinking the image","submitter":{"id":64343,"url":"http://patchwork.ozlabs.org/api/people/64343/","name":"John Snow","email":"jsnow@redhat.com"},"content":"On 09/21/2017 05:49 AM, Pavel Butsykin wrote:\n> On 21.09.2017 00:38, John Snow wrote:\n>>\n>>\n>> On 09/20/2017 09:58 AM, Pavel Butsykin wrote:\n>>> Now after shrinking the image, at the end of the image file, there\n>>> might be a\n>>> tail that probably will never be used. So we can find the last used\n>>> cluster and\n>>> cut the tail.\n>>>\n>>> Signed-off-by: Pavel Butsykin <pbutsykin@virtuozzo.com>\n>>> ---\n>>>   block/qcow2-refcount.c | 21 +++++++++++++++++++++\n>>>   block/qcow2.c          | 19 +++++++++++++++++++\n>>>   block/qcow2.h          |  1 +\n>>>   3 files changed, 41 insertions(+)\n>>>\n>>> diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c\n>>> index 88d5a3f1ad..5e221a166c 100644\n>>> --- a/block/qcow2-refcount.c\n>>> +++ b/block/qcow2-refcount.c\n>>> @@ -3181,3 +3181,24 @@ out:\n>>>       g_free(reftable_tmp);\n>>>       return ret;\n>>>   }\n>>> +\n>>> +int64_t qcow2_get_last_cluster(BlockDriverState *bs, int64_t size)\n>>> +{\n>>> +    BDRVQcow2State *s = bs->opaque;\n>>> +    int64_t i, last_cluster, nb_clusters = size_to_clusters(s, size);\n>>> +    uint64_t refcount;\n>>> +\n>>> +    for (i = 0, last_cluster = 0; i < nb_clusters; i++) {\n>>> +        int ret = qcow2_get_refcount(bs, i, &refcount);\n>>> +        if (ret < 0) {\n>>> +            fprintf(stderr, \"Can't get refcount for cluster %\"\n>>> PRId64 \": %s\\n\",\n>>> +                    i, strerror(-ret));\n>>> +            continue;\n>>> +        }\n>>> +\n>>> +        if (refcount > 0) {\n>>> +            last_cluster = i;\n>>> +        }\n>>> +    }\n>>> +    return last_cluster;\n>>> +}> diff --git a/block/qcow2.c b/block/qcow2.c\n>>> index 8a4311d338..c3b6dd44c4 100644\n>>> --- a/block/qcow2.c\n>>> +++ b/block/qcow2.c\n>>> @@ -3106,6 +3106,7 @@ static int qcow2_truncate(BlockDriverState *bs,\n>>> int64_t offset,\n>>>       new_l1_size = size_to_l1(s, offset);\n>>>         if (offset < old_length) {\n>>> +        int64_t image_end_offset, old_file_size;\n>>>           if (prealloc != PREALLOC_MODE_OFF) {\n>>>               error_setg(errp,\n>>>                          \"Preallocation can't be used for shrinking\n>>> an image\");\n>>> @@ -3134,6 +3135,24 @@ static int qcow2_truncate(BlockDriverState\n>>> *bs, int64_t offset,\n>>>                                \"Failed to discard unused refblocks\");\n>>>               return ret;\n>>>           }\n>>> +\n>>> +        old_file_size = bdrv_getlength(bs->file->bs);\n>>> +        if (old_file_size < 0) {\n>>> +            error_setg_errno(errp, -old_file_size,\n>>> +                             \"Failed to inquire current file length\");\n>>> +            return old_file_size;\n>>> +        }\n>>> +        image_end_offset = (qcow2_get_last_cluster(bs,\n>>> old_file_size) + 1) *\n>>> +                           s->cluster_size;\n>>> +        if (image_end_offset < old_file_size) {\n>>> +            ret = bdrv_truncate(bs->file, image_end_offset,\n>>> +                                PREALLOC_MODE_OFF, NULL);\n>>> +            if (ret < 0) {\n>>> +                error_setg_errno(errp, -ret,\n>>> +                                 \"Failed to truncate the tail of the\n>>> image\");\n>>\n>> I've recently become skeptical of what partial resize successes look\n>> like, but that's an issue for another day entirely.\n>>\n>>> +                return ret;\n>>> +            }\n>>> +        }\n>>>       } else {\n>>>           ret = qcow2_grow_l1_table(bs, new_l1_size, true);\n>>>           if (ret < 0) {\n>>> diff --git a/block/qcow2.h b/block/qcow2.h\n>>> index 5a289a81e2..782a206ecb 100644\n>>> --- a/block/qcow2.h\n>>> +++ b/block/qcow2.h\n>>> @@ -597,6 +597,7 @@ int qcow2_change_refcount_order(BlockDriverState\n>>> *bs, int refcount_order,\n>>>                                   BlockDriverAmendStatusCB *status_cb,\n>>>                                   void *cb_opaque, Error **errp);\n>>>   int qcow2_shrink_reftable(BlockDriverState *bs);\n>>> +int64_t qcow2_get_last_cluster(BlockDriverState *bs, int64_t size);\n>>>     /* qcow2-cluster.c functions */\n>>>   int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,\n>>>\n>>\n>> Reviewed-by: John Snow <jsnow@redhat.com>\n>>\n>> Looks sane to me, but under which circumstances might we grow such a\n>> tail? I assume the actual truncate call aligns to cluster boundaries as\n>> appropriate, so is this a bit of a \"quick fix\" to cull unused clusters\n>> that happened to be near the truncate boundary?\n>>\n>> It might be worth documenting the circumstances that produces this\n>> unused space that will never get used. My hunch is that such unused\n>> space should likely be getting reclaimed elsewhere and not here, but\n>> perhaps I'm misunderstanding the causal factors.\n>>\n> \n> This is a consequence of how we implemented shrinking the qcow2 image.\n> (https://lists.gnu.org/archive/html/qemu-devel/2017-09/msg04580.html)\n> But on the other hand, if we need to shrink the qcow2 image without\n> copying the data, this is the only way. The same guest offset can be\n> converted to almost any host offset in the file i.e. the first guest\n> cluster may be located somewhere at the end or the middle of the image\n> file. So we can't just take and truncate the image file on the border of\n> the truncation, therefore to shrink the image we just discard the\n> clusters that corresponds to the truncated area. The result is a\n> sparse image file where the apparent file size differs from actual size.\n> And the tail in this case is the difference between the actual size and\n> last used cluster in the image, so in fact the cutting of the tail does\n> not change the apparent file size.\n> \n\nOh, duh, I get it. The truncation itself creates a lot of sparseness.\n\nThanks for the explanation.","headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":["ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=nongnu.org\n\t(client-ip=2001:4830:134:3::11; helo=lists.gnu.org;\n\tenvelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n\treceiver=<UNKNOWN>)","ext-mx03.extmail.prod.ext.phx2.redhat.com;\n\tdmarc=none (p=none dis=none) header.from=redhat.com","ext-mx03.extmail.prod.ext.phx2.redhat.com;\n\tspf=fail smtp.mailfrom=jsnow@redhat.com"],"Received":["from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11])\n\t(using TLSv1 with cipher AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3xyjhN4b6hz9t4c\n\tfor <incoming@patchwork.ozlabs.org>;\n\tFri, 22 Sep 2017 03:07:00 +1000 (AEST)","from localhost ([::1]:54748 helo=lists.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.71) (envelope-from\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>)\n\tid 1dv4wE-0007eB-0N\n\tfor incoming@patchwork.ozlabs.org; Thu, 21 Sep 2017 13:06:58 -0400","from eggs.gnu.org ([2001:4830:134:3::10]:34783)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <jsnow@redhat.com>) id 1dv4eW-0001G0-FR\n\tfor qemu-devel@nongnu.org; Thu, 21 Sep 2017 12:48:42 -0400","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <jsnow@redhat.com>) id 1dv4eU-000174-Nk\n\tfor qemu-devel@nongnu.org; Thu, 21 Sep 2017 12:48:40 -0400","from mx1.redhat.com ([209.132.183.28]:34348)\n\tby eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32)\n\t(Exim 4.71) (envelope-from <jsnow@redhat.com>)\n\tid 1dv4eP-00012U-9k; Thu, 21 Sep 2017 12:48:33 -0400","from smtp.corp.redhat.com\n\t(int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14])\n\t(using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby mx1.redhat.com (Postfix) with ESMTPS id 4DA717E428;\n\tThu, 21 Sep 2017 16:48:32 +0000 (UTC)","from [10.10.121.182] (ovpn-121-182.rdu2.redhat.com [10.10.121.182])\n\tby smtp.corp.redhat.com (Postfix) with ESMTP id C40675D9CC;\n\tThu, 21 Sep 2017 16:48:30 +0000 (UTC)"],"DMARC-Filter":"OpenDMARC Filter v1.3.2 mx1.redhat.com 4DA717E428","To":"Pavel Butsykin <pbutsykin@virtuozzo.com>, qemu-block@nongnu.org,\n\tqemu-devel@nongnu.org","References":"<20170920135833.20472-1-pbutsykin@virtuozzo.com>\n\t<20170920135833.20472-3-pbutsykin@virtuozzo.com>\n\t<ac533c95-a0f5-c6ce-cc6a-7fab63eb0517@redhat.com>\n\t<79eb6628-130e-af9d-cd39-56bf71194ae3@virtuozzo.com>","From":"John Snow <jsnow@redhat.com>","Message-ID":"<ccbd7d7e-512a-76ea-5b03-8853fd88fda2@redhat.com>","Date":"Thu, 21 Sep 2017 12:48:30 -0400","User-Agent":"Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101\n\tThunderbird/52.3.0","MIME-Version":"1.0","In-Reply-To":"<79eb6628-130e-af9d-cd39-56bf71194ae3@virtuozzo.com>","Content-Type":"text/plain; charset=utf-8","Content-Language":"en-US","X-Scanned-By":"MIMEDefang 2.79 on 10.5.11.14","X-Greylist":"Sender IP whitelisted, not delayed by milter-greylist-4.5.16\n\t(mx1.redhat.com [10.5.110.27]);\n\tThu, 21 Sep 2017 16:48:32 +0000 (UTC)","Content-Transfer-Encoding":"quoted-printable","X-detected-operating-system":"by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic]\n\t[fuzzy]","X-Received-From":"209.132.183.28","Subject":"Re: [Qemu-devel] [Qemu-block] [PATCH 2/2] qcow2: truncate the tail\n\tof the image file after shrinking the image","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.21","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<http://lists.nongnu.org/archive/html/qemu-devel/>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Cc":"kwolf@redhat.com, den@openvz.org, mreitz@redhat.com","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"\"Qemu-devel\"\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>"}},{"id":1773046,"web_url":"http://patchwork.ozlabs.org/comment/1773046/","msgid":"<4393a82e-de82-ed8d-15ae-8defe59413ac@redhat.com>","list_archive_url":null,"date":"2017-09-21T18:23:32","subject":"Re: [Qemu-devel] [Qemu-block] [PATCH 2/2] qcow2: truncate the tail\n\tof the image file after shrinking the image","submitter":{"id":6591,"url":"http://patchwork.ozlabs.org/api/people/6591/","name":"Eric Blake","email":"eblake@redhat.com"},"content":"On 09/21/2017 11:48 AM, John Snow wrote:\n>>> Looks sane to me, but under which circumstances might we grow such a\n>>> tail? I assume the actual truncate call aligns to cluster boundaries as\n>>> appropriate, so is this a bit of a \"quick fix\" to cull unused clusters\n>>> that happened to be near the truncate boundary?\n>>>\n>>> It might be worth documenting the circumstances that produces this\n>>> unused space that will never get used. My hunch is that such unused\n>>> space should likely be getting reclaimed elsewhere and not here, but\n>>> perhaps I'm misunderstanding the causal factors.\n>>>\n>>\n>> This is a consequence of how we implemented shrinking the qcow2 image.\n>> (https://lists.gnu.org/archive/html/qemu-devel/2017-09/msg04580.html)\n>> But on the other hand, if we need to shrink the qcow2 image without\n>> copying the data, this is the only way. The same guest offset can be\n>> converted to almost any host offset in the file i.e. the first guest\n>> cluster may be located somewhere at the end or the middle of the image\n>> file. So we can't just take and truncate the image file on the border of\n>> the truncation, therefore to shrink the image we just discard the\n>> clusters that corresponds to the truncated area. The result is a\n>> sparse image file where the apparent file size differs from actual size.\n>> And the tail in this case is the difference between the actual size and\n>> last used cluster in the image, so in fact the cutting of the tail does\n>> not change the apparent file size.\n>>\n> \n> Oh, duh, I get it. The truncation itself creates a lot of sparseness.\n\nIt is also interesting to think about whether we should someday\nimplement a qcow2 defragmenting operation (either a simple one: pull all\nlater clusters into earlier holes, but the end result is not necessarily\ncontiguous; or a complex one: shuffle things so that all clusters are in\norder, even though it may require moving some clusters around twice), so\nthat you can remove all holes from the file (perhaps useful if the file\nis stored on a system that does not support holes).  But not for this\nseries :)","headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":["ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=nongnu.org\n\t(client-ip=2001:4830:134:3::11; helo=lists.gnu.org;\n\tenvelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n\treceiver=<UNKNOWN>)","ext-mx06.extmail.prod.ext.phx2.redhat.com;\n\tdmarc=none (p=none dis=none) header.from=redhat.com","ext-mx06.extmail.prod.ext.phx2.redhat.com;\n\tspf=fail smtp.mailfrom=eblake@redhat.com"],"Received":["from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11])\n\t(using TLSv1 with cipher AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3xylPW67CSz9sP1\n\tfor <incoming@patchwork.ozlabs.org>;\n\tFri, 22 Sep 2017 04:24:14 +1000 (AEST)","from localhost ([::1]:55023 helo=lists.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.71) (envelope-from\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>)\n\tid 1dv68x-0002nr-UZ\n\tfor incoming@patchwork.ozlabs.org; Thu, 21 Sep 2017 14:24:11 -0400","from eggs.gnu.org ([2001:4830:134:3::10]:60802)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <eblake@redhat.com>) id 1dv68Z-0002mh-I3\n\tfor qemu-devel@nongnu.org; Thu, 21 Sep 2017 14:23:48 -0400","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <eblake@redhat.com>) id 1dv68Y-0003c1-AN\n\tfor qemu-devel@nongnu.org; Thu, 21 Sep 2017 14:23:47 -0400","from mx1.redhat.com ([209.132.183.28]:52512)\n\tby eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32)\n\t(Exim 4.71) (envelope-from <eblake@redhat.com>)\n\tid 1dv68T-0003ZD-Rg; Thu, 21 Sep 2017 14:23:42 -0400","from smtp.corp.redhat.com\n\t(int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15])\n\t(using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby mx1.redhat.com (Postfix) with ESMTPS id 2FDEB267D4;\n\tThu, 21 Sep 2017 18:23:40 +0000 (UTC)","from [10.10.124.97] (ovpn-124-97.rdu2.redhat.com [10.10.124.97])\n\tby smtp.corp.redhat.com (Postfix) with ESMTP id 3830E5EDE1;\n\tThu, 21 Sep 2017 18:23:35 +0000 (UTC)"],"DMARC-Filter":"OpenDMARC Filter v1.3.2 mx1.redhat.com 2FDEB267D4","To":"John Snow <jsnow@redhat.com>, Pavel Butsykin <pbutsykin@virtuozzo.com>, \n\tqemu-block@nongnu.org, qemu-devel@nongnu.org","References":"<20170920135833.20472-1-pbutsykin@virtuozzo.com>\n\t<20170920135833.20472-3-pbutsykin@virtuozzo.com>\n\t<ac533c95-a0f5-c6ce-cc6a-7fab63eb0517@redhat.com>\n\t<79eb6628-130e-af9d-cd39-56bf71194ae3@virtuozzo.com>\n\t<ccbd7d7e-512a-76ea-5b03-8853fd88fda2@redhat.com>","From":"Eric Blake <eblake@redhat.com>","Openpgp":"url=http://people.redhat.com/eblake/eblake.gpg","Organization":"Red Hat, Inc.","Message-ID":"<4393a82e-de82-ed8d-15ae-8defe59413ac@redhat.com>","Date":"Thu, 21 Sep 2017 13:23:32 -0500","User-Agent":"Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101\n\tThunderbird/52.3.0","MIME-Version":"1.0","In-Reply-To":"<ccbd7d7e-512a-76ea-5b03-8853fd88fda2@redhat.com>","Content-Type":"multipart/signed; micalg=pgp-sha256;\n\tprotocol=\"application/pgp-signature\";\n\tboundary=\"HMupOISu5dlbE4vKPVNvg4dbMEIfXquAP\"","X-Scanned-By":"MIMEDefang 2.79 on 10.5.11.15","X-Greylist":"Sender IP whitelisted, not delayed by milter-greylist-4.5.16\n\t(mx1.redhat.com [10.5.110.30]);\n\tThu, 21 Sep 2017 18:23:40 +0000 (UTC)","X-detected-operating-system":"by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic]\n\t[fuzzy]","X-Received-From":"209.132.183.28","X-Content-Filtered-By":"Mailman/MimeDel 2.1.21","Subject":"Re: [Qemu-devel] [Qemu-block] [PATCH 2/2] qcow2: truncate the tail\n\tof the image file after shrinking the image","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.21","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<http://lists.nongnu.org/archive/html/qemu-devel/>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Cc":"kwolf@redhat.com, den@openvz.org, mreitz@redhat.com","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"\"Qemu-devel\"\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>"}}]