]> git.proxmox.com Git - pve-qemu-kvm.git/blobdiff - debian/patches/internal-snapshot-async.patch
refresh and enable debian/patches/internal-snapshot-async.patch
[pve-qemu-kvm.git] / debian / patches / internal-snapshot-async.patch
index 6c86de3a6160c58d77baa41a7774c4a80e63639e..ef6d62dafe94f4d436cd9ccfcd78b3ad7817af72 100644 (file)
-Index: new/qapi-schema.json
+From 46fd4bb673a91d40352c95e9d3f62f63b5021053 Mon Sep 17 00:00:00 2001
+From: Stefan Priebe <s.priebe@profihost.ag>
+Date: Fri, 29 Nov 2013 22:17:03 +0100
+Subject: [PATCH] internal-snapshot-async-qemu1.7.patch
+
+---
+ Makefile.objs           |    1 +
+ block.c                 |    2 +-
+ hmp-commands.hx         |   34 ++++
+ hmp.c                   |   57 ++++++
+ hmp.h                   |    5 +
+ include/block/block.h   |    1 +
+ include/sysemu/sysemu.h |    5 +-
+ monitor.c               |    7 +
+ qapi-schema.json        |   46 +++++
+ qemu-options.hx         |   13 ++
+ qmp-commands.hx         |   31 +++
+ savevm-async.c          |  478 +++++++++++++++++++++++++++++++++++++++++++++++
+ savevm.c                |   10 +-
+ vl.c                    |    9 +
+ 14 files changed, 692 insertions(+), 7 deletions(-)
+ create mode 100644 savevm-async.c
+
+Index: new/Makefile.objs
 ===================================================================
---- new.orig/qapi-schema.json  2012-11-23 07:30:15.000000000 +0100
-+++ new/qapi-schema.json       2012-11-23 07:31:44.000000000 +0100
-@@ -461,6 +461,40 @@
-            '*downtime': 'int'} }
+--- new.orig/Makefile.objs     2014-05-05 09:49:29.000000000 +0200
++++ new/Makefile.objs  2014-05-05 09:54:17.000000000 +0200
+@@ -55,6 +55,7 @@
+ common-obj-y += qemu-char.o #aio.o
+ common-obj-y += block-migration.o
+ common-obj-y += page_cache.o xbzrle.o
++common-obj-y += savevm-async.o
  
- ##
-+# @SaveVMInfo
-+#
-+# Information about current migration process.
-+#
-+# @status: #optional string describing the current savevm status.
-+#          This can be 'active', 'completed', 'failed'.
-+#          If this field is not returned, no savevm process
-+#          has been initiated
-+#
-+# @error: #optional string containing error message is status is failed.
-+#
-+# @total-time: #optional total amount of milliseconds since savevm started.
-+#        If savevm has ended, it returns the total save time
-+#
-+# @bytes: #optional total amount of data transfered
-+#
-+# Since: 1.3
-+##
-+{ 'type': 'SaveVMInfo',
-+  'data': {'*status': 'str', '*error': 'str',
-+           '*total-time': 'int', '*bytes': 'int'} }
-+
-+##
-+# @query-savevm
-+#
-+# Returns information about current savevm process.
-+#
-+# Returns: @SaveVMInfo
-+#
-+# Since: 1.3
-+##
-+{ 'command': 'query-savevm', 'returns': 'SaveVMInfo' }
-+
-+##
- # @query-migrate
- #
- # Returns information about current migration process.
-@@ -2902,6 +2936,14 @@
- ##
- { 'command': 'query-target', 'returns': 'TargetInfo' }
+ common-obj-$(CONFIG_POSIX) += migration-exec.o migration-unix.o migration-fd.o
  
-+{ 'command': 'savevm-start' 'data': { '*statefile': 'str' } }
-+
-+{ 'command': 'snapshot-drive', 'data': { 'device': 'str', 'name': 'str' } }
-+
-+{ 'command': 'delete-drive-snapshot', 'data': { 'device': 'str', 'name': 'str' } }
-+
-+{ 'command': 'savevm-end' }
-+
- ##
- # @QKeyCode:
- #
-Index: new/qmp-commands.hx
+Index: new/block.c
 ===================================================================
