77c23f
From 50127f0ff9e13a15fd5bfeb2662e2404ff20f364 Mon Sep 17 00:00:00 2001
77c23f
From: Kevin Wolf <kwolf@redhat.com>
77c23f
Date: Mon, 8 Jun 2020 15:01:31 +0100
77c23f
Subject: [PATCH 03/17] block: Add flags to bdrv(_co)_truncate()
77c23f
77c23f
RH-Author: Kevin Wolf <kwolf@redhat.com>
77c23f
Message-id: <20200608150140.38218-3-kwolf@redhat.com>
77c23f
Patchwork-id: 97445
77c23f
O-Subject: [RHEL-AV-8.2.1 qemu-kvm PATCH 02/11] block: Add flags to bdrv(_co)_truncate()
77c23f
Bugzilla: 1780574
77c23f
RH-Acked-by: Sergio Lopez Pascual <slp@redhat.com>
77c23f
RH-Acked-by: Eric Blake <eblake@redhat.com>
77c23f
RH-Acked-by: Max Reitz <mreitz@redhat.com>
77c23f
77c23f
Now that block drivers can support flags for .bdrv_co_truncate, expose
77c23f
the parameter in the node level interfaces bdrv_co_truncate() and
77c23f
bdrv_truncate().
77c23f
77c23f
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
77c23f
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
77c23f
Reviewed-by: Alberto Garcia <berto@igalia.com>
77c23f
Reviewed-by: Max Reitz <mreitz@redhat.com>
77c23f
Message-Id: <20200424125448.63318-3-kwolf@redhat.com>
77c23f
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
77c23f
(cherry picked from commit 7b8e4857426f2e2de2441749996c6161b550bada)
77c23f
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
77c23f
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
77c23f
---
77c23f
 block/block-backend.c       |  2 +-
77c23f
 block/crypto.c              |  2 +-
77c23f
 block/io.c                  | 12 +++++++-----
77c23f
 block/parallels.c           |  6 +++---
77c23f
 block/qcow.c                |  4 ++--
77c23f
 block/qcow2-refcount.c      |  2 +-
77c23f
 block/qcow2.c               | 15 +++++++++------
77c23f
 block/raw-format.c          |  2 +-
77c23f
 block/vhdx-log.c            |  2 +-
77c23f
 block/vhdx.c                |  2 +-
77c23f
 block/vmdk.c                |  2 +-
77c23f
 include/block/block.h       |  5 +++--
77c23f
 tests/test-block-iothread.c |  6 +++---
77c23f
 13 files changed, 34 insertions(+), 28 deletions(-)
77c23f
77c23f
diff --git a/block/block-backend.c b/block/block-backend.c
77c23f
index 38ae413..8be2006 100644
77c23f
--- a/block/block-backend.c
77c23f
+++ b/block/block-backend.c
77c23f
@@ -2144,7 +2144,7 @@ int blk_truncate(BlockBackend *blk, int64_t offset, bool exact,
77c23f
         return -ENOMEDIUM;
77c23f
     }
77c23f
 
77c23f
-    return bdrv_truncate(blk->root, offset, exact, prealloc, errp);
77c23f
+    return bdrv_truncate(blk->root, offset, exact, prealloc, 0, errp);
77c23f
 }
77c23f
 
77c23f
 int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,
77c23f
diff --git a/block/crypto.c b/block/crypto.c
77c23f
index 6e4b726..fcb4a97 100644
77c23f
--- a/block/crypto.c
77c23f
+++ b/block/crypto.c
77c23f
@@ -313,7 +313,7 @@ block_crypto_co_truncate(BlockDriverState *bs, int64_t offset, bool exact,
77c23f
 
77c23f
     offset += payload_offset;
77c23f
 
77c23f
-    return bdrv_co_truncate(bs->file, offset, exact, prealloc, errp);
77c23f
+    return bdrv_co_truncate(bs->file, offset, exact, prealloc, 0, errp);
77c23f
 }
77c23f
 
77c23f
 static void block_crypto_close(BlockDriverState *bs)
