Browse Source

qemu-img: Use zero writes after source backing EOF

Past the end of the source backing file, we memset() buf_old to zero, so
it is clearly easy to use blk_pwrite_zeroes() instead of blk_pwrite()
then.

Signed-off-by: Max Reitz <mreitz@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
master
Max Reitz 1 month ago
parent
commit
1c6e877992
1 changed files with 9 additions and 2 deletions
  1. 9
    2
      qemu-img.c

+ 9
- 2
qemu-img.c View File

@@ -3432,6 +3432,8 @@ static int img_rebase(int argc, char **argv)
3432 3432
         }
3433 3433
 
3434 3434
         for (offset = 0; offset < size; offset += n) {
3435
+            bool buf_old_is_zero = false;
3436
+
3435 3437
             /* How many bytes can we handle with the next read? */
3436 3438
             n = MIN(IO_BUF_SIZE, size - offset);
3437 3439
 
@@ -3452,6 +3454,7 @@ static int img_rebase(int argc, char **argv)
3452 3454
              */
3453 3455
             if (offset >= old_backing_size) {
3454 3456
                 memset(buf_old, 0, n);
3457
+                buf_old_is_zero = true;
3455 3458
             } else {
3456 3459
                 if (offset + n > old_backing_size) {
3457 3460
                     n = old_backing_size - offset;
@@ -3487,8 +3490,12 @@ static int img_rebase(int argc, char **argv)
3487 3490
                 if (compare_buffers(buf_old + written, buf_new + written,
3488 3491
                                     n - written, &pnum))
3489 3492
                 {
3490
-                    ret = blk_pwrite(blk, offset + written,
3491
-                                     buf_old + written, pnum, 0);
3493
+                    if (buf_old_is_zero) {
3494
+                        ret = blk_pwrite_zeroes(blk, offset + written, pnum, 0);
3495
+                    } else {
3496
+                        ret = blk_pwrite(blk, offset + written,
3497
+                                         buf_old + written, pnum, 0);
3498
+                    }
3492 3499
                     if (ret < 0) {
3493 3500
                         error_report("Error while writing to COW image: %s",
3494 3501
                             strerror(-ret));

Loading…
Cancel
Save