---- new.orig/qmp-commands.hx   2012-11-23 07:30:15.000000000 +0100
-+++ new/qmp-commands.hx        2012-11-23 07:31:44.000000000 +0100
-@@ -2654,3 +2654,34 @@
-         .args_type  = "",
-         .mhandler.cmd_new = qmp_marshal_input_query_target,
-     },
+--- new.orig/block.c   2014-05-05 09:49:29.000000000 +0200
++++ new/block.c        2014-05-05 09:54:17.000000000 +0200
+@@ -1967,7 +1967,7 @@
+             bs_new->drv ? bs_new->drv->format_name : "");
+ }
+-static void bdrv_delete(BlockDriverState *bs)
++void bdrv_delete(BlockDriverState *bs)
+ {
+     assert(!bs->dev);
+     assert(!bs->job);
+Index: new/hmp-commands.hx
+===================================================================
+--- new.orig/hmp-commands.hx   2014-05-05 09:49:29.000000000 +0200
++++ new/hmp-commands.hx        2014-05-05 09:54:17.000000000 +0200
+@@ -1777,6 +1777,8 @@
+ show current migration capabilities
+ @item info migrate_cache_size
+ show current migration XBZRLE cache size
++@item info savevm
++show savevm status
+ @item info balloon
+ show balloon information
+ @item info qtree
+@@ -1798,3 +1800,35 @@
+ STEXI
+ @end table
+ ETEXI
 +
 +    {
 +        .name       = "savevm-start",
 +        .args_type  = "statefile:s?",
-+        .mhandler.cmd_new = qmp_marshal_input_savevm_start,
++        .params     = "[statefile]",
++        .help       = "Prepare for snapshot and halt VM. Save VM state to statefile.",
++        .mhandler.cmd = hmp_savevm_start,
 +    },
 +
 +    {
 +        .name       = "snapshot-drive",
 +        .args_type  = "device:s,name:s",
-+        .mhandler.cmd_new = qmp_marshal_input_snapshot_drive,
++        .params     = "device name",
++        .help       = "Create internal snapshot.",
++        .mhandler.cmd = hmp_snapshot_drive,
 +    },
 +
 +    {
 +        .name       = "delete-drive-snapshot",
 +        .args_type  = "device:s,name:s",
-+        .mhandler.cmd_new = qmp_marshal_input_delete_drive_snapshot,
++        .params     = "device name",
++        .help       = "Delete internal snapshot.",
++        .mhandler.cmd = hmp_delete_drive_snapshot,
 +    },
 +
 +    {
 +        .name       = "savevm-end",
 +        .args_type  = "",
-+        .mhandler.cmd_new = qmp_marshal_input_savevm_end,
-+    },
-+
-+    {
-+        .name       = "query-savevm",
-+        .args_type  = "",
-+        .mhandler.cmd_new = qmp_marshal_input_query_savevm,
++        .params     = "",
++        .help       = "Resume VM after snaphot.",
++        .mhandler.cmd = hmp_savevm_end,
 +    },
-+
 Index: new/hmp.c
 ===================================================================
---- new.orig/hmp.c     2012-11-23 07:30:15.000000000 +0100
-+++ new/hmp.c  2012-11-23 07:31:44.000000000 +0100
-@@ -1335,3 +1335,60 @@
-     qmp_nbd_server_stop(&errp);
-     hmp_handle_error(mon, &errp);
+--- new.orig/hmp.c     2014-05-05 09:49:29.000000000 +0200
++++ new/hmp.c  2014-05-05 09:54:17.000000000 +0200
+@@ -1738,3 +1738,60 @@
+     qmp_object_del(id, &err);
+     hmp_handle_error(mon, &err);
  }
 +
 +void hmp_savevm_start(Monitor *mon, const QDict *qdict)
@@ -143,7 +141,7 @@ Index: new/hmp.c
 +    hmp_handle_error(mon, &errp);
 +}
 +
-+void hmp_info_savevm(Monitor *mon)
++void hmp_info_savevm(Monitor *mon, const QDict *qdict)
 +{
 +    SaveVMInfo *info;
 +    info = qmp_query_savevm(NULL);
@@ -164,17 +162,17 @@ Index: new/hmp.c
 +}
 Index: new/hmp.h
 ===================================================================
