[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCHv2 09/15] xml: use better types for memory values



Using 'unsigned long' for memory values is risky on 32-bit platforms,
as a PAE guest can have more than 4GiB memory.  Our API is
(unfortunately) locked at 'unsigned long' and a scale of 1024, but
the rest of our system should consistently use 64-bit values,
especially since the previous patch centralized overflow checking.

* src/conf/domain_conf.h (_virDomainDef): Always use 64-bit values
for memory.  Change hugepage_backed to a bool.
* src/conf/domain_conf.c (virDomainDefParseXML)
(virDomainDefCheckABIStability, virDomainDefFormatInternal): Fix
clients.
* src/vmx/vmx.c (virVMXFormatConfig): Likewise.
* src/xenxs/xen_sxpr.c (xenParseSxpr, xenFormatSxpr): Likewise.
* src/xenxs/xen_xm.c (xenXMConfigGetULongLong): New function.
(xenXMConfigGetULong, xenXMConfigSetInt): Avoid truncation.
(xenParseXM, xenFormatXM): Fix clients.
* src/phyp/phyp_driver.c (phypBuildLpar): Likewise.
* src/openvz/openvz_driver.c (openvzDomainSetMemoryInternal):
Likewise.
* src/vbox/vbox_tmpl.c (vboxDomainDefineXML): Likewise.
* src/qemu/qemu_command.c (qemuBuildCommandLine): Likewise.
* src/qemu/qemu_process.c (qemuProcessStart): Likewise.
* src/qemu/qemu_monitor.h (qemuMonitorGetBalloonInfo): Likewise.
* src/qemu/qemu_monitor_text.h (qemuMonitorTextGetBalloonInfo):
Likewise.
* src/qemu/qemu_monitor_text.c (qemuMonitorTextGetBalloonInfo):
Likewise.
* src/qemu/qemu_monitor_json.h (qemuMonitorJSONGetBalloonInfo):
Likewise.
* src/qemu/qemu_monitor_json.c (qemuMonitorJSONGetBalloonInfo):
Likewise.
* src/qemu/qemu_driver.c (qemudDomainGetInfo)
(qemuDomainGetXMLDesc): Likewise.
* src/uml/uml_conf.c (umlBuildCommandLine): Likewise.
---

v2: new

 src/conf/domain_audit.c      |    2 +-
 src/conf/domain_conf.c       |   46 ++++++++++++++++----------------
 src/conf/domain_conf.h       |   14 +++++-----
 src/openvz/openvz_driver.c   |    8 +++---
 src/phyp/phyp_driver.c       |   10 +++---
 src/qemu/qemu_command.c      |    2 +-
 src/qemu/qemu_driver.c       |    4 +-
 src/qemu/qemu_monitor.c      |    2 +-
 src/qemu/qemu_monitor.h      |    2 +-
 src/qemu/qemu_monitor_json.c |    2 +-
 src/qemu/qemu_monitor_json.h |    2 +-
 src/qemu/qemu_monitor_text.c |    2 +-
 src/qemu/qemu_monitor_text.h |    2 +-
 src/qemu/qemu_process.c      |    6 ++++
 src/uml/uml_conf.c           |    2 +-
 src/vbox/vbox_tmpl.c         |    4 +-
 src/vmx/vmx.c                |   10 +++---
 src/xenxs/xen_sxpr.c         |    8 ++---
 src/xenxs/xen_xm.c           |   58 +++++++++++++++++++++++++++++++++++------
 19 files changed, 115 insertions(+), 71 deletions(-)

diff --git a/src/conf/domain_audit.c b/src/conf/domain_audit.c
index 934e546..b885906 100644
--- a/src/conf/domain_audit.c
+++ b/src/conf/domain_audit.c
@@ -1,7 +1,7 @@
 /*
  * domain_audit.c: Domain audit management
  *
- * Copyright (C) 2006-2011 Red Hat, Inc.
+ * Copyright (C) 2006-2012 Red Hat, Inc.
  * Copyright (C) 2006 Daniel P. Berrange
  *
  * This library is free software; you can redistribute it and/or
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 41d0eca..fa44d3e 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -7292,27 +7292,27 @@ static virDomainDefPtr virDomainDefParseXML(virCapsPtr caps,
         goto error;

     /* Extract domain memory */
