Blame SOURCES/kvm-file-posix-Mitigate-file-fragmentation-with-extent-s.patch

f7c9e9
From 7ee01b5ccb7fc660dafaf3fdb1578649d17fbddf Mon Sep 17 00:00:00 2001
f7c9e9
From: Kevin Wolf <kwolf@redhat.com>
f7c9e9
Date: Wed, 26 May 2021 09:05:52 -0400
f7c9e9
Subject: [PATCH 1/4] file-posix: Mitigate file fragmentation with extent size
f7c9e9
 hints
f7c9e9
f7c9e9
RH-Author: Kevin Wolf <kwolf@redhat.com>
f7c9e9
Message-id: <20210526090552.155820-2-kwolf@redhat.com>
f7c9e9
Patchwork-id: 101638
f7c9e9
O-Subject: [RHEL-8.5.0 qemu-kvm PATCH 1/1] file-posix: Mitigate file fragmentation with extent size hints
f7c9e9
Bugzilla: 1877163
f7c9e9
RH-Acked-by: Sergio Lopez Pascual <slp@redhat.com>
f7c9e9
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
f7c9e9
RH-Acked-by: Max Reitz <mreitz@redhat.com>
f7c9e9
f7c9e9
Especially when O_DIRECT is used with image files so that the page cache
f7c9e9
indirection can't cause a merge of allocating requests, the file will
f7c9e9
fragment on the file system layer, with a potentially very small
f7c9e9
fragment size (this depends on the requests the guest sent).
f7c9e9
f7c9e9
On Linux, fragmentation can be reduced by setting an extent size hint
f7c9e9
when creating the file (at least on XFS, it can't be set any more after
f7c9e9
the first extent has been allocated), basically giving raw files a
f7c9e9
"cluster size" for allocation.
f7c9e9
f7c9e9
This adds a create option to set the extent size hint, and changes the
f7c9e9
default from not setting a hint to setting it to 1 MB. The main reason
f7c9e9
why qcow2 defaults to smaller cluster sizes is that COW becomes more
f7c9e9
expensive, which is not an issue with raw files, so we can choose a
f7c9e9
larger size. The tradeoff here is only potentially wasted disk space.
f7c9e9
f7c9e9
For qcow2 (or other image formats) over file-posix, the advantage should
f7c9e9
even be greater because they grow sequentially without leaving holes, so
f7c9e9
there won't be wasted space. Setting even larger extent size hints for
f7c9e9
such images may make sense. This can be done with the new option, but
f7c9e9
let's keep the default conservative for now.
f7c9e9
f7c9e9
The effect is very visible with a test that intentionally creates a
f7c9e9
badly fragmented file with qemu-img bench (the time difference while
f7c9e9
creating the file is already remarkable) and then looks at the number of
f7c9e9
extents and the time a simple "qemu-img map" takes.
f7c9e9
f7c9e9
Without an extent size hint:
f7c9e9
f7c9e9
    $ ./qemu-img create -f raw -o extent_size_hint=0 ~/tmp/test.raw 10G
f7c9e9
    Formatting '/home/kwolf/tmp/test.raw', fmt=raw size=10737418240 extent_size_hint=0
f7c9e9
    $ ./qemu-img bench -f raw -t none -n -w ~/tmp/test.raw -c 1000000 -S 8192 -o 0
f7c9e9
    Sending 1000000 write requests, 4096 bytes each, 64 in parallel (starting at offset 0, step size 8192)
f7c9e9
    Run completed in 25.848 seconds.
f7c9e9
    $ ./qemu-img bench -f raw -t none -n -w ~/tmp/test.raw -c 1000000 -S 8192 -o 4096
f7c9e9
    Sending 1000000 write requests, 4096 bytes each, 64 in parallel (starting at offset 4096, step size 8192)
f7c9e9
    Run completed in 19.616 seconds.
f7c9e9
    $ filefrag ~/tmp/test.raw
f7c9e9
    /home/kwolf/tmp/test.raw: 2000000 extents found
f7c9e9
    $ time ./qemu-img map ~/tmp/test.raw
f7c9e9
    Offset          Length          Mapped to       File
f7c9e9
    0               0x1e8480000     0               /home/kwolf/tmp/test.raw
f7c9e9
f7c9e9
    real    0m1,279s
f7c9e9
    user    0m0,043s
f7c9e9
    sys     0m1,226s
f7c9e9
f7c9e9
With the new default extent size hint of 1 MB:
f7c9e9
f7c9e9
    $ ./qemu-img create -f raw -o extent_size_hint=1M ~/tmp/test.raw 10G