---- new.orig/hmp.h     2012-11-23 07:30:15.000000000 +0100
-+++ new/hmp.h  2012-11-23 07:31:44.000000000 +0100
+--- new.orig/hmp.h     2014-05-05 09:49:29.000000000 +0200
++++ new/hmp.h  2014-05-05 09:54:17.000000000 +0200
 @@ -25,6 +25,7 @@
- void hmp_info_uuid(Monitor *mon);
- void hmp_info_chardev(Monitor *mon);
- void hmp_info_mice(Monitor *mon);
-+void hmp_info_savevm(Monitor *mon);
- void hmp_info_migrate(Monitor *mon);
- void hmp_info_migrate_capabilities(Monitor *mon);
- void hmp_info_migrate_cache_size(Monitor *mon);
-@@ -75,6 +76,10 @@
+ void hmp_info_uuid(Monitor *mon, const QDict *qdict);
+ void hmp_info_chardev(Monitor *mon, const QDict *qdict);
+ void hmp_info_mice(Monitor *mon, const QDict *qdict);
++void hmp_info_savevm(Monitor *mon, const QDict *qdict);
+ void hmp_info_migrate(Monitor *mon, const QDict *qdict);
+ void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict);
+ void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict);
+@@ -84,6 +85,10 @@
  void hmp_netdev_del(Monitor *mon, const QDict *qdict);
  void hmp_getfd(Monitor *mon, const QDict *qdict);
  void hmp_closefd(Monitor *mon, const QDict *qdict);
@@ -185,68 +183,206 @@ Index: new/hmp.h
  void hmp_send_key(Monitor *mon, const QDict *qdict);
  void hmp_screen_dump(Monitor *mon, const QDict *qdict);
  void hmp_nbd_server_start(Monitor *mon, const QDict *qdict);
-Index: new/hmp-commands.hx
+Index: new/include/block/block.h
 ===================================================================
