diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:54:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:54:28 +0000 |
commit | e6918187568dbd01842d8d1d2c808ce16a894239 (patch) | |
tree | 64f88b554b444a49f656b6c656111a145cbbaa28 /src/spdk/module/bdev/ftl | |
parent | Initial commit. (diff) | |
download | ceph-e6918187568dbd01842d8d1d2c808ce16a894239.tar.xz ceph-e6918187568dbd01842d8d1d2c808ce16a894239.zip |
Adding upstream version 18.2.2.upstream/18.2.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/spdk/module/bdev/ftl')
-rw-r--r-- | src/spdk/module/bdev/ftl/Makefile | 45 | ||||
-rw-r--r-- | src/spdk/module/bdev/ftl/bdev_ftl.c | 517 | ||||
-rw-r--r-- | src/spdk/module/bdev/ftl/bdev_ftl.h | 70 | ||||
-rw-r--r-- | src/spdk/module/bdev/ftl/bdev_ftl_rpc.c | 258 |
4 files changed, 890 insertions, 0 deletions
diff --git a/src/spdk/module/bdev/ftl/Makefile b/src/spdk/module/bdev/ftl/Makefile new file mode 100644 index 000000000..d0bfe1078 --- /dev/null +++ b/src/spdk/module/bdev/ftl/Makefile @@ -0,0 +1,45 @@ +# +# BSD LICENSE +# +# 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. +# + +SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../..) +include $(SPDK_ROOT_DIR)/mk/spdk.common.mk + +SO_VER := 2 +SO_MINOR := 0 + +C_SRCS += bdev_ftl.c bdev_ftl_rpc.c +LIBNAME = bdev_ftl + +SPDK_MAP_FILE = $(SPDK_ROOT_DIR)/mk/spdk_blank.map + +include $(SPDK_ROOT_DIR)/mk/spdk.lib.mk diff --git a/src/spdk/module/bdev/ftl/bdev_ftl.c b/src/spdk/module/bdev/ftl/bdev_ftl.c new file mode 100644 index 000000000..e959c8677 --- /dev/null +++ b/src/spdk/module/bdev/ftl/bdev_ftl.c @@ -0,0 +1,517 @@ +/*- + * BSD LICENSE + * + * 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. + */ + +#include "spdk/stdinc.h" +#include "spdk/bdev.h" +#include "spdk/conf.h" +#include "spdk/env.h" +#include "spdk/thread.h" +#include "spdk/json.h" +#include "spdk/string.h" +#include "spdk/likely.h" +#include "spdk/util.h" +#include "spdk/string.h" +#include "spdk/ftl.h" +#include "spdk_internal/log.h" + +#include "bdev_ftl.h" + +struct ftl_bdev { + struct spdk_bdev bdev; + + struct spdk_ftl_dev *dev; + + ftl_bdev_init_fn init_cb; + + void *init_arg; +}; + +struct ftl_deferred_init { + struct ftl_bdev_init_opts opts; + + LIST_ENTRY(ftl_deferred_init) entry; +}; + +static LIST_HEAD(, ftl_deferred_init) g_deferred_init = LIST_HEAD_INITIALIZER(g_deferred_init); + +static int bdev_ftl_initialize(void); +static void bdev_ftl_finish(void); +static void bdev_ftl_examine(struct spdk_bdev *bdev); + +static struct spdk_bdev_module g_ftl_if = { + .name = "ftl", + .module_init = bdev_ftl_initialize, + .module_fini = bdev_ftl_finish, + .examine_disk = bdev_ftl_examine, +}; + +SPDK_BDEV_MODULE_REGISTER(ftl, &g_ftl_if) + +static void +bdev_ftl_free_cb(struct spdk_ftl_dev *dev, void *ctx, int status) +{ + struct ftl_bdev *ftl_bdev = ctx; + + spdk_bdev_destruct_done(&ftl_bdev->bdev, status); + free(ftl_bdev->bdev.name); + free(ftl_bdev); +} + +static int +bdev_ftl_destruct(void *ctx) +{ + struct ftl_bdev *ftl_bdev = ctx; + spdk_ftl_dev_free(ftl_bdev->dev, bdev_ftl_free_cb, ftl_bdev); + + /* return 1 to indicate that the destruction is asynchronous */ + return 1; +} + +static void +bdev_ftl_cb(void *arg, int rc) +{ + struct spdk_bdev_io *bdev_io = arg; + enum spdk_bdev_io_status status; + + switch (rc) { + case 0: + status = SPDK_BDEV_IO_STATUS_SUCCESS; + break; + case -ENOMEM: + status = SPDK_BDEV_IO_STATUS_NOMEM; + break; + default: + status = SPDK_BDEV_IO_STATUS_FAILED; + break; + } + + spdk_bdev_io_complete(bdev_io, status); +} + +static void +bdev_ftl_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io, + bool success) +{ + struct ftl_bdev *ftl_bdev; + int rc; + + ftl_bdev = bdev_io->bdev->ctxt; + + if (!success) { + spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); + return; + } + + rc = spdk_ftl_read(ftl_bdev->dev, + ch, + bdev_io->u.bdev.offset_blocks, + bdev_io->u.bdev.num_blocks, + bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt, bdev_ftl_cb, bdev_io); + + if (spdk_unlikely(rc != 0)) { + spdk_bdev_io_complete(bdev_io, rc); + } +} + +static int +_bdev_ftl_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io) +{ + struct ftl_bdev *ftl_bdev = (struct ftl_bdev *)bdev_io->bdev->ctxt; + + switch (bdev_io->type) { + case SPDK_BDEV_IO_TYPE_READ: + spdk_bdev_io_get_buf(bdev_io, bdev_ftl_get_buf_cb, + bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen); + return 0; + + case SPDK_BDEV_IO_TYPE_WRITE: + return spdk_ftl_write(ftl_bdev->dev, ch, bdev_io->u.bdev.offset_blocks, + bdev_io->u.bdev.num_blocks, bdev_io->u.bdev.iovs, + bdev_io->u.bdev.iovcnt, bdev_ftl_cb, bdev_io); + + case SPDK_BDEV_IO_TYPE_FLUSH: + return spdk_ftl_flush(ftl_bdev->dev, bdev_ftl_cb, bdev_io); + + case SPDK_BDEV_IO_TYPE_WRITE_ZEROES: + case SPDK_BDEV_IO_TYPE_RESET: + case SPDK_BDEV_IO_TYPE_UNMAP: + default: + return -ENOTSUP; + break; + } +} + +static void +bdev_ftl_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io) +{ + int rc = _bdev_ftl_submit_request(ch, bdev_io); + + if (spdk_unlikely(rc != 0)) { + spdk_bdev_io_complete(bdev_io, rc); + } +} + +static bool +bdev_ftl_io_type_supported(void *ctx, enum spdk_bdev_io_type io_type) +{ + switch (io_type) { + case SPDK_BDEV_IO_TYPE_READ: + case SPDK_BDEV_IO_TYPE_WRITE: + case SPDK_BDEV_IO_TYPE_FLUSH: + return true; + case SPDK_BDEV_IO_TYPE_WRITE_ZEROES: + case SPDK_BDEV_IO_TYPE_RESET: + case SPDK_BDEV_IO_TYPE_UNMAP: + default: + return false; + } +} + +static struct spdk_io_channel * +bdev_ftl_get_io_channel(void *ctx) +{ + struct ftl_bdev *ftl_bdev = ctx; + + return spdk_get_io_channel(ftl_bdev->dev); +} + +static void +_bdev_ftl_write_config_info(struct ftl_bdev *ftl_bdev, struct spdk_json_write_ctx *w) +{ + struct spdk_ftl_attrs attrs = {}; + + spdk_ftl_dev_get_attrs(ftl_bdev->dev, &attrs); + + spdk_json_write_named_string(w, "base_bdev", attrs.base_bdev); + + if (attrs.cache_bdev) { + spdk_json_write_named_string(w, "cache", attrs.cache_bdev); + } +} + +static void +bdev_ftl_write_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w) +{ + struct ftl_bdev *ftl_bdev = bdev->ctxt; + struct spdk_ftl_attrs attrs; + struct spdk_ftl_conf *conf = &attrs.conf; + char uuid[SPDK_UUID_STRING_LEN]; + + spdk_ftl_dev_get_attrs(ftl_bdev->dev, &attrs); + + spdk_json_write_object_begin(w); + + spdk_json_write_named_string(w, "method", "bdev_ftl_create"); + + spdk_json_write_named_object_begin(w, "params"); + spdk_json_write_named_string(w, "name", ftl_bdev->bdev.name); + + spdk_json_write_named_bool(w, "allow_open_bands", conf->allow_open_bands); + spdk_json_write_named_uint64(w, "overprovisioning", conf->lba_rsvd); + spdk_json_write_named_uint64(w, "limit_crit", conf->limits[SPDK_FTL_LIMIT_CRIT].limit); + spdk_json_write_named_uint64(w, "limit_crit_threshold", conf->limits[SPDK_FTL_LIMIT_CRIT].thld); + spdk_json_write_named_uint64(w, "limit_high", conf->limits[SPDK_FTL_LIMIT_HIGH].limit); + spdk_json_write_named_uint64(w, "limit_high_threshold", conf->limits[SPDK_FTL_LIMIT_HIGH].thld); + spdk_json_write_named_uint64(w, "limit_low", conf->limits[SPDK_FTL_LIMIT_LOW].limit); + spdk_json_write_named_uint64(w, "limit_low_threshold", conf->limits[SPDK_FTL_LIMIT_LOW].thld); + spdk_json_write_named_uint64(w, "limit_start", conf->limits[SPDK_FTL_LIMIT_START].limit); + spdk_json_write_named_uint64(w, "limit_start_threshold", conf->limits[SPDK_FTL_LIMIT_START].thld); + if (conf->l2p_path) { + spdk_json_write_named_string(w, "l2p_path", conf->l2p_path); + } + + spdk_uuid_fmt_lower(uuid, sizeof(uuid), &attrs.uuid); + spdk_json_write_named_string(w, "uuid", uuid); + + _bdev_ftl_write_config_info(ftl_bdev, w); + + spdk_json_write_object_end(w); + spdk_json_write_object_end(w); +} + +static int +bdev_ftl_dump_info_json(void *ctx, struct spdk_json_write_ctx *w) +{ + struct ftl_bdev *ftl_bdev = ctx; + struct spdk_ftl_attrs attrs; + + spdk_ftl_dev_get_attrs(ftl_bdev->dev, &attrs); + + spdk_json_write_named_object_begin(w, "ftl"); + + _bdev_ftl_write_config_info(ftl_bdev, w); + spdk_json_write_named_string_fmt(w, "num_zones", "%zu", attrs.num_zones); + spdk_json_write_named_string_fmt(w, "zone_size", "%zu", attrs.zone_size); + + /* ftl */ + spdk_json_write_object_end(w); + + return 0; +} + +static const struct spdk_bdev_fn_table ftl_fn_table = { + .destruct = bdev_ftl_destruct, + .submit_request = bdev_ftl_submit_request, + .io_type_supported = bdev_ftl_io_type_supported, + .get_io_channel = bdev_ftl_get_io_channel, + .write_config_json = bdev_ftl_write_config_json, + .dump_info_json = bdev_ftl_dump_info_json, +}; + +static void +bdev_ftl_create_cb(struct spdk_ftl_dev *dev, void *ctx, int status) +{ + struct ftl_bdev *ftl_bdev = ctx; + struct ftl_bdev_info info = {}; + struct spdk_ftl_attrs attrs; + ftl_bdev_init_fn init_cb = ftl_bdev->init_cb; + void *init_arg = ftl_bdev->init_arg; + int rc = -ENODEV; + + if (status) { + SPDK_ERRLOG("Failed to create FTL device (%d)\n", status); + rc = status; + goto error; + } + + spdk_ftl_dev_get_attrs(dev, &attrs); + + ftl_bdev->dev = dev; + ftl_bdev->bdev.product_name = "FTL disk"; + ftl_bdev->bdev.write_cache = 0; + ftl_bdev->bdev.blocklen = attrs.block_size; + ftl_bdev->bdev.blockcnt = attrs.num_blocks; + ftl_bdev->bdev.uuid = attrs.uuid; + + SPDK_DEBUGLOG(SPDK_LOG_BDEV_FTL, "Creating bdev %s:\n", ftl_bdev->bdev.name); + SPDK_DEBUGLOG(SPDK_LOG_BDEV_FTL, "\tblock_len:\t%zu\n", attrs.block_size); + SPDK_DEBUGLOG(SPDK_LOG_BDEV_FTL, "\tnum_blocks:\t%"PRIu64"\n", attrs.num_blocks); + + ftl_bdev->bdev.ctxt = ftl_bdev; + ftl_bdev->bdev.fn_table = &ftl_fn_table; + ftl_bdev->bdev.module = &g_ftl_if; + + if (spdk_bdev_register(&ftl_bdev->bdev)) { + goto error; + } + + info.name = ftl_bdev->bdev.name; + info.uuid = ftl_bdev->bdev.uuid; + + init_cb(&info, init_arg, 0); + return; + +error: + free(ftl_bdev->bdev.name); + free(ftl_bdev); + + init_cb(NULL, init_arg, rc); +} + +static void +bdev_ftl_defer_free(struct ftl_deferred_init *init) +{ + free((char *)init->opts.name); + free((char *)init->opts.base_bdev); + free((char *)init->opts.cache_bdev); + free(init); +} + +static int +bdev_ftl_defer_init(const struct ftl_bdev_init_opts *opts) +{ + struct ftl_deferred_init *init; + + init = calloc(1, sizeof(*init)); + if (!init) { + return -ENOMEM; + } + + init->opts.mode = opts->mode; + init->opts.uuid = opts->uuid; + init->opts.ftl_conf = opts->ftl_conf; + + init->opts.name = strdup(opts->name); + if (!init->opts.name) { + SPDK_ERRLOG("Could not allocate bdev name\n"); + goto error; + } + + init->opts.base_bdev = strdup(opts->base_bdev); + if (!init->opts.base_bdev) { + SPDK_ERRLOG("Could not allocate base bdev name\n"); + goto error; + } + + if (opts->cache_bdev) { + init->opts.cache_bdev = strdup(opts->cache_bdev); + if (!init->opts.cache_bdev) { + SPDK_ERRLOG("Could not allocate cache bdev name\n"); + goto error; + } + } + + LIST_INSERT_HEAD(&g_deferred_init, init, entry); + + return 0; + +error: + bdev_ftl_defer_free(init); + return -ENOMEM; +} + +int +bdev_ftl_create_bdev(const struct ftl_bdev_init_opts *bdev_opts, + ftl_bdev_init_fn cb, void *cb_arg) +{ + struct ftl_bdev *ftl_bdev = NULL; + struct spdk_ftl_dev_init_opts opts = {}; + int rc; + + ftl_bdev = calloc(1, sizeof(*ftl_bdev)); + if (!ftl_bdev) { + SPDK_ERRLOG("Could not allocate ftl_bdev\n"); + return -ENOMEM; + } + + ftl_bdev->bdev.name = strdup(bdev_opts->name); + if (!ftl_bdev->bdev.name) { + rc = -ENOMEM; + goto error_bdev; + } + + if (spdk_bdev_get_by_name(bdev_opts->base_bdev) == NULL || + (bdev_opts->cache_bdev && spdk_bdev_get_by_name(bdev_opts->cache_bdev) == NULL)) { + rc = bdev_ftl_defer_init(bdev_opts); + if (rc == 0) { + rc = -ENODEV; + } + goto error_name; + } + + ftl_bdev->init_cb = cb; + ftl_bdev->init_arg = cb_arg; + + opts.mode = bdev_opts->mode; + opts.uuid = bdev_opts->uuid; + opts.name = ftl_bdev->bdev.name; + opts.base_bdev = bdev_opts->base_bdev; + opts.cache_bdev = bdev_opts->cache_bdev; + opts.conf = &bdev_opts->ftl_conf; + + /* TODO: set threads based on config */ + opts.core_thread = spdk_get_thread(); + + rc = spdk_ftl_dev_init(&opts, bdev_ftl_create_cb, ftl_bdev); + if (rc) { + SPDK_ERRLOG("Could not create FTL device\n"); + goto error_name; + } + + return 0; + +error_name: + free(ftl_bdev->bdev.name); +error_bdev: + free(ftl_bdev); + return rc; +} + +static int +bdev_ftl_initialize(void) +{ + return 0; +} + +void +bdev_ftl_delete_bdev(const char *name, spdk_bdev_unregister_cb cb_fn, void *cb_arg) +{ + struct spdk_bdev *bdev; + + bdev = spdk_bdev_get_by_name(name); + if (bdev) { + spdk_bdev_unregister(bdev, cb_fn, cb_arg); + return; + } + + cb_fn(cb_arg, -ENODEV); +} + +static void +bdev_ftl_finish(void) +{ +} + +static void +bdev_ftl_create_defered_cb(const struct ftl_bdev_info *info, void *ctx, int status) +{ + struct ftl_deferred_init *opts = ctx; + + if (status) { + SPDK_ERRLOG("Failed to initialize FTL bdev '%s'\n", opts->opts.name); + } + + bdev_ftl_defer_free(opts); + + spdk_bdev_module_examine_done(&g_ftl_if); +} + +static void +bdev_ftl_examine(struct spdk_bdev *bdev) +{ + struct ftl_deferred_init *opts; + + LIST_FOREACH(opts, &g_deferred_init, entry) { + if (spdk_bdev_get_by_name(opts->opts.base_bdev) == NULL) { + continue; + } + + if (opts->opts.cache_bdev && spdk_bdev_get_by_name(opts->opts.base_bdev) == NULL) { + continue; + } + + LIST_REMOVE(opts, entry); + + /* spdk_bdev_module_examine_done will be called by bdev_ftl_create_defered_cb */ + if (bdev_ftl_create_bdev(&opts->opts, bdev_ftl_create_defered_cb, opts)) { + SPDK_ERRLOG("Failed to initialize FTL bdev '%s'\n", opts->opts.name); + bdev_ftl_defer_free(opts); + break; + } + return; + } + + spdk_bdev_module_examine_done(&g_ftl_if); +} + +SPDK_LOG_REGISTER_COMPONENT("bdev_ftl", SPDK_LOG_BDEV_FTL) diff --git a/src/spdk/module/bdev/ftl/bdev_ftl.h b/src/spdk/module/bdev/ftl/bdev_ftl.h new file mode 100644 index 000000000..019a3b8f3 --- /dev/null +++ b/src/spdk/module/bdev/ftl/bdev_ftl.h @@ -0,0 +1,70 @@ +/*- + * BSD LICENSE + * + * 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. + */ + +#ifndef SPDK_BDEV_FTL_H +#define SPDK_BDEV_FTL_H + +#include "spdk/stdinc.h" +#include "spdk/bdev_module.h" +#include "spdk/ftl.h" + +struct spdk_bdev; +struct spdk_uuid; + +struct ftl_bdev_info { + const char *name; + struct spdk_uuid uuid; +}; + +struct ftl_bdev_init_opts { + /* Bdev's name */ + const char *name; + /* Base bdev's name */ + const char *base_bdev; + /* Write buffer bdev's name */ + const char *cache_bdev; + /* Bdev's mode */ + uint32_t mode; + /* UUID if device is restored from SSD */ + struct spdk_uuid uuid; + /* FTL library configuration */ + struct spdk_ftl_conf ftl_conf; +}; + +typedef void (*ftl_bdev_init_fn)(const struct ftl_bdev_info *, void *, int); + +int bdev_ftl_create_bdev(const struct ftl_bdev_init_opts *bdev_opts, + ftl_bdev_init_fn cb, void *cb_arg); +void bdev_ftl_delete_bdev(const char *name, spdk_bdev_unregister_cb cb_fn, void *cb_arg); + +#endif /* SPDK_BDEV_FTL_H */ diff --git a/src/spdk/module/bdev/ftl/bdev_ftl_rpc.c b/src/spdk/module/bdev/ftl/bdev_ftl_rpc.c new file mode 100644 index 000000000..045619342 --- /dev/null +++ b/src/spdk/module/bdev/ftl/bdev_ftl_rpc.c @@ -0,0 +1,258 @@ +/*- + * BSD LICENSE + * + * 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. + */ + +#include "spdk/rpc.h" +#include "spdk/util.h" +#include "spdk/bdev_module.h" +#include "spdk/string.h" +#include "spdk_internal/log.h" + +#include "bdev_ftl.h" + +struct rpc_bdev_ftl_create { + char *name; + char *base_bdev; + char *uuid; + char *cache_bdev; + struct spdk_ftl_conf ftl_conf; +}; + +static void +free_rpc_bdev_ftl_create(struct rpc_bdev_ftl_create *req) +{ + free(req->name); + free(req->base_bdev); + free(req->uuid); + free(req->cache_bdev); + free((char *)req->ftl_conf.l2p_path); +} + +static const struct spdk_json_object_decoder rpc_bdev_ftl_create_decoders[] = { + {"name", offsetof(struct rpc_bdev_ftl_create, name), spdk_json_decode_string}, + {"base_bdev", offsetof(struct rpc_bdev_ftl_create, base_bdev), spdk_json_decode_string}, + {"uuid", offsetof(struct rpc_bdev_ftl_create, uuid), spdk_json_decode_string, true}, + {"cache", offsetof(struct rpc_bdev_ftl_create, cache_bdev), spdk_json_decode_string, true}, + { + "allow_open_bands", offsetof(struct rpc_bdev_ftl_create, ftl_conf) + + offsetof(struct spdk_ftl_conf, allow_open_bands), spdk_json_decode_bool, true + }, + { + "overprovisioning", offsetof(struct rpc_bdev_ftl_create, ftl_conf) + + offsetof(struct spdk_ftl_conf, lba_rsvd), spdk_json_decode_uint64, true + }, + { + "use_append", offsetof(struct rpc_bdev_ftl_create, ftl_conf) + + offsetof(struct spdk_ftl_conf, use_append), spdk_json_decode_bool, true + }, + { + "l2p_path", offsetof(struct rpc_bdev_ftl_create, ftl_conf) + + offsetof(struct spdk_ftl_conf, l2p_path), + spdk_json_decode_string, true + }, + { + "limit_crit", offsetof(struct rpc_bdev_ftl_create, ftl_conf) + + offsetof(struct spdk_ftl_conf, limits[SPDK_FTL_LIMIT_CRIT]) + + offsetof(struct spdk_ftl_limit, limit), + spdk_json_decode_uint64, true + }, + { + "limit_crit_threshold", offsetof(struct rpc_bdev_ftl_create, ftl_conf) + + offsetof(struct spdk_ftl_conf, limits[SPDK_FTL_LIMIT_CRIT]) + + offsetof(struct spdk_ftl_limit, thld), + spdk_json_decode_uint64, true + }, + { + "limit_high", offsetof(struct rpc_bdev_ftl_create, ftl_conf) + + offsetof(struct spdk_ftl_conf, limits[SPDK_FTL_LIMIT_HIGH]) + + offsetof(struct spdk_ftl_limit, limit), + spdk_json_decode_uint64, true + }, + { + "limit_high_threshold", offsetof(struct rpc_bdev_ftl_create, ftl_conf) + + offsetof(struct spdk_ftl_conf, limits[SPDK_FTL_LIMIT_HIGH]) + + offsetof(struct spdk_ftl_limit, thld), + spdk_json_decode_uint64, true + }, + { + "limit_low", offsetof(struct rpc_bdev_ftl_create, ftl_conf) + + offsetof(struct spdk_ftl_conf, limits[SPDK_FTL_LIMIT_LOW]) + + offsetof(struct spdk_ftl_limit, limit), + spdk_json_decode_uint64, true + }, + { + "limit_low_threshold", offsetof(struct rpc_bdev_ftl_create, ftl_conf) + + offsetof(struct spdk_ftl_conf, limits[SPDK_FTL_LIMIT_LOW]) + + offsetof(struct spdk_ftl_limit, thld), + spdk_json_decode_uint64, true + }, + { + "limit_start", offsetof(struct rpc_bdev_ftl_create, ftl_conf) + + offsetof(struct spdk_ftl_conf, limits[SPDK_FTL_LIMIT_START]) + + offsetof(struct spdk_ftl_limit, limit), + spdk_json_decode_uint64, true + }, + { + "limit_start_threshold", offsetof(struct rpc_bdev_ftl_create, ftl_conf) + + offsetof(struct spdk_ftl_conf, limits[SPDK_FTL_LIMIT_START]) + + offsetof(struct spdk_ftl_limit, thld), + spdk_json_decode_uint64, true + }, +}; + +static void +rpc_bdev_ftl_create_cb(const struct ftl_bdev_info *bdev_info, void *ctx, int status) +{ + struct spdk_jsonrpc_request *request = ctx; + char bdev_uuid[SPDK_UUID_STRING_LEN]; + struct spdk_json_write_ctx *w; + + if (status) { + spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, + "Failed to create FTL bdev: %s", + spdk_strerror(-status)); + return; + } + + w = spdk_jsonrpc_begin_result(request); + spdk_uuid_fmt_lower(bdev_uuid, sizeof(bdev_uuid), &bdev_info->uuid); + spdk_json_write_object_begin(w); + spdk_json_write_named_string(w, "name", bdev_info->name); + spdk_json_write_named_string(w, "uuid", bdev_uuid); + spdk_json_write_object_end(w); + spdk_jsonrpc_end_result(request, w); +} + +static void +rpc_bdev_ftl_create(struct spdk_jsonrpc_request *request, + const struct spdk_json_val *params) +{ + struct rpc_bdev_ftl_create req = {}; + struct ftl_bdev_init_opts opts = {}; + struct spdk_json_write_ctx *w; + int rc; + + spdk_ftl_conf_init_defaults(&req.ftl_conf); + + if (spdk_json_decode_object(params, rpc_bdev_ftl_create_decoders, + SPDK_COUNTOF(rpc_bdev_ftl_create_decoders), + &req)) { + spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, + "Invalid parameters"); + goto invalid; + } + + if (req.cache_bdev && !spdk_bdev_get_by_name(req.cache_bdev)) { + spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, + "No such bdev: %s", req.cache_bdev); + goto invalid; + } + + opts.name = req.name; + opts.mode = SPDK_FTL_MODE_CREATE; + opts.base_bdev = req.base_bdev; + opts.cache_bdev = req.cache_bdev; + opts.ftl_conf = req.ftl_conf; + + if (req.uuid) { + if (spdk_uuid_parse(&opts.uuid, req.uuid) < 0) { + spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, + "Failed to parse uuid: %s", + req.uuid); + goto invalid; + } + + if (!spdk_mem_all_zero(&opts.uuid, sizeof(opts.uuid))) { + opts.mode &= ~SPDK_FTL_MODE_CREATE; + } + } + + rc = bdev_ftl_create_bdev(&opts, rpc_bdev_ftl_create_cb, request); + if (rc) { + if (rc == -ENODEV) { + w = spdk_jsonrpc_begin_result(request); + spdk_json_write_string_fmt(w, "FTL bdev: %s creation deferred", req.name); + spdk_jsonrpc_end_result(request, w); + } else { + spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, + "Failed to create FTL bdev: %s", + spdk_strerror(-rc)); + } + goto invalid; + } + +invalid: + free_rpc_bdev_ftl_create(&req); +} + +SPDK_RPC_REGISTER("bdev_ftl_create", rpc_bdev_ftl_create, SPDK_RPC_RUNTIME) +SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_ftl_create, construct_ftl_bdev) + +struct rpc_delete_ftl { + char *name; +}; + +static const struct spdk_json_object_decoder rpc_delete_ftl_decoders[] = { + {"name", offsetof(struct rpc_bdev_ftl_create, name), spdk_json_decode_string}, +}; + +static void +rpc_bdev_ftl_delete_cb(void *cb_arg, int bdeverrno) +{ + struct spdk_jsonrpc_request *request = cb_arg; + struct spdk_json_write_ctx *w = spdk_jsonrpc_begin_result(request); + + spdk_json_write_bool(w, bdeverrno == 0); + spdk_jsonrpc_end_result(request, w); +} + +static void +rpc_bdev_ftl_delete(struct spdk_jsonrpc_request *request, + const struct spdk_json_val *params) +{ + struct rpc_delete_ftl attrs = {}; + + if (spdk_json_decode_object(params, rpc_delete_ftl_decoders, + SPDK_COUNTOF(rpc_delete_ftl_decoders), + &attrs)) { + spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, + "Invalid parameters"); + goto invalid; + } + + bdev_ftl_delete_bdev(attrs.name, rpc_bdev_ftl_delete_cb, request); +invalid: + free(attrs.name); +} + +SPDK_RPC_REGISTER("bdev_ftl_delete", rpc_bdev_ftl_delete, SPDK_RPC_RUNTIME) +SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_ftl_delete, delete_ftl_bdev) |