77c23f
diff --git a/block/io.c b/block/io.c
77c23f
index 549e5a4..3235ce5 100644
77c23f
--- a/block/io.c
77c23f
+++ b/block/io.c
77c23f
@@ -3315,12 +3315,12 @@ static void bdrv_parent_cb_resize(BlockDriverState *bs)
77c23f
  * 'offset' bytes in length.
77c23f
  */
77c23f
 int coroutine_fn bdrv_co_truncate(BdrvChild *child, int64_t offset, bool exact,
77c23f
-                                  PreallocMode prealloc, Error **errp)
77c23f
+                                  PreallocMode prealloc, BdrvRequestFlags flags,
77c23f
+                                  Error **errp)
77c23f
 {
77c23f
     BlockDriverState *bs = child->bs;
77c23f
     BlockDriver *drv = bs->drv;
77c23f
     BdrvTrackedRequest req;
77c23f
-    BdrvRequestFlags flags = 0;
77c23f
     int64_t old_size, new_bytes;
77c23f
     int ret;
77c23f
 
77c23f
@@ -3378,7 +3378,7 @@ int coroutine_fn bdrv_co_truncate(BdrvChild *child, int64_t offset, bool exact,
77c23f
         }
77c23f
         ret = drv->bdrv_co_truncate(bs, offset, exact, prealloc, flags, errp);
77c23f
     } else if (bs->file && drv->is_filter) {
77c23f
-        ret = bdrv_co_truncate(bs->file, offset, exact, prealloc, errp);
77c23f
+        ret = bdrv_co_truncate(bs->file, offset, exact, prealloc, flags, errp);
77c23f
     } else {
77c23f
         error_setg(errp, "Image format driver does not support resize");
77c23f
         ret = -ENOTSUP;
77c23f
@@ -3411,6 +3411,7 @@ typedef struct TruncateCo {
77c23f
     int64_t offset;
77c23f
     bool exact;
77c23f
     PreallocMode prealloc;
77c23f
+    BdrvRequestFlags flags;
77c23f
     Error **errp;
77c23f
     int ret;
77c23f
 } TruncateCo;
77c23f
@@ -3419,12 +3420,12 @@ static void coroutine_fn bdrv_truncate_co_entry(void *opaque)
77c23f
 {
77c23f
     TruncateCo *tco = opaque;
77c23f
     tco->ret = bdrv_co_truncate(tco->child, tco->offset, tco->exact,
77c23f
-                                tco->prealloc, tco->errp);
77c23f
+                                tco->prealloc, tco->flags, tco->errp);
77c23f
     aio_wait_kick();
77c23f
 }
77c23f
 
77c23f
 int bdrv_truncate(BdrvChild *child, int64_t offset, bool exact,
77c23f
-                  PreallocMode prealloc, Error **errp)
77c23f
+                  PreallocMode prealloc, BdrvRequestFlags flags, Error **errp)
77c23f
 {
77c23f
     Coroutine *co;
77c23f
     TruncateCo tco = {
77c23f
@@ -3432,6 +3433,7 @@ int bdrv_truncate(BdrvChild *child, int64_t offset, bool exact,
77c23f
         .offset     = offset,
77c23f
         .exact      = exact,
77c23f
         .prealloc   = prealloc,
77c23f
+        .flags      = flags,
77c23f
         .errp       = errp,
77c23f
         .ret        = NOT_DONE,
77c23f
     };
77c23f
diff --git a/block/parallels.c b/block/parallels.c
77c23f
index 6d4ed77..2be92cf 100644
77c23f
--- a/block/parallels.c
77c23f
+++ b/block/parallels.c
77c23f
@@ -203,7 +203,7 @@ static int64_t allocate_clusters(BlockDriverState *bs, int64_t sector_num,
77c23f
         } else {
77c23f
             ret = bdrv_truncate(bs->file,
77c23f
                                 (s->data_end + space) << BDRV_SECTOR_BITS,
77c23f
-                                false, PREALLOC_MODE_OFF, NULL);
77c23f
+                                false, PREALLOC_MODE_OFF, 0, NULL);
77c23f
         }
77c23f
         if (ret < 0) {
77c23f
             return ret;
77c23f
@@ -493,7 +493,7 @@ static int coroutine_fn parallels_co_check(BlockDriverState *bs,
77c23f
              * That means we have to pass exact=true.
77c23f
              */
77c23f
             ret = bdrv_truncate(bs->file, res->image_end_offset, true,
77c23f
-                                PREALLOC_MODE_OFF, &local_err);
77c23f
+                                PREALLOC_MODE_OFF, 0, &local_err);
77c23f
             if (ret < 0) {
77c23f
                 error_report_err(local_err);
77c23f
                 res->check_errors++;
77c23f
@@ -889,7 +889,7 @@ static void parallels_close(BlockDriverState *bs)
77c23f
 
77c23f
         /* errors are ignored, so we might as well pass exact=true */
77c23f
         bdrv_truncate(bs->file, s->data_end << BDRV_SECTOR_BITS, true,
77c23f
-                      PREALLOC_MODE_OFF, NULL);
77c23f
+                      PREALLOC_MODE_OFF, 0, NULL);
77c23f
     }
77c23f
 
77c23f
     g_free(s->bat_dirty_bmap);
77c23f
diff --git a/block/qcow.c b/block/qcow.c
77c23f
index 8973e4e..6b5f226 100644
77c23f
--- a/block/qcow.c
77c23f
+++ b/block/qcow.c
77c23f
@@ -480,7 +480,7 @@ static int get_cluster_offset(BlockDriverState *bs,
77c23f
                     return -E2BIG;
77c23f
                 }
77c23f
                 ret = bdrv_truncate(bs->file, cluster_offset + s->cluster_size,
77c23f
-                                    false, PREALLOC_MODE_OFF, NULL);
77c23f
+                                    false, PREALLOC_MODE_OFF, 0, NULL);
77c23f
                 if (ret < 0) {
77c23f
                     return ret;
77c23f
                 }