---- new.orig/hmp-commands.hx   2012-11-23 07:30:15.000000000 +0100
-+++ new/hmp-commands.hx        2012-11-23 07:31:44.000000000 +0100
-@@ -1562,6 +1562,8 @@
- show current migration capabilities
- @item info migrate_cache_size
- show current migration XBZRLE cache size
-+@item info savevm
-+show savevm status
- @item info balloon
- show balloon information
- @item info qtree
-@@ -1581,3 +1583,35 @@
- STEXI
- @end table
+--- new.orig/include/block/block.h     2014-05-05 09:49:29.000000000 +0200
++++ new/include/block/block.h  2014-05-05 09:54:17.000000000 +0200
+@@ -251,6 +251,7 @@
+     const char *backing_file);
+ int bdrv_get_backing_file_depth(BlockDriverState *bs);
+ int bdrv_truncate(BlockDriverState *bs, int64_t offset);
++void bdrv_delete(BlockDriverState *bs);
+ int64_t bdrv_getlength(BlockDriverState *bs);
+ int64_t bdrv_get_allocated_file_size(BlockDriverState *bs);
+ void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr);
+Index: new/include/sysemu/sysemu.h
+===================================================================
+--- new.orig/include/sysemu/sysemu.h   2014-05-05 09:49:29.000000000 +0200
++++ new/include/sysemu/sysemu.h        2014-05-05 09:54:17.000000000 +0200
+@@ -73,16 +73,17 @@
+ void do_savevm(Monitor *mon, const QDict *qdict);
+ int load_vmstate(const char *name);
++int load_state_from_blockdev(const char *filename);
+ void do_delvm(Monitor *mon, const QDict *qdict);
+ void do_info_snapshots(Monitor *mon, const QDict *qdict);
+ void qemu_announce_self(void);
+ bool qemu_savevm_state_blocked(Error **errp);
+-void qemu_savevm_state_begin(QEMUFile *f,
++int qemu_savevm_state_begin(QEMUFile *f,
+                              const MigrationParams *params);
+ int qemu_savevm_state_iterate(QEMUFile *f);
+-void qemu_savevm_state_complete(QEMUFile *f);
++int qemu_savevm_state_complete(QEMUFile *f);
+ void qemu_savevm_state_cancel(void);
+ uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size);
+ int qemu_loadvm_state(QEMUFile *f);
+Index: new/monitor.c
+===================================================================
+--- new.orig/monitor.c 2014-05-05 09:49:29.000000000 +0200
++++ new/monitor.c      2014-05-05 09:54:17.000000000 +0200
+@@ -2931,6 +2931,13 @@
+         .mhandler.cmd = hmp_info_migrate_cache_size,
+     },
+     {
++        .name       = "savevm",
++        .args_type  = "",
++        .params     = "",
++        .help       = "show savevm status",
++        .mhandler.cmd = hmp_info_savevm,
++    },
++    {
+         .name       = "balloon",
+         .args_type  = "",
+         .params     = "",
+Index: new/qapi-schema.json
+===================================================================
+--- new.orig/qapi-schema.json  2014-05-05 09:49:29.000000000 +0200
++++ new/qapi-schema.json       2014-05-05 09:54:17.000000000 +0200
+@@ -820,6 +820,42 @@
+            '*downtime': 'int',
+            '*setup-time': 'int'} }
++
++# @SaveVMInfo
++#
++# Information about current migration process.
++#
++# @status: #optional string describing the current savevm status.
++#          This can be 'active', 'completed', 'failed'.
++#          If this field is not returned, no savevm process
++#          has been initiated
++#
++# @error: #optional string containing error message is status is failed.
++#
++# @total-time: #optional total amount of milliseconds since savevm started.
++#        If savevm has ended, it returns the total save time
++#
++# @bytes: #optional total amount of data transfered
++#
++# Since: 1.3
++##
++{ 'type': 'SaveVMInfo',
++  'data': {'*status': 'str', '*error': 'str',
++           '*total-time': 'int', '*bytes': 'int'} }
++
++##
++# @query-savevm
++#
++# Returns information about current savevm process.
++#
++# Returns: @SaveVMInfo
++#
++# Since: 1.3
++##
++{ 'command': 'query-savevm', 'returns': 'SaveVMInfo' }
++
++##
++
+ ##
+ # @query-migrate
+ #
+@@ -3687,8 +3723,18 @@
+ #
+ # Since: 1.2.0
+ ##
++
+ { 'command': 'query-target', 'returns': 'TargetInfo' }
++{ 'command': 'savevm-start', 'data': { '*statefile': 'str' } }
++
++{ 'command': 'snapshot-drive', 'data': { 'device': 'str', 'name': 'str' } }
++
++{ 'command': 'delete-drive-snapshot', 'data': { 'device': 'str', 'name': 'str' } }
++
++{ 'command': 'savevm-end' }
++
++
+ ##
+ # @QKeyCode:
+ #
+Index: new/qemu-options.hx
+===================================================================
+--- new.orig/qemu-options.hx   2014-05-05 09:49:29.000000000 +0200
++++ new/qemu-options.hx        2014-05-05 09:54:17.000000000 +0200
+@@ -2759,6 +2759,19 @@
+ Start right away with a saved state (@code{loadvm} in monitor)
  ETEXI