f7c9e9
    Formatting '/home/kwolf/tmp/test.raw', fmt=raw size=10737418240 extent_size_hint=1048576
f7c9e9
    $ ./qemu-img bench -f raw -t none -n -w ~/tmp/test.raw -c 1000000 -S 8192 -o 0
f7c9e9
    Sending 1000000 write requests, 4096 bytes each, 64 in parallel (starting at offset 0, step size 8192)
f7c9e9
    Run completed in 11.833 seconds.
f7c9e9
    $ ./qemu-img bench -f raw -t none -n -w ~/tmp/test.raw -c 1000000 -S 8192 -o 4096
f7c9e9
    Sending 1000000 write requests, 4096 bytes each, 64 in parallel (starting at offset 4096, step size 8192)
f7c9e9
    Run completed in 10.155 seconds.
f7c9e9
    $ filefrag ~/tmp/test.raw
f7c9e9
    /home/kwolf/tmp/test.raw: 178 extents found
f7c9e9
    $ time ./qemu-img map ~/tmp/test.raw
f7c9e9
    Offset          Length          Mapped to       File
f7c9e9
    0               0x1e8480000     0               /home/kwolf/tmp/test.raw
f7c9e9
f7c9e9
    real    0m0,061s
f7c9e9
    user    0m0,040s
f7c9e9
    sys     0m0,014s
f7c9e9
f7c9e9
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
f7c9e9
Message-Id: <20200707142329.48303-1-kwolf@redhat.com>
f7c9e9
Reviewed-by: Eric Blake <eblake@redhat.com>
f7c9e9
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
f7c9e9
(cherry picked from commit ffa244c84a1a30dff69ecc80b0137a2b6d428ecb)
f7c9e9
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
f7c9e9
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
f7c9e9
---
f7c9e9
 block/file-posix.c               | 44 ++++++++++++++++++++++++++++++++
f7c9e9
 include/block/block_int.h        |  1 +
f7c9e9
 qapi/block-core.json             | 11 +++++---
f7c9e9
 tests/qemu-iotests/082.out       | 16 ++++++++++++
f7c9e9
 tests/qemu-iotests/106           |  7 +++--
f7c9e9
 tests/qemu-iotests/175           |  6 ++---
f7c9e9
 tests/qemu-iotests/243           |  6 ++---
f7c9e9
 tests/qemu-iotests/common.filter |  1 +
f7c9e9
 8 files changed, 80 insertions(+), 12 deletions(-)
f7c9e9
f7c9e9
diff --git a/block/file-posix.c b/block/file-posix.c
f7c9e9
index 2d834fbdf6..62a463229f 100644
f7c9e9
--- a/block/file-posix.c
f7c9e9
+++ b/block/file-posix.c
f7c9e9
@@ -30,6 +30,7 @@
f7c9e9
 #include "block/block_int.h"
f7c9e9
 #include "qemu/module.h"
f7c9e9
 #include "qemu/option.h"
f7c9e9
+#include "qemu/units.h"
f7c9e9
 #include "trace.h"
f7c9e9
 #include "block/thread-pool.h"
f7c9e9
 #include "qemu/iov.h"
f7c9e9
@@ -2289,6 +2290,14 @@ raw_co_create(BlockdevCreateOptions *options, Error **errp)
f7c9e9
     if (!file_opts->has_preallocation) {
f7c9e9
         file_opts->preallocation = PREALLOC_MODE_OFF;
f7c9e9
     }
f7c9e9
+    if (!file_opts->has_extent_size_hint) {
f7c9e9
+        file_opts->extent_size_hint = 1 * MiB;
f7c9e9
+    }
f7c9e9
+    if (file_opts->extent_size_hint > UINT32_MAX) {
f7c9e9
+        result = -EINVAL;
f7c9e9
+        error_setg(errp, "Extent size hint is too large");
f7c9e9
+        goto out;
f7c9e9
+    }
f7c9e9
 
f7c9e9
     /* Create file */
f7c9e9
     fd = qemu_open(file_opts->filename, O_RDWR | O_CREAT | O_BINARY, 0644);
f7c9e9
@@ -2346,6 +2355,27 @@ raw_co_create(BlockdevCreateOptions *options, Error **errp)
f7c9e9
         }
f7c9e9
 #endif
f7c9e9
     }
