Blame SOURCES/kvm-block-Move-bdrv_truncate-implementation-to-io.c.patch

357786
From 630e021373d8b88a5d9698c843811a518463072a Mon Sep 17 00:00:00 2001
357786
From: Kevin Wolf <kwolf@redhat.com>
357786
Date: Thu, 12 Jul 2018 14:42:56 +0200
357786
Subject: [PATCH 37/89] block: Move bdrv_truncate() implementation to io.c
357786
357786
RH-Author: Kevin Wolf <kwolf@redhat.com>
357786
Message-id: <20180712144258.17303-5-kwolf@redhat.com>
357786
Patchwork-id: 81328
357786
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH 4/6] block: Move bdrv_truncate() implementation to io.c
357786
Bugzilla: 1595173
357786
RH-Acked-by: Max Reitz <mreitz@redhat.com>
357786
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
357786
RH-Acked-by: John Snow <jsnow@redhat.com>
357786
357786
This moves the bdrv_truncate() implementation from block.c to block/io.c
357786
so it can have access to the tracked requests infrastructure.
357786
357786
This involves making refresh_total_sectors() public (in block_int.h).
357786
357786
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
357786
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
357786
(cherry picked from commit 3d9f2d2af63fda5f0404fb85ea80161837a4e4e3)
357786
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
357786
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
357786
---
357786
 block.c                   | 111 +---------------------------------------------
357786
 block/io.c                | 109 +++++++++++++++++++++++++++++++++++++++++++++
357786
 include/block/block_int.h |   2 +
357786
 3 files changed, 112 insertions(+), 110 deletions(-)
357786
357786
diff --git a/block.c b/block.c
357786
index 0af08ca..10a1ece 100644
357786
--- a/block.c
357786
+++ b/block.c
357786
@@ -721,7 +721,7 @@ static int find_image_format(BlockBackend *file, const char *filename,
357786
  * Set the current 'total_sectors' value
357786
  * Return 0 on success, -errno on error.
357786
  */
357786
-static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
357786
+int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
357786
 {
357786
     BlockDriver *drv = bs->drv;
357786
 
357786
@@ -2200,16 +2200,6 @@ static void bdrv_parent_cb_change_media(BlockDriverState *bs, bool load)
357786
     }
357786
 }
357786
 
