Merge remote-tracking branch 'remotes/ericb/tags/pull-nbd-2019-11-19' into staging
[qemu.git] / backends / hostmem-file.c
blobbe64020746d100f84e7353cf84849689bac3bb15
1 /*
2 * QEMU Host Memory Backend for hugetlbfs
4 * Copyright (C) 2013-2014 Red Hat Inc
6 * Authors:
7 * Paolo Bonzini <pbonzini@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "qemu/error-report.h"
16 #include "qemu/module.h"
17 #include "sysemu/hostmem.h"
18 #include "sysemu/sysemu.h"
19 #include "qom/object_interfaces.h"
21 /* hostmem-file.c */
22 /**
23 * @TYPE_MEMORY_BACKEND_FILE:
24 * name of backend that uses mmap on a file descriptor
26 #define TYPE_MEMORY_BACKEND_FILE "memory-backend-file"
28 #define MEMORY_BACKEND_FILE(obj) \
29 OBJECT_CHECK(HostMemoryBackendFile, (obj), TYPE_MEMORY_BACKEND_FILE)
31 typedef struct HostMemoryBackendFile HostMemoryBackendFile;
33 struct HostMemoryBackendFile {
34 HostMemoryBackend parent_obj;
36 char *mem_path;
37 uint64_t align;
38 bool discard_data;
39 bool is_pmem;
42 static void
43 file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
45 #ifndef CONFIG_POSIX
46 error_setg(errp, "backend '%s' not supported on this host",
47 object_get_typename(OBJECT(backend)));
48 #else
49 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend);
50 gchar *name;
52 if (!backend->size) {
53 error_setg(errp, "can't create backend with size 0");
54 return;
56 if (!fb->mem_path) {
57 error_setg(errp, "mem-path property not set");
58 return;
61 backend->force_prealloc = mem_prealloc;
62 name = host_memory_backend_get_name(backend);
63 memory_region_init_ram_from_file(&backend->mr, OBJECT(backend),
64 name,
65 backend->size, fb->align,
66 (backend->share ? RAM_SHARED : 0) |
67 (fb->is_pmem ? RAM_PMEM : 0),
68 fb->mem_path, errp);
69 g_free(name);
70 #endif
73 static char *get_mem_path(Object *o, Error **errp)
75 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
77 return g_strdup(fb->mem_path);
80 static void set_mem_path(Object *o, const char *str, Error **errp)
82 HostMemoryBackend *backend = MEMORY_BACKEND(o);
83 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
85 if (host_memory_backend_mr_inited(backend)) {
86 error_setg(errp, "cannot change property 'mem-path' of %s",
87 object_get_typename(o));
88 return;
90 g_free(fb->mem_path);
91 fb->mem_path = g_strdup(str);
94 static bool file_memory_backend_get_discard_data(Object *o, Error **errp)
96 return MEMORY_BACKEND_FILE(o)->discard_data;
99 static void file_memory_backend_set_discard_data(Object *o, bool value,
100 Error **errp)
102 MEMORY_BACKEND_FILE(o)->discard_data = value;
105 static void file_memory_backend_get_align(Object *o, Visitor *v,
106 const char *name, void *opaque,
107 Error **errp)
109 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
110 uint64_t val = fb->align;
112 visit_type_size(v, name, &val, errp);
115 static void file_memory_backend_set_align(Object *o, Visitor *v,
116 const char *name, void *opaque,
117 Error **errp)
119 HostMemoryBackend *backend = MEMORY_BACKEND(o);
120 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
121 Error *local_err = NULL;
122 uint64_t val;
124 if (host_memory_backend_mr_inited(backend)) {
125 error_setg(&local_err, "cannot change property '%s' of %s",
126 name, object_get_typename(o));
127 goto out;
130 visit_type_size(v, name, &val, &local_err);
131 if (local_err) {
132 goto out;
134 fb->align = val;
136 out:
137 error_propagate(errp, local_err);
140 static bool file_memory_backend_get_pmem(Object *o, Error **errp)
142 return MEMORY_BACKEND_FILE(o)->is_pmem;
145 static void file_memory_backend_set_pmem(Object *o, bool value, Error **errp)
147 HostMemoryBackend *backend = MEMORY_BACKEND(o);
148 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
150 if (host_memory_backend_mr_inited(backend)) {
152 error_setg(errp, "cannot change property 'pmem' of %s.",
153 object_get_typename(o));
154 return;
157 #ifndef CONFIG_LIBPMEM
158 if (value) {
159 Error *local_err = NULL;
161 error_setg(&local_err,
162 "Lack of libpmem support while setting the 'pmem=on'"
163 " of %s. We can't ensure data persistence.",
164 object_get_typename(o));
165 error_propagate(errp, local_err);
166 return;
168 #endif
170 fb->is_pmem = value;
173 static void file_backend_unparent(Object *obj)
175 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
176 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
178 if (host_memory_backend_mr_inited(backend) && fb->discard_data) {
179 void *ptr = memory_region_get_ram_ptr(&backend->mr);
180 uint64_t sz = memory_region_size(&backend->mr);
182 qemu_madvise(ptr, sz, QEMU_MADV_REMOVE);
186 static void
187 file_backend_class_init(ObjectClass *oc, void *data)
189 HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
191 bc->alloc = file_backend_memory_alloc;
192 oc->unparent = file_backend_unparent;
194 object_class_property_add_bool(oc, "discard-data",
195 file_memory_backend_get_discard_data, file_memory_backend_set_discard_data,
196 &error_abort);
197 object_class_property_add_str(oc, "mem-path",
198 get_mem_path, set_mem_path,
199 &error_abort);
200 object_class_property_add(oc, "align", "int",
201 file_memory_backend_get_align,
202 file_memory_backend_set_align,
203 NULL, NULL, &error_abort);
204 object_class_property_add_bool(oc, "pmem",
205 file_memory_backend_get_pmem, file_memory_backend_set_pmem,
206 &error_abort);
209 static void file_backend_instance_finalize(Object *o)
211 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
213 g_free(fb->mem_path);
216 static const TypeInfo file_backend_info = {
217 .name = TYPE_MEMORY_BACKEND_FILE,
218 .parent = TYPE_MEMORY_BACKEND,
219 .class_init = file_backend_class_init,
220 .instance_finalize = file_backend_instance_finalize,
221 .instance_size = sizeof(HostMemoryBackendFile),
224 static void register_types(void)
226 type_register_static(&file_backend_info);
229 type_init(register_types);