f7c9e9
+#ifdef FS_IOC_FSSETXATTR
f7c9e9
+    /*
f7c9e9
+     * Try to set the extent size hint. Failure is not fatal, and a warning is
f7c9e9
+     * only printed if the option was explicitly specified.
f7c9e9
+     */
f7c9e9
+    {
f7c9e9
+        struct fsxattr attr;
f7c9e9
+        result = ioctl(fd, FS_IOC_FSGETXATTR, &attr);
f7c9e9
+        if (result == 0) {
f7c9e9
+            attr.fsx_xflags |= FS_XFLAG_EXTSIZE;
f7c9e9
+            attr.fsx_extsize = file_opts->extent_size_hint;
f7c9e9
+            result = ioctl(fd, FS_IOC_FSSETXATTR, &attr);
f7c9e9
+        }
f7c9e9
+        if (result < 0 && file_opts->has_extent_size_hint &&
f7c9e9
+            file_opts->extent_size_hint)
f7c9e9
+        {
f7c9e9
+            warn_report("Failed to set extent size hint: %s",
f7c9e9
+                        strerror(errno));
f7c9e9
+        }
f7c9e9
+    }
f7c9e9
+#endif
f7c9e9
 
f7c9e9
     /* Resize and potentially preallocate the file to the desired
f7c9e9
      * final size */
f7c9e9
@@ -2381,6 +2411,8 @@ static int coroutine_fn raw_co_create_opts(BlockDriver *drv,
f7c9e9
 {
f7c9e9
     BlockdevCreateOptions options;
f7c9e9
     int64_t total_size = 0;
f7c9e9
+    int64_t extent_size_hint = 0;
f7c9e9
+    bool has_extent_size_hint = false;
f7c9e9
     bool nocow = false;
f7c9e9
     PreallocMode prealloc;
f7c9e9
     char *buf = NULL;
f7c9e9
@@ -2392,6 +2424,11 @@ static int coroutine_fn raw_co_create_opts(BlockDriver *drv,
f7c9e9
     /* Read out options */
f7c9e9
     total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
f7c9e9
                           BDRV_SECTOR_SIZE);
f7c9e9
+    if (qemu_opt_get(opts, BLOCK_OPT_EXTENT_SIZE_HINT)) {
f7c9e9
+        has_extent_size_hint = true;
f7c9e9
+        extent_size_hint =
f7c9e9
+            qemu_opt_get_size_del(opts, BLOCK_OPT_EXTENT_SIZE_HINT, -1);
f7c9e9
+    }
f7c9e9
     nocow = qemu_opt_get_bool(opts, BLOCK_OPT_NOCOW, false);
f7c9e9
     buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
f7c9e9
     prealloc = qapi_enum_parse(&PreallocMode_lookup, buf,
f7c9e9
@@ -2411,6 +2448,8 @@ static int coroutine_fn raw_co_create_opts(BlockDriver *drv,
f7c9e9
             .preallocation      = prealloc,
f7c9e9
             .has_nocow          = true,
f7c9e9
             .nocow              = nocow,
f7c9e9
+            .has_extent_size_hint = has_extent_size_hint,
f7c9e9
+            .extent_size_hint   = extent_size_hint,
f7c9e9
         },
f7c9e9
     };
f7c9e9
     return raw_co_create(&options, errp);
f7c9e9
@@ -2902,6 +2941,11 @@ static QemuOptsList raw_create_opts = {
f7c9e9
 #endif
f7c9e9
                     ", full)"
f7c9e9
         },
f7c9e9
+        {
f7c9e9
+            .name = BLOCK_OPT_EXTENT_SIZE_HINT,
f7c9e9
+            .type = QEMU_OPT_SIZE,
f7c9e9
+            .help = "Extent size hint for the image file, 0 to disable"
f7c9e9
+        },
f7c9e9
         { /* end of list */ }
f7c9e9
     }
f7c9e9
 };
f7c9e9
diff --git a/include/block/block_int.h b/include/block/block_int.h
f7c9e9
index 41f13ecbed..4b23da2eb0 100644
f7c9e9
--- a/include/block/block_int.h
f7c9e9
+++ b/include/block/block_int.h
f7c9e9
@@ -53,6 +53,7 @@
f7c9e9
 #define BLOCK_OPT_ADAPTER_TYPE      "adapter_type"
f7c9e9
 #define BLOCK_OPT_REDUNDANCY        "redundancy"
f7c9e9
 #define BLOCK_OPT_NOCOW             "nocow"
f7c9e9
+#define BLOCK_OPT_EXTENT_SIZE_HINT  "extent_size_hint"
f7c9e9
 #define BLOCK_OPT_OBJECT_SIZE       "object_size"
f7c9e9
 #define BLOCK_OPT_REFCOUNT_BITS     "refcount_bits"
