1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
/* nbdkit
* Copyright Red Hat
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Red Hat nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
#include <pthread.h>
#include <nbdkit-plugin.h>
#include "cleanup.h"
#include "vector.h"
#include "allocator.h"
#include "allocator-internal.h"
/* This allocator implements a direct-mapped non-sparse RAM disk using
* malloc, with optional mlock.
*/
DEFINE_VECTOR_TYPE (bytearray, uint8_t);
struct m_alloc {
struct allocator a; /* Must come first. */
bool use_mlock;
/* Byte array (vector) implementing the direct-mapped disk. Note we
* don't use the .size field. Accesses must be protected by the
* lock since writes may try to extend the array.
*/
pthread_rwlock_t lock;
bytearray ba;
};
static void
m_alloc_free (struct allocator *a)
{
struct m_alloc *ma = (struct m_alloc *) a;
if (ma) {
free (ma->ba.ptr);
pthread_rwlock_destroy (&ma->lock);
free (ma);
}
}
/* Extend the underlying bytearray if needed. */
static int
extend_without_mlock (struct m_alloc *ma, uint64_t new_size)
{
size_t old_size, n;
if (ma->ba.cap < new_size) {
old_size = ma->ba.cap;
n = new_size - ma->ba.cap;
if (bytearray_reserve (&ma->ba, n) == -1) {
nbdkit_error ("realloc: %m");
return -1;
}
/* Initialize the newly allocated memory to 0. */
memset (ma->ba.ptr + old_size, 0, n);
}
return 0;
}
#ifdef HAVE_MLOCK
static int
extend_with_mlock (struct m_alloc *ma, uint64_t new_size)
{
size_t old_size, n;
if (ma->ba.cap < new_size) {
old_size = ma->ba.cap;
n = new_size - ma->ba.cap;
#ifdef HAVE_MUNLOCK
/* Since the memory might be moved by realloc, we must unlock the
* original array.
*/
if (ma->use_mlock && ma->ba.ptr != NULL)
munlock (ma->ba.ptr, ma->ba.cap);
#endif
if (bytearray_reserve_page_aligned (&ma->ba, n) == -1) {
nbdkit_error ("realloc: %m");
return -1;
}
/* Initialize the newly allocated memory to 0. */
memset (ma->ba.ptr + old_size, 0, n);
if (mlock (ma->ba.ptr, ma->ba.cap) == -1) {
nbdkit_error ("allocator=malloc: mlock: %m");
return -1;
}
}
return 0;
}
#endif /* HAVE_MLOCK */
static int
extend (struct m_alloc *ma, uint64_t new_size)
{
ACQUIRE_WRLOCK_FOR_CURRENT_SCOPE (&ma->lock);
#ifdef HAVE_MLOCK
if (ma->use_mlock)
return extend_with_mlock (ma, new_size);
#endif
return extend_without_mlock (ma, new_size);
}
static int
m_alloc_set_size_hint (struct allocator *a, uint64_t size_hint)
{
struct m_alloc *ma = (struct m_alloc *) a;
return extend (ma, size_hint);
}
static int
m_alloc_read (struct allocator *a, void *buf,
uint64_t count, uint64_t offset)
{
struct m_alloc *ma = (struct m_alloc *) a;
ACQUIRE_RDLOCK_FOR_CURRENT_SCOPE (&ma->lock);
/* Avoid reading beyond the end of the allocated array. Return
* zeroes for that part.
*/
if (offset >= ma->ba.cap)
memset (buf, 0, count);
else if (offset + count > ma->ba.cap) {
memcpy (buf, ma->ba.ptr + offset, ma->ba.cap - offset);
memset (buf + ma->ba.cap - offset, 0, offset + count - ma->ba.cap);
}
else
memcpy (buf, ma->ba.ptr + offset, count);
return 0;
}
static int
m_alloc_write (struct allocator *a, const void *buf,
uint64_t count, uint64_t offset)
{
struct m_alloc *ma = (struct m_alloc *) a;
if (extend (ma, offset+count) == -1)
return -1;
/* This is correct: Even though we are writing, we only need to
* acquire the read lock here. The write lock applies to changing
* the metadata and it was acquired if we called extend().
*/
ACQUIRE_RDLOCK_FOR_CURRENT_SCOPE (&ma->lock);
memcpy (ma->ba.ptr + offset, buf, count);
return 0;
}
static int
m_alloc_fill (struct allocator *a, char c, uint64_t count, uint64_t offset)
{
struct m_alloc *ma = (struct m_alloc *) a;
if (extend (ma, offset+count) == -1)
return -1;
/* See comment in m_alloc_write. */
ACQUIRE_RDLOCK_FOR_CURRENT_SCOPE (&ma->lock);
memset (ma->ba.ptr + offset, c, count);
return 0;
}
static int
m_alloc_zero (struct allocator *a, uint64_t count, uint64_t offset)
{
struct m_alloc *ma = (struct m_alloc *) a;
ACQUIRE_RDLOCK_FOR_CURRENT_SCOPE (&ma->lock);
/* Try to avoid extending the array, since the unallocated part
* always reads as zero.
*/
if (offset < ma->ba.cap) {
if (offset + count > ma->ba.cap)
memset (ma->ba.ptr + offset, 0, ma->ba.cap - offset);
else
memset (ma->ba.ptr + offset, 0, count);
}
return 0;
}
static int
m_alloc_blit (struct allocator *a1, struct allocator *a2,
uint64_t count, uint64_t offset1, uint64_t offset2)
{
struct m_alloc *ma2 = (struct m_alloc *) a2;
assert (a1 != a2);
assert (strcmp (a2->f->type, "malloc") == 0);
if (extend (ma2, offset2+count) == -1)
return -1;
/* See comment in m_alloc_write. */
ACQUIRE_RDLOCK_FOR_CURRENT_SCOPE (&ma2->lock);
return a1->f->read (a1, ma2->ba.ptr + offset2, count, offset1);
}
static int
m_alloc_extents (struct allocator *a,
uint64_t count, uint64_t offset,
struct nbdkit_extents *extents)
{
/* Always fully allocated. XXX In theory we could detect zeroes
* quite quickly and return that information, allowing the client to
* avoid reads. However we'd probably want to store a bitmap of
* which sectors we are known to have written to, and that
* complicates the implementation quite a lot.
*/
return nbdkit_add_extent (extents, offset, count, 0);
}
struct allocator *
m_alloc_create (const void *paramsv)
{
const allocator_parameters *params = paramsv;
struct m_alloc *ma;
bool use_mlock = false;
size_t i;
/* Parse the optional mlock=true|false parameter. */
for (i = 0; i < params->len; ++i) {
if (strcmp (params->ptr[i].key, "mlock") == 0) {
int r = nbdkit_parse_bool (params->ptr[i].value);
if (r == -1) return NULL;
use_mlock = r;
#ifndef HAVE_MLOCK
if (use_mlock) {
nbdkit_error ("mlock is not supported on this platform");
return NULL;
}
#endif
}
else {
nbdkit_error ("allocator=malloc: unknown parameter %s",
params->ptr[i].key);
return NULL;
}
}
ma = calloc (1, sizeof *ma);
if (ma == NULL) {
nbdkit_error ("calloc: %m");
return NULL;
}
ma->use_mlock = use_mlock;
pthread_rwlock_init (&ma->lock, NULL);
ma->ba = (bytearray) empty_vector;
return (struct allocator *) ma;
}
static struct allocator_functions functions = {
.type = "malloc",
.preferred = 512, /* Not really any, so return a general default. */
.create = m_alloc_create,
.free = m_alloc_free,
.set_size_hint = m_alloc_set_size_hint,
.read = m_alloc_read,
.write = m_alloc_write,
.fill = m_alloc_fill,
.zero = m_alloc_zero,
.blit = m_alloc_blit,
.extents = m_alloc_extents,
};
static void register_malloc (void) __attribute__ ((constructor));
static void
register_malloc (void)
{
register_allocator (&functions);
}
|