yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-block-move-snapshot-code-in-block.c-to-block-snapsho.patch

218e99
From 94c066bcc75fa2e57d1bc861a7a65f21891425fe Mon Sep 17 00:00:00 2001
218e99
From: Max Reitz <mreitz@redhat.com>
218e99
Date: Mon, 4 Nov 2013 22:31:53 +0100
218e99
Subject: [PATCH 02/87] block: move snapshot code in block.c to block/snapshot.c
218e99
218e99
RH-Author: Max Reitz <mreitz@redhat.com>
218e99
Message-id: <1383604354-12743-3-git-send-email-mreitz@redhat.com>
218e99
Patchwork-id: 55302
218e99
O-Subject: [RHEL-7.0 qemu-kvm PATCH 02/43] block: move snapshot code in block.c to block/snapshot.c
218e99
Bugzilla: 1026524
218e99
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
218e99
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
218e99
RH-Acked-by: Fam Zheng <famz@redhat.com>
218e99
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
218e99
218e99
From: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
218e99
218e99
BZ: 1026524
218e99
218e99
All snapshot related code, except bdrv_snapshot_dump() and
218e99
bdrv_is_snapshot(), is moved to block/snapshot.c. bdrv_snapshot_dump()
218e99
will be moved to another file later. bdrv_is_snapshot() is not related
218e99
with internal snapshot. It also fixes small code style errors reported
218e99
by check script.
218e99
218e99
Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
218e99
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
218e99
(cherry picked from commit de08c606f9ddafe647b6843e2b10a6d6030b0fc0)
218e99
218e99
Signed-off-by: Max Reitz <mreitz@redhat.com>
218e99
---
218e99
 block.c                  | 100 ------------------------------
218e99
 block/Makefile.objs      |   1 +
218e99
 block/snapshot.c         | 157 +++++++++++++++++++++++++++++++++++++++++++++++
218e99
 include/block/block.h    |  26 ++------
218e99
 include/block/snapshot.h |  53 ++++++++++++++++
218e99
 savevm.c                 |  23 +------
218e99
 6 files changed, 217 insertions(+), 143 deletions(-)
218e99
 create mode 100644 block/snapshot.c
218e99
 create mode 100644 include/block/snapshot.h
218e99
218e99
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
218e99
---
218e99
 block.c                  |  100 -----------------------------
218e99
 block/Makefile.objs      |    1 +
218e99
 block/snapshot.c         |  157 ++++++++++++++++++++++++++++++++++++++++++++++
218e99
 include/block/block.h    |   26 ++------
218e99
 include/block/snapshot.h |   53 ++++++++++++++++
218e99
 savevm.c                 |   23 +-------
218e99
 6 files changed, 217 insertions(+), 143 deletions(-)
218e99
 create mode 100644 block/snapshot.c
218e99
 create mode 100644 include/block/snapshot.h
218e99
218e99
diff --git a/block.c b/block.c
218e99
index 84a3a8d..69b70d0 100644
218e99
--- a/block.c
218e99
+++ b/block.c
218e99
@@ -3458,111 +3458,11 @@ bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag)
218e99
     return false;
218e99
 }
218e99
 
218e99
-/**************************************************************/
218e99
-/* handling of snapshots */
218e99
-
218e99
-int bdrv_can_snapshot(BlockDriverState *bs)
218e99
-{
218e99
-    BlockDriver *drv = bs->drv;
218e99
-    if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
218e99
-        return 0;
218e99
-    }
218e99
-
218e99
-    if (!drv->bdrv_snapshot_create) {
218e99
-        if (bs->file != NULL) {
218e99
-            return bdrv_can_snapshot(bs->file);
218e99
-        }
218e99
-        return 0;
218e99
-    }
218e99
-
218e99
-    return 1;
218e99
-}
218e99
-
218e99
 int bdrv_is_snapshot(BlockDriverState *bs)
218e99
 {
218e99
     return !!(bs->open_flags & BDRV_O_SNAPSHOT);
218e99
 }
218e99
 