f7c9e9
 #define BLOCK_OPT_DATA_FILE         "data_file"
f7c9e9
diff --git a/qapi/block-core.json b/qapi/block-core.json
f7c9e9
index 289320902d..c7aa919fa3 100644
f7c9e9
--- a/qapi/block-core.json
f7c9e9
+++ b/qapi/block-core.json
f7c9e9
@@ -4272,14 +4272,17 @@
f7c9e9
 #                   falloc (if defined CONFIG_POSIX_FALLOCATE),
f7c9e9
 #                   full (if defined CONFIG_POSIX))
f7c9e9
 # @nocow            Turn off copy-on-write (valid only on btrfs; default: off)
f7c9e9
+# @extent-size-hint: Extent size hint to add to the image file; 0 for not
f7c9e9
+#                    adding an extent size hint (default: 1 MB, since 5.1)
f7c9e9
 #
f7c9e9
 # Since: 2.12
f7c9e9
 ##
f7c9e9
 { 'struct': 'BlockdevCreateOptionsFile',
f7c9e9
-  'data': { 'filename':         'str',
f7c9e9
-            'size':             'size',
f7c9e9
-            '*preallocation':   'PreallocMode',
f7c9e9
-            '*nocow':           'bool' } }
f7c9e9
+  'data': { 'filename':             'str',
f7c9e9
+            'size':                 'size',
f7c9e9
+            '*preallocation':       'PreallocMode',
f7c9e9
+            '*nocow':               'bool',
f7c9e9
+            '*extent-size-hint':    'size'} }
f7c9e9
 
f7c9e9
 ##
f7c9e9
 # @BlockdevCreateOptionsGluster:
f7c9e9
diff --git a/tests/qemu-iotests/082.out b/tests/qemu-iotests/082.out
f7c9e9
index 9d4ed4dc9d..7a87946fa2 100644
f7c9e9
--- a/tests/qemu-iotests/082.out
f7c9e9
+++ b/tests/qemu-iotests/082.out
f7c9e9
@@ -59,6 +59,7 @@ Supported options:
f7c9e9
   encrypt.ivgen-hash-alg=<str> - Name of IV generator hash algorithm
f7c9e9
   encrypt.key-secret=<str> - ID of secret providing qcow AES key or LUKS passphrase
f7c9e9
   encryption=<bool (on/off)> - Encrypt the image with format 'aes'. (Deprecated in favor of encrypt.format=aes)
f7c9e9
+  extent_size_hint=<size> - Extent size hint for the image file, 0 to disable
f7c9e9
   lazy_refcounts=<bool (on/off)> - Postpone refcount updates
f7c9e9
   nocow=<bool (on/off)>  - Turn off copy-on-write (valid only on btrfs)
f7c9e9
   preallocation=<str>    - Preallocation mode (allowed values: off, metadata, falloc, full)
f7c9e9
@@ -82,6 +83,7 @@ Supported options:
f7c9e9
   encrypt.ivgen-hash-alg=<str> - Name of IV generator hash algorithm
f7c9e9
   encrypt.key-secret=<str> - ID of secret providing qcow AES key or LUKS passphrase
f7c9e9
   encryption=<bool (on/off)> - Encrypt the image with format 'aes'. (Deprecated in favor of encrypt.format=aes)
f7c9e9
+  extent_size_hint=<size> - Extent size hint for the image file, 0 to disable
f7c9e9
   lazy_refcounts=<bool (on/off)> - Postpone refcount updates
f7c9e9
   nocow=<bool (on/off)>  - Turn off copy-on-write (valid only on btrfs)
f7c9e9
   preallocation=<str>    - Preallocation mode (allowed values: off, metadata, falloc, full)
f7c9e9
@@ -105,6 +107,7 @@ Supported options:
f7c9e9
   encrypt.ivgen-hash-alg=<str> - Name of IV generator hash algorithm
f7c9e9
   encrypt.key-secret=<str> - ID of secret providing qcow AES key or LUKS passphrase
f7c9e9
   encryption=<bool (on/off)> - Encrypt the image with format 'aes'. (Deprecated in favor of encrypt.format=aes)
f7c9e9
+  extent_size_hint=<size> - Extent size hint for the image file, 0 to disable
f7c9e9
   lazy_refcounts=<bool (on/off)> - Postpone refcount updates
f7c9e9
   nocow=<bool (on/off)>  - Turn off copy-on-write (valid only on btrfs)
f7c9e9
   preallocation=<str>    - Preallocation mode (allowed values: off, metadata, falloc, full)