++DEF("loadstate", HAS_ARG, QEMU_OPTION_loadstate, \
++    "-loadstate file\n" \
++    "                start right away with a saved state\n",
++    QEMU_ARCH_ALL)
++STEXI
++@item -loadstate @var{file}
++@findex -loadstate
++Start right away with a saved state. This option does not rollback
++disk state like @code{loadvm}, so user must make sure that disk
++have correct state. @var{file} can be any valid device URL. See the section
++for "Device URL Syntax" for more information.
++ETEXI
++
+ #ifndef _WIN32
+ DEF("daemonize", 0, QEMU_OPTION_daemonize, \
+     "-daemonize      daemonize QEMU after initializing\n", QEMU_ARCH_ALL)
+Index: new/qmp-commands.hx
+===================================================================
+--- new.orig/qmp-commands.hx   2014-05-05 09:49:29.000000000 +0200
++++ new/qmp-commands.hx        2014-05-05 09:54:17.000000000 +0200
+@@ -3593,3 +3593,34 @@
+                    } } ] }
+ EQMP
++
 +
 +    {
 +        .name       = "savevm-start",
 +        .args_type  = "statefile:s?",
-+        .params     = "[statefile]",
-+        .help       = "Prepare for snapshot and halt VM. Save VM state to statefile.",
-+        .mhandler.cmd = hmp_savevm_start,
++        .mhandler.cmd_new = qmp_marshal_input_savevm_start,
 +    },
 +
 +    {
 +        .name       = "snapshot-drive",
 +        .args_type  = "device:s,name:s",
-+        .params     = "device name",
-+        .help       = "Create internal snapshot.",
-+        .mhandler.cmd = hmp_snapshot_drive,
++        .mhandler.cmd_new = qmp_marshal_input_snapshot_drive,
 +    },
 +
 +    {
 +        .name       = "delete-drive-snapshot",
 +        .args_type  = "device:s,name:s",
-+        .params     = "device name",
-+        .help       = "Delete internal snapshot.",
-+        .mhandler.cmd = hmp_delete_drive_snapshot,
++        .mhandler.cmd_new = qmp_marshal_input_delete_drive_snapshot,
 +    },
 +
 +    {
 +        .name       = "savevm-end",
 +        .args_type  = "",
-+        .params     = "",
-+        .help       = "Resume VM after snaphot.",
-+        .mhandler.cmd = hmp_savevm_end,
++        .mhandler.cmd_new = qmp_marshal_input_savevm_end,
++    },
++
++    {
++        .name       = "query-savevm",
++        .args_type  = "",
++        .mhandler.cmd_new = qmp_marshal_input_query_savevm,
 +    },
 Index: new/savevm-async.c
 ===================================================================
 --- /dev/null  1970-01-01 00:00:00.000000000 +0000
-+++ new/savevm-async.c 2012-11-23 07:32:48.000000000 +0100
-@@ -0,0 +1,458 @@
++++ new/savevm-async.c 2014-05-05 09:55:53.000000000 +0200
+@@ -0,0 +1,478 @@
 +#include "qemu-common.h"
-+#include "qerror.h"
-+#include "sysemu.h"
++#include "qapi/qmp/qerror.h"
++#include "sysemu/sysemu.h"
 +#include "qmp-commands.h"
-+#include "blockdev.h"
-+#include "qemu/qom-qobject.h"
-+#include "buffered_file.h"
-+#include "migration.h"
++#include "qemu-options.h"
++#include "migration/qemu-file.h"
++#include "qom/qom-qobject.h"
++#include "migration/migration.h"
++#include "block/snapshot.h"
++#include "block/qapi.h"
++#include "block/block.h"
++#include "qemu/timer.h"
 +
 +/* #define DEBUG_SAVEVM_STATE */
 +
@@ -265,6 +401,7 @@ Index: new/savevm-async.c
 +    SAVE_STATE_COMPLETED,
 +};
 +
