Skip to contentSkip to conversation

qemu-img: Add --target-is-zero to convert

PATCH New David Edmondson 5 participants 9 messages Jan 17, 2020 – Jan 24, 2020 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): id:[email protected]

#1 lore raw

PATCH 1/1 qemu-img: Add --target-is-zero to convert

In many cases the target of a convert operation is a newly provisioned target that the user knows is blank (filled with zeroes). In this situation there is no requirement for qemu-img to wastefully zero out the entire device.

Add a new option, --target-is-zero, allowing the user to indicate that an existing target device is already zero filled.
---

Apologies if this arrives twice - this From address wasn't subscribed the first time around.

 qemu-img.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
qemu-img.c +16 -3
index 95a24b9762..56ca727e8c 100644
@@ -70,6 +70,7 @@ enum {
     OPTION_PREALLOCATION = 265,
     OPTION_SHRINK = 266,
     OPTION_SALVAGE = 267,
+    OPTION_TARGET_IS_ZERO = 268,
 };
 
 typedef enum OutputFormat {
@@ -1593,6 +1594,7 @@ typedef struct ImgConvertState {
     bool copy_range;
     bool salvage;
     bool quiet;
+    bool target_is_zero;
     int min_sparse;
     int alignment;
     size_t cluster_sectors;
@@ -1984,10 +1986,11 @@ static int convert_do_copy(ImgConvertState *s)
     int64_t sector_num = 0;
 
     /* Check whether we have zero initialisation or can get it efficiently */
-    if (s->target_is_new && s->min_sparse && !s->target_has_backing) {
+    s->has_zero_init = s->target_is_zero;
+
+    if (!s->has_zero_init && s->target_is_new && s->min_sparse &&
+        !s->target_has_backing) {
         s->has_zero_init = bdrv_has_zero_init(blk_bs(s->target));
-    } else {
-        s->has_zero_init = false;
     }
 
     if (!s->has_zero_init && !s->target_has_backing &&
@@ -2076,6 +2079,7 @@ static int img_convert(int argc, char **argv)
         .buf_sectors        = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
         .wr_in_order        = true,
         .num_coroutines     = 8,
+        .target_is_zero     = false,
     };
 
     for(;;) {
@@ -2086,6 +2090,7 @@ static int img_convert(int argc, char **argv)
             {"force-share", no_argument, 0, 'U'},
             {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
             {"salvage", no_argument, 0, OPTION_SALVAGE},
+            {"target-is-zero", no_argument, 0, OPTION_TARGET_IS_ZERO},
             {0, 0, 0, 0}
         };
         c = getopt_long(argc, argv, ":hf:O:B:Cco:l:S:pt:T:qnm:WU",
@@ -2209,6 +2214,9 @@ static int img_convert(int argc, char **argv)
         case OPTION_TARGET_IMAGE_OPTS:
             tgt_image_opts = true;
             break;
+        case OPTION_TARGET_IS_ZERO:
+            s.target_is_zero = true;
+            break;
         }
     }
 
@@ -2247,6 +2255,11 @@ static int img_convert(int argc, char **argv)
         warn_report("This will become an error in future QEMU versions.");
     }
 
+    if (s.target_is_zero && !skip_create) {
+        error_report("--target-is-zero requires use of -n flag");
+        goto fail_getopt;
+    }
+
     s.src_num = argc - optind - 1;
     out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
 
Signature · 1 line
2.24.1
#2 lore raw
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series seems to have some coding style problems. See output below for more information:

Message-id: [email protected]
Type: series
Subject: [PATCH] qemu-img: Add --target-is-zero to convert

=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram
./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
Switched to a new branch 'test'
c181366 qemu-img: Add --target-is-zero to convert

=== OUTPUT BEGIN ===
ERROR: Missing Signed-off-by: line(s)

total: 1 errors, 0 warnings, 62 lines checked

Commit c181366f92d4 (qemu-img: Add --target-is-zero to convert) has style problems, please review. If any of these errors are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
=== OUTPUT END ===

Test command exited with code: 1

The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]

#3 lore raw

PATCH 1/1 qemu-img: Add --target-is-zero to convert

In reply to a message not in the archive

In many cases the target of a convert operation is a newly provisioned target that the user knows is blank (filled with zeroes). In this situation there is no requirement for qemu-img to wastefully zero out the entire device.

Add a new option, --target-is-zero, allowing the user to indicate that an existing target device is already zero filled.

---
 qemu-img.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
qemu-img.c +16 -3
index 95a24b9762..56ca727e8c 100644
@@ -70,6 +70,7 @@ enum {
     OPTION_PREALLOCATION = 265,
     OPTION_SHRINK = 266,
     OPTION_SALVAGE = 267,
+    OPTION_TARGET_IS_ZERO = 268,
 };
 
 typedef enum OutputFormat {
@@ -1593,6 +1594,7 @@ typedef struct ImgConvertState {
     bool copy_range;
     bool salvage;
     bool quiet;
+    bool target_is_zero;
     int min_sparse;
     int alignment;
     size_t cluster_sectors;
@@ -1984,10 +1986,11 @@ static int convert_do_copy(ImgConvertState *s)
     int64_t sector_num = 0;
 
     /* Check whether we have zero initialisation or can get it efficiently */
-    if (s->target_is_new && s->min_sparse && !s->target_has_backing) {
+    s->has_zero_init = s->target_is_zero;
+
+    if (!s->has_zero_init && s->target_is_new && s->min_sparse &&
+        !s->target_has_backing) {
         s->has_zero_init = bdrv_has_zero_init(blk_bs(s->target));
-    } else {
-        s->has_zero_init = false;
     }
 
     if (!s->has_zero_init && !s->target_has_backing &&
@@ -2076,6 +2079,7 @@ static int img_convert(int argc, char **argv)
         .buf_sectors        = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
         .wr_in_order        = true,
         .num_coroutines     = 8,
+        .target_is_zero     = false,
     };
 
     for(;;) {
@@ -2086,6 +2090,7 @@ static int img_convert(int argc, char **argv)
             {"force-share", no_argument, 0, 'U'},
             {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
             {"salvage", no_argument, 0, OPTION_SALVAGE},
+            {"target-is-zero", no_argument, 0, OPTION_TARGET_IS_ZERO},
             {0, 0, 0, 0}
         };
         c = getopt_long(argc, argv, ":hf:O:B:Cco:l:S:pt:T:qnm:WU",
@@ -2209,6 +2214,9 @@ static int img_convert(int argc, char **argv)
         case OPTION_TARGET_IMAGE_OPTS:
             tgt_image_opts = true;
             break;
+        case OPTION_TARGET_IS_ZERO:
+            s.target_is_zero = true;
+            break;
         }
     }
 
@@ -2247,6 +2255,11 @@ static int img_convert(int argc, char **argv)
         warn_report("This will become an error in future QEMU versions.");
     }
 
+    if (s.target_is_zero && !skip_create) {
+        error_report("--target-is-zero requires use of -n flag");
+        goto fail_getopt;
+    }
+
     s.src_num = argc - optind - 1;
     out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
 
Signature · 1 line
2.24.1
#4 lore raw
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series seems to have some coding style problems. See output below for more information:

Message-id: [email protected]
Type: series
Subject: [PATCH] qemu-img: Add --target-is-zero to convert

=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram
./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===

From https://github.com/patchew-project/qemu
* [new tag] patchew/[email protected] -> patchew/[email protected]
Switched to a new branch 'test'
71f2a28 qemu-img: Add --target-is-zero to convert

=== OUTPUT BEGIN ===
ERROR: Missing Signed-off-by: line(s)

total: 1 errors, 0 warnings, 62 lines checked

Commit 71f2a28bebbe (qemu-img: Add --target-is-zero to convert) has style problems, please review. If any of these errors are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
=== OUTPUT END ===

Test command exited with code: 1

The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]

#5 lore raw
In reply to David Edmondson

CC qemu-block and block maintainers

Quoted 5 lines · David Edmondson

In many cases the target of a convert operation is a newly provisioned target that the user knows is blank (filled with zeroes). In this situation there is no requirement for qemu-img to wastefully zero out the entire device.

Is there no way to convince bdrv_has_zero_init to return what we want already in this case? I cannot recall off hand, but wonder if there's an advanced syntax method of specifying the target image that can set this flag already.

Quoted 80 lines

Add a new option, --target-is-zero, allowing the user to indicate that an existing target device is already zero filled.

---
 qemu-img.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
qemu-img.c +16 -3
index 95a24b9762..56ca727e8c 100644
@@ -70,6 +70,7 @@ enum {
     OPTION_PREALLOCATION = 265,
     OPTION_SHRINK = 266,
     OPTION_SALVAGE = 267,
+    OPTION_TARGET_IS_ZERO = 268,
 };
 
 typedef enum OutputFormat {
@@ -1593,6 +1594,7 @@ typedef struct ImgConvertState {
     bool copy_range;
     bool salvage;
     bool quiet;
+    bool target_is_zero;
     int min_sparse;
     int alignment;
     size_t cluster_sectors;
@@ -1984,10 +1986,11 @@ static int convert_do_copy(ImgConvertState *s)
     int64_t sector_num = 0;
 
     /* Check whether we have zero initialisation or can get it efficiently */
-    if (s->target_is_new && s->min_sparse && !s->target_has_backing) {
+    s->has_zero_init = s->target_is_zero;
+
+    if (!s->has_zero_init && s->target_is_new && s->min_sparse &&
+        !s->target_has_backing) {
         s->has_zero_init = bdrv_has_zero_init(blk_bs(s->target));
-    } else {
-        s->has_zero_init = false;
     }
 
     if (!s->has_zero_init && !s->target_has_backing &&
@@ -2076,6 +2079,7 @@ static int img_convert(int argc, char **argv)
         .buf_sectors        = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
         .wr_in_order        = true,
         .num_coroutines     = 8,
+        .target_is_zero     = false,
     };
 
     for(;;) {
@@ -2086,6 +2090,7 @@ static int img_convert(int argc, char **argv)
             {"force-share", no_argument, 0, 'U'},
             {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
             {"salvage", no_argument, 0, OPTION_SALVAGE},
+            {"target-is-zero", no_argument, 0, OPTION_TARGET_IS_ZERO},
             {0, 0, 0, 0}
         };
         c = getopt_long(argc, argv, ":hf:O:B:Cco:l:S:pt:T:qnm:WU",
@@ -2209,6 +2214,9 @@ static int img_convert(int argc, char **argv)
         case OPTION_TARGET_IMAGE_OPTS:
             tgt_image_opts = true;
             break;
+        case OPTION_TARGET_IS_ZERO:
+            s.target_is_zero = true;
+            break;
         }
     }
 
@@ -2247,6 +2255,11 @@ static int img_convert(int argc, char **argv)
         warn_report("This will become an error in future QEMU versions.");
     }
 
+    if (s.target_is_zero && !skip_create) {
+        error_report("--target-is-zero requires use of -n flag");
+        goto fail_getopt;
+    }
+
     s.src_num = argc - optind - 1;
     out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
 
#6 lore raw
Quoted 11 lines · John Snow

CC qemu-block and block maintainers

Quoted 5 lines · David Edmondson

In many cases the target of a convert operation is a newly provisioned target that the user knows is blank (filled with zeroes). In this situation there is no requirement for qemu-img to wastefully zero out the entire device.

Is there no way to convince bdrv_has_zero_init to return what we want already in this case?

In the current HEAD code, bdrv_has_zero_init will never be called for “convert -n” (skip target volume creation).

If -n is not specified the host_device block driver doesn't provide a bdrv_has_zero_init function, so it's assumed not supported.

Quoted 2 lines

I cannot recall off hand, but wonder if there's an advanced syntax method of specifying the target image that can set this flag already.

I couldn't see one, but I'd be happy to learn of its existence. My first approach was to add a raw specific option and add it using --target-image-opts, resulting in something like:

qemu-img convert -n --target-image-opts sparse.qcow2 \
driver=raw,file.filename=/dev/sdg,assume-blank=on

(assume-blank=on is the new bit). This worked, but is only useful for raw targets.

The discussion here led me to switch to --target-is-zero.

Mark Kanda sent me some comments suggesting that I get rid of the new target_is_zero boolean and simply set has_zero_init, which I will do before sending out another patch if this overall approach is considered appropriate.

Quoted 71 lines
Quoted 71 lines

Add a new option, --target-is-zero, allowing the user to indicate that an existing target device is already zero filled.

---
 qemu-img.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
qemu-img.c +11 -3
index 95a24b9762..56ca727e8c 100644
@@ -70,6 +70,7 @@ enum {
     OPTION_PREALLOCATION = 265,
     OPTION_SHRINK = 266,
     OPTION_SALVAGE = 267,
+    OPTION_TARGET_IS_ZERO = 268,
 };
 
 typedef enum OutputFormat {
@@ -1593,6 +1594,7 @@ typedef struct ImgConvertState {
     bool copy_range;
     bool salvage;
     bool quiet;
+    bool target_is_zero;
     int min_sparse;
     int alignment;
     size_t cluster_sectors;
@@ -1984,10 +1986,11 @@ static int convert_do_copy(ImgConvertState *s)
     int64_t sector_num = 0;
 
     /* Check whether we have zero initialisation or can get it efficiently */
-    if (s->target_is_new && s->min_sparse && !s->target_has_backing) {
+    s->has_zero_init = s->target_is_zero;
+
+    if (!s->has_zero_init && s->target_is_new && s->min_sparse &&
+        !s->target_has_backing) {
         s->has_zero_init = bdrv_has_zero_init(blk_bs(s->target));
-    } else {
-        s->has_zero_init = false;
     }
 
     if (!s->has_zero_init && !s->target_has_backing &&
@@ -2076,6 +2079,7 @@ static int img_convert(int argc, char **argv)
         .buf_sectors        = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
         .wr_in_order        = true,
         .num_coroutines     = 8,
+        .target_is_zero     = false,
     };
 
     for(;;) {
@@ -2086,6 +2090,7 @@ static int img_convert(int argc, char **argv)
             {"force-share", no_argument, 0, 'U'},
             {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
             {"salvage", no_argument, 0, OPTION_SALVAGE},
+            {"target-is-zero", no_argument, 0, OPTION_TARGET_IS_ZERO},
             {0, 0, 0, 0}
         };
         c = getopt_long(argc, argv, ":hf:O:B:Cco:l:S:pt:T:qnm:WU",
@@ -2209,6 +2214,9 @@ static int img_convert(int argc, char **argv)
         case OPTION_TARGET_IMAGE_OPTS:
             tgt_image_opts = true;
             break;
+        case OPTION_TARGET_IS_ZERO:
+            s.target_is_zero = true;
+            break;
         }
     }
 
@@ -2247,6 +2255,11 @@ static int img_convert(int argc, char **argv)
         warn_report("This will become an error in future QEMU versions.");
     }
 
Quoted 9 lines

qemu-img.c static int img_convert(int argc, char **argv) · line 2265

Quoted 9 lines
qemu-img.c +5 -0
+    if (s.target_is_zero && !skip_create) {
+        error_report("--target-is-zero requires use of -n flag");
+        goto fail_getopt;
+    }
+
     s.src_num = argc - optind - 1;
     out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
 

dme.

Signature · 1 line
Please forgive me if I act a little strange, for I know not what I do.
#7 lore raw
In reply to David Edmondson

Hi,

Quoted 24 lines · David Edmondson

In many cases the target of a convert operation is a newly provisioned target that the user knows is blank (filled with zeroes). In this situation there is no requirement for qemu-img to wastefully zero out the entire device.

Add a new option, --target-is-zero, allowing the user to indicate that an existing target device is already zero filled.

---
 qemu-img.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
qemu-img.c +1 -0
index 95a24b9762..56ca727e8c 100644
@@ -70,6 +70,7 @@ enum {
     OPTION_PREALLOCATION = 265,
     OPTION_SHRINK = 266,
     OPTION_SALVAGE = 267,
+    OPTION_TARGET_IS_ZERO = 268,
 };
 
 typedef enum OutputFormat {

qemu-img.c typedef struct ImgConvertState { · line 1597

Quoted 4 lines
qemu-img.c +1 -0
@@ -1593,6 +1594,7 @@ typedef struct ImgConvertState {
     bool copy_range;
     bool salvage;
     bool quiet;
+    bool target_is_zero;

As you already said, we probably don’t need this and it’d be sufficient to set the has_zero_init value directly.

qemu-img.c static int convert_do_copy(ImgConvertState *s) · line 1989

Quoted 9 lines
qemu-img.c +1 -1
     int min_sparse;
     int alignment;
     size_t cluster_sectors;
@@ -1984,10 +1986,11 @@ static int convert_do_copy(ImgConvertState *s)
     int64_t sector_num = 0;
 
     /* Check whether we have zero initialisation or can get it efficiently */
-    if (s->target_is_new && s->min_sparse && !s->target_has_backing) {
+    s->has_zero_init = s->target_is_zero;

We cannot has_zero_init to true if the target has a backing file, because convert_co_write() asserts that the target must not have a backing file if has_zero_init is true. (It’s impossible for a file to be initialized to zeroes if it has a backing file; or at least it doesn’t make sense then to have a backing file.)

Case in point:

$ ./qemu-img create -f qcow2 src.qcow2 64M
Formatting 'src.qcow2', fmt=qcow2 size=67108864 cluster_size=65536
lazy_refcounts=off refcount_bits=16
$ ./qemu-img create -f qcow2 backing.qcow2 64M
Formatting 'backing.qcow2', fmt=qcow2 size=67108864 cluster_size=65536
lazy_refcounts=off refcount_bits=16
$ ./qemu-img create -f qcow2 -b backing.qcow2 dst.qcow2 64M

Formatting 'dst.qcow2', fmt=qcow2 size=67108864 backing_file=backing.qcow2 cluster_size=65536 lazy_refcounts=off refcount_bits=16

$ ./qemu-img convert -n -B backing.qcow2 -f qcow2 -O qcow2
--target-is-zero src.qcow2 dst.qcow2
qemu-img: qemu-img.c:1812: convert_co_write: Assertion
`!s->target_has_backing' failed.
[1]    80813 abort (core dumped)  ./qemu-img convert -n -B backing.qcow2
-f qcow2 -O qcow2 --target-is-zero

qemu-img.c static int convert_do_copy(ImgConvertState *s) · line 1992

Quoted 3 lines
qemu-img.c +3 -0
+
+    if (!s->has_zero_init && s->target_is_new && s->min_sparse &&
+        !s->target_has_backing) {

(This will be irrelevant after target_has_backing is gone, but because has_zero_init and target_has_backing are equivalent here, there is no need to check both.)

Quoted 33 lines
qemu-img.c +5 -2
         s->has_zero_init = bdrv_has_zero_init(blk_bs(s->target));
-    } else {
-        s->has_zero_init = false;
     }
 
     if (!s->has_zero_init && !s->target_has_backing &&
@@ -2076,6 +2079,7 @@ static int img_convert(int argc, char **argv)
         .buf_sectors        = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
         .wr_in_order        = true,
         .num_coroutines     = 8,
+        .target_is_zero     = false,
     };
 
     for(;;) {
@@ -2086,6 +2090,7 @@ static int img_convert(int argc, char **argv)
             {"force-share", no_argument, 0, 'U'},
             {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
             {"salvage", no_argument, 0, OPTION_SALVAGE},
+            {"target-is-zero", no_argument, 0, OPTION_TARGET_IS_ZERO},
             {0, 0, 0, 0}
         };
         c = getopt_long(argc, argv, ":hf:O:B:Cco:l:S:pt:T:qnm:WU",
@@ -2209,6 +2214,9 @@ static int img_convert(int argc, char **argv)
         case OPTION_TARGET_IMAGE_OPTS:
             tgt_image_opts = true;
             break;
+        case OPTION_TARGET_IS_ZERO:
+            s.target_is_zero = true;
+            break;
         }
     }
 

qemu-img.c static int img_convert(int argc, char **argv) · line 2259

Quoted 5 lines
qemu-img.c +2 -0
@@ -2247,6 +2255,11 @@ static int img_convert(int argc, char **argv)
         warn_report("This will become an error in future QEMU versions.");
     }
 
+    if (s.target_is_zero && !skip_create) {
+        error_report("--target-is-zero requires use of -n flag");

Hm, I could imagine it being useful even without -n, but maybe it’s safer to forbid this case for now and reconsider if someone were to ask.

qemu-img.c static int img_convert(int argc, char **argv) · line 2264

Quoted 5 lines
qemu-img.c +3 -0
+        goto fail_getopt;
+    }
+
     s.src_num = argc - optind - 1;
     out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;

This patch should also add some documentation for the new option (in qemu-img-cmds.hx and in qemu-img.texi for the man page).

Max

#8 lore raw
Quoted 41 lines · Max Reitz

Hi,

Quoted 24 lines · David Edmondson

In many cases the target of a convert operation is a newly provisioned target that the user knows is blank (filled with zeroes). In this situation there is no requirement for qemu-img to wastefully zero out the entire device.

Add a new option, --target-is-zero, allowing the user to indicate that an existing target device is already zero filled.

---
 qemu-img.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
qemu-img.c +1 -0
index 95a24b9762..56ca727e8c 100644
@@ -70,6 +70,7 @@ enum {
     OPTION_PREALLOCATION = 265,
     OPTION_SHRINK = 266,
     OPTION_SALVAGE = 267,
+    OPTION_TARGET_IS_ZERO = 268,
 };
 
 typedef enum OutputFormat {

qemu-img.c typedef struct ImgConvertState { · line 1597

Quoted 4 lines
qemu-img.c +1 -0
@@ -1593,6 +1594,7 @@ typedef struct ImgConvertState {
     bool copy_range;
     bool salvage;
     bool quiet;
+    bool target_is_zero;

As you already said, we probably don’t need this and it’d be sufficient to set the has_zero_init value directly.

Quoted 6 lines
qemu-img.c +0 -0
     int min_sparse;
     int alignment;
     size_t cluster_sectors;
@@ -1984,10 +1986,11 @@ static int convert_do_copy(ImgConvertState *s)
     int64_t sector_num = 0;
 
Quoted 9 lines

qemu-img.c static int convert_do_copy(ImgConvertState *s) · line 1989

Quoted 3 lines
qemu-img.c +1 -1
     /* Check whether we have zero initialisation or can get it efficiently */
-    if (s->target_is_new && s->min_sparse && !s->target_has_backing) {
+    s->has_zero_init = s->target_is_zero;

We cannot has_zero_init to true if the target has a backing file, because convert_co_write() asserts that the target must not have a backing file if has_zero_init is true. (It’s impossible for a file to be initialized to zeroes if it has a backing file; or at least it doesn’t make sense then to have a backing file.)

I'll add a check causing (has_zero_init && target_has_backing) to throw an error after the target_has_backing is determined.

Quoted 20 lines

Case in point:

$ ./qemu-img create -f qcow2 src.qcow2 64M
Formatting 'src.qcow2', fmt=qcow2 size=67108864 cluster_size=65536
lazy_refcounts=off refcount_bits=16
$ ./qemu-img create -f qcow2 backing.qcow2 64M
Formatting 'backing.qcow2', fmt=qcow2 size=67108864 cluster_size=65536
lazy_refcounts=off refcount_bits=16
$ ./qemu-img create -f qcow2 -b backing.qcow2 dst.qcow2 64M

Formatting 'dst.qcow2', fmt=qcow2 size=67108864 backing_file=backing.qcow2 cluster_size=65536 lazy_refcounts=off refcount_bits=16

$ ./qemu-img convert -n -B backing.qcow2 -f qcow2 -O qcow2
--target-is-zero src.qcow2 dst.qcow2
qemu-img: qemu-img.c:1812: convert_co_write: Assertion
`!s->target_has_backing' failed.
[1]    80813 abort (core dumped)  ./qemu-img convert -n -B backing.qcow2
-f qcow2 -O qcow2 --target-is-zero
Quoted 7 lines

qemu-img.c static int convert_do_copy(ImgConvertState *s) · line 1992

Quoted 3 lines
qemu-img.c +3 -0
+
+    if (!s->has_zero_init && s->target_is_new && s->min_sparse &&
+        !s->target_has_backing) {

(This will be irrelevant after target_has_backing is gone, but because has_zero_init and target_has_backing are equivalent here, there is no need to check both.)

I don't understand this comment - I must be missing something.

If both has_zero_init and target_has_backing are false here, we should go and check bdrv_has_zero_init().

They can't both be true (when the above mentioned test is added) and if either is true, we don't want to call brv_has_zero_init(), as either the user has asserted that the target is blank or we have a backing file.

Quoted 42 lines
Quoted 33 lines
qemu-img.c +5 -2
         s->has_zero_init = bdrv_has_zero_init(blk_bs(s->target));
-    } else {
-        s->has_zero_init = false;
     }
 
     if (!s->has_zero_init && !s->target_has_backing &&
@@ -2076,6 +2079,7 @@ static int img_convert(int argc, char **argv)
         .buf_sectors        = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
         .wr_in_order        = true,
         .num_coroutines     = 8,
+        .target_is_zero     = false,
     };
 
     for(;;) {
@@ -2086,6 +2090,7 @@ static int img_convert(int argc, char **argv)
             {"force-share", no_argument, 0, 'U'},
             {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
             {"salvage", no_argument, 0, OPTION_SALVAGE},
+            {"target-is-zero", no_argument, 0, OPTION_TARGET_IS_ZERO},
             {0, 0, 0, 0}
         };
         c = getopt_long(argc, argv, ":hf:O:B:Cco:l:S:pt:T:qnm:WU",
@@ -2209,6 +2214,9 @@ static int img_convert(int argc, char **argv)
         case OPTION_TARGET_IMAGE_OPTS:
             tgt_image_opts = true;
             break;
+        case OPTION_TARGET_IS_ZERO:
+            s.target_is_zero = true;
+            break;
         }
     }
 

qemu-img.c static int img_convert(int argc, char **argv) · line 2259

Quoted 5 lines
qemu-img.c +2 -0
@@ -2247,6 +2255,11 @@ static int img_convert(int argc, char **argv)
         warn_report("This will become an error in future QEMU versions.");
     }
 
+    if (s.target_is_zero && !skip_create) {
+        error_report("--target-is-zero requires use of -n flag");

Hm, I could imagine it being useful even without -n, but maybe it’s safer to forbid this case for now and reconsider if someone were to ask.

Quoted 8 lines

qemu-img.c static int img_convert(int argc, char **argv) · line 2264

Quoted 5 lines
qemu-img.c +3 -0
+        goto fail_getopt;
+    }
+
     s.src_num = argc - optind - 1;
     out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;

This patch should also add some documentation for the new option (in qemu-img-cmds.hx and in qemu-img.texi for the man page).

Will do.

dme.

Signature · 1 line
You can't hide from the flipside.
#9 lore raw
Quoted 5 lines · David Edmondson
Quoted 3 lines · Max Reitz

Hi,

On 17.01.20 11:34, David Edmondson wrote:

[...]

Quoted 9 lines
Quoted 7 lines

qemu-img.c static int convert_do_copy(ImgConvertState *s) · line 1992

Quoted 3 lines
qemu-img.c +3 -0
+
+    if (!s->has_zero_init && s->target_is_new && s->min_sparse &&
+        !s->target_has_backing) {

(This will be irrelevant after target_has_backing is gone, but because has_zero_init and target_has_backing are equivalent here, there is no need to check both.)

I don't understand this comment - I must be missing something.

Just the fact that for some reason I read “target_has_backing” as “target_is_zero”. Sorry for the false alarm. O:-)

Max