2016-07-20 18:16:23 +00:00
|
|
|
/*-
|
|
|
|
* BSD LICENSE
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>.
|
|
|
|
* Copyright (c) Intel Corporation.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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 Intel Corporation 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 THE COPYRIGHT HOLDERS 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 THE COPYRIGHT
|
|
|
|
* OWNER 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.
|
|
|
|
*/
|
|
|
|
|
2017-05-02 18:18:25 +00:00
|
|
|
#include "spdk/stdinc.h"
|
2016-07-20 18:16:23 +00:00
|
|
|
|
2017-07-13 04:08:53 +00:00
|
|
|
#include "bdev_malloc.h"
|
2016-07-20 18:16:23 +00:00
|
|
|
#include "spdk/bdev.h"
|
|
|
|
#include "spdk/conf.h"
|
2016-08-26 22:35:02 +00:00
|
|
|
#include "spdk/endian.h"
|
2016-08-17 20:35:18 +00:00
|
|
|
#include "spdk/env.h"
|
2016-07-20 18:16:23 +00:00
|
|
|
#include "spdk/copy_engine.h"
|
2016-09-16 19:53:32 +00:00
|
|
|
#include "spdk/io_channel.h"
|
2017-12-01 22:19:23 +00:00
|
|
|
#include "spdk/queue.h"
|
2017-06-02 17:25:43 +00:00
|
|
|
#include "spdk/string.h"
|
2016-07-20 18:16:23 +00:00
|
|
|
|
2017-01-04 22:30:04 +00:00
|
|
|
#include "spdk_internal/bdev.h"
|
2016-11-07 22:10:28 +00:00
|
|
|
#include "spdk_internal/log.h"
|
|
|
|
|
2016-07-20 18:16:23 +00:00
|
|
|
struct malloc_disk {
|
2017-12-01 22:19:23 +00:00
|
|
|
struct spdk_bdev disk;
|
|
|
|
void *malloc_buf;
|
|
|
|
TAILQ_ENTRY(malloc_disk) link;
|
2016-07-20 18:16:23 +00:00
|
|
|
};
|
|
|
|
|
2016-10-13 18:38:17 +00:00
|
|
|
struct malloc_task {
|
|
|
|
int num_outstanding;
|
|
|
|
enum spdk_bdev_io_status status;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct malloc_task *
|
2016-11-16 21:11:58 +00:00
|
|
|
__malloc_task_from_copy_task(struct spdk_copy_task *ct)
|
2016-10-13 18:38:17 +00:00
|
|
|
{
|
|
|
|
return (struct malloc_task *)((uintptr_t)ct - sizeof(struct malloc_task));
|
|
|
|
}
|
|
|
|
|
2016-11-16 21:11:58 +00:00
|
|
|
static struct spdk_copy_task *
|
2016-10-13 18:38:17 +00:00
|
|
|
__copy_task_from_malloc_task(struct malloc_task *mt)
|
|
|
|
{
|
2016-11-16 21:11:58 +00:00
|
|
|
return (struct spdk_copy_task *)((uintptr_t)mt + sizeof(struct malloc_task));
|
2016-10-13 18:38:17 +00:00
|
|
|
}
|
|
|
|
|
2016-07-20 18:16:23 +00:00
|
|
|
static void
|
|
|
|
malloc_done(void *ref, int status)
|
|
|
|
{
|
2016-10-13 18:38:17 +00:00
|
|
|
struct malloc_task *task = __malloc_task_from_copy_task(ref);
|
2016-07-20 18:16:23 +00:00
|
|
|
|
|
|
|
if (status != 0) {
|
bdev: add ENOMEM handling
At very high queue depths, bdev modules may not have enough
internal resources to track all of the incoming I/O. For example,
we allocate a finite number of nvme_request objects per allocated
queue pair. Currently if these resources are exhausted, the
bdev module will return failure (with no indication why) which
gets propagated all the way back to the application.
So instead, add SPDK_BDEV_IO_STATUS_NOMEM to allow bdev modules
to indicate this type of failure. Also add handling for this
status type in the generic bdev layer, involving queuing these
I/O for later retry after other I/O on the failing channel have
completed.
This does place an expectation on the bdev module that these
internal resources are allocated per io_channel. Otherwise we
cannot guarantee forward progress solely on reception of
completions. For example, without this guarantee, a bdev
module could theoretically return ENOMEM even if there were
no I/O oustanding for that io_channel. nvme, aio, rbd,
virtio and null drivers comply with this expectation already.
malloc only complies though when not using copy offload.
This patch will fix malloc w/ copy engine to at least
return ENOMEM when no copy descriptors are available. If the
condition above occurs, I/O waiting for resources will get
failed as part of a subsequent reset which matches the
behavior it has today.
Signed-off-by: Jim Harris <james.r.harris@intel.com>
Change-Id: Iea7cd51a611af8abe882794d0b2361fdbb74e84e
Reviewed-on: https://review.gerrithub.io/378853
Tested-by: SPDK Automated Test System <sys_sgsw@intel.com>
Reviewed-by: Daniel Verkamp <daniel.verkamp@intel.com>
Reviewed-by: Changpeng Liu <changpeng.liu@intel.com>
2017-09-15 20:47:17 +00:00
|
|
|
if (status == -ENOMEM) {
|
|
|
|
task->status = SPDK_BDEV_IO_STATUS_NOMEM;
|
|
|
|
} else {
|
|
|
|
task->status = SPDK_BDEV_IO_STATUS_FAILED;
|
|
|
|
}
|
2016-10-13 18:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (--task->num_outstanding == 0) {
|
|
|
|
spdk_bdev_io_complete(spdk_bdev_io_from_ctx(task), task->status);
|
2016-07-20 18:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-01 22:19:23 +00:00
|
|
|
static TAILQ_HEAD(, malloc_disk) g_malloc_disks = TAILQ_HEAD_INITIALIZER(g_malloc_disks);
|
2016-07-20 18:16:23 +00:00
|
|
|
|
|
|
|
int malloc_disk_count = 0;
|
|
|
|
|
2017-07-13 17:36:19 +00:00
|
|
|
static int bdev_malloc_initialize(void);
|
2017-07-13 04:08:53 +00:00
|
|
|
static void bdev_malloc_finish(void);
|
|
|
|
static void bdev_malloc_get_spdk_running_config(FILE *fp);
|
2016-07-20 18:16:23 +00:00
|
|
|
|
|
|
|
static int
|
2017-07-13 04:08:53 +00:00
|
|
|
bdev_malloc_get_ctx_size(void)
|
2016-07-20 18:16:23 +00:00
|
|
|
{
|
2016-11-16 21:23:57 +00:00
|
|
|
return sizeof(struct malloc_task) + spdk_copy_task_size();
|
2016-07-20 18:16:23 +00:00
|
|
|
}
|
|
|
|
|
2017-07-13 04:08:53 +00:00
|
|
|
SPDK_BDEV_MODULE_REGISTER(malloc, bdev_malloc_initialize, bdev_malloc_finish,
|
2017-07-13 04:06:22 +00:00
|
|
|
bdev_malloc_get_spdk_running_config, bdev_malloc_get_ctx_size, NULL)
|
2016-07-20 18:16:23 +00:00
|
|
|
|
2017-06-02 17:25:43 +00:00
|
|
|
static void
|
|
|
|
malloc_disk_free(struct malloc_disk *malloc_disk)
|
|
|
|
{
|
|
|
|
if (!malloc_disk) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(malloc_disk->disk.name);
|
|
|
|
spdk_dma_free(malloc_disk->malloc_buf);
|
|
|
|
spdk_dma_free(malloc_disk);
|
|
|
|
}
|
|
|
|
|
2016-07-20 18:16:23 +00:00
|
|
|
static int
|
2017-07-13 04:08:53 +00:00
|
|
|
bdev_malloc_destruct(void *ctx)
|
2016-07-20 18:16:23 +00:00
|
|
|
{
|
2017-04-04 21:10:00 +00:00
|
|
|
struct malloc_disk *malloc_disk = ctx;
|
2017-12-01 22:19:23 +00:00
|
|
|
|
|
|
|
TAILQ_REMOVE(&g_malloc_disks, malloc_disk, link);
|
2017-06-02 17:25:43 +00:00
|
|
|
malloc_disk_free(malloc_disk);
|
2016-07-20 18:16:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-04 14:39:27 +00:00
|
|
|
static int
|
2017-07-13 04:08:53 +00:00
|
|
|
bdev_malloc_check_iov_len(struct iovec *iovs, int iovcnt, size_t nbytes)
|
2016-10-04 14:39:27 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < iovcnt; i++) {
|
2017-12-07 23:23:48 +00:00
|
|
|
if (nbytes < iovs[i].iov_len) {
|
2016-10-04 14:39:27 +00:00
|
|
|
return 0;
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2016-10-04 14:39:27 +00:00
|
|
|
|
|
|
|
nbytes -= iovs[i].iov_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nbytes != 0;
|
|
|
|
}
|
|
|
|
|
2016-10-13 18:19:47 +00:00
|
|
|
static void
|
2017-07-13 04:08:53 +00:00
|
|
|
bdev_malloc_readv(struct malloc_disk *mdisk, struct spdk_io_channel *ch,
|
|
|
|
struct malloc_task *task,
|
|
|
|
struct iovec *iov, int iovcnt, size_t len, uint64_t offset)
|
2016-07-20 18:16:23 +00:00
|
|
|
{
|
2016-10-04 14:39:27 +00:00
|
|
|
int64_t res = 0;
|
|
|
|
void *src = mdisk->malloc_buf + offset;
|
|
|
|
int i;
|
2016-10-13 18:19:47 +00:00
|
|
|
|
2017-07-13 04:08:53 +00:00
|
|
|
if (bdev_malloc_check_iov_len(iov, iovcnt, len)) {
|
2016-10-04 14:39:27 +00:00
|
|
|
spdk_bdev_io_complete(spdk_bdev_io_from_ctx(task),
|
|
|
|
SPDK_BDEV_IO_STATUS_FAILED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-30 18:06:33 +00:00
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_BDEV_MALLOC, "read %lu bytes from offset %#lx\n",
|
2016-10-04 14:39:27 +00:00
|
|
|
len, offset);
|
2016-10-04 14:39:27 +00:00
|
|
|
|
2016-10-13 18:38:17 +00:00
|
|
|
task->status = SPDK_BDEV_IO_STATUS_SUCCESS;
|
2016-10-04 14:39:27 +00:00
|
|
|
task->num_outstanding = iovcnt;
|
2016-10-13 18:38:17 +00:00
|
|
|
|
2016-10-04 14:39:27 +00:00
|
|
|
for (i = 0; i < iovcnt; i++) {
|
|
|
|
res = spdk_copy_submit(__copy_task_from_malloc_task(task),
|
|
|
|
ch, iov[i].iov_base,
|
|
|
|
src, iov[i].iov_len, malloc_done);
|
2016-10-13 18:19:47 +00:00
|
|
|
|
2017-09-16 00:17:57 +00:00
|
|
|
if (res != 0) {
|
|
|
|
malloc_done(__copy_task_from_malloc_task(task), res);
|
2016-10-04 14:39:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
src += iov[i].iov_len;
|
|
|
|
len -= iov[i].iov_len;
|
2016-10-13 18:19:47 +00:00
|
|
|
}
|
2016-07-20 18:16:23 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 18:19:47 +00:00
|
|
|
static void
|
2017-07-13 04:08:53 +00:00
|
|
|
bdev_malloc_writev(struct malloc_disk *mdisk, struct spdk_io_channel *ch,
|
|
|
|
struct malloc_task *task,
|
|
|
|
struct iovec *iov, int iovcnt, size_t len, uint64_t offset)
|
2016-07-20 18:16:23 +00:00
|
|
|
{
|
2016-10-04 14:39:27 +00:00
|
|
|
int64_t res = 0;
|
|
|
|
void *dst = mdisk->malloc_buf + offset;
|
|
|
|
int i;
|
2016-10-13 18:19:47 +00:00
|
|
|
|
2017-07-13 04:08:53 +00:00
|
|
|
if (bdev_malloc_check_iov_len(iov, iovcnt, len)) {
|
2016-10-04 14:39:27 +00:00
|
|
|
spdk_bdev_io_complete(spdk_bdev_io_from_ctx(task),
|
|
|
|
SPDK_BDEV_IO_STATUS_FAILED);
|
2016-10-13 18:19:47 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-07-20 18:16:23 +00:00
|
|
|
|
2017-08-30 18:06:33 +00:00
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_BDEV_MALLOC, "wrote %lu bytes to offset %#lx\n",
|
2016-10-04 14:39:27 +00:00
|
|
|
len, offset);
|
2016-07-20 18:16:23 +00:00
|
|
|
|
2016-10-13 18:38:17 +00:00
|
|
|
task->status = SPDK_BDEV_IO_STATUS_SUCCESS;
|
2016-10-04 14:39:27 +00:00
|
|
|
task->num_outstanding = iovcnt;
|
2016-10-13 18:38:17 +00:00
|
|
|
|
2016-10-04 14:39:27 +00:00
|
|
|
for (i = 0; i < iovcnt; i++) {
|
|
|
|
res = spdk_copy_submit(__copy_task_from_malloc_task(task),
|
|
|
|
ch, dst, iov[i].iov_base,
|
|
|
|
iov[i].iov_len, malloc_done);
|
|
|
|
|
2017-09-16 00:17:57 +00:00
|
|
|
if (res != 0) {
|
|
|
|
malloc_done(__copy_task_from_malloc_task(task), res);
|
2016-10-04 14:39:27 +00:00
|
|
|
}
|
2016-10-13 18:19:47 +00:00
|
|
|
|
2016-10-04 14:39:27 +00:00
|
|
|
dst += iov[i].iov_len;
|
2016-10-13 18:19:47 +00:00
|
|
|
}
|
2016-07-20 18:16:23 +00:00
|
|
|
}
|
|
|
|
|
2016-08-26 22:35:02 +00:00
|
|
|
static int
|
2017-07-13 04:08:53 +00:00
|
|
|
bdev_malloc_unmap(struct malloc_disk *mdisk,
|
|
|
|
struct spdk_io_channel *ch,
|
|
|
|
struct malloc_task *task,
|
2017-07-19 21:32:04 +00:00
|
|
|
uint64_t offset,
|
|
|
|
uint64_t byte_count)
|
2016-08-26 22:35:02 +00:00
|
|
|
{
|
2016-10-13 18:38:17 +00:00
|
|
|
task->status = SPDK_BDEV_IO_STATUS_SUCCESS;
|
|
|
|
task->num_outstanding = 1;
|
|
|
|
|
|
|
|
return spdk_copy_submit_fill(__copy_task_from_malloc_task(task), ch,
|
|
|
|
mdisk->malloc_buf + offset, 0, byte_count, malloc_done);
|
2016-07-20 18:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t
|
2017-07-13 04:08:53 +00:00
|
|
|
bdev_malloc_flush(struct malloc_disk *mdisk, struct malloc_task *task,
|
|
|
|
uint64_t offset, uint64_t nbytes)
|
2016-07-20 18:16:23 +00:00
|
|
|
{
|
2016-10-13 18:38:17 +00:00
|
|
|
spdk_bdev_io_complete(spdk_bdev_io_from_ctx(task), SPDK_BDEV_IO_STATUS_SUCCESS);
|
2016-07-20 18:16:23 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2017-07-13 04:08:53 +00:00
|
|
|
bdev_malloc_reset(struct malloc_disk *mdisk, struct malloc_task *task)
|
2016-07-20 18:16:23 +00:00
|
|
|
{
|
2016-10-13 18:38:17 +00:00
|
|
|
spdk_bdev_io_complete(spdk_bdev_io_from_ctx(task), SPDK_BDEV_IO_STATUS_SUCCESS);
|
2016-07-20 18:16:23 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-13 04:08:53 +00:00
|
|
|
static int _bdev_malloc_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io)
|
2016-07-20 18:16:23 +00:00
|
|
|
{
|
2017-08-29 00:15:53 +00:00
|
|
|
uint32_t block_size = bdev_io->bdev->blocklen;
|
|
|
|
|
2016-07-20 18:16:23 +00:00
|
|
|
switch (bdev_io->type) {
|
|
|
|
case SPDK_BDEV_IO_TYPE_READ:
|
2017-09-20 13:10:17 +00:00
|
|
|
if (bdev_io->u.bdev.iovs[0].iov_base == NULL) {
|
|
|
|
assert(bdev_io->u.bdev.iovcnt == 1);
|
|
|
|
bdev_io->u.bdev.iovs[0].iov_base =
|
2017-05-05 20:06:39 +00:00
|
|
|
((struct malloc_disk *)bdev_io->bdev->ctxt)->malloc_buf +
|
2017-09-20 13:10:17 +00:00
|
|
|
bdev_io->u.bdev.offset_blocks * block_size;
|
|
|
|
bdev_io->u.bdev.iovs[0].iov_len = bdev_io->u.bdev.num_blocks * block_size;
|
2016-07-20 18:16:23 +00:00
|
|
|
spdk_bdev_io_complete(spdk_bdev_io_from_ctx(bdev_io->driver_ctx),
|
|
|
|
SPDK_BDEV_IO_STATUS_SUCCESS);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-13 04:08:53 +00:00
|
|
|
bdev_malloc_readv((struct malloc_disk *)bdev_io->bdev->ctxt,
|
|
|
|
ch,
|
|
|
|
(struct malloc_task *)bdev_io->driver_ctx,
|
2017-09-20 13:10:17 +00:00
|
|
|
bdev_io->u.bdev.iovs,
|
|
|
|
bdev_io->u.bdev.iovcnt,
|
|
|
|
bdev_io->u.bdev.num_blocks * block_size,
|
|
|
|
bdev_io->u.bdev.offset_blocks * block_size);
|
2016-10-13 18:19:47 +00:00
|
|
|
return 0;
|
2016-07-20 18:16:23 +00:00
|
|
|
|
|
|
|
case SPDK_BDEV_IO_TYPE_WRITE:
|
2017-07-13 04:08:53 +00:00
|
|
|
bdev_malloc_writev((struct malloc_disk *)bdev_io->bdev->ctxt,
|
|
|
|
ch,
|
|
|
|
(struct malloc_task *)bdev_io->driver_ctx,
|
2017-09-20 13:10:17 +00:00
|
|
|
bdev_io->u.bdev.iovs,
|
|
|
|
bdev_io->u.bdev.iovcnt,
|
|
|
|
bdev_io->u.bdev.num_blocks * block_size,
|
|
|
|
bdev_io->u.bdev.offset_blocks * block_size);
|
2016-10-13 18:19:47 +00:00
|
|
|
return 0;
|
2016-07-20 18:16:23 +00:00
|
|
|
|
|
|
|
case SPDK_BDEV_IO_TYPE_RESET:
|
2017-07-13 04:08:53 +00:00
|
|
|
return bdev_malloc_reset((struct malloc_disk *)bdev_io->bdev->ctxt,
|
|
|
|
(struct malloc_task *)bdev_io->driver_ctx);
|
2016-07-20 18:16:23 +00:00
|
|
|
|
|
|
|
case SPDK_BDEV_IO_TYPE_FLUSH:
|
2017-07-13 04:08:53 +00:00
|
|
|
return bdev_malloc_flush((struct malloc_disk *)bdev_io->bdev->ctxt,
|
|
|
|
(struct malloc_task *)bdev_io->driver_ctx,
|
2017-09-20 13:10:17 +00:00
|
|
|
bdev_io->u.bdev.offset_blocks * block_size,
|
|
|
|
bdev_io->u.bdev.num_blocks * block_size);
|
2016-08-26 22:35:02 +00:00
|
|
|
|
|
|
|
case SPDK_BDEV_IO_TYPE_UNMAP:
|
2017-07-13 04:08:53 +00:00
|
|
|
return bdev_malloc_unmap((struct malloc_disk *)bdev_io->bdev->ctxt,
|
|
|
|
ch,
|
|
|
|
(struct malloc_task *)bdev_io->driver_ctx,
|
2017-09-20 13:10:17 +00:00
|
|
|
bdev_io->u.bdev.offset_blocks * block_size,
|
|
|
|
bdev_io->u.bdev.num_blocks * block_size);
|
2017-08-11 19:04:21 +00:00
|
|
|
|
|
|
|
case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
|
|
|
|
/* bdev_malloc_unmap is implemented with a call to mem_cpy_fill which zeroes out all of the requested bytes. */
|
|
|
|
return bdev_malloc_unmap((struct malloc_disk *)bdev_io->bdev->ctxt,
|
|
|
|
ch,
|
|
|
|
(struct malloc_task *)bdev_io->driver_ctx,
|
2017-09-20 13:10:17 +00:00
|
|
|
bdev_io->u.bdev.offset_blocks * block_size,
|
|
|
|
bdev_io->u.bdev.num_blocks * block_size);
|
2017-08-11 19:04:21 +00:00
|
|
|
|
2016-07-20 18:16:23 +00:00
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-13 04:08:53 +00:00
|
|
|
static void bdev_malloc_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io)
|
2016-07-20 18:16:23 +00:00
|
|
|
{
|
2017-09-16 00:17:57 +00:00
|
|
|
if (_bdev_malloc_submit_request(ch, bdev_io) != 0) {
|
2016-07-20 18:16:23 +00:00
|
|
|
spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 17:25:49 +00:00
|
|
|
static bool
|
2017-07-13 04:08:53 +00:00
|
|
|
bdev_malloc_io_type_supported(void *ctx, enum spdk_bdev_io_type io_type)
|
2016-08-24 17:25:49 +00:00
|
|
|
{
|
|
|
|
switch (io_type) {
|
|
|
|
case SPDK_BDEV_IO_TYPE_READ:
|
|
|
|
case SPDK_BDEV_IO_TYPE_WRITE:
|
|
|
|
case SPDK_BDEV_IO_TYPE_FLUSH:
|
|
|
|
case SPDK_BDEV_IO_TYPE_RESET:
|
2016-08-26 22:35:02 +00:00
|
|
|
case SPDK_BDEV_IO_TYPE_UNMAP:
|
2017-08-11 19:04:21 +00:00
|
|
|
case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
|
2016-08-24 17:25:49 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-16 19:53:32 +00:00
|
|
|
static struct spdk_io_channel *
|
2017-07-13 04:08:53 +00:00
|
|
|
bdev_malloc_get_io_channel(void *ctx)
|
2016-09-16 19:53:32 +00:00
|
|
|
{
|
2017-05-18 17:48:04 +00:00
|
|
|
return spdk_copy_engine_get_io_channel();
|
2016-09-16 19:53:32 +00:00
|
|
|
}
|
|
|
|
|
2016-08-30 19:56:06 +00:00
|
|
|
static const struct spdk_bdev_fn_table malloc_fn_table = {
|
2017-07-13 04:08:53 +00:00
|
|
|
.destruct = bdev_malloc_destruct,
|
|
|
|
.submit_request = bdev_malloc_submit_request,
|
|
|
|
.io_type_supported = bdev_malloc_io_type_supported,
|
|
|
|
.get_io_channel = bdev_malloc_get_io_channel,
|
2016-07-20 18:16:23 +00:00
|
|
|
};
|
|
|
|
|
2017-10-06 22:00:28 +00:00
|
|
|
struct spdk_bdev *create_malloc_disk(const char *name, uint64_t num_blocks, uint32_t block_size)
|
2016-07-20 18:16:23 +00:00
|
|
|
{
|
|
|
|
struct malloc_disk *mdisk;
|
2017-11-20 09:31:39 +00:00
|
|
|
int rc;
|
2016-07-20 18:16:23 +00:00
|
|
|
|
|
|
|
if (block_size % 512 != 0) {
|
|
|
|
SPDK_ERRLOG("Block size %u is not a multiple of 512.\n", block_size);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_blocks == 0) {
|
|
|
|
SPDK_ERRLOG("Disk must be more than 0 blocks\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-05-25 18:21:30 +00:00
|
|
|
mdisk = spdk_dma_zmalloc(sizeof(*mdisk), 0, NULL);
|
2016-07-20 18:16:23 +00:00
|
|
|
if (!mdisk) {
|
2017-12-15 06:25:39 +00:00
|
|
|
SPDK_ERRLOG("mdisk spdk_dma_zmalloc() failed\n");
|
2016-07-20 18:16:23 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-08-17 20:35:18 +00:00
|
|
|
* Allocate the large backend memory buffer from pinned memory.
|
2016-07-20 18:16:23 +00:00
|
|
|
*
|
|
|
|
* TODO: need to pass a hint so we know which socket to allocate
|
|
|
|
* from on multi-socket systems.
|
|
|
|
*/
|
2017-05-25 18:21:30 +00:00
|
|
|
mdisk->malloc_buf = spdk_dma_zmalloc(num_blocks * block_size, 2 * 1024 * 1024, NULL);
|
2016-07-20 18:16:23 +00:00
|
|
|
if (!mdisk->malloc_buf) {
|
2017-12-15 06:25:39 +00:00
|
|
|
SPDK_ERRLOG("malloc_buf spdk_dma_zmalloc() failed\n");
|
2017-06-02 17:25:43 +00:00
|
|
|
malloc_disk_free(mdisk);
|
2016-07-20 18:16:23 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-10-06 22:00:28 +00:00
|
|
|
if (name) {
|
|
|
|
mdisk->disk.name = strdup(name);
|
|
|
|
} else {
|
|
|
|
/* Auto-generate a name */
|
|
|
|
mdisk->disk.name = spdk_sprintf_alloc("Malloc%d", malloc_disk_count);
|
|
|
|
malloc_disk_count++;
|
|
|
|
}
|
2017-06-02 17:25:43 +00:00
|
|
|
if (!mdisk->disk.name) {
|
|
|
|
malloc_disk_free(mdisk);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
mdisk->disk.product_name = "Malloc disk";
|
2016-07-20 18:16:23 +00:00
|
|
|
|
|
|
|
mdisk->disk.write_cache = 1;
|
|
|
|
mdisk->disk.blocklen = block_size;
|
|
|
|
mdisk->disk.blockcnt = num_blocks;
|
|
|
|
|
|
|
|
mdisk->disk.ctxt = mdisk;
|
|
|
|
mdisk->disk.fn_table = &malloc_fn_table;
|
2017-07-07 00:36:17 +00:00
|
|
|
mdisk->disk.module = SPDK_GET_BDEV_MODULE(malloc);
|
2016-07-20 18:16:23 +00:00
|
|
|
|
2017-11-20 09:31:39 +00:00
|
|
|
rc = spdk_bdev_register(&mdisk->disk);
|
|
|
|
if (rc) {
|
|
|
|
malloc_disk_free(mdisk);
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-07-20 18:16:23 +00:00
|
|
|
|
2017-12-01 22:19:23 +00:00
|
|
|
TAILQ_INSERT_TAIL(&g_malloc_disks, mdisk, link);
|
2016-07-20 18:16:23 +00:00
|
|
|
|
2016-10-25 23:19:53 +00:00
|
|
|
return &mdisk->disk;
|
2016-07-20 18:16:23 +00:00
|
|
|
}
|
|
|
|
|
2017-07-13 17:36:19 +00:00
|
|
|
static int bdev_malloc_initialize(void)
|
2016-07-20 18:16:23 +00:00
|
|
|
{
|
|
|
|
struct spdk_conf_section *sp = spdk_conf_find_section(NULL, "Malloc");
|
2017-05-26 04:58:04 +00:00
|
|
|
int NumberOfLuns, LunSizeInMB, BlockSize, i, rc = 0;
|
2016-07-20 18:16:23 +00:00
|
|
|
uint64_t size;
|
2016-10-25 23:19:53 +00:00
|
|
|
struct spdk_bdev *bdev;
|
2016-07-20 18:16:23 +00:00
|
|
|
|
|
|
|
if (sp != NULL) {
|
|
|
|
NumberOfLuns = spdk_conf_section_get_intval(sp, "NumberOfLuns");
|
|
|
|
LunSizeInMB = spdk_conf_section_get_intval(sp, "LunSizeInMB");
|
|
|
|
BlockSize = spdk_conf_section_get_intval(sp, "BlockSize");
|
|
|
|
if ((NumberOfLuns < 1) || (LunSizeInMB < 1)) {
|
|
|
|
SPDK_ERRLOG("Malloc section present, but no devices specified\n");
|
2017-05-26 04:58:04 +00:00
|
|
|
rc = EINVAL;
|
|
|
|
goto end;
|
2016-07-20 18:16:23 +00:00
|
|
|
}
|
|
|
|
if (BlockSize < 1) {
|
|
|
|
/* Default is 512 bytes */
|
|
|
|
BlockSize = 512;
|
|
|
|
}
|
|
|
|
size = (uint64_t)LunSizeInMB * 1024 * 1024;
|
|
|
|
for (i = 0; i < NumberOfLuns; i++) {
|
2017-10-06 22:00:28 +00:00
|
|
|
bdev = create_malloc_disk(NULL, size / BlockSize, BlockSize);
|
2016-10-25 23:19:53 +00:00
|
|
|
if (bdev == NULL) {
|
2016-07-20 18:16:23 +00:00
|
|
|
SPDK_ERRLOG("Could not create malloc disk\n");
|
2017-05-26 04:58:04 +00:00
|
|
|
rc = EINVAL;
|
|
|
|
goto end;
|
2016-07-20 18:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-26 04:58:04 +00:00
|
|
|
|
|
|
|
end:
|
2017-07-13 17:36:19 +00:00
|
|
|
return rc;
|
2016-07-20 18:16:23 +00:00
|
|
|
}
|
|
|
|
|
2017-07-13 04:08:53 +00:00
|
|
|
static void bdev_malloc_finish(void)
|
2016-07-20 18:16:23 +00:00
|
|
|
{
|
2017-12-01 22:19:23 +00:00
|
|
|
struct malloc_disk *mdisk, *tmp;
|
2016-07-20 18:16:23 +00:00
|
|
|
|
2017-12-01 22:19:23 +00:00
|
|
|
TAILQ_FOREACH_SAFE(mdisk, &g_malloc_disks, link, tmp) {
|
2017-12-01 00:34:38 +00:00
|
|
|
spdk_bdev_unregister(&mdisk->disk, NULL, NULL);
|
2016-07-20 18:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-07-13 04:08:53 +00:00
|
|
|
bdev_malloc_get_spdk_running_config(FILE *fp)
|
2016-07-20 18:16:23 +00:00
|
|
|
{
|
|
|
|
int num_malloc_luns = 0;
|
|
|
|
uint64_t malloc_lun_size = 0;
|
2017-12-01 22:19:23 +00:00
|
|
|
struct malloc_disk *mdisk;
|
2016-07-20 18:16:23 +00:00
|
|
|
|
|
|
|
/* count number of malloc LUNs, get LUN size */
|
2017-12-01 22:19:23 +00:00
|
|
|
TAILQ_FOREACH(mdisk, &g_malloc_disks, link) {
|
2016-07-20 18:16:23 +00:00
|
|
|
if (0 == malloc_lun_size) {
|
|
|
|
/* assume all malloc luns the same size */
|
|
|
|
malloc_lun_size = mdisk->disk.blocklen * mdisk->disk.blockcnt;
|
|
|
|
malloc_lun_size /= (1024 * 1024);
|
|
|
|
}
|
|
|
|
num_malloc_luns++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_malloc_luns > 0) {
|
|
|
|
fprintf(fp,
|
|
|
|
"\n"
|
|
|
|
"# Users may change this section to create a different number or size of\n"
|
|
|
|
"# malloc LUNs.\n"
|
|
|
|
"# This will generate %d LUNs with a malloc-allocated backend. Each LUN \n"
|
|
|
|
"# will be %" PRIu64 "MB in size and these will be named Malloc0 through Malloc%d.\n"
|
|
|
|
"# Not all LUNs defined here are necessarily used below.\n"
|
|
|
|
"[Malloc]\n"
|
|
|
|
" NumberOfLuns %d\n"
|
|
|
|
" LunSizeInMB %" PRIu64 "\n",
|
|
|
|
num_malloc_luns, malloc_lun_size,
|
|
|
|
num_malloc_luns - 1, num_malloc_luns,
|
|
|
|
malloc_lun_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-30 18:06:33 +00:00
|
|
|
SPDK_LOG_REGISTER_COMPONENT("bdev_malloc", SPDK_LOG_BDEV_MALLOC)
|