-    if (virXPathULong("string(./memory[1])", ctxt,
-                      &def->mem.max_balloon) < 0) {
+    if (virXPathULongLong("string(./memory[1])", ctxt,
+                          &def->mem.max_balloon) < 0) {
         virDomainReportError(VIR_ERR_INTERNAL_ERROR,
                              "%s", _("missing memory element"));
         goto error;
     }

-    if (virXPathULong("string(./currentMemory[1])", ctxt,
-                      &def->mem.cur_balloon) < 0)
+    if (virXPathULongLong("string(./currentMemory[1])", ctxt,
+                          &def->mem.cur_balloon) < 0)
         def->mem.cur_balloon = def->mem.max_balloon;

     if (def->mem.cur_balloon > def->mem.max_balloon) {
         virDomainReportError(VIR_ERR_INTERNAL_ERROR,
-                             _("current memory '%luk' exceeds maximum '%luk'"),
+                             _("current memory '%lluk' exceeds maximum '%lluk'"),
                              def->mem.cur_balloon, def->mem.max_balloon);
         goto error;
     }

     node = virXPathNode("./memoryBacking/hugepages", ctxt);
     if (node)
-        def->mem.hugepage_backed = 1;
+        def->mem.hugepage_backed = true;

     /* Extract blkio cgroup tunables */
     if (virXPathUInt("string(./blkiotune/weight)", ctxt,
@@ -7346,20 +7346,20 @@ static virDomainDefPtr virDomainDefParseXML(virCapsPtr caps,
     VIR_FREE(nodes);

     /* Extract other memory tunables */
-    if (virXPathULong("string(./memtune/hard_limit)", ctxt,
-                      &def->mem.hard_limit) < 0)
+    if (virXPathULongLong("string(./memtune/hard_limit)", ctxt,
+                          &def->mem.hard_limit) < 0)
         def->mem.hard_limit = 0;

-    if (virXPathULong("string(./memtune/soft_limit[1])", ctxt,
-                      &def->mem.soft_limit) < 0)
+    if (virXPathULongLong("string(./memtune/soft_limit[1])", ctxt,
+                          &def->mem.soft_limit) < 0)
         def->mem.soft_limit = 0;

-    if (virXPathULong("string(./memtune/min_guarantee[1])", ctxt,
-                      &def->mem.min_guarantee) < 0)
+    if (virXPathULongLong("string(./memtune/min_guarantee[1])", ctxt,
+                          &def->mem.min_guarantee) < 0)
         def->mem.min_guarantee = 0;

-    if (virXPathULong("string(./memtune/swap_hard_limit[1])", ctxt,
-                      &def->mem.swap_hard_limit) < 0)
+    if (virXPathULongLong("string(./memtune/swap_hard_limit[1])", ctxt,
+                          &def->mem.swap_hard_limit) < 0)
         def->mem.swap_hard_limit = 0;

     n = virXPathULong("string(./vcpu[1])", ctxt, &count);
@@ -9210,19 +9210,19 @@ bool virDomainDefCheckABIStability(virDomainDefPtr src,

     if (src->mem.max_balloon != dst->mem.max_balloon) {
         virDomainReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                             _("Target domain max memory %ld does not match source %ld"),
+                             _("Target domain max memory %lld does not match source %lld"),
                              dst->mem.max_balloon, src->mem.max_balloon);
         goto cleanup;
     }
     if (src->mem.cur_balloon != dst->mem.cur_balloon) {
         virDomainReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                             _("Target domain current memory %ld does not match source %ld"),
+                             _("Target domain current memory %lld does not match source %lld"),
                              dst->mem.cur_balloon, src->mem.cur_balloon);
         goto cleanup;
     }
     if (src->mem.hugepage_backed != dst->mem.hugepage_backed) {
         virDomainReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                             _("Target domain huge page backing %ld does not match source %ld"),
+                             _("Target domain huge page backing %d does not match source %d"),
                              dst->mem.hugepage_backed,
                              src->mem.hugepage_backed);
         goto cleanup;
@@ -11645,9 +11645,9 @@ virDomainDefFormatInternal(virDomainDefPtr def,
         xmlIndentTreeOutput = oldIndentTreeOutput;
     }

-    virBufferAsprintf(buf, "  <memory unit='KiB'>%lu</memory>\n",
+    virBufferAsprintf(buf, "  <memory unit='KiB'>%llu</memory>\n",
                       def->mem.max_balloon);
-    virBufferAsprintf(buf, "  <currentMemory unit='KiB'>%lu</currentMemory>\n",
+    virBufferAsprintf(buf, "  <currentMemory unit='KiB'>%llu</currentMemory>\n",
                       def->mem.cur_balloon);

     /* add blkiotune only if there are any */
@@ -11689,19 +11689,19 @@ virDomainDefFormatInternal(virDomainDefPtr def,
         virBufferAddLit(buf, "  <memtune>\n");
     if (def->mem.hard_limit) {
         virBufferAsprintf(buf, "    <hard_limit unit='KiB'>"
-                          "%lu</hard_limit>\n", def->mem.hard_limit);
+                          "%llu</hard_limit>\n", def->mem.hard_limit);
     }
     if (def->mem.soft_limit) {
         virBufferAsprintf(buf, "    <soft_limit unit='KiB'>"
-                          "%lu</soft_limit>\n", def->mem.soft_limit);
+                          "%llu</soft_limit>\n", def->mem.soft_limit);
     }
     if (def->mem.min_guarantee) {
         virBufferAsprintf(buf, "    <min_guarantee unit='KiB'>"
-                          "%lu</min_guarantee>\n", def->mem.min_guarantee);
+                          "%llu</min_guarantee>\n", def->mem.min_guarantee);
     }
     if (def->mem.swap_hard_limit) {
         virBufferAsprintf(buf, "    <swap_hard_limit unit='KiB'>"
-                          "%lu</swap_hard_limit>\n", def->mem.swap_hard_limit);
+                          "%llu</swap_hard_limit>\n", def->mem.swap_hard_limit);
     }
     if (def->mem.hard_limit || def->mem.soft_limit || def->mem.min_guarantee ||
         def->mem.swap_hard_limit)
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index 2aa489d..f565169 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -1454,13 +1454,13 @@ struct _virDomainDef {
     } blkio;

     struct {
-        unsigned long max_balloon; /* in kibibytes */
-        unsigned long cur_balloon; /* in kibibytes */
-        unsigned long hugepage_backed;
-        unsigned long hard_limit; /* in kibibytes */
-        unsigned long soft_limit; /* in kibibytes */
-        unsigned long min_guarantee; /* in kibibytes */
-        unsigned long swap_hard_limit; /* in kibibytes */
+        unsigned long long max_balloon; /* in kibibytes */
+        unsigned long long cur_balloon; /* in kibibytes */
+        bool hugepage_backed;
+        unsigned long long hard_limit; /* in kibibytes */
+        unsigned long long soft_limit; /* in kibibytes */
+        unsigned long long min_guarantee; /* in kibibytes */
+        unsigned long long swap_hard_limit; /* in kibibytes */
     } mem;
     unsigned short vcpus;
     unsigned short maxvcpus;
diff --git a/src/openvz/openvz_driver.c b/src/openvz/openvz_driver.c
index aef1491..4e369ea 100644
--- a/src/openvz/openvz_driver.c
+++ b/src/openvz/openvz_driver.c
@@ -1,7 +1,7 @@
 /*
  * openvz_driver.c: core driver methods for managing OpenVZ VEs
  *
- * Copyright (C) 2010-2011 Red Hat, Inc.
+ * Copyright (C) 2010-2012 Red Hat, Inc.
  * Copyright (C) 2006, 2007 Binary Karma
  * Copyright (C) 2006 Shuveb Hussain
  * Copyright (C) 2007 Anoop Joe Cyriac
@@ -70,7 +70,7 @@ static int openvzDomainGetMaxVcpus(virDomainPtr dom);
 static int openvzDomainSetVcpusInternal(virDomainObjPtr vm,
                                         unsigned int nvcpus);
 static int openvzDomainSetMemoryInternal(virDomainObjPtr vm,
-                                         unsigned long memory);
+                                         unsigned long long memory);
 static int openvzGetVEStatus(virDomainObjPtr vm, int *status, int *reason);

 static void openvzDriverLock(struct openvz_driver *driver)
@@ -1612,7 +1612,7 @@ static int openvzNumDefinedDomains(virConnectPtr conn) {

 static int
 openvzDomainSetMemoryInternal(virDomainObjPtr vm,
-                              unsigned long mem)
+                              unsigned long long mem)
 {
     char str_mem[16];
     const char *prog[] = { VZCTL, "--quiet", "set", PROGRAM_SENTINAL,
@@ -1620,7 +1620,7 @@ openvzDomainSetMemoryInternal(virDomainObjPtr vm,
     };

     /* memory has to be changed its format from kbyte to byte */
-    snprintf(str_mem, sizeof(str_mem), "%lu", mem * 1024);
+    snprintf(str_mem, sizeof(str_mem), "%llu", mem * 1024);

     openvzSetProgramSentinal(prog, vm->def->name);
     if (virRun(prog, NULL) < 0) {
diff --git a/src/phyp/phyp_driver.c b/src/phyp/phyp_driver.c
index e08b1b6..bdf4a7b 100644
--- a/src/phyp/phyp_driver.c
+++ b/src/phyp/phyp_driver.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010-2011 Red Hat, Inc.
+ * Copyright (C) 2010-2012 Red Hat, Inc.
  * Copyright IBM Corp. 2009
  *
  * phyp_driver.c: ssh layer to access Power Hypervisors
@@ -3608,10 +3608,10 @@ phypBuildLpar(virConnectPtr conn, virDomainDefPtr def)
     virBufferAddLit(&buf, "mksyscfg");
     if (system_type == HMC)
         virBufferAsprintf(&buf, " -m %s", managed_system);
-    virBufferAsprintf(&buf, " -r lpar -p %s -i min_mem=%d,desired_mem=%d,"
-                      "max_mem=%d,desired_procs=%d,virtual_scsi_adapters=%s",
-                      def->name, (int) def->mem.cur_balloon,
-                      (int) def->mem.cur_balloon, (int) def->mem.max_balloon,
+    virBufferAsprintf(&buf, " -r lpar -p %s -i min_mem=%lld,desired_mem=%lld,"
+                      "max_mem=%lld,desired_procs=%d,virtual_scsi_adapters=%s",
+                      def->name, def->mem.cur_balloon,
+                      def->mem.cur_balloon, def->mem.max_balloon,
                       (int) def->vcpus, def->disks[0]->src);
     ret = phypExecBuffer(session, &buf, &exit_status, conn, false);

diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index cddd6aa..e6a646c 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -4011,7 +4011,7 @@ qemuBuildCommandLine(virConnectPtr conn,
      * is not supported, then they're out of luck anyway
      */
     virCommandAddArg(cmd, "-m");
-    virCommandAddArgFormat(cmd, "%lu", VIR_DIV_UP(def->mem.max_balloon, 1024));
+    virCommandAddArgFormat(cmd, "%llu", VIR_DIV_UP(def->mem.max_balloon, 1024));
     if (def->mem.hugepage_backed) {
         if (!driver->hugetlbfs_mount) {
             qemuReportError(VIR_ERR_INTERNAL_ERROR,
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index aee95e4..bed1a7c 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -2139,7 +2139,7 @@ static int qemudDomainGetInfo(virDomainPtr dom,
     virDomainObjPtr vm;
     int ret = -1;
     int err;
-    unsigned long balloon;
+    unsigned long long balloon;

     qemuDriverLock(driver);
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
@@ -4416,7 +4416,7 @@ static char *qemuDomainGetXMLDesc(virDomainPtr dom,
     struct qemud_driver *driver = dom->conn->privateData;
     virDomainObjPtr vm;
     char *ret = NULL;
-    unsigned long balloon;
+    unsigned long long balloon;
     int err = 0;

     /* Flags checked by virDomainDefFormat */
diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
index 1da73f6..78eb492 100644
--- a/src/qemu/qemu_monitor.c
+++ b/src/qemu/qemu_monitor.c
@@ -1260,7 +1260,7 @@ int qemuMonitorGetVirtType(qemuMonitorPtr mon,


 int qemuMonitorGetBalloonInfo(qemuMonitorPtr mon,
-                              unsigned long *currmem)
+                              unsigned long long *currmem)
 {
     int ret;
     VIR_DEBUG("mon=%p", mon);
diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h
index b1c956c..654d9bd 100644
--- a/src/qemu/qemu_monitor.h
+++ b/src/qemu/qemu_monitor.h
@@ -234,7 +234,7 @@ int qemuMonitorGetCPUInfo(qemuMonitorPtr mon,
 int qemuMonitorGetVirtType(qemuMonitorPtr mon,
                            int *virtType);
 int qemuMonitorGetBalloonInfo(qemuMonitorPtr mon,
-                              unsigned long *currmem);
+                              unsigned long long *currmem);
 int qemuMonitorGetMemoryStats(qemuMonitorPtr mon,
                               virDomainMemoryStatPtr stats,
                               unsigned int nr_stats);
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index dc67b4b..afd744a 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -1178,7 +1178,7 @@ cleanup:
  * or -1 on failure
  */
 int qemuMonitorJSONGetBalloonInfo(qemuMonitorPtr mon,
-                                  unsigned long *currmem)
+                                  unsigned long long *currmem)
 {
     int ret;
     virJSONValuePtr cmd = qemuMonitorJSONMakeCommand("query-balloon",
diff --git a/src/qemu/qemu_monitor_json.h b/src/qemu/qemu_monitor_json.h
index 0932a2c..04e9d86 100644
--- a/src/qemu/qemu_monitor_json.h
+++ b/src/qemu/qemu_monitor_json.h
@@ -61,7 +61,7 @@ int qemuMonitorJSONGetCPUInfo(qemuMonitorPtr mon,
 int qemuMonitorJSONGetVirtType(qemuMonitorPtr mon,
                                int *virtType);
 int qemuMonitorJSONGetBalloonInfo(qemuMonitorPtr mon,
-                                  unsigned long *currmem);
+                                  unsigned long long *currmem);
 int qemuMonitorJSONGetMemoryStats(qemuMonitorPtr mon,
                                   virDomainMemoryStatPtr stats,
                                   unsigned int nr_stats);
diff --git a/src/qemu/qemu_monitor_text.c b/src/qemu/qemu_monitor_text.c
index a7ebfba..30a0416 100644
--- a/src/qemu/qemu_monitor_text.c
+++ b/src/qemu/qemu_monitor_text.c
@@ -705,7 +705,7 @@ static int qemuMonitorParseBalloonInfo(char *text,
  * or -1 on failure
  */
 int qemuMonitorTextGetBalloonInfo(qemuMonitorPtr mon,
-                                  unsigned long *currmem)
+                                  unsigned long long *currmem)
 {
     char *reply = NULL;
     int ret = -1;
diff --git a/src/qemu/qemu_monitor_text.h b/src/qemu/qemu_monitor_text.h
index 719fc82..4525864 100644
--- a/src/qemu/qemu_monitor_text.h
+++ b/src/qemu/qemu_monitor_text.h
@@ -54,7 +54,7 @@ int qemuMonitorTextGetCPUInfo(qemuMonitorPtr mon,
 int qemuMonitorTextGetVirtType(qemuMonitorPtr mon,
                                int *virtType);
 int qemuMonitorTextGetBalloonInfo(qemuMonitorPtr mon,
-                                  unsigned long *currmem);
+                                  unsigned long long *currmem);
 int qemuMonitorTextGetMemoryStats(qemuMonitorPtr mon,
                                   virDomainMemoryStatPtr stats,
                                   unsigned int nr_stats);
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index 7b99814..5d7cf4d 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -3488,6 +3488,12 @@ int qemuProcessStart(virConnectPtr conn,
      * migration completes.  */
     VIR_DEBUG("Setting initial memory amount");
     cur_balloon = vm->def->mem.cur_balloon;
+    if (cur_balloon != vm->def->mem.cur_balloon) {
+        qemuReportError(VIR_ERR_INTERNAL_ERROR,
+                        _("integer overflow trying to set balloon to %lld"),
+                        vm->def->mem.cur_balloon);
+        goto cleanup;
+    }
     qemuDomainObjEnterMonitorWithDriver(driver, vm);
     if (qemuMonitorSetBalloon(priv->mon, cur_balloon) < 0) {
         qemuDomainObjExitMonitorWithDriver(driver, vm);
diff --git a/src/uml/uml_conf.c b/src/uml/uml_conf.c
index 89fdd9f..da38cc2 100644
--- a/src/uml/uml_conf.c
+++ b/src/uml/uml_conf.c
@@ -416,7 +416,7 @@ virCommandPtr umlBuildCommandLine(virConnectPtr conn,
     virCommandAddEnvPassCommon(cmd);

     //virCommandAddArgPair(cmd, "con0", "fd:0,fd:1");
-    virCommandAddArgFormat(cmd, "mem=%luK", vm->def->mem.cur_balloon);
+    virCommandAddArgFormat(cmd, "mem=%lluK", vm->def->mem.cur_balloon);
     virCommandAddArgPair(cmd, "umid", vm->def->name);
     virCommandAddArgPair(cmd, "uml_dir", driver->monitorDir);

diff --git a/src/vbox/vbox_tmpl.c b/src/vbox/vbox_tmpl.c
index a39b567..853a599 100644
--- a/src/vbox/vbox_tmpl.c
+++ b/src/vbox/vbox_tmpl.c
@@ -8,7 +8,7 @@
  */

 /*
- * Copyright (C) 2010-2011 Red Hat, Inc.
+ * Copyright (C) 2010-2012 Red Hat, Inc.
  * Copyright (C) 2008-2009 Sun Microsystems, Inc.
  *
  * This file is part of a free software library; you can redistribute
@@ -5052,7 +5052,7 @@ static virDomainPtr vboxDomainDefineXML(virConnectPtr conn, const char *xml) {
                                       VIR_DIV_UP(def->mem.cur_balloon, 1024));
     if (NS_FAILED(rc)) {
         vboxError(VIR_ERR_INTERNAL_ERROR,
-                  _("could not set the memory size of the domain to: %lu Kb, "
+                  _("could not set the memory size of the domain to: %llu Kb, "
                     "rc=%08x"),
                   def->mem.cur_balloon, (unsigned)rc);
     }
diff --git a/src/vmx/vmx.c b/src/vmx/vmx.c
index 75cb6d1..cc426da 100644
--- a/src/vmx/vmx.c
+++ b/src/vmx/vmx.c
@@ -2,7 +2,7 @@
 /*
  * vmx.c: VMware VMX parsing/formatting functions
  *
- * Copyright (C) 2010-2011 Red Hat, Inc.
+ * Copyright (C) 2010-2012 Red Hat, Inc.
  * Copyright (C) 2009-2010 Matthias Bolte <matthias bolte googlemail com>
  *
  * This library is free software; you can redistribute it and/or
@@ -2883,7 +2883,7 @@ virVMXFormatConfig(virVMXContext *ctx, virCapsPtr caps, virDomainDefPtr def,
     char *preliminaryDisplayName = NULL;
     char *displayName = NULL;
     char *annotation = NULL;
-    unsigned long max_balloon;
+    unsigned long long max_balloon;
     bool scsi_present[4] = { false, false, false, false };
     int scsi_virtualDev[4] = { -1, -1, -1, -1 };
     bool floppy_present[2] = { false, false };
@@ -2978,19 +2978,19 @@ virVMXFormatConfig(virVMXContext *ctx, virCapsPtr caps, virDomainDefPtr def,
     /* max-memory must be a multiple of 4096 kilobyte */
     max_balloon = VIR_DIV_UP(def->mem.max_balloon, 4096) * 4096;

-    virBufferAsprintf(&buffer, "memsize = \"%lu\"\n",
+    virBufferAsprintf(&buffer, "memsize = \"%llu\"\n",
                       max_balloon / 1024); /* Scale from kilobytes to megabytes */

     /* def:mem.cur_balloon -> vmx:sched.mem.max */
     if (def->mem.cur_balloon < max_balloon) {
-        virBufferAsprintf(&buffer, "sched.mem.max = \"%lu\"\n",
+        virBufferAsprintf(&buffer, "sched.mem.max = \"%llu\"\n",
                           VIR_DIV_UP(def->mem.cur_balloon,
                                      1024)); /* Scale from kilobytes to megabytes */
     }

     /* def:mem.min_guarantee -> vmx:sched.mem.minsize */
     if (def->mem.min_guarantee > 0) {
-        virBufferAsprintf(&buffer, "sched.mem.minsize = \"%lu\"\n",
+        virBufferAsprintf(&buffer, "sched.mem.minsize = \"%llu\"\n",
                           VIR_DIV_UP(def->mem.min_guarantee,
                                      1024)); /* Scale from kilobytes to megabytes */
     }
diff --git a/src/xenxs/xen_sxpr.c b/src/xenxs/xen_sxpr.c
index f8390ea..beafb95 100644
--- a/src/xenxs/xen_sxpr.c
+++ b/src/xenxs/xen_sxpr.c
@@ -1190,10 +1190,8 @@ xenParseSxpr(const struct sexpr *root,
         }
     }

-    def->mem.max_balloon = (unsigned long)
-                           (sexpr_u64(root, "domain/maxmem") << 10);
-    def->mem.cur_balloon = (unsigned long)
-                           (sexpr_u64(root, "domain/memory") << 10);
+    def->mem.max_balloon = (sexpr_u64(root, "domain/maxmem") << 10);
+    def->mem.cur_balloon = (sexpr_u64(root, "domain/memory") << 10);
     if (def->mem.cur_balloon > def->mem.max_balloon)
         def->mem.cur_balloon = def->mem.max_balloon;

@@ -2201,7 +2199,7 @@ xenFormatSxpr(virConnectPtr conn,

     virBufferAddLit(&buf, "(vm ");
     virBufferEscapeSexpr(&buf, "(name '%s')", def->name);
-    virBufferAsprintf(&buf, "(memory %lu)(maxmem %lu)",
+    virBufferAsprintf(&buf, "(memory %llu)(maxmem %llu)",
                       VIR_DIV_UP(def->mem.cur_balloon, 1024),
                       VIR_DIV_UP(def->mem.max_balloon, 1024));
     virBufferAsprintf(&buf, "(vcpus %u)", def->maxvcpus);
diff --git a/src/xenxs/xen_xm.c b/src/xenxs/xen_xm.c
index e580a3e..6676e52 100644
--- a/src/xenxs/xen_xm.c
+++ b/src/xenxs/xen_xm.c
@@ -1,8 +1,8 @@
 /*
  * xen_xm.c: Xen XM parsing functions
  *
+ * Copyright (C) 2006-2007, 2009-2010, 2012 Red Hat, Inc.
  * Copyright (C) 2011 Univention GmbH
- * Copyright (C) 2006-2007, 2009-2010 Red Hat, Inc.
  * Copyright (C) 2006 Daniel P. Berrange
  *
  * This library is free software; you can redistribute it and/or
@@ -68,7 +68,7 @@ static int xenXMConfigGetBool(virConfPtr conf,
 static int xenXMConfigGetULong(virConfPtr conf,
                                const char *name,
                                unsigned long *value,
-                               int def) {
+                               unsigned long def) {
     virConfValuePtr val;

     *value = 0;
@@ -96,6 +96,38 @@ static int xenXMConfigGetULong(virConfPtr conf,
 }


+/* Convenience method to grab a int from the config file object */
+static int xenXMConfigGetULongLong(virConfPtr conf,
+                                   const char *name,
+                                   unsigned long long *value,
+                                   unsigned long long def) {
+    virConfValuePtr val;
+
+    *value = 0;
+    if (!(val = virConfGetValue(conf, name))) {
+        *value = def;
+        return 0;
+    }
+
+    if (val->type == VIR_CONF_LONG) {
+        *value = val->l;
+    } else if (val->type == VIR_CONF_STRING) {
+        char *ret;
+        *value = strtoll(val->str, &ret, 10);
+        if (ret == val->str) {
+            XENXS_ERROR(VIR_ERR_INTERNAL_ERROR,
+                       _("config value %s was malformed"), name);
+            return -1;
+        }
+    } else {
+        XENXS_ERROR(VIR_ERR_INTERNAL_ERROR,
+                   _("config value %s was malformed"), name);
+        return -1;
+    }
+    return 0;
+}
+
+
 /* Convenience method to grab a string from the config file object */
 static int xenXMConfigGetString(virConfPtr conf,
                                 const char *name,
@@ -312,12 +344,12 @@ xenParseXM(virConfPtr conf, int xendConfigVersion,
             goto cleanup;
     }

-    if (xenXMConfigGetULong(conf, "memory", &def->mem.cur_balloon,
-                            MIN_XEN_GUEST_SIZE * 2) < 0)
+    if (xenXMConfigGetULongLong(conf, "memory", &def->mem.cur_balloon,
+                                MIN_XEN_GUEST_SIZE * 2) < 0)
         goto cleanup;

-    if (xenXMConfigGetULong(conf, "maxmem", &def->mem.max_balloon,
-                            def->mem.cur_balloon) < 0)
+    if (xenXMConfigGetULongLong(conf, "maxmem", &def->mem.max_balloon,
+                                def->mem.cur_balloon) < 0)
         goto cleanup;

     def->mem.cur_balloon *= 1024;
@@ -1101,9 +1133,15 @@ cleanup:


 static
-int xenXMConfigSetInt(virConfPtr conf, const char *setting, long l) {
+int xenXMConfigSetInt(virConfPtr conf, const char *setting, long long l) {
     virConfValuePtr value = NULL;

+    if ((long) l != l) {
+        XENXS_ERROR(VIR_ERR_INTERNAL_ERROR,
+                    _("integer overflow in trying to store %lld to %s"),
+                    l, setting);
+        return -1;
+    }
     if (VIR_ALLOC(value) < 0) {
         virReportOOMError();
         return -1;
@@ -1468,10 +1506,12 @@ virConfPtr xenFormatXM(virConnectPtr conn,
     if (xenXMConfigSetString(conf, "uuid", uuid) < 0)
         goto no_memory;

-    if (xenXMConfigSetInt(conf, "maxmem", VIR_DIV_UP(def->mem.max_balloon, 1024)) < 0)
+    if (xenXMConfigSetInt(conf, "maxmem",
+                          VIR_DIV_UP(def->mem.max_balloon, 1024)) < 0)
         goto no_memory;

-    if (xenXMConfigSetInt(conf, "memory", VIR_DIV_UP(def->mem.cur_balloon, 1024)) < 0)
+    if (xenXMConfigSetInt(conf, "memory",
+                          VIR_DIV_UP(def->mem.cur_balloon, 1024)) < 0)
         goto no_memory;

     if (xenXMConfigSetInt(conf, "vcpus", def->maxvcpus) < 0)
-- 
1.7.7.6


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]