[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 2/9] exec: split file_ram_alloc()
From: |
marcandre . lureau |
Subject: |
[Qemu-devel] [PATCH 2/9] exec: split file_ram_alloc() |
Date: |
Tue, 12 Apr 2016 13:55:22 +0200 |
From: Marc-André Lureau <address@hidden>
Move file opening part in a seperate function file_ram_open(). This
allows for reuse of file_ram_alloc() with only a fd.
Signed-off-by: Marc-André Lureau <address@hidden>
---
exec.c | 58 ++++++++++++++++++++++++++++++++++------------------------
1 file changed, 34 insertions(+), 24 deletions(-)
diff --git a/exec.c b/exec.c
index dcc6632..422ea3e 100644
--- a/exec.c
+++ b/exec.c
@@ -1229,19 +1229,17 @@ void qemu_mutex_unlock_ramlist(void)
}
#ifdef __linux__
-static void *file_ram_alloc(RAMBlock *block,
- ram_addr_t memory,
- const char *path,
- Error **errp)
+static int file_ram_open(const char *path,
+ const char *region_name,
+ bool *created,
+ Error **errp)
{
- bool unlink_on_error = false;
- char *filename;
char *sanitized_name;
+ char *filename;
char *c;
- void *area;
int fd = -1;
- int64_t page_size;
+ *created = false;
for (;;) {
fd = open(path, O_RDWR);
if (fd >= 0) {
@@ -1252,13 +1250,13 @@ static void *file_ram_alloc(RAMBlock *block,
/* @path names a file that doesn't exist, create it */
fd = open(path, O_RDWR | O_CREAT | O_EXCL, 0644);
if (fd >= 0) {
- unlink_on_error = true;
+ *created = true;
break;
}
} else if (errno == EISDIR) {
/* @path names a directory, create a file there */
/* Make name safe to use with mkstemp by replacing '/' with '_'. */
- sanitized_name = g_strdup(memory_region_name(block->mr));
+ sanitized_name = g_strdup(region_name);
for (c = sanitized_name; *c != '\0'; c++) {
if (*c == '/') {
*c = '_';
@@ -1281,7 +1279,7 @@ static void *file_ram_alloc(RAMBlock *block,
error_setg_errno(errp, errno,
"can't open backing store %s for guest RAM",
path);
- goto error;
+ return -1;
}
/*
* Try again on EINTR and EEXIST. The latter happens when
@@ -1289,6 +1287,17 @@ static void *file_ram_alloc(RAMBlock *block,
*/
}
+ return fd;
+}
+
+static void *file_ram_alloc(RAMBlock *block,
+ ram_addr_t memory,
+ int fd,
+ Error **errp)
+{
+ void *area;
+ int64_t page_size;
+
page_size = qemu_fd_getpagesize(fd);
block->mr->align = page_size;
@@ -1296,7 +1305,7 @@ static void *file_ram_alloc(RAMBlock *block,
error_setg(errp, "memory size 0x" RAM_ADDR_FMT " must be equal to "
"or larger than page size 0x%" PRIx64,
memory, page_size);
- goto error;
+ return NULL;
}
memory = ROUND_UP(memory, page_size);
@@ -1315,7 +1324,7 @@ static void *file_ram_alloc(RAMBlock *block,
if (area == MAP_FAILED) {
error_setg_errno(errp, errno,
"unable to map backing store for guest RAM");
- goto error;
+ return NULL;
}
if (mem_prealloc) {
@@ -1324,15 +1333,6 @@ static void *file_ram_alloc(RAMBlock *block,
block->fd = fd;
return area;
-
-error:
- if (unlink_on_error) {
- unlink(path);
- }
- if (fd != -1) {
- close(fd);
- }
- return NULL;
}
#endif
@@ -1652,6 +1652,8 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size,
MemoryRegion *mr,
{
RAMBlock *new_block;
Error *local_err = NULL;
+ int fd;
+ bool created;
if (xen_enabled()) {
error_setg(errp, "-mem-path not supported with Xen");
@@ -1675,15 +1677,23 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size,
MemoryRegion *mr,
return NULL;
}
+ fd = file_ram_open(mem_path, memory_region_name(mr), &created, errp);
+ if (fd < 0) {
+ return NULL;
+ }
+
size = HOST_PAGE_ALIGN(size);
new_block = g_malloc0(sizeof(*new_block));
new_block->mr = mr;
new_block->used_length = size;
new_block->max_length = size;
new_block->flags = share ? RAM_SHARED : 0;
- new_block->host = file_ram_alloc(new_block, size,
- mem_path, errp);
+ new_block->host = file_ram_alloc(new_block, size, fd, errp);
if (!new_block->host) {
+ if (created) {
+ unlink(mem_path);
+ }
+ close(fd);
g_free(new_block);
return NULL;
}
--
2.5.5
- [Qemu-devel] [PATCH 0/9] RFC: add memfd memory backend, marcandre . lureau, 2016/04/12
- [Qemu-devel] [PATCH 1/9] exec: check kvm mmu notifiers earlier, marcandre . lureau, 2016/04/12
- [Qemu-devel] [PATCH 2/9] exec: split file_ram_alloc(),
marcandre . lureau <=
- [Qemu-devel] [PATCH 3/9] exec: split qemu_ram_alloc_from_file(), marcandre . lureau, 2016/04/12
- [Qemu-devel] [PATCH 5/9] RFC: ivshmem: use ram_from_fd(), marcandre . lureau, 2016/04/12
- [Qemu-devel] [PATCH 4/9] Add memory_region_init_ram_from_fd(), marcandre . lureau, 2016/04/12
- [Qemu-devel] [PATCH 6/9] exec: remove qemu_set_ram_fd(), marcandre . lureau, 2016/04/12
- [Qemu-devel] [PATCH 7/9] memfd: split qemu_memfd_alloc(), marcandre . lureau, 2016/04/12
- [Qemu-devel] [PATCH 8/9] Add memfd based hostmem, marcandre . lureau, 2016/04/12
- [Qemu-devel] [PATCH 9/9] tests: use memfd in vhost-user-test, marcandre . lureau, 2016/04/12