[PATCH 7/9] parallels: Rework truncation logic

Eric Blake posted 9 patches 5 years, 9 months ago
Maintainers: Peter Lieven <pl@kamp.de>, "Denis V. Lunev" <den@openvz.org>, Stefan Weil <sw@weilnetz.de>, Jason Dillaman <dillaman@redhat.com>, Max Reitz <mreitz@redhat.com>, Jeff Cody <codyprime@gmail.com>, Kevin Wolf <kwolf@redhat.com>, Liu Yuan <namei.unix@gmail.com>, "Richard W.M. Jones" <rjones@redhat.com>, Stefan Hajnoczi <stefanha@redhat.com>
[PATCH 7/9] parallels: Rework truncation logic
Posted by Eric Blake 5 years, 9 months ago
The parallels driver tries to use truncation for image growth, but can
only do so when reads are guaranteed as zero.  Now that we have a way
to request zero contents from truncation, we can defer the decision to
actual allocation attempts rather than up front, reducing the number
of places that still use bdrv_has_zero_init_truncate.

Signed-off-by: Eric Blake <eblake@redhat.com>
---
 block/parallels.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/block/parallels.c b/block/parallels.c
index 2be92cf41708..9dadaa3217b9 100644
--- a/block/parallels.c
+++ b/block/parallels.c
@@ -196,14 +196,24 @@ static int64_t allocate_clusters(BlockDriverState *bs, int64_t sector_num,
     }
     if (s->data_end + space > (len >> BDRV_SECTOR_BITS)) {
         space += s->prealloc_size;
+        /*
+         * We require the expanded size to read back as zero. If the
+         * user permitted truncation, we try that; but if it fails, we
+         * force the safer-but-slower fallocate.
+         */
+        if (s->prealloc_mode == PRL_PREALLOC_MODE_TRUNCATE) {
+            ret = bdrv_truncate(bs->file,
+                                (s->data_end + space) << BDRV_SECTOR_BITS,
+                                false, PREALLOC_MODE_OFF, BDRV_REQ_ZERO_WRITE,
+                                NULL);
+            if (ret == -ENOTSUP) {
+                s->prealloc_mode = PRL_PREALLOC_MODE_FALLOCATE;
+            }
+        }
         if (s->prealloc_mode == PRL_PREALLOC_MODE_FALLOCATE) {
             ret = bdrv_pwrite_zeroes(bs->file,
                                      s->data_end << BDRV_SECTOR_BITS,
                                      space << BDRV_SECTOR_BITS, 0);
-        } else {
-            ret = bdrv_truncate(bs->file,
-                                (s->data_end + space) << BDRV_SECTOR_BITS,
-                                false, PREALLOC_MODE_OFF, 0, NULL);
         }
         if (ret < 0) {
             return ret;
@@ -828,6 +838,7 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
         qemu_opt_get_size_del(opts, PARALLELS_OPT_PREALLOC_SIZE, 0);
     s->prealloc_size = MAX(s->tracks, s->prealloc_size >> BDRV_SECTOR_BITS);
     buf = qemu_opt_get_del(opts, PARALLELS_OPT_PREALLOC_MODE);
+    /* prealloc_mode can be downgraded later during allocate_clusters */
     s->prealloc_mode = qapi_enum_parse(&prealloc_mode_lookup, buf,
                                        PRL_PREALLOC_MODE_FALLOCATE,
                                        &local_err);
@@ -836,10 +847,6 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
         goto fail_options;
     }

-    if (!bdrv_has_zero_init_truncate(bs->file->bs)) {
-        s->prealloc_mode = PRL_PREALLOC_MODE_FALLOCATE;
-    }
-
     if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_INACTIVE)) {
         s->header->inuse = cpu_to_le32(HEADER_INUSE_MAGIC);
         ret = parallels_update_header(bs);
-- 
2.26.2


Re: [PATCH 7/9] parallels: Rework truncation logic
Posted by Denis V. Lunev 5 years, 9 months ago
On 4/28/20 11:29 PM, Eric Blake wrote:
> The parallels driver tries to use truncation for image growth, but can
> only do so when reads are guaranteed as zero.  Now that we have a way
> to request zero contents from truncation, we can defer the decision to
> actual allocation attempts rather than up front, reducing the number
> of places that still use bdrv_has_zero_init_truncate.
>
> Signed-off-by: Eric Blake <eblake@redhat.com>
> ---
>  block/parallels.c | 23 +++++++++++++++--------
>  1 file changed, 15 insertions(+), 8 deletions(-)
>
> diff --git a/block/parallels.c b/block/parallels.c
> index 2be92cf41708..9dadaa3217b9 100644
> --- a/block/parallels.c
> +++ b/block/parallels.c
> @@ -196,14 +196,24 @@ static int64_t allocate_clusters(BlockDriverState *bs, int64_t sector_num,
>      }
>      if (s->data_end + space > (len >> BDRV_SECTOR_BITS)) {
>          space += s->prealloc_size;
> +        /*
> +         * We require the expanded size to read back as zero. If the
> +         * user permitted truncation, we try that; but if it fails, we
> +         * force the safer-but-slower fallocate.
> +         */
> +        if (s->prealloc_mode == PRL_PREALLOC_MODE_TRUNCATE) {
> +            ret = bdrv_truncate(bs->file,
> +                                (s->data_end + space) << BDRV_SECTOR_BITS,
> +                                false, PREALLOC_MODE_OFF, BDRV_REQ_ZERO_WRITE,
> +                                NULL);
> +            if (ret == -ENOTSUP) {
> +                s->prealloc_mode = PRL_PREALLOC_MODE_FALLOCATE;
> +            }
> +        }
>          if (s->prealloc_mode == PRL_PREALLOC_MODE_FALLOCATE) {
>              ret = bdrv_pwrite_zeroes(bs->file,
>                                       s->data_end << BDRV_SECTOR_BITS,
>                                       space << BDRV_SECTOR_BITS, 0);
> -        } else {
> -            ret = bdrv_truncate(bs->file,
> -                                (s->data_end + space) << BDRV_SECTOR_BITS,
> -                                false, PREALLOC_MODE_OFF, 0, NULL);
>          }
>          if (ret < 0) {
>              return ret;
> @@ -828,6 +838,7 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
>          qemu_opt_get_size_del(opts, PARALLELS_OPT_PREALLOC_SIZE, 0);
>      s->prealloc_size = MAX(s->tracks, s->prealloc_size >> BDRV_SECTOR_BITS);
>      buf = qemu_opt_get_del(opts, PARALLELS_OPT_PREALLOC_MODE);
> +    /* prealloc_mode can be downgraded later during allocate_clusters */
>      s->prealloc_mode = qapi_enum_parse(&prealloc_mode_lookup, buf,
>                                         PRL_PREALLOC_MODE_FALLOCATE,
>                                         &local_err);
> @@ -836,10 +847,6 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
>          goto fail_options;
>      }
>
> -    if (!bdrv_has_zero_init_truncate(bs->file->bs)) {
> -        s->prealloc_mode = PRL_PREALLOC_MODE_FALLOCATE;
> -    }
> -
>      if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_INACTIVE)) {
>          s->header->inuse = cpu_to_le32(HEADER_INUSE_MAGIC);
>          ret = parallels_update_header(bs);
Reviewed-by: Denis V. Lunev <den@openvz.org>