77c23f
@@ -1035,7 +1035,7 @@ static int qcow_make_empty(BlockDriverState *bs)
77c23f
             l1_length) < 0)
77c23f
         return -1;
77c23f
     ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length, false,
77c23f
-                        PREALLOC_MODE_OFF, NULL);
77c23f
+                        PREALLOC_MODE_OFF, 0, NULL);
77c23f
     if (ret < 0)
77c23f
         return ret;
77c23f
 
77c23f
diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
77c23f
index f67ac6b..3a90d75 100644
77c23f
--- a/block/qcow2-refcount.c
77c23f
+++ b/block/qcow2-refcount.c
77c23f
@@ -2017,7 +2017,7 @@ static int check_refblocks(BlockDriverState *bs, BdrvCheckResult *res,
77c23f
                 }
77c23f
 
77c23f
                 ret = bdrv_truncate(bs->file, offset + s->cluster_size, false,
77c23f
-                                    PREALLOC_MODE_OFF, &local_err);
77c23f
+                                    PREALLOC_MODE_OFF, 0, &local_err);
77c23f
                 if (ret < 0) {
77c23f
                     error_report_err(local_err);
77c23f
                     goto resize_fail;
77c23f
diff --git a/block/qcow2.c b/block/qcow2.c
77c23f
index 977445e..c0fdcb9 100644
77c23f
--- a/block/qcow2.c
77c23f
+++ b/block/qcow2.c
77c23f
@@ -3082,7 +3082,7 @@ static int coroutine_fn preallocate_co(BlockDriverState *bs, uint64_t offset,
77c23f
             mode = PREALLOC_MODE_OFF;
77c23f
         }
77c23f
         ret = bdrv_co_truncate(s->data_file, host_offset + cur_bytes, false,
77c23f
-                               mode, errp);
77c23f
+                               mode, 0, errp);
77c23f
         if (ret < 0) {
77c23f
             return ret;
77c23f
         }
77c23f
@@ -4044,7 +4044,7 @@ static int coroutine_fn qcow2_co_truncate(BlockDriverState *bs, int64_t offset,
77c23f
              * always fulfilled, so there is no need to pass it on.)
77c23f
              */