218e99
-int bdrv_snapshot_create(BlockDriverState *bs,
218e99
-                         QEMUSnapshotInfo *sn_info)
218e99
-{
218e99
-    BlockDriver *drv = bs->drv;
218e99
-    if (!drv)
218e99
-        return -ENOMEDIUM;
218e99
-    if (drv->bdrv_snapshot_create)
218e99
-        return drv->bdrv_snapshot_create(bs, sn_info);
218e99
-    if (bs->file)
218e99
-        return bdrv_snapshot_create(bs->file, sn_info);
218e99
-    return -ENOTSUP;
218e99
-}
218e99
-
218e99
-int bdrv_snapshot_goto(BlockDriverState *bs,
218e99
-                       const char *snapshot_id)
218e99
-{
218e99
-    BlockDriver *drv = bs->drv;
218e99
-    int ret, open_ret;
218e99
-
218e99
-    if (!drv)
218e99
-        return -ENOMEDIUM;
218e99
-    if (drv->bdrv_snapshot_goto)
218e99
-        return drv->bdrv_snapshot_goto(bs, snapshot_id);
218e99
-
218e99
-    if (bs->file) {
218e99
-        drv->bdrv_close(bs);
218e99
-        ret = bdrv_snapshot_goto(bs->file, snapshot_id);
218e99
-        open_ret = drv->bdrv_open(bs, NULL, bs->open_flags);
218e99
-        if (open_ret < 0) {
218e99
-            bdrv_delete(bs->file);
218e99
-            bs->drv = NULL;
218e99
-            return open_ret;
218e99
-        }
218e99
-        return ret;
218e99
-    }
218e99
-
218e99
-    return -ENOTSUP;
218e99
-}
218e99
-
218e99
-int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
218e99
-{
218e99
-    BlockDriver *drv = bs->drv;
218e99
-    if (!drv)
218e99
-        return -ENOMEDIUM;
218e99
-    if (drv->bdrv_snapshot_delete)
218e99
-        return drv->bdrv_snapshot_delete(bs, snapshot_id);
218e99
-    if (bs->file)
218e99
-        return bdrv_snapshot_delete(bs->file, snapshot_id);
218e99
-    return -ENOTSUP;
218e99
-}
218e99
-
218e99
-int bdrv_snapshot_list(BlockDriverState *bs,
218e99
-                       QEMUSnapshotInfo **psn_info)
218e99
-{
218e99
-    BlockDriver *drv = bs->drv;
218e99
-    if (!drv)
218e99
-        return -ENOMEDIUM;
218e99
-    if (drv->bdrv_snapshot_list)
218e99
-        return drv->bdrv_snapshot_list(bs, psn_info);
218e99
-    if (bs->file)
218e99
-        return bdrv_snapshot_list(bs->file, psn_info);
218e99
-    return -ENOTSUP;
218e99
-}
218e99
-
218e99
-int bdrv_snapshot_load_tmp(BlockDriverState *bs,
218e99
-        const char *snapshot_name)
218e99
-{
218e99
-    BlockDriver *drv = bs->drv;
218e99
-    if (!drv) {
218e99
-        return -ENOMEDIUM;
218e99
-    }
218e99
-    if (!bs->read_only) {
218e99
-        return -EINVAL;
218e99
-    }
218e99
-    if (drv->bdrv_snapshot_load_tmp) {
218e99
-        return drv->bdrv_snapshot_load_tmp(bs, snapshot_name);
218e99
-    }
218e99
-    return -ENOTSUP;
218e99
-}
218e99
-
218e99
 /* backing_file can either be relative, or absolute, or a protocol.  If it is
218e99
  * relative, it must be relative to the chain.  So, passing in bs->filename
218e99
  * from a BDS as backing_file should not be done, as that may be relative to
218e99
diff --git a/block/Makefile.objs b/block/Makefile.objs
218e99
index 41a284b..162f270 100644
218e99
--- a/block/Makefile.objs
218e99
+++ b/block/Makefile.objs
218e99
@@ -4,6 +4,7 @@ block-obj-y += qed.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o
218e99
 block-obj-y += qed-check.o
218e99
 block-obj-y += vhdx.o
218e99
 block-obj-y += parallels.o blkdebug.o blkverify.o
218e99
+block-obj-y += snapshot.o
218e99
 block-obj-$(CONFIG_WIN32) += raw-win32.o win32-aio.o
218e99
 block-obj-$(CONFIG_POSIX) += raw-posix.o
218e99
 block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
218e99
diff --git a/block/snapshot.c b/block/snapshot.c
218e99
new file mode 100644
218e99
index 0000000..6c6d9de
218e99
--- /dev/null
218e99
+++ b/block/snapshot.c
218e99
@@ -0,0 +1,157 @@
218e99
+/*
218e99
+ * Block layer snapshot related functions
218e99
+ *
218e99
+ * Copyright (c) 2003-2008 Fabrice Bellard
218e99
+ *
218e99
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
218e99
+ * of this software and associated documentation files (the "Software"), to deal
218e99
+ * in the Software without restriction, including without limitation the rights
218e99
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
218e99
+ * copies of the Software, and to permit persons to whom the Software is
218e99
+ * furnished to do so, subject to the following conditions:
218e99
+ *
218e99
+ * The above copyright notice and this permission notice shall be included in
218e99
+ * all copies or substantial portions of the Software.
218e99
+ *
218e99
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
218e99
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
218e99
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
218e99
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
218e99
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
218e99
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
218e99
+ * THE SOFTWARE.
218e99
+ */
218e99
+
218e99
+#include "block/snapshot.h"
218e99
+#include "block/block_int.h"
218e99
+
218e99
+int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
218e99
+                       const char *name)
218e99
+{
218e99
+    QEMUSnapshotInfo *sn_tab, *sn;
218e99
+    int nb_sns, i, ret;
218e99
+
218e99
+    ret = -ENOENT;
218e99
+    nb_sns = bdrv_snapshot_list(bs, &sn_tab);
218e99
+    if (nb_sns < 0) {
218e99
+        return ret;
218e99
+    }
218e99
+    for (i = 0; i < nb_sns; i++) {
218e99
+        sn = &sn_tab[i];
218e99
+        if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
218e99
+            *sn_info = *sn;
218e99
+            ret = 0;
218e99
+            break;
218e99
+        }
218e99
+    }
218e99
+    g_free(sn_tab);
218e99
+    return ret;
218e99
+}
218e99
+
218e99
+int bdrv_can_snapshot(BlockDriverState *bs)
218e99
+{
218e99
+    BlockDriver *drv = bs->drv;
218e99
+    if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
218e99
+        return 0;
218e99
+    }
218e99
+
218e99
+    if (!drv->bdrv_snapshot_create) {
218e99
+        if (bs->file != NULL) {
218e99
+            return bdrv_can_snapshot(bs->file);
218e99
+        }
218e99
+        return 0;
218e99
+    }
218e99
+
218e99
+    return 1;
218e99
+}
218e99
+
218e99
+int bdrv_snapshot_create(BlockDriverState *bs,
218e99
+                         QEMUSnapshotInfo *sn_info)
218e99
+{
218e99
+    BlockDriver *drv = bs->drv;
218e99
+    if (!drv) {
218e99
+        return -ENOMEDIUM;
218e99
+    }
218e99
+    if (drv->bdrv_snapshot_create) {
218e99
+        return drv->bdrv_snapshot_create(bs, sn_info);
218e99
+    }
218e99
+    if (bs->file) {
218e99
+        return bdrv_snapshot_create(bs->file, sn_info);
218e99
+    }
218e99
+    return -ENOTSUP;
218e99
+}
218e99
+
218e99
+int bdrv_snapshot_goto(BlockDriverState *bs,
218e99
+                       const char *snapshot_id)
218e99
+{
218e99
+    BlockDriver *drv = bs->drv;
218e99
+    int ret, open_ret;
218e99
+
218e99
+    if (!drv) {
218e99
+        return -ENOMEDIUM;
218e99
+    }
218e99
+    if (drv->bdrv_snapshot_goto) {
218e99
+        return drv->bdrv_snapshot_goto(bs, snapshot_id);
218e99
+    }
218e99
+
218e99
+    if (bs->file) {
218e99
+        drv->bdrv_close(bs);
218e99
+        ret = bdrv_snapshot_goto(bs->file, snapshot_id);
218e99
+        open_ret = drv->bdrv_open(bs, NULL, bs->open_flags);
218e99
+        if (open_ret < 0) {
218e99
+            bdrv_delete(bs->file);
218e99
+            bs->drv = NULL;
218e99
+            return open_ret;
218e99
+        }
218e99
+        return ret;
218e99
+    }
218e99
+
218e99
+    return -ENOTSUP;
218e99
+}
218e99
+
218e99
+int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
218e99
+{
218e99
+    BlockDriver *drv = bs->drv;
218e99
+    if (!drv) {
218e99
+        return -ENOMEDIUM;
218e99
+    }
218e99
+    if (drv->bdrv_snapshot_delete) {
218e99
+        return drv->bdrv_snapshot_delete(bs, snapshot_id);
218e99
+    }
218e99
+    if (bs->file) {
218e99
+        return bdrv_snapshot_delete(bs->file, snapshot_id);
218e99
+    }
218e99
+    return -ENOTSUP;
218e99
+}
218e99
+
218e99
+int bdrv_snapshot_list(BlockDriverState *bs,
218e99
+                       QEMUSnapshotInfo **psn_info)
218e99
+{
218e99
+    BlockDriver *drv = bs->drv;
218e99
+    if (!drv) {
218e99
+        return -ENOMEDIUM;
218e99
+    }
218e99
+    if (drv->bdrv_snapshot_list) {
218e99
+        return drv->bdrv_snapshot_list(bs, psn_info);
218e99
+    }
218e99
+    if (bs->file) {
218e99
+        return bdrv_snapshot_list(bs->file, psn_info);
218e99
+    }
218e99
+    return -ENOTSUP;
218e99
+}
218e99
+
218e99
+int bdrv_snapshot_load_tmp(BlockDriverState *bs,
218e99
+        const char *snapshot_name)
218e99
+{
218e99
+    BlockDriver *drv = bs->drv;
218e99
+    if (!drv) {
218e99
+        return -ENOMEDIUM;
218e99
+    }
218e99
+    if (!bs->read_only) {
218e99
+        return -EINVAL;
218e99
+    }
218e99
+    if (drv->bdrv_snapshot_load_tmp) {
218e99
+        return drv->bdrv_snapshot_load_tmp(bs, snapshot_name);
218e99
+    }
218e99
+    return -ENOTSUP;
218e99
+}
218e99
diff --git a/include/block/block.h b/include/block/block.h
218e99
index 3b3f40e..03ebc47 100644
218e99
--- a/include/block/block.h
218e99
+++ b/include/block/block.h
218e99
@@ -7,6 +7,11 @@
218e99
 #include "block/coroutine.h"
