diff mbox

[2/2] migration/block: use blk_pwrite_zeroes for each zero cluster

Message ID 1491484536-22776-3-git-send-email-lidongchen@tencent.com (mailing list archive)
State New, archived
Headers show

Commit Message

858585 jemmy April 6, 2017, 1:15 p.m. UTC
From: Lidong Chen <lidongchen@tencent.com>

BLOCK_SIZE is (1 << 20), qcow2 cluster size is 65536 by default,
this maybe cause the qcow2 file size is bigger after migration.
This patch check each cluster, use blk_pwrite_zeroes for each
zero cluster.

Signed-off-by: Lidong Chen <lidongchen@tencent.com>
---
 migration/block.c | 34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)

Comments

Fam Zheng April 7, 2017, 7:08 a.m. UTC | #1
On Thu, 04/06 21:15, jemmy858585@gmail.com wrote:
> From: Lidong Chen <lidongchen@tencent.com>
> 
> BLOCK_SIZE is (1 << 20), qcow2 cluster size is 65536 by default,
> this maybe cause the qcow2 file size is bigger after migration.
> This patch check each cluster, use blk_pwrite_zeroes for each
> zero cluster.
> 
> Signed-off-by: Lidong Chen <lidongchen@tencent.com>
> ---
>  migration/block.c | 34 ++++++++++++++++++++++++++++++++--
>  1 file changed, 32 insertions(+), 2 deletions(-)
> 
> diff --git a/migration/block.c b/migration/block.c
> index 7734ff7..1fce9b9 100644
> --- a/migration/block.c
> +++ b/migration/block.c
> @@ -885,6 +885,10 @@ static int block_load(QEMUFile *f, void *opaque, int version_id)
>      int64_t total_sectors = 0;
>      int nr_sectors;
>      int ret;
> +    int i;
> +    int cluster_size;
> +    int64_t addr_offset;
> +    uint8_t *buf_offset;
>  
>      do {
>          addr = qemu_get_be64(f);
> @@ -934,8 +938,34 @@ static int block_load(QEMUFile *f, void *opaque, int version_id)
>              } else {
>                  buf = g_malloc(BLOCK_SIZE);
>                  qemu_get_buffer(f, buf, BLOCK_SIZE);
> -                ret = blk_pwrite(blk, addr * BDRV_SECTOR_SIZE, buf,
> -                                 nr_sectors * BDRV_SECTOR_SIZE, 0);
> +
> +                cluster_size = bdrv_get_cluster_size(blk_bs(blk));
> +
> +                if (cluster_size > 0) {
> +                    for (i = 0; i < BLOCK_SIZE / cluster_size; i++) {

Should we check that cluster_size < BLOCK_SIZE and (BLOCK_SIZE % cluster_size ==
0)?

Fam

> +                        addr_offset = addr * BDRV_SECTOR_SIZE
> +                                        + i * cluster_size;
> +                        buf_offset = buf + i * cluster_size;
> +
> +                        if (buffer_is_zero(buf_offset, cluster_size)) {
> +                            ret = blk_pwrite_zeroes(blk, addr_offset,
> +                                                    cluster_size,
> +                                                    BDRV_REQ_MAY_UNMAP);
> +                        } else {
> +                            ret = blk_pwrite(blk, addr_offset,
> +                                             buf_offset, cluster_size, 0);
> +                        }
> +
> +                        if (ret < 0) {
> +                            g_free(buf);
> +                            return ret;
> +                        }
> +                    }
> +                } else {
> +                    ret = blk_pwrite(blk, addr * BDRV_SECTOR_SIZE, buf,
> +                                     nr_sectors * BDRV_SECTOR_SIZE, 0);
> +                }
> +
>                  g_free(buf);
>              }
>  
> -- 
> 1.8.3.1
>
858585 jemmy April 7, 2017, 7:15 a.m. UTC | #2
On Fri, Apr 7, 2017 at 3:08 PM, Fam Zheng <famz@redhat.com> wrote:
> On Thu, 04/06 21:15, jemmy858585@gmail.com wrote:
>> From: Lidong Chen <lidongchen@tencent.com>
>>
>> BLOCK_SIZE is (1 << 20), qcow2 cluster size is 65536 by default,
>> this maybe cause the qcow2 file size is bigger after migration.
>> This patch check each cluster, use blk_pwrite_zeroes for each
>> zero cluster.
>>
>> Signed-off-by: Lidong Chen <lidongchen@tencent.com>
>> ---
>>  migration/block.c | 34 ++++++++++++++++++++++++++++++++--
>>  1 file changed, 32 insertions(+), 2 deletions(-)
>>
>> diff --git a/migration/block.c b/migration/block.c
>> index 7734ff7..1fce9b9 100644
>> --- a/migration/block.c
>> +++ b/migration/block.c
>> @@ -885,6 +885,10 @@ static int block_load(QEMUFile *f, void *opaque, int version_id)
>>      int64_t total_sectors = 0;
>>      int nr_sectors;
>>      int ret;
>> +    int i;
>> +    int cluster_size;
>> +    int64_t addr_offset;
>> +    uint8_t *buf_offset;
>>
>>      do {
>>          addr = qemu_get_be64(f);
>> @@ -934,8 +938,34 @@ static int block_load(QEMUFile *f, void *opaque, int version_id)
>>              } else {
>>                  buf = g_malloc(BLOCK_SIZE);
>>                  qemu_get_buffer(f, buf, BLOCK_SIZE);
>> -                ret = blk_pwrite(blk, addr * BDRV_SECTOR_SIZE, buf,
>> -                                 nr_sectors * BDRV_SECTOR_SIZE, 0);
>> +
>> +                cluster_size = bdrv_get_cluster_size(blk_bs(blk));
>> +
>> +                if (cluster_size > 0) {
>> +                    for (i = 0; i < BLOCK_SIZE / cluster_size; i++) {
>
> Should we check that cluster_size < BLOCK_SIZE and (BLOCK_SIZE % cluster_size ==
> 0)?
I think this is necessary.
Thanks.

>
> Fam
>
>> +                        addr_offset = addr * BDRV_SECTOR_SIZE
>> +                                        + i * cluster_size;
>> +                        buf_offset = buf + i * cluster_size;
>> +
>> +                        if (buffer_is_zero(buf_offset, cluster_size)) {
>> +                            ret = blk_pwrite_zeroes(blk, addr_offset,
>> +                                                    cluster_size,
>> +                                                    BDRV_REQ_MAY_UNMAP);
>> +                        } else {
>> +                            ret = blk_pwrite(blk, addr_offset,
>> +                                             buf_offset, cluster_size, 0);
>> +                        }
>> +
>> +                        if (ret < 0) {
>> +                            g_free(buf);
>> +                            return ret;
>> +                        }
>> +                    }
>> +                } else {
>> +                    ret = blk_pwrite(blk, addr * BDRV_SECTOR_SIZE, buf,
>> +                                     nr_sectors * BDRV_SECTOR_SIZE, 0);
>> +                }
>> +
>>                  g_free(buf);
>>              }
>>
>> --
>> 1.8.3.1
>>
diff mbox

