yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

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

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