From a87537fc90cb6607050a4c1d52df77197fefbf57 Mon Sep 17 00:00:00 2001 From: Alex Williamson Date: Tue, 6 Jul 2010 22:28:15 -0300 Subject: [PATCH 07/24] savevm: Make use of DeviceState RH-Author: Alex Williamson Message-id: <20100706222815.1033.62345.stgit@localhost.localdomain> Patchwork-id: 10510 O-Subject: [RHEL6.0 qemu-kvm PATCH 07/17] savevm: Make use of DeviceState Bugzilla: 596328 RH-Acked-by: Juan Quintela RH-Acked-by: Amit Shah RH-Acked-by: Jes Sorensen Bugzilla: 596328 Upstream commit: 7685ee6abcb939104801f84b3fe9645412528088 For callers that pass a device we can traverse up the qdev tree and make use of the BusInfo.get_dev_path information for creating unique savevm id strings. This avoids needing to rely on the instance number, which can cause problems with device initialization order and hotplug. For compatibility, we also store away the old id string and instance so we can accept migrations from VMs as we add new get_dev_path implementations. Signed-off-by: Alex Williamson Signed-off-by: Anthony Liguori --- savevm.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 78 insertions(+), 5 deletions(-) Signed-off-by: Eduardo Habkost --- savevm.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 78 insertions(+), 5 deletions(-) diff --git a/savevm.c b/savevm.c index 72ad844..b008e87 100644 --- a/savevm.c +++ b/savevm.c @@ -72,6 +72,7 @@ #include "qemu-common.h" #include "hw/hw.h" +#include "hw/qdev.h" #include "net.h" #include "monitor.h" #include "sysemu.h" @@ -1024,6 +1025,11 @@ const VMStateInfo vmstate_info_unused_buffer = { .put = put_unused_buffer, }; +typedef struct CompatEntry { + char idstr[256]; + int instance_id; +} CompatEntry; + typedef struct SaveStateEntry { QTAILQ_ENTRY(SaveStateEntry) entry; char idstr[256]; @@ -1036,6 +1042,7 @@ typedef struct SaveStateEntry { LoadStateHandler *load_state; const VMStateDescription *vmsd; void *opaque; + CompatEntry *compat; } SaveStateEntry; @@ -1057,6 +1064,23 @@ static int calculate_new_instance_id(const char *idstr) return instance_id; } +static int calculate_compat_instance_id(const char *idstr) +{ + SaveStateEntry *se; + int instance_id = 0; + + QTAILQ_FOREACH(se, &savevm_handlers, entry) { + if (!se->compat) + continue; + + if (strcmp(idstr, se->compat->idstr) == 0 + && instance_id <= se->compat->instance_id) { + instance_id = se->compat->instance_id + 1; + } + } + return instance_id; +} + /* TODO: Individual devices generally have very little idea about the rest of the system, so instance_id should be removed/replaced. Meanwhile pass -1 as instance_id if you do not already have a clearly @@ -1074,7 +1098,6 @@ int register_savevm_live(DeviceState *dev, SaveStateEntry *se; se = qemu_mallocz(sizeof(SaveStateEntry)); - pstrcpy(se->idstr, sizeof(se->idstr), idstr); se->version_id = version_id; se->section_id = global_section_id++; se->set_params = set_params; @@ -1084,11 +1107,28 @@ int register_savevm_live(DeviceState *dev, se->opaque = opaque; se->vmsd = NULL; + if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) { + char *id = dev->parent_bus->info->get_dev_path(dev); + if (id) { + pstrcpy(se->idstr, sizeof(se->idstr), id); + pstrcat(se->idstr, sizeof(se->idstr), "/"); + qemu_free(id); + + se->compat = qemu_mallocz(sizeof(CompatEntry)); + pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr); + se->compat->instance_id = instance_id == -1 ? + calculate_compat_instance_id(idstr) : instance_id; + instance_id = -1; + } + } + pstrcat(se->idstr, sizeof(se->idstr), idstr); + if (instance_id == -1) { - se->instance_id = calculate_new_instance_id(idstr); + se->instance_id = calculate_new_instance_id(se->idstr); } else { se->instance_id = instance_id; } + assert(!se->compat || se->instance_id == 0); /* add at the end of list */ QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry); return 0; @@ -1109,9 +1149,20 @@ int register_savevm(DeviceState *dev, void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque) { SaveStateEntry *se, *new_se; + char id[256] = ""; + + if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) { + char *path = dev->parent_bus->info->get_dev_path(dev); + if (path) { + pstrcpy(id, sizeof(id), path); + pstrcat(id, sizeof(id), "/"); + qemu_free(path); + } + } + pstrcat(id, sizeof(id), idstr); QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) { - if (strcmp(se->idstr, idstr) == 0 && se->opaque == opaque) { + if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) { QTAILQ_REMOVE(&savevm_handlers, se, entry); qemu_free(se); } @@ -1124,7 +1175,6 @@ int vmstate_register(DeviceState *dev, int instance_id, SaveStateEntry *se; se = qemu_mallocz(sizeof(SaveStateEntry)); - pstrcpy(se->idstr, sizeof(se->idstr), vmsd->name); se->version_id = vmsd->version_id; se->section_id = global_section_id++; se->save_live_state = NULL; @@ -1133,11 +1183,28 @@ int vmstate_register(DeviceState *dev, int instance_id, se->opaque = opaque; se->vmsd = vmsd; + if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) { + char *id = dev->parent_bus->info->get_dev_path(dev); + if (id) { + pstrcpy(se->idstr, sizeof(se->idstr), id); + pstrcat(se->idstr, sizeof(se->idstr), "/"); + qemu_free(id); + + se->compat = qemu_mallocz(sizeof(CompatEntry)); + pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name); + se->compat->instance_id = instance_id == -1 ? + calculate_compat_instance_id(vmsd->name) : instance_id; + instance_id = -1; + } + } + pstrcat(se->idstr, sizeof(se->idstr), vmsd->name); + if (instance_id == -1) { - se->instance_id = calculate_new_instance_id(vmsd->name); + se->instance_id = calculate_new_instance_id(se->idstr); } else { se->instance_id = instance_id; } + assert(!se->compat || se->instance_id == 0); /* add at the end of list */ QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry); return 0; @@ -1488,6 +1555,12 @@ static SaveStateEntry *find_se(const char *idstr, int instance_id) if (!strcmp(se->idstr, idstr) && instance_id == se->instance_id) return se; + /* Migrating from an older version? */ + if (strstr(se->idstr, idstr) && se->compat) { + if (!strcmp(se->compat->idstr, idstr) && + (instance_id == se->compat->instance_id)) + return se; + } } return NULL; } -- 1.7.0.3