f7c9e9
@@ -128,6 +131,7 @@ Supported options:
f7c9e9
   encrypt.ivgen-hash-alg=<str> - Name of IV generator hash algorithm
f7c9e9
   encrypt.key-secret=<str> - ID of secret providing qcow AES key or LUKS passphrase
f7c9e9
   encryption=<bool (on/off)> - Encrypt the image with format 'aes'. (Deprecated in favor of encrypt.format=aes)
f7c9e9
+  extent_size_hint=<size> - Extent size hint for the image file, 0 to disable
f7c9e9
   lazy_refcounts=<bool (on/off)> - Postpone refcount updates
f7c9e9
   nocow=<bool (on/off)>  - Turn off copy-on-write (valid only on btrfs)
f7c9e9
   preallocation=<str>    - Preallocation mode (allowed values: off, metadata, falloc, full)
f7c9e9
@@ -151,6 +155,7 @@ Supported options:
f7c9e9
   encrypt.ivgen-hash-alg=<str> - Name of IV generator hash algorithm
f7c9e9
   encrypt.key-secret=<str> - ID of secret providing qcow AES key or LUKS passphrase
f7c9e9
   encryption=<bool (on/off)> - Encrypt the image with format 'aes'. (Deprecated in favor of encrypt.format=aes)
f7c9e9
+  extent_size_hint=<size> - Extent size hint for the image file, 0 to disable
f7c9e9
   lazy_refcounts=<bool (on/off)> - Postpone refcount updates
f7c9e9
   nocow=<bool (on/off)>  - Turn off copy-on-write (valid only on btrfs)
f7c9e9
   preallocation=<str>    - Preallocation mode (allowed values: off, metadata, falloc, full)
f7c9e9
@@ -174,6 +179,7 @@ Supported options:
f7c9e9
   encrypt.ivgen-hash-alg=<str> - Name of IV generator hash algorithm
f7c9e9
   encrypt.key-secret=<str> - ID of secret providing qcow AES key or LUKS passphrase
f7c9e9
   encryption=<bool (on/off)> - Encrypt the image with format 'aes'. (Deprecated in favor of encrypt.format=aes)
f7c9e9
+  extent_size_hint=<size> - Extent size hint for the image file, 0 to disable
f7c9e9
   lazy_refcounts=<bool (on/off)> - Postpone refcount updates
f7c9e9
   nocow=<bool (on/off)>  - Turn off copy-on-write (valid only on btrfs)
f7c9e9
   preallocation=<str>    - Preallocation mode (allowed values: off, metadata, falloc, full)
f7c9e9
@@ -197,6 +203,7 @@ Supported options:
f7c9e9
   encrypt.ivgen-hash-alg=<str> - Name of IV generator hash algorithm
f7c9e9
   encrypt.key-secret=<str> - ID of secret providing qcow AES key or LUKS passphrase
f7c9e9
   encryption=<bool (on/off)> - Encrypt the image with format 'aes'. (Deprecated in favor of encrypt.format=aes)
f7c9e9
+  extent_size_hint=<size> - Extent size hint for the image file, 0 to disable
f7c9e9
   lazy_refcounts=<bool (on/off)> - Postpone refcount updates
f7c9e9
   nocow=<bool (on/off)>  - Turn off copy-on-write (valid only on btrfs)
f7c9e9
   preallocation=<str>    - Preallocation mode (allowed values: off, metadata, falloc, full)
f7c9e9
@@ -220,6 +227,7 @@ Supported options:
f7c9e9
   encrypt.ivgen-hash-alg=<str> - Name of IV generator hash algorithm
f7c9e9
   encrypt.key-secret=<str> - ID of secret providing qcow AES key or LUKS passphrase
f7c9e9
   encryption=<bool (on/off)> - Encrypt the image with format 'aes'. (Deprecated in favor of encrypt.format=aes)
f7c9e9
+  extent_size_hint=<size> - Extent size hint for the image file, 0 to disable
f7c9e9
   lazy_refcounts=<bool (on/off)> - Postpone refcount updates
f7c9e9
   nocow=<bool (on/off)>  - Turn off copy-on-write (valid only on btrfs)
f7c9e9
   preallocation=<str>    - Preallocation mode (allowed values: off, metadata, falloc, full)
f7c9e9
@@ -339,6 +347,7 @@ Supported options:
f7c9e9
   encrypt.ivgen-hash-alg=<str> - Name of IV generator hash algorithm
f7c9e9
   encrypt.key-secret=<str> - ID of secret providing qcow AES key or LUKS passphrase