357786
-static void bdrv_parent_cb_resize(BlockDriverState *bs)
357786
-{
357786
-    BdrvChild *c;
357786
-    QLIST_FOREACH(c, &bs->parents, next_parent) {
357786
-        if (c->role->resize) {
357786
-            c->role->resize(c);
357786
-        }
357786
-    }
357786
-}
357786
-
357786
 /*
357786
  * Sets the backing file link of a BDS. A new reference is created; callers
357786
  * which don't need their own reference any more must call bdrv_unref().
357786
@@ -3736,105 +3726,6 @@ exit:
357786
 }
357786
 
357786
 /**
357786
- * Truncate file to 'offset' bytes (needed only for file protocols)
357786
- */
357786
-int coroutine_fn bdrv_co_truncate(BdrvChild *child, int64_t offset,
357786
-                                  PreallocMode prealloc, Error **errp)
357786
-{
357786
-    BlockDriverState *bs = child->bs;
357786
-    BlockDriver *drv = bs->drv;
357786
-    int ret;
357786
-
357786
-    assert(child->perm & BLK_PERM_RESIZE);
357786
-
357786
-    /* if bs->drv == NULL, bs is closed, so there's nothing to do here */
357786
-    if (!drv) {
357786
-        error_setg(errp, "No medium inserted");
357786
-        return -ENOMEDIUM;
357786
-    }
357786
-    if (offset < 0) {
357786
-        error_setg(errp, "Image size cannot be negative");
357786
-        return -EINVAL;
357786
-    }
357786
-
357786
-    bdrv_inc_in_flight(bs);
357786
-
357786
-    if (!drv->bdrv_co_truncate) {
357786
-        if (bs->file && drv->is_filter) {
357786
-            ret = bdrv_co_truncate(bs->file, offset, prealloc, errp);
357786
-            goto out;
357786
-        }
357786
-        error_setg(errp, "Image format driver does not support resize");
357786
-        ret = -ENOTSUP;
357786
-        goto out;
357786
-    }
357786
-    if (bs->read_only) {
357786
-        error_setg(errp, "Image is read-only");
357786
-        ret = -EACCES;
357786
-        goto out;
357786
-    }
357786
-
357786
-    assert(!(bs->open_flags & BDRV_O_INACTIVE));
357786
-
357786
-    ret = drv->bdrv_co_truncate(bs, offset, prealloc, errp);
357786
-    if (ret < 0) {
357786
-        goto out;
357786
-    }
357786
-    ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
357786
-    if (ret < 0) {
357786
-        error_setg_errno(errp, -ret, "Could not refresh total sector count");
357786
-    } else {
357786
-        offset = bs->total_sectors * BDRV_SECTOR_SIZE;
357786
-    }
357786
-    bdrv_dirty_bitmap_truncate(bs, offset);
357786
-    bdrv_parent_cb_resize(bs);
357786
-    atomic_inc(&bs->write_gen);
357786
-
357786
-out:
357786
-    bdrv_dec_in_flight(bs);
357786
-    return ret;
357786
-}
357786
-
357786
-typedef struct TruncateCo {
357786
-    BdrvChild *child;
357786
-    int64_t offset;
357786
-    PreallocMode prealloc;
357786
-    Error **errp;
357786
-    int ret;
357786
-} TruncateCo;
357786
-
357786
-static void coroutine_fn bdrv_truncate_co_entry(void *opaque)
357786
-{
357786
-    TruncateCo *tco = opaque;
357786
-    tco->ret = bdrv_co_truncate(tco->child, tco->offset, tco->prealloc,
357786
-                                tco->errp);
357786
-}
357786
-
357786
-int bdrv_truncate(BdrvChild *child, int64_t offset, PreallocMode prealloc,
357786
-                  Error **errp)
357786
-{
357786
-    Coroutine *co;
357786
-    TruncateCo tco = {
357786
-        .child      = child,
357786
-        .offset     = offset,
357786
-        .prealloc   = prealloc,
357786
-        .errp       = errp,
357786
-        .ret        = NOT_DONE,
357786
-    };
357786
-
357786
-    if (qemu_in_coroutine()) {
357786
-        /* Fast-path if already in coroutine context */
357786
-        bdrv_truncate_co_entry(&tco;;
357786
-    } else {
357786
-        co = qemu_coroutine_create(bdrv_truncate_co_entry, &tco;;
357786
-        qemu_coroutine_enter(co);
357786
-        BDRV_POLL_WHILE(child->bs, tco.ret == NOT_DONE);
357786
-    }
357786
-
357786
-    return tco.ret;
357786
-}
357786
-
357786
-/**
357786
  * Length of a allocated file in bytes. Sparse files are counted by actual
357786
  * allocated space. Return < 0 if error or unknown.
357786
  */
357786
diff --git a/block/io.c b/block/io.c
357786
index 5c043a4..32a82e3 100644
357786
--- a/block/io.c
357786
+++ b/block/io.c
357786
@@ -2929,3 +2929,112 @@ int coroutine_fn bdrv_co_copy_range(BdrvChild *src, uint64_t src_offset,
357786
     bdrv_dec_in_flight(dst_bs);
357786
     return ret;
357786
 }
357786
+
357786
+static void bdrv_parent_cb_resize(BlockDriverState *bs)
357786
+{
357786
+    BdrvChild *c;
357786
+    QLIST_FOREACH(c, &bs->parents, next_parent) {
357786
+        if (c->role->resize) {
357786
+            c->role->resize(c);
357786
+        }
357786
+    }
357786
+}
357786
+
357786
+/**
357786
+ * Truncate file to 'offset' bytes (needed only for file protocols)
357786
+ */
357786
+int coroutine_fn bdrv_co_truncate(BdrvChild *child, int64_t offset,
357786
+                                  PreallocMode prealloc, Error **errp)
357786
+{
357786
+    BlockDriverState *bs = child->bs;
357786
+    BlockDriver *drv = bs->drv;
357786
+    int ret;
357786
+
357786
+    assert(child->perm & BLK_PERM_RESIZE);
357786
+
357786
+    /* if bs->drv == NULL, bs is closed, so there's nothing to do here */
357786
+    if (!drv) {
357786
+        error_setg(errp, "No medium inserted");
357786
+        return -ENOMEDIUM;
357786
+    }
357786
+    if (offset < 0) {
357786
+        error_setg(errp, "Image size cannot be negative");
357786
+        return -EINVAL;
357786
+    }
357786
+
357786
+    bdrv_inc_in_flight(bs);
357786
+
357786
+    if (!drv->bdrv_co_truncate) {
357786
+        if (bs->file && drv->is_filter) {
357786
+            ret = bdrv_co_truncate(bs->file, offset, prealloc, errp);
357786
+            goto out;
357786
+        }
357786
+        error_setg(errp, "Image format driver does not support resize");
357786
+        ret = -ENOTSUP;
357786
+        goto out;
357786
+    }
357786
+    if (bs->read_only) {
357786
+        error_setg(errp, "Image is read-only");
357786
+        ret = -EACCES;
357786
+        goto out;
357786
+    }
357786
+
357786
+    assert(!(bs->open_flags & BDRV_O_INACTIVE));
357786
+
357786
+    ret = drv->bdrv_co_truncate(bs, offset, prealloc, errp);
357786
+    if (ret < 0) {
357786
+        goto out;
357786
+    }
357786
+    ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
357786
+    if (ret < 0) {
357786
+        error_setg_errno(errp, -ret, "Could not refresh total sector count");
357786
+    } else {
357786
+        offset = bs->total_sectors * BDRV_SECTOR_SIZE;
357786
+    }
357786
+    bdrv_dirty_bitmap_truncate(bs, offset);
357786
+    bdrv_parent_cb_resize(bs);
357786
+    atomic_inc(&bs->write_gen);
357786
+
357786
+out:
357786
+    bdrv_dec_in_flight(bs);
357786
+    return ret;
357786
+}
357786
+
357786
+typedef struct TruncateCo {
357786
+    BdrvChild *child;
357786
+    int64_t offset;
357786
+    PreallocMode prealloc;
357786
+    Error **errp;
357786
+    int ret;
357786
+} TruncateCo;
357786
+
357786
+static void coroutine_fn bdrv_truncate_co_entry(void *opaque)
357786
+{
357786
+    TruncateCo *tco = opaque;
357786
+    tco->ret = bdrv_co_truncate(tco->child, tco->offset, tco->prealloc,
357786
+                                tco->errp);
357786
+}
357786
+
357786
+int bdrv_truncate(BdrvChild *child, int64_t offset, PreallocMode prealloc,
357786
+                  Error **errp)
357786
+{
357786
+    Coroutine *co;
357786
+    TruncateCo tco = {
357786
+        .child      = child,
357786
+        .offset     = offset,
357786
+        .prealloc   = prealloc,
357786
+        .errp       = errp,
357786
+        .ret        = NOT_DONE,
357786
+    };
357786
+
357786
+    if (qemu_in_coroutine()) {
357786
+        /* Fast-path if already in coroutine context */
357786
+        bdrv_truncate_co_entry(&tco;;
357786
+    } else {
357786
+        co = qemu_coroutine_create(bdrv_truncate_co_entry, &tco;;
357786
+        qemu_coroutine_enter(co);
357786
+        BDRV_POLL_WHILE(child->bs, tco.ret == NOT_DONE);
357786
+    }
357786
+
357786
+    return tco.ret;
357786
+}
357786
diff --git a/include/block/block_int.h b/include/block/block_int.h
357786
index 46b2f87..6a844ec 100644
357786
--- a/include/block/block_int.h
357786
+++ b/include/block/block_int.h
357786
@@ -1129,4 +1129,6 @@ int coroutine_fn bdrv_co_copy_range_to(BdrvChild *src, uint64_t src_offset,
357786
                                        BdrvChild *dst, uint64_t dst_offset,
357786
                                        uint64_t bytes, BdrvRequestFlags flags);
357786
 
357786
+int refresh_total_sectors(BlockDriverState *bs, int64_t hint);
357786
+
357786
 #endif /* BLOCK_INT_H */
357786
-- 
357786
1.8.3.1
357786