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

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