f7c9e9
   encryption=<bool (on/off)> - Encrypt the image with format 'aes'. (Deprecated in favor of encrypt.format=aes)
f7c9e9
+  extent_size_hint=<size> - Extent size hint for the image file, 0 to disable
f7c9e9
   lazy_refcounts=<bool (on/off)> - Postpone refcount updates
f7c9e9
   nocow=<bool (on/off)>  - Turn off copy-on-write (valid only on btrfs)
f7c9e9
   preallocation=<str>    - Preallocation mode (allowed values: off, metadata, falloc, full)
f7c9e9
@@ -362,6 +371,7 @@ Supported options:
f7c9e9
   encrypt.ivgen-hash-alg=<str> - Name of IV generator hash algorithm
f7c9e9
   encrypt.key-secret=<str> - ID of secret providing qcow AES key or LUKS passphrase
f7c9e9
   encryption=<bool (on/off)> - Encrypt the image with format 'aes'. (Deprecated in favor of encrypt.format=aes)
f7c9e9
+  extent_size_hint=<size> - Extent size hint for the image file, 0 to disable
f7c9e9
   lazy_refcounts=<bool (on/off)> - Postpone refcount updates
f7c9e9
   nocow=<bool (on/off)>  - Turn off copy-on-write (valid only on btrfs)
f7c9e9
   preallocation=<str>    - Preallocation mode (allowed values: off, metadata, falloc, full)
f7c9e9
@@ -385,6 +395,7 @@ Supported options:
f7c9e9
   encrypt.ivgen-hash-alg=<str> - Name of IV generator hash algorithm
f7c9e9
   encrypt.key-secret=<str> - ID of secret providing qcow AES key or LUKS passphrase
f7c9e9
   encryption=<bool (on/off)> - Encrypt the image with format 'aes'. (Deprecated in favor of encrypt.format=aes)
f7c9e9
+  extent_size_hint=<size> - Extent size hint for the image file, 0 to disable
f7c9e9
   lazy_refcounts=<bool (on/off)> - Postpone refcount updates
f7c9e9
   nocow=<bool (on/off)>  - Turn off copy-on-write (valid only on btrfs)
f7c9e9
   preallocation=<str>    - Preallocation mode (allowed values: off, metadata, falloc, full)
f7c9e9
@@ -408,6 +419,7 @@ Supported options:
f7c9e9
   encrypt.ivgen-hash-alg=<str> - Name of IV generator hash algorithm
f7c9e9
   encrypt.key-secret=<str> - ID of secret providing qcow AES key or LUKS passphrase
f7c9e9
   encryption=<bool (on/off)> - Encrypt the image with format 'aes'. (Deprecated in favor of encrypt.format=aes)
f7c9e9
+  extent_size_hint=<size> - Extent size hint for the image file, 0 to disable
f7c9e9
   lazy_refcounts=<bool (on/off)> - Postpone refcount updates
f7c9e9
   nocow=<bool (on/off)>  - Turn off copy-on-write (valid only on btrfs)
f7c9e9
   preallocation=<str>    - Preallocation mode (allowed values: off, metadata, falloc, full)
f7c9e9
@@ -431,6 +443,7 @@ Supported options:
f7c9e9
   encrypt.ivgen-hash-alg=<str> - Name of IV generator hash algorithm
f7c9e9
   encrypt.key-secret=<str> - ID of secret providing qcow AES key or LUKS passphrase
f7c9e9
   encryption=<bool (on/off)> - Encrypt the image with format 'aes'. (Deprecated in favor of encrypt.format=aes)
f7c9e9
+  extent_size_hint=<size> - Extent size hint for the image file, 0 to disable
f7c9e9
   lazy_refcounts=<bool (on/off)> - Postpone refcount updates
f7c9e9
   nocow=<bool (on/off)>  - Turn off copy-on-write (valid only on btrfs)
f7c9e9
   preallocation=<str>    - Preallocation mode (allowed values: off, metadata, falloc, full)
f7c9e9
@@ -454,6 +467,7 @@ Supported options:
f7c9e9
   encrypt.ivgen-hash-alg=<str> - Name of IV generator hash algorithm
f7c9e9
   encrypt.key-secret=<str> - ID of secret providing qcow AES key or LUKS passphrase
f7c9e9
   encryption=<bool (on/off)> - Encrypt the image with format 'aes'. (Deprecated in favor of encrypt.format=aes)
f7c9e9
+  extent_size_hint=<size> - Extent size hint for the image file, 0 to disable
f7c9e9
   lazy_refcounts=<bool (on/off)> - Postpone refcount updates
