qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Haozhong Zhang <haozhong.zhang@intel.com>
To: qemu-devel@nongnu.org
Cc: Eduardo Habkost <ehabkost@redhat.com>,
	Igor Mammedov <imammedo@redhat.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Peter Crosthwaite <crosthwaite.peter@gmail.com>,
	Richard Henderson <rth@twiddle.net>,
	Haozhong Zhang <haozhong.zhang@intel.com>
Subject: [Qemu-devel] [PATCH 3/3] hostmem-file: make option 'size' optional
Date: Mon,  7 Nov 2016 13:08:59 +0800	[thread overview]
Message-ID: <20161107050859.31058-4-haozhong.zhang@intel.com> (raw)
In-Reply-To: <20161107050859.31058-1-haozhong.zhang@intel.com>

If 'size' option is not specified, QEMU will use the file size of
'mem-path' instead. If an empty file, a non-existing file or a directory
is specified by option 'mem-path', a non-zero option 'size' is still
needed.

Signed-off-by: Haozhong Zhang <haozhong.zhang@intel.com>
---
 backends/hostmem-file.c | 28 ++++++++++++++++++++--------
 exec.c                  | 27 +++++++++++++++++++++++++++
 2 files changed, 47 insertions(+), 8 deletions(-)

diff --git a/backends/hostmem-file.c b/backends/hostmem-file.c
index 42efb2f..56cc96b 100644
--- a/backends/hostmem-file.c
+++ b/backends/hostmem-file.c
@@ -39,17 +39,14 @@ static void
 file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
 {
     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend);
+    Error *local_err = NULL;
 
-    if (!backend->size) {
-        error_setg(errp, "can't create backend with size 0");
-        return;
-    }
     if (!fb->mem_path) {
-        error_setg(errp, "mem-path property not set");
-        return;
+        error_setg(&local_err, "mem-path property not set");
+        goto out;
     }
 #ifndef CONFIG_LINUX
-    error_setg(errp, "-mem-path not supported on this host");
+    error_setg(&local_err, "-mem-path not supported on this host");
 #else
     if (!memory_region_size(&backend->mr)) {
         gchar *path;
@@ -58,10 +55,25 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
         memory_region_init_ram_from_file(&backend->mr, OBJECT(backend),
                                  path,
                                  backend->size, fb->share,
-                                 fb->mem_path, errp);
+                                 fb->mem_path, &local_err);
         g_free(path);
+
+        if (local_err) {
+            goto out;
+        }
+
+        if (!backend->size) {
+            backend->size = memory_region_size(&backend->mr);
+        }
+    }
+
+    if (!backend->size) {
+        error_setg(&local_err, "can't create backend with size 0");
     }
 #endif
+
+ out:
+    error_propagate(errp, local_err);
 }
 
 static char *get_mem_path(Object *o, Error **errp)
diff --git a/exec.c b/exec.c
index 42045aa..b81c9a8 100644
--- a/exec.c
+++ b/exec.c
@@ -1676,6 +1676,7 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
     int fd = -1;
     bool unlink_on_error = false;
     int64_t file_size;
+    uint64_t mr_size;
     Error *local_err = NULL;
 
     if (xen_enabled()) {
@@ -1704,6 +1705,32 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
      */
     file_size = get_file_size(fd);
 
+    if (!size && file_size >= 0) {
+        if (!file_size) {
+            error_setg(&local_err,
+                       "%s is empty or a directory, 'size' option must be specified",
+                       mem_path);
+            goto out;
+        }
+
+        mr_size = memory_region_size(mr);
+        if (mr_size && mr_size != size) {
+            error_setg(&local_err, "cannot resize non-empty memory region");
+            goto out;
+        }
+        if (!mr_size) {
+            memory_region_set_size(mr, file_size);
+        }
+        size = file_size;
+    }
+
+    if (!size) {
+        error_setg(&local_err,
+                   "cannot get size of %s, 'size' option must be specified",
+                   mem_path);
+        goto out;
+    }
+
     size = HOST_PAGE_ALIGN(size);
     if (file_size > 0 && file_size < size) {
         error_setg(&local_err, "backing store %s size 0x%"PRIx64
-- 
2.10.1

  parent reply	other threads:[~2016-11-07  5:09 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-07  5:08 [Qemu-devel] [PATCH 0/3] hostmem-file: make option 'size' optional Haozhong Zhang
2016-11-07  5:08 ` [Qemu-devel] [PATCH 1/3] exec.c: add comment for errors of get_file_size() Haozhong Zhang
2016-11-07  5:08 ` [Qemu-devel] [PATCH 2/3] exec.c: move file operations to qemu_ram_alloc_from_file() Haozhong Zhang
2016-11-07  5:08 ` Haozhong Zhang [this message]
2016-11-07  7:52 ` [Qemu-devel] [PATCH 0/3] hostmem-file: make option 'size' optional Markus Armbruster
2016-11-07  7:56   ` Haozhong Zhang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20161107050859.31058-4-haozhong.zhang@intel.com \
    --to=haozhong.zhang@intel.com \
    --cc=crosthwaite.peter@gmail.com \
    --cc=ehabkost@redhat.com \
    --cc=imammedo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=rth@twiddle.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).