218e99
 #include "qapi/qmp/qobject.h"
218e99
 #include "qapi-types.h"
218e99
+/*
218e99
+ * snapshot.h is needed since bdrv_snapshot_dump(), it can be removed when the
218e99
+ * function is moved to other file.
218e99
+ */
218e99
+#include "block/snapshot.h"
218e99
 
218e99
 /* block.c */
218e99
 typedef struct BlockDriver BlockDriver;
218e99
@@ -27,17 +32,6 @@ typedef struct BlockFragInfo {
218e99
     uint64_t compressed_clusters;
218e99
 } BlockFragInfo;
218e99
 
218e99
-typedef struct QEMUSnapshotInfo {
218e99
-    char id_str[128]; /* unique snapshot id */
218e99
-    /* the following fields are informative. They are not needed for
218e99
-       the consistency of the snapshot */
218e99
-    char name[256]; /* user chosen name */
218e99
-    uint64_t vm_state_size; /* VM state info size */
218e99
-    uint32_t date_sec; /* UTC date of the snapshot */
218e99
-    uint32_t date_nsec;
218e99
-    uint64_t vm_clock_nsec; /* VM clock relative to boot */
218e99
-} QEMUSnapshotInfo;
218e99
-
218e99
 /* Callbacks for block device models */
218e99
 typedef struct BlockDevOps {
218e99
     /*
218e99
@@ -360,17 +354,7 @@ void bdrv_get_full_backing_filename(BlockDriverState *bs,
218e99
                                     char *dest, size_t sz);
218e99
 BlockInfo *bdrv_query_info(BlockDriverState *s);
218e99
 BlockStats *bdrv_query_stats(const BlockDriverState *bs);
218e99
-int bdrv_can_snapshot(BlockDriverState *bs);
218e99
 int bdrv_is_snapshot(BlockDriverState *bs);
218e99
-int bdrv_snapshot_create(BlockDriverState *bs,
218e99
-                         QEMUSnapshotInfo *sn_info);
218e99
-int bdrv_snapshot_goto(BlockDriverState *bs,
218e99
-                       const char *snapshot_id);
218e99
-int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
218e99
-int bdrv_snapshot_list(BlockDriverState *bs,
218e99
-                       QEMUSnapshotInfo **psn_info);
218e99
-int bdrv_snapshot_load_tmp(BlockDriverState *bs,
218e99
-                           const char *snapshot_name);
218e99
 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn);
218e99
 
218e99
 char *get_human_readable_size(char *buf, int buf_size, int64_t size);
218e99
diff --git a/include/block/snapshot.h b/include/block/snapshot.h
218e99
new file mode 100644
218e99
index 0000000..eaf61f0
218e99
--- /dev/null
218e99
+++ b/include/block/snapshot.h
218e99
@@ -0,0 +1,53 @@
218e99
+/*
218e99
+ * Block layer snapshot related functions
218e99
+ *
218e99
+ * Copyright (c) 2003-2008 Fabrice Bellard
218e99
+ *
218e99
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
218e99
+ * of this software and associated documentation files (the "Software"), to deal
218e99
+ * in the Software without restriction, including without limitation the rights
218e99
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
218e99
+ * copies of the Software, and to permit persons to whom the Software is
218e99
+ * furnished to do so, subject to the following conditions:
218e99
+ *
218e99
+ * The above copyright notice and this permission notice shall be included in
218e99
+ * all copies or substantial portions of the Software.
218e99
+ *
218e99
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
218e99
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
218e99
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
218e99
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
218e99
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
218e99
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
218e99
+ * THE SOFTWARE.
218e99
+ */
218e99
+
218e99
+#ifndef SNAPSHOT_H
218e99
+#define SNAPSHOT_H
218e99
+
218e99
+#include "qemu-common.h"
218e99
+
218e99
+typedef struct QEMUSnapshotInfo {
218e99
+    char id_str[128]; /* unique snapshot id */
218e99
+    /* the following fields are informative. They are not needed for
218e99
+       the consistency of the snapshot */
218e99
+    char name[256]; /* user chosen name */
218e99
+    uint64_t vm_state_size; /* VM state info size */
218e99
+    uint32_t date_sec; /* UTC date of the snapshot */
218e99
+    uint32_t date_nsec;
218e99
+    uint64_t vm_clock_nsec; /* VM clock relative to boot */
218e99
+} QEMUSnapshotInfo;
218e99
+
218e99
+int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
218e99
+                       const char *name);
218e99
+int bdrv_can_snapshot(BlockDriverState *bs);
218e99
+int bdrv_snapshot_create(BlockDriverState *bs,
218e99
+                         QEMUSnapshotInfo *sn_info);
218e99
+int bdrv_snapshot_goto(BlockDriverState *bs,
218e99
+                       const char *snapshot_id);
218e99
+int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
218e99
+int bdrv_snapshot_list(BlockDriverState *bs,
218e99
+                       QEMUSnapshotInfo **psn_info);
218e99
+int bdrv_snapshot_load_tmp(BlockDriverState *bs,
218e99
+                           const char *snapshot_name);
218e99
+#endif
218e99
diff --git a/savevm.c b/savevm.c
218e99
index d9f3787..005bf35 100644
218e99
--- a/savevm.c
218e99
+++ b/savevm.c
218e99
@@ -40,6 +40,7 @@
218e99
 #include "trace.h"