f7c9e9
   nocow=<bool (on/off)>  - Turn off copy-on-write (valid only on btrfs)
f7c9e9
   preallocation=<str>    - Preallocation mode (allowed values: off, metadata, falloc, full)
f7c9e9
@@ -477,6 +491,7 @@ Supported options:
f7c9e9
   encrypt.ivgen-hash-alg=<str> - Name of IV generator hash algorithm
f7c9e9
   encrypt.key-secret=<str> - ID of secret providing qcow AES key or LUKS passphrase
f7c9e9
   encryption=<bool (on/off)> - Encrypt the image with format 'aes'. (Deprecated in favor of encrypt.format=aes)
f7c9e9
+  extent_size_hint=<size> - Extent size hint for the image file, 0 to disable
f7c9e9
   lazy_refcounts=<bool (on/off)> - Postpone refcount updates
f7c9e9
   nocow=<bool (on/off)>  - Turn off copy-on-write (valid only on btrfs)
f7c9e9
   preallocation=<str>    - Preallocation mode (allowed values: off, metadata, falloc, full)
f7c9e9
@@ -500,6 +515,7 @@ Supported options:
f7c9e9
   encrypt.ivgen-hash-alg=<str> - Name of IV generator hash algorithm
f7c9e9
   encrypt.key-secret=<str> - ID of secret providing qcow AES key or LUKS passphrase
f7c9e9
   encryption=<bool (on/off)> - Encrypt the image with format 'aes'. (Deprecated in favor of encrypt.format=aes)
f7c9e9
+  extent_size_hint=<size> - Extent size hint for the image file, 0 to disable
f7c9e9
   lazy_refcounts=<bool (on/off)> - Postpone refcount updates
f7c9e9
   nocow=<bool (on/off)>  - Turn off copy-on-write (valid only on btrfs)
f7c9e9
   preallocation=<str>    - Preallocation mode (allowed values: off, metadata, falloc, full)
f7c9e9
diff --git a/tests/qemu-iotests/106 b/tests/qemu-iotests/106
f7c9e9
index ac47eaa0f5..ee6f51d08b 100755
f7c9e9
--- a/tests/qemu-iotests/106
f7c9e9
+++ b/tests/qemu-iotests/106
f7c9e9
@@ -51,7 +51,10 @@ for create_mode in off falloc full; do
f7c9e9
         echo
f7c9e9
         echo "--- create_mode=$create_mode growth_mode=$growth_mode ---"
f7c9e9
 
f7c9e9
-        IMGOPTS="preallocation=$create_mode" _make_test_img ${CREATION_SIZE}K
f7c9e9
+        # Our calculation below assumes kilobytes as unit for the actual size.
f7c9e9
+        # Disable the extent size hint because it would give us a result in
f7c9e9
+        # megabytes.
f7c9e9
+        IMGOPTS="preallocation=$create_mode,extent_size_hint=0" _make_test_img ${CREATION_SIZE}K
f7c9e9
         $QEMU_IMG resize -f "$IMGFMT" --preallocation=$growth_mode "$TEST_IMG" +${GROWTH_SIZE}K
f7c9e9
 
f7c9e9
         expected_size=0
f7c9e9
@@ -98,7 +101,7 @@ for growth_mode in falloc full; do
f7c9e9
     # plain int.  We should use the correct type for the result, and
f7c9e9
     # this tests we do.
f7c9e9
 
f7c9e9
-    _make_test_img 2G
f7c9e9
+    _make_test_img -o "extent_size_hint=0" 2G
f7c9e9
     $QEMU_IMG resize -f "$IMGFMT" --preallocation=$growth_mode "$TEST_IMG" +${GROWTH_SIZE}K
f7c9e9
 
f7c9e9
     actual_size=$($QEMU_IMG info -f "$IMGFMT" "$TEST_IMG" | grep 'disk size')
f7c9e9
diff --git a/tests/qemu-iotests/175 b/tests/qemu-iotests/175
f7c9e9
index 55db2803ed..8a8494aeb6 100755
f7c9e9
--- a/tests/qemu-iotests/175
f7c9e9
+++ b/tests/qemu-iotests/175
f7c9e9
@@ -89,20 +89,20 @@ min_blocks=$(stat -c '%b' "$TEST_DIR/empty")
f7c9e9
 
f7c9e9
 echo
f7c9e9
 echo "== creating image with default preallocation =="