Patch

diff --git a/migration/block.c b/migration/block.c
index 7734ff7..1fce9b9 100644
--- a/migration/block.c
+++ b/migration/block.c
@@ -885,6 +885,10 @@  static int block_load(QEMUFile *f, void *opaque, int version_id)
     int64_t total_sectors = 0;
     int nr_sectors;
     int ret;
+    int i;
+    int cluster_size;
+    int64_t addr_offset;
+    uint8_t *buf_offset;
 
     do {
         addr = qemu_get_be64(f);
@@ -934,8 +938,34 @@  static int block_load(QEMUFile *f, void *opaque, int version_id)
             } else {
                 buf = g_malloc(BLOCK_SIZE);
                 qemu_get_buffer(f, buf, BLOCK_SIZE);
-                ret = blk_pwrite(blk, addr * BDRV_SECTOR_SIZE, buf,
-                                 nr_sectors * BDRV_SECTOR_SIZE, 0);
+
+                cluster_size = bdrv_get_cluster_size(blk_bs(blk));
+
+                if (cluster_size > 0) {
+                    for (i = 0; i < BLOCK_SIZE / cluster_size; i++) {
+                        addr_offset = addr * BDRV_SECTOR_SIZE
+                                        + i * cluster_size;
+                        buf_offset = buf + i * cluster_size;
+
+                        if (buffer_is_zero(buf_offset, cluster_size)) {
+                            ret = blk_pwrite_zeroes(blk, addr_offset,
+                                                    cluster_size,
+                                                    BDRV_REQ_MAY_UNMAP);
+                        } else {
+                            ret = blk_pwrite(blk, addr_offset,
+                                             buf_offset, cluster_size, 0);
+                        }
+
+                        if (ret < 0) {
+                            g_free(buf);
+                            return ret;
+                        }
+                    }
+                } else {
+                    ret = blk_pwrite(blk, addr * BDRV_SECTOR_SIZE, buf,
+                                     nr_sectors * BDRV_SECTOR_SIZE, 0);
+                }
+
                 g_free(buf);
             }