++
 +static struct SnapshotState {
 +    BlockDriverState *bs;
 +    size_t bs_pos;
@@ -298,7 +435,7 @@ Index: new/savevm-async.c
 +            info->has_status = true;
 +            info->status = g_strdup("active");
 +            info->has_total_time = true;
-+            info->total_time = qemu_get_clock_ms(rt_clock)
++            info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
 +                - s->total_time;
 +            break;
 +        case SAVE_STATE_COMPLETED:
@@ -319,7 +456,7 @@ Index: new/savevm-async.c
 +
 +    DPRINTF("save_snapshot_cleanup\n");
 +
-+    snap_state.total_time = qemu_get_clock_ms(rt_clock) -
++    snap_state.total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
 +        snap_state.total_time;
 +
 +    if (snap_state.file) {
@@ -332,7 +469,8 @@ Index: new/savevm-async.c
 +         */
 +        size_t size = (snap_state.bs_pos + BDRV_SECTOR_SIZE) & BDRV_SECTOR_MASK;
 +        bdrv_truncate(snap_state.bs, size);
-+        bdrv_delete(snap_state.bs);
++        bdrv_set_in_use(snap_state.bs, 0);
++        bdrv_unref(snap_state.bs);
 +        snap_state.bs = NULL;
 +    }
 +
@@ -395,7 +533,6 @@ Index: new/savevm-async.c
 +static void process_savevm_co(void *opaque)
 +{
 +    int ret;
-+    uint64_t remaining;
 +    int64_t maxlen;
 +    MigrationParams params = {
 +        .blk = 0,
@@ -404,48 +541,48 @@ Index: new/savevm-async.c
 +
 +    snap_state.state = SAVE_STATE_ACTIVE;
 +
++    qemu_mutex_unlock_iothread();
 +    ret = qemu_savevm_state_begin(snap_state.file, &params);
++    qemu_mutex_lock_iothread();
++
 +    if (ret < 0) {
 +        save_snapshot_error("qemu_savevm_state_begin failed");
 +        return;
 +    }
 +
 +    while (snap_state.state == SAVE_STATE_ACTIVE) {
-+
-+        ret = qemu_savevm_state_iterate(snap_state.file);
-+        remaining = ram_bytes_remaining();
-+
-+        DPRINTF("savevm inerate %zd %d\n", remaining, ret);
-+
-+        if (ret < 0) {
-+            save_snapshot_error("qemu_savevm_state_iterate error %d", ret);
-+            return;
-+        }
-+
-+        /* stop the VM if we get to the end of available space,
-+         * or if remaining is just a few MB
-+         */
-+        maxlen = bdrv_getlength(snap_state.bs) - 30*1024*1024;
-+        if ((remaining < 100000) ||
-+            ((snap_state.bs_pos + remaining) >= maxlen)) {
++        uint64_t pending_size;
++
++        pending_size = qemu_savevm_state_pending(snap_state.file, 0);
++
++        if (pending_size) {
++                ret = qemu_savevm_state_iterate(snap_state.file);
++                if (ret < 0) {
++                    save_snapshot_error("qemu_savevm_state_iterate error %d", ret);
++                    break;
++                }
++                DPRINTF("savevm inerate pending size %lu ret %d\n", pending_size, ret);
++        } else {
++            DPRINTF("done iterating\n");
 +            if (runstate_is_running()) {
 +                vm_stop(RUN_STATE_SAVE_VM);
 +            }
++            DPRINTF("savevm inerate finished\n");
++            qemu_savevm_state_complete(snap_state.file);
++            DPRINTF("save complete\n");
++            save_snapshot_completed();
++            break;
 +        }
 +
-+        if (ret == 1) { /* finished */
++        /* stop the VM if we get to the end of available space,
++         * or if pending_size is just a few MB
++         */
++        maxlen = bdrv_getlength(snap_state.bs) - 30*1024*1024;
++        if ((pending_size < 100000) ||
++            ((snap_state.bs_pos + pending_size) >= maxlen)) {
 +            if (runstate_is_running()) {
 +                vm_stop(RUN_STATE_SAVE_VM);
 +            }
-+            DPRINTF("savevm inerate finished\n");
-+            if ((ret = qemu_savevm_state_complete(snap_state.file)) < 0) {
-+                save_snapshot_error("qemu_savevm_state_complete error %d", ret);
-+                return;
-+            } else {
-+                DPRINTF("save complete\n");
-+                save_snapshot_completed();
-+                return;
-+            }
 +        }
 +    }
 +}
@@ -459,6 +596,8 @@ Index: new/savevm-async.c
 +void qmp_savevm_start(bool has_statefile, const char *statefile, Error **errp)
 +{
 +    BlockDriver *drv = NULL;
++    Error *local_err = NULL;
++
 +    int bdrv_oflags = BDRV_O_CACHE_WB | BDRV_O_RDWR;
 +    int ret;
 +
@@ -471,7 +610,7 @@ Index: new/savevm-async.c
 +    /* initialize snapshot info */
 +    snap_state.saved_vm_running = runstate_is_running();
 +    snap_state.bs_pos = 0;
-+    snap_state.total_time = qemu_get_clock_ms(rt_clock);
++    snap_state.total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
 +
 +    if (snap_state.error) {
 +        error_free(snap_state.error);
@@ -490,19 +629,24 @@ Index: new/savevm-async.c
 +
 +    /* Open the image */
 +    snap_state.bs = bdrv_new("vmstate");
-+    ret = bdrv_open(snap_state.bs, statefile, bdrv_oflags, drv);
++    ret = bdrv_open(&snap_state.bs, statefile, NULL, NULL, bdrv_oflags, drv, &local_err);
++
 +    if (ret < 0) {
-+        error_set(errp, QERR_OPEN_FILE_FAILED, statefile);
++        error_set(errp, ERROR_CLASS_GENERIC_ERROR, "failed to open '%s'", statefile);
 +        goto restart;
 +    }
 +
 +    snap_state.file = qemu_fopen_ops(&snap_state, &block_file_ops);
 +
 +    if (!snap_state.file) {
-+        error_set(errp, QERR_OPEN_FILE_FAILED, statefile);
++        error_set(errp, ERROR_CLASS_GENERIC_ERROR, "failed to open '%s'", statefile);
 +        goto restart;
 +    }
 +
++
++    bdrv_set_in_use(snap_state.bs, 1);
++    bdrv_ref(snap_state.bs);
++
 +    Coroutine *co = qemu_coroutine_create(process_savevm_co);
 +    qemu_coroutine_enter(co, NULL);
 +
@@ -588,7 +732,7 @@ Index: new/savevm-async.c
 +    sn->date_sec = tv.tv_sec;
 +    sn->date_nsec = tv.tv_usec * 1000;
 +#endif
-+    sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
++    sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
 +
 +    pstrcpy(sn->name, sizeof(sn->name), name);
 +
@@ -607,6 +751,8 @@ Index: new/savevm-async.c
 +{
 +    BlockDriverState *bs;
 +    QEMUSnapshotInfo sn1, *sn = &sn1;
++    Error *local_err = NULL;
++
 +    int ret;
 +
 +    bs = bdrv_find(device);
@@ -629,7 +775,7 @@ Index: new/savevm-async.c
 +        return;
 +    }
 +
-+    ret = bdrv_snapshot_delete(bs, name);
++    ret = bdrv_snapshot_delete(bs, NULL, name, &local_err);
 +    if (ret < 0) {
 +        error_set(errp, ERROR_CLASS_GENERIC_ERROR,
 +                  "Error while deleting snapshot on '%s'\n", device);
@@ -662,11 +808,16 @@ Index: new/savevm-async.c
 +{
 +    BlockDriverState *bs = NULL;
 +    BlockDriver *drv = NULL;
++    Error *local_err = NULL;
++
 +    QEMUFile *f;
 +    int ret = -1;
 +
 +    bs = bdrv_new("vmstate");
-+    ret = bdrv_open(bs, filename, BDRV_O_CACHE_WB, drv);
++    ret = bdrv_open(&bs, filename, NULL, NULL, BDRV_O_CACHE_WB, drv, &local_err);
++    bdrv_set_in_use(bs, 1);
++    bdrv_ref(bs);
++
 +    if (ret < 0) {
 +        error_report("Could not open VM state file");
 +        goto the_end;
@@ -693,71 +844,76 @@ Index: new/savevm-async.c
 +
 + the_end:
 +    if (bs) {
-+        bdrv_delete(bs);
++        bdrv_set_in_use(bs, 0);
++        bdrv_unref(bs);
 +    }
 +    return ret;
 +}
-Index: new/Makefile.objs
+Index: new/savevm.c
 ===================================================================
---- new.orig/Makefile.objs     2012-11-23 07:30:15.000000000 +0100
-+++ new/Makefile.objs  2012-11-23 07:31:44.000000000 +0100
-@@ -84,6 +84,7 @@
- common-obj-y += block-migration.o iohandler.o
- common-obj-y += bitmap.o bitops.o
- common-obj-y += page_cache.o
-+common-obj-y += savevm-async.o
+--- new.orig/savevm.c  2014-05-05 09:49:29.000000000 +0200
++++ new/savevm.c       2014-05-05 09:54:17.000000000 +0200
+@@ -460,11 +460,11 @@
+     return false;
+ }
  
- common-obj-$(CONFIG_POSIX) += migration-exec.o migration-unix.o migration-fd.o
- common-obj-$(CONFIG_WIN32) += version.o
-Index: new/sysemu.h
-===================================================================
---- new.orig/sysemu.h  2012-11-23 07:30:15.000000000 +0100
-+++ new/sysemu.h       2012-11-23 07:31:44.000000000 +0100
-@@ -67,6 +67,7 @@
+-void qemu_savevm_state_begin(QEMUFile *f,
++int qemu_savevm_state_begin(QEMUFile *f,
+                              const MigrationParams *params)
+ {
+     SaveStateEntry *se;
+-    int ret;
++    int ret = 0;
  
- void do_savevm(Monitor *mon, const QDict *qdict);
- int load_vmstate(const char *name);
-+int load_state_from_blockdev(const char *filename);
- void do_delvm(Monitor *mon, const QDict *qdict);
- void do_info_snapshots(Monitor *mon);
+     trace_savevm_state_begin();
+     QTAILQ_FOREACH(se, &savevm_handlers, entry) {
+@@ -506,6 +506,7 @@
+             break;
+         }
+     }
++    return ret;
+ }
  
-Index: new/qemu-options.hx
-===================================================================
---- new.orig/qemu-options.hx   2012-11-23 07:31:24.000000000 +0100
-+++ new/qemu-options.hx        2012-11-23 07:31:44.000000000 +0100
-@@ -2575,6 +2575,19 @@
- Start right away with a saved state (@code{loadvm} in monitor)
- ETEXI
+ /*
+@@ -554,7 +555,7 @@
+     return ret;
+ }
  
-+DEF("loadstate", HAS_ARG, QEMU_OPTION_loadstate, \
-+    "-loadstate file\n" \
-+    "                start right away with a saved state\n",
-+    QEMU_ARCH_ALL)
-+STEXI
-+@item -loadstate @var{file}
-+@findex -loadstate
-+Start right away with a saved state. This option does not rollback
-+disk state like @code{loadvm}, so user must make sure that disk
-+have correct state. @var{file} can be any valid device URL. See the section
-+for "Device URL Syntax" for more information.
-+ETEXI
-+
- #ifndef _WIN32
- DEF("daemonize", 0, QEMU_OPTION_daemonize, \
-     "-daemonize      daemonize QEMU after initializing\n", QEMU_ARCH_ALL)
+-void qemu_savevm_state_complete(QEMUFile *f)
++int qemu_savevm_state_complete(QEMUFile *f)
+ {
+     SaveStateEntry *se;
+     int ret;
+@@ -581,7 +582,7 @@
+         trace_savevm_section_end(se->idstr, se->section_id);
+         if (ret < 0) {
+             qemu_file_set_error(f, ret);
+-            return;
++            return ret;
+         }
+     }
+@@ -610,6 +611,7 @@
+     qemu_put_byte(f, QEMU_VM_EOF);
+     qemu_fflush(f);
++    return qemu_file_get_error(f);
+ }
+ uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size)
 Index: new/vl.c
 ===================================================================
---- new.orig/vl.c      2012-11-23 07:31:29.000000000 +0100
-+++ new/vl.c   2012-11-23 07:31:44.000000000 +0100
-@@ -2545,6 +2545,7 @@
+--- new.orig/vl.c      2014-05-05 09:49:29.000000000 +0200
++++ new/vl.c   2014-05-05 09:54:17.000000000 +0200
+@@ -2953,6 +2953,7 @@
      int optind;
      const char *optarg;
      const char *loadvm = NULL;
 +    const char *loadstate = NULL;
+     MachineClass *machine_class;
      QEMUMachine *machine;
      const char *cpu_model;
-     const char *vga_model = "none";
-@@ -3185,6 +3186,9 @@
+@@ -3574,6 +3575,9 @@
            case QEMU_OPTION_loadvm:
                loadvm = optarg;
                break;
@@ -767,7 +923,7 @@ Index: new/vl.c
              case QEMU_OPTION_full_screen:
                  full_screen = 1;
                  break;
-@@ -4038,6 +4042,10 @@
+@@ -4541,6 +4545,10 @@
          if (load_vmstate(loadvm) < 0) {
              autostart = 0;
          }
@@ -778,21 +934,3 @@ Index: new/vl.c
      }
  
      if (incoming) {
-Index: new/monitor.c
-===================================================================
---- new.orig/monitor.c 2012-11-23 07:30:15.000000000 +0100
-+++ new/monitor.c      2012-11-23 07:31:44.000000000 +0100
-@@ -2701,6 +2701,13 @@
-         .mhandler.info = hmp_info_migrate_cache_size,
-     },
-     {
-+        .name       = "savevm",
-+        .args_type  = "",
-+        .params     = "",
-+        .help       = "show savevm status",
-+        .mhandler.info = hmp_info_savevm,
-+    },
-+    {
-         .name       = "balloon",
-         .args_type  = "",
-         .params     = "",