77c23f
             bdrv_co_truncate(bs->file, (last_cluster + 1) * s->cluster_size,
77c23f
-                             false, PREALLOC_MODE_OFF, &local_err);
77c23f
+                             false, PREALLOC_MODE_OFF, 0, &local_err);
77c23f
             if (local_err) {
77c23f
                 warn_reportf_err(local_err,
77c23f
                                  "Failed to truncate the tail of the image: ");
77c23f
@@ -4066,7 +4066,8 @@ static int coroutine_fn qcow2_co_truncate(BlockDriverState *bs, int64_t offset,
77c23f
              * file should be resized to the exact target size, too,
77c23f
              * so we pass @exact here.
77c23f
              */
77c23f
-            ret = bdrv_co_truncate(s->data_file, offset, exact, prealloc, errp);
77c23f
+            ret = bdrv_co_truncate(s->data_file, offset, exact, prealloc, 0,
77c23f
+                                   errp);
77c23f
             if (ret < 0) {
77c23f
                 goto fail;
77c23f
             }
77c23f
@@ -4152,7 +4153,8 @@ static int coroutine_fn qcow2_co_truncate(BlockDriverState *bs, int64_t offset,
77c23f
         new_file_size = allocation_start +
77c23f
                         nb_new_data_clusters * s->cluster_size;
77c23f
         /* Image file grows, so @exact does not matter */
77c23f
-        ret = bdrv_co_truncate(bs->file, new_file_size, false, prealloc, errp);
77c23f
+        ret = bdrv_co_truncate(bs->file, new_file_size, false, prealloc, 0,
77c23f
+                               errp);
77c23f
         if (ret < 0) {
77c23f
             error_prepend(errp, "Failed to resize underlying file: ");
77c23f
             qcow2_free_clusters(bs, allocation_start,
77c23f
@@ -4255,7 +4257,8 @@ qcow2_co_pwritev_compressed_part(BlockDriverState *bs,
77c23f
         if (len < 0) {
77c23f
             return len;
77c23f
         }
77c23f
-        return bdrv_co_truncate(bs->file, len, false, PREALLOC_MODE_OFF, NULL);
77c23f
+        return bdrv_co_truncate(bs->file, len, false, PREALLOC_MODE_OFF, 0,
77c23f
+                                NULL);
77c23f
     }
77c23f
 
77c23f
     if (offset_into_cluster(s, offset)) {
77c23f
@@ -4493,7 +4496,7 @@ static int make_completely_empty(BlockDriverState *bs)
77c23f
     }
77c23f
 
77c23f
     ret = bdrv_truncate(bs->file, (3 + l1_clusters) * s->cluster_size, false,
77c23f
-                        PREALLOC_MODE_OFF, &local_err);
77c23f
+                        PREALLOC_MODE_OFF, 0, &local_err);
77c23f
     if (ret < 0) {
77c23f
         error_report_err(local_err);
77c23f
         goto fail;
77c23f
diff --git a/block/raw-format.c b/block/raw-format.c
77c23f
index f994c4a..c3acf9a 100644
77c23f
--- a/block/raw-format.c
77c23f
+++ b/block/raw-format.c
77c23f
@@ -387,7 +387,7 @@ static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset,
77c23f
 
77c23f
     s->size = offset;
77c23f
     offset += s->offset;
77c23f
-    return bdrv_co_truncate(bs->file, offset, exact, prealloc, errp);
77c23f
+    return bdrv_co_truncate(bs->file, offset, exact, prealloc, 0, errp);
77c23f
 }
77c23f
 
77c23f
 static void raw_eject(BlockDriverState *bs, bool eject_flag)
77c23f
diff --git a/block/vhdx-log.c b/block/vhdx-log.c
77c23f
index 13a49c2..404fb5f 100644
77c23f
--- a/block/vhdx-log.c
77c23f
+++ b/block/vhdx-log.c
77c23f
@@ -558,7 +558,7 @@ static int vhdx_log_flush(BlockDriverState *bs, BDRVVHDXState *s,
77c23f
                     goto exit;
77c23f
                 }
77c23f
                 ret = bdrv_truncate(bs->file, new_file_size, false,
77c23f
-                                    PREALLOC_MODE_OFF, NULL);
77c23f
+                                    PREALLOC_MODE_OFF, 0, NULL);
77c23f
                 if (ret < 0) {
77c23f
                     goto exit;
77c23f
                 }
77c23f
diff --git a/block/vhdx.c b/block/vhdx.c
77c23f
index 33e57cd..5dfbb20 100644
77c23f
--- a/block/vhdx.c
77c23f
+++ b/block/vhdx.c
77c23f
@@ -1264,7 +1264,7 @@ static int vhdx_allocate_block(BlockDriverState *bs, BDRVVHDXState *s,
77c23f
     }
77c23f
 
77c23f
     return bdrv_truncate(bs->file, *new_offset + s->block_size, false,
77c23f
-                         PREALLOC_MODE_OFF, NULL);
77c23f
+                         PREALLOC_MODE_OFF, 0, NULL);
77c23f
 }
77c23f
 
77c23f
 /*
77c23f
diff --git a/block/vmdk.c b/block/vmdk.c
77c23f
index eb726f2..1bbf937 100644
77c23f
--- a/block/vmdk.c
77c23f
+++ b/block/vmdk.c
77c23f
@@ -2077,7 +2077,7 @@ vmdk_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset,
77c23f
             }
77c23f
             length = QEMU_ALIGN_UP(length, BDRV_SECTOR_SIZE);
77c23f
             ret = bdrv_truncate(s->extents[i].file, length, false,
77c23f
-                                PREALLOC_MODE_OFF, NULL);
77c23f
+                                PREALLOC_MODE_OFF, 0, NULL);
77c23f
             if (ret < 0) {
77c23f
                 return ret;
77c23f
             }
77c23f
diff --git a/include/block/block.h b/include/block/block.h
77c23f
index b2a3074..4913596 100644
77c23f
--- a/include/block/block.h
77c23f
+++ b/include/block/block.h
77c23f
@@ -348,9 +348,10 @@ BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
77c23f
 void bdrv_refresh_filename(BlockDriverState *bs);
77c23f
 
77c23f
 int coroutine_fn bdrv_co_truncate(BdrvChild *child, int64_t offset, bool exact,
77c23f
-                                  PreallocMode prealloc, Error **errp);
77c23f
+                                  PreallocMode prealloc, BdrvRequestFlags flags,
77c23f
+                                  Error **errp);
77c23f
 int bdrv_truncate(BdrvChild *child, int64_t offset, bool exact,
77c23f
-                  PreallocMode prealloc, Error **errp);
77c23f
+                  PreallocMode prealloc, BdrvRequestFlags flags, Error **errp);
77c23f
 
77c23f
 int64_t bdrv_nb_sectors(BlockDriverState *bs);
77c23f
 int64_t bdrv_getlength(BlockDriverState *bs);
77c23f
diff --git a/tests/test-block-iothread.c b/tests/test-block-iothread.c
77c23f
index 2f3b763..71e9bce 100644
77c23f
--- a/tests/test-block-iothread.c
77c23f
+++ b/tests/test-block-iothread.c
77c23f
@@ -186,18 +186,18 @@ static void test_sync_op_truncate(BdrvChild *c)
77c23f
     int ret;
77c23f
 
77c23f
     /* Normal success path */
77c23f
-    ret = bdrv_truncate(c, 65536, false, PREALLOC_MODE_OFF, NULL);
77c23f
+    ret = bdrv_truncate(c, 65536, false, PREALLOC_MODE_OFF, 0, NULL);
77c23f
     g_assert_cmpint(ret, ==, 0);
77c23f
 
77c23f
     /* Early error: Negative offset */
77c23f
-    ret = bdrv_truncate(c, -2, false, PREALLOC_MODE_OFF, NULL);
77c23f
+    ret = bdrv_truncate(c, -2, false, PREALLOC_MODE_OFF, 0, NULL);
77c23f
     g_assert_cmpint(ret, ==, -EINVAL);
77c23f
 
77c23f
     /* Error: Read-only image */
77c23f
     c->bs->read_only = true;
77c23f
     c->bs->open_flags &= ~BDRV_O_RDWR;
77c23f
 
77c23f
-    ret = bdrv_truncate(c, 65536, false, PREALLOC_MODE_OFF, NULL);
77c23f
+    ret = bdrv_truncate(c, 65536, false, PREALLOC_MODE_OFF, 0, NULL);
77c23f
     g_assert_cmpint(ret, ==, -EACCES);
77c23f
 
77c23f
     c->bs->read_only = false;
77c23f
-- 
77c23f
1.8.3.1
77c23f