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

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