218e99
 #include "qemu/bitops.h"
218e99
 #include "qemu/iov.h"
218e99
+#include "block/snapshot.h"
218e99
 
218e99
 #define SELF_ANNOUNCE_ROUNDS 5
218e99
 
218e99
@@ -2273,28 +2274,6 @@ static BlockDriverState *find_vmstate_bs(void)
218e99
     return NULL;
218e99
 }
218e99
 
218e99
-static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
218e99
-                              const char *name)
218e99
-{
218e99
-    QEMUSnapshotInfo *sn_tab, *sn;
218e99
-    int nb_sns, i, ret;
218e99
-
218e99
-    ret = -ENOENT;
218e99
-    nb_sns = bdrv_snapshot_list(bs, &sn_tab);
218e99
-    if (nb_sns < 0)
218e99
-        return ret;
218e99
-    for(i = 0; i < nb_sns; i++) {
218e99
-        sn = &sn_tab[i];
218e99
-        if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
218e99
-            *sn_info = *sn;
218e99
-            ret = 0;
218e99
-            break;
218e99
-        }
218e99
-    }
218e99
-    g_free(sn_tab);
218e99
-    return ret;
218e99
-}
218e99
-
218e99
 /*
218e99
  * Deletes snapshots of a given name in all opened images.
218e99
  */
218e99
-- 
218e99
1.7.1
218e99