f7c9e9
-_make_test_img $size | _filter_imgfmt
f7c9e9
+_make_test_img -o extent_size_hint=0 $size | _filter_imgfmt
f7c9e9
 stat -c "size=%s, blocks=%b" $TEST_IMG | _filter_blocks $extra_blocks $min_blocks $size
f7c9e9
 
f7c9e9
 for mode in off full falloc; do
f7c9e9
     echo
f7c9e9
     echo "== creating image with preallocation $mode =="
f7c9e9
-    IMGOPTS=preallocation=$mode _make_test_img $size | _filter_imgfmt
f7c9e9
+    IMGOPTS="preallocation=$mode,extent_size_hint=0" _make_test_img $size | _filter_imgfmt
f7c9e9
     stat -c "size=%s, blocks=%b" $TEST_IMG | _filter_blocks $extra_blocks $min_blocks $size
f7c9e9
 done
f7c9e9
 
f7c9e9
 for new_size in 4096 1048576; do
f7c9e9
     echo
f7c9e9
     echo "== resize empty image with block_resize =="
f7c9e9
-    _make_test_img 0 | _filter_imgfmt
f7c9e9
+    _make_test_img -o extent_size_hint=0 0 | _filter_imgfmt
f7c9e9
     _block_resize $TEST_IMG $new_size >/dev/null
f7c9e9
     stat -c "size=%s, blocks=%b" $TEST_IMG | _filter_blocks $extra_blocks $min_blocks $new_size
f7c9e9
 done
f7c9e9
diff --git a/tests/qemu-iotests/243 b/tests/qemu-iotests/243
f7c9e9
index e563761307..104c7256c4 100755
f7c9e9
--- a/tests/qemu-iotests/243
f7c9e9
+++ b/tests/qemu-iotests/243
f7c9e9
@@ -47,7 +47,7 @@ for mode in off metadata falloc full; do
f7c9e9
     echo "=== preallocation=$mode ==="
f7c9e9
     echo
f7c9e9
 
f7c9e9
-    IMGOPTS="preallocation=$mode" _make_test_img 64M
f7c9e9
+    IMGOPTS="preallocation=$mode,extent_size_hint=0" _make_test_img 64M
f7c9e9
 
f7c9e9
     printf "File size: "
f7c9e9
     du -b $TEST_IMG | cut -f1
f7c9e9
@@ -64,7 +64,7 @@ for mode in off metadata falloc full; do
f7c9e9
     echo "=== External data file: preallocation=$mode ==="
f7c9e9
     echo
f7c9e9
 
f7c9e9
-    IMGOPTS="data_file=$TEST_IMG.data,preallocation=$mode" _make_test_img 64M
f7c9e9
+    IMGOPTS="data_file=$TEST_IMG.data,preallocation=$mode,extent_size_hint=0" _make_test_img 64M
f7c9e9
 
f7c9e9
     echo -n "qcow2 file size: "
f7c9e9
     du -b $TEST_IMG | cut -f1
f7c9e9
@@ -75,7 +75,7 @@ for mode in off metadata falloc full; do
f7c9e9
     echo -n "qcow2 disk usage: "
f7c9e9
     [ $(du -B1 $TEST_IMG | cut -f1) -lt 1048576 ] && echo "low" || echo "high"
f7c9e9
     echo -n "data disk usage:  "
f7c9e9
-    [ $(du -B1 $TEST_IMG.data | cut -f1) -lt 1048576 ] && echo "low" || echo "high"
f7c9e9
+    [ $(du -B1 $TEST_IMG.data | cut -f1) -lt 2097152 ] && echo "low" || echo "high"
f7c9e9
 
f7c9e9
 done
f7c9e9
 
f7c9e9
diff --git a/tests/qemu-iotests/common.filter b/tests/qemu-iotests/common.filter
f7c9e9
index c8e8663665..f29c1d3238 100644
f7c9e9
--- a/tests/qemu-iotests/common.filter
f7c9e9
+++ b/tests/qemu-iotests/common.filter
f7c9e9
@@ -146,6 +146,7 @@ _filter_img_create()
f7c9e9
         -e "s# refcount_bits=[0-9]\\+##g" \
f7c9e9
         -e "s# key-secret=[a-zA-Z0-9]\\+##g" \
f7c9e9
         -e "s# iter-time=[0-9]\\+##g" \
f7c9e9
+        -e "s# extent_size_hint=[0-9]\\+##g" \
f7c9e9
         -e "s# force_size=\\(on\\|off\\)##g"
f7c9e9
 }
f7c9e9
 
f7c9e9
-- 
f7c9e9
